有没有办法“观看”使用 rake 任务插入新数据库表?

发布于 2024-10-29 07:10:16 字数 609 浏览 1 评论 0原文

我有 rufus 调度程序,我计划查看我的日程表以获取新的日程安排。有办法做到这一点吗?类似于:

schedule.every "1m" do
  #check for new entries in database
end

已回答

scheduler.every '1m' do
  #check for new stuff in the database
  newest_jobs = Schedule.all_cron_jobs.select{ |x| x.created_at > 1.minute.ago } - all_schedules_with_cron
  unless newest_jobs.empty?
    newest_jobs.each do |new_job|
      scheduler.every new_job.cron_string do
        job.call
      end
    end
  end
end

jobs.call 是对延迟作业的调用,以对该计划进行排队。所以基本上,获取最新的计划,并在每次 cron 匹配时调用 rufus 将作业放入队列

I have rufus scheduler and i was planning to watch my schedules table for new schedules. Is there a way to do that? something like:

schedule.every "1m" do
  #check for new entries in database
end

ANSWERED

scheduler.every '1m' do
  #check for new stuff in the database
  newest_jobs = Schedule.all_cron_jobs.select{ |x| x.created_at > 1.minute.ago } - all_schedules_with_cron
  unless newest_jobs.empty?
    newest_jobs.each do |new_job|
      scheduler.every new_job.cron_string do
        job.call
      end
    end
  end
end

jobs.call is a call to delayed job to queue that schedule. So basically, get the newest schedules, and call rufus on them to put the jobs on queue every time the cron matches

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倒数 2024-11-05 07:10:16

使用表中的 created_at 列检查自上次检查以来是否有新条目。

last_checked = 1.minute.ago
schedule.every "1m" do
  new_entries = Schedule.where(['created_at >= ?', last_checked])
  last_checked = Time.now

  # deal with entries here.
end

您可以通过进一步最小化 last_checked 周期之间的间隙来改进这一点。

在模型上使用观察者不是更好吗?保证不会因时间问题而错过。

Use the created_at column on the table to check for new entries since you last checked.

last_checked = 1.minute.ago
schedule.every "1m" do
  new_entries = Schedule.where(['created_at >= ?', last_checked])
  last_checked = Time.now

  # deal with entries here.
end

You could improve this by minimising the gap between last_checked cycles even more.

Would you not be better to use an observer on the model instead of this? Guarantees no misses due to timing issues.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文