如何在轨道上的ruby中每10秒运行一次cron作业

问题描述 投票:0回答:2

我试图在运行一段代码的每10秒内运行一个cron作业。我使用了一种方法,它需要运行代码并使其休眠10秒,但它似乎会大大降低应用程序的性能。我正在使用宝石,每分钟运行并睡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

我的代码的目标是将对象的布尔状态更改为true,每10秒检查一次。该表有200万条记录。

ruby-on-rails ruby ruby-on-rails-4 cron whenever
2个回答
1
投票

你会使用clockwork宝石。它在一个单独的过程中运行。配置非常简单。

require 'clockwork'
include Clockwork

every(10.seconds, 'frequent.job') { DailyNotificationChecker.process_notes }

2
投票

我建议使用Sidekiq后台工作。使用sidekiq-scheduler gem,您可以在任何内部运行普通的sidekiq作业计划。通过Sidekiq gem处理和监控作业的Web界面的奖励积分。

© www.soinside.com 2019 - 2024. All rights reserved.