如何在 rails ruby 中每 10 秒 运行 执行一次 cron 作业
How to run a cron job every 10 seconds in ruby on rails
我正在尝试 运行 每 10 秒执行一次 cron 作业,运行 是一段代码。我使用了一种需要 运行 编码并使其休眠 10 秒的方法,但它似乎会大大降低应用程序的性能。我正在使用 gem,每分钟 运行 并休眠 10 秒。如何使用睡眠方法实现相同的 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
Objective 我的代码是将对象的布尔状态更改为 true,每 10 秒检查一次。这个table有200万条记录。
您将使用 clockwork gem。它在一个单独的进程中运行。配置非常简单。
require 'clockwork'
include Clockwork
every(10.seconds, 'frequent.job') { DailyNotificationChecker.process_notes }
我建议为此使用 Sidekiq 后台作业。使用 sidekiq-scheduler
gem 你可以 运行 普通的 sidekiq 作业调度在你需要的任何内部。通过 Sidekiq gem.
拥有一个网络界面来处理和监控作业的奖励积分
我正在尝试 运行 每 10 秒执行一次 cron 作业,运行 是一段代码。我使用了一种需要 运行 编码并使其休眠 10 秒的方法,但它似乎会大大降低应用程序的性能。我正在使用 gem,每分钟 运行 并休眠 10 秒。如何使用睡眠方法实现相同的 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
Objective 我的代码是将对象的布尔状态更改为 true,每 10 秒检查一次。这个table有200万条记录。
您将使用 clockwork gem。它在一个单独的进程中运行。配置非常简单。
require 'clockwork'
include Clockwork
every(10.seconds, 'frequent.job') { DailyNotificationChecker.process_notes }
我建议为此使用 Sidekiq 后台作业。使用 sidekiq-scheduler
gem 你可以 运行 普通的 sidekiq 作业调度在你需要的任何内部。通过 Sidekiq gem.