[英]How to run a cron job every 10 seconds in ruby on rails
I am trying to run a cron job in every 10 seconds that runs a piece of code. 我试图每10秒运行一次cron作业,它运行一段代码。 I have used an approach which requires running a code and making it sleep for 10 seconds, but it seems to make drastically degrading the app performance. 我已经使用了一种方法,该方法需要运行代码并使它休眠10秒钟,但这似乎会使应用程序性能急剧下降。 I am using whenever gem, which run every minute and sleeps for 10 seconds. 每当宝石每分钟运行一次并睡眠10秒钟时,我就会使用它。 How can I achieve the same w/o using sleep method. 如何使用睡眠方法实现相同的效果。 Following is my code. 以下是我的代码。
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
Objective of my code is to change the boolean status of objects to true which gets checked every 10 seconds. 我的代码的目标是将对象的布尔状态更改为true ,每10秒检查一次。 This table has 2 million records. 该表有200万条记录。
I would suggest using Sidekiq background jobs for this. 我建议为此使用Sidekiq后台作业。 With the sidekiq-scheduler
gem you can run ordinary sidekiq jobs schedules in whatever internal you need. 使用sidekiq-scheduler
gem,您可以在所需的任何内部运行常规的sidekiq作业计划。 Bonus points for having a web-interface to handle and monitor the jobs via the Sidekiq gem. 具有Web界面以通过Sidekiq gem处理和监视作业的奖励积分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.