【发布时间】:2019-07-10 09:37:48
【问题描述】:
我试图每 10 秒运行一次运行一段代码的 cron 作业。我使用了一种需要运行代码并使其休眠 10 秒的方法,但这似乎会大大降低应用程序的性能。我正在使用 gem,它每分钟运行一次并休眠 10 秒。如何使用 sleep 方法实现相同的 w/o。以下是我的代码。
every 1.minute do
runner "DailyNotificationChecker.send_notifications"
end
class DailyNotificationChecker
def self.send_notifications
puts "Triggered send_notifications"
expiry_time = Time.now + 57
while (Time.now < expiry_time)
if RUN_SCHEDULER == "true" || RUN_SCHEDULER == true
process_notes
end
sleep 10 #seconds
end
def self.process_notes
notes = nil
time = Benchmark.measure do
Note.uncached do
notes = Note.where(status: false)
notes.update_all(status: true)
end
end
puts "time #{time}"
end
end
我的代码的目标是将对象的布尔状态更改为 true,每 10 秒检查一次。该表有 200 万条记录。
【问题讨论】:
-
听起来您需要在后台运行一个守护进程,而不是每 10 秒启动一次 cronjob。我不确定你为什么不想要
sleep,但我认为每 10 秒启动一个新进程并不比睡眠一段时间更好(它必须每 10 秒启动整个 rails 应用程序!!)。
标签: ruby-on-rails ruby ruby-on-rails-4 cron whenever