Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何使用rails中的Where gem将作业安排为每20秒一次_Ruby On Rails_Ruby_Ruby On Rails 3 - Fatal编程技术网

Ruby on rails 如何使用rails中的Where gem将作业安排为每20秒一次

Ruby on rails 如何使用rails中的Where gem将作业安排为每20秒一次,ruby-on-rails,ruby,ruby-on-rails-3,Ruby On Rails,Ruby,Ruby On Rails 3,我试图每20秒运行一次作业。我在用宝石 但根据其文件,它允许至少1分钟的时间 调度程序 every 1.minute do runner "DailyNotificationChecker.send_notifications" end 工作 我想我可以做一个无限循环,使用sleep方法,让它睡眠20秒。这样,此作业将每20秒运行一次,但如果我的cron再次使用此方法,将会发生什么情况 class DailyNotificationChecker def self.send_no

我试图每20秒运行一次作业。我在用宝石 但根据其文件,它允许至少1分钟的时间

调度程序

every 1.minute do
  runner "DailyNotificationChecker.send_notifications"
end
工作

我想我可以做一个无限循环,使用sleep方法,让它睡眠20秒。这样,此作业将每20秒运行一次,但如果我的cron再次使用此方法,将会发生什么情况

class DailyNotificationChecker 
    def self.send_notifications
        Rails.logger.info "Triggered NotificationChecker"
        if RUN_SCHEDULER == "true"
          Rails.logger.info "Running Performer"
          notes = Note.unprocessed
          if notes.present?
            notes.each do |note|
              RealtimeNotificationChecker.performer(note.user_id,"note_created")
              note_hash = {}
              note_hash[:note_id] = note.id
              url = URI.parse(PROCESS_NOTE_API_URL)
              resp, data = Net::HTTP.post_form(url, note_hash)
            end
          end
        sleep 20  #=> workaround for to sleep 20 seconds
        end
      end
end

有几种方法可以解决此问题:

  • 接受每分钟粒度限制
    (即
    cron
    作业),每分钟只运行一次此任务,无循环
  • 将脚本作为服务运行;任何时候都不要使用
    。您可以使用类似于
    monit
    的方法来确保脚本不会死机
  • 重构代码,在每次创建
    通知时触发新的后台作业。使用诸如或之类的工具来处理工作负载

  • 有几种方法可以解决此问题:

  • 接受每分钟粒度限制
    (即
    cron
    作业),每分钟只运行一次此任务,无循环
  • 将脚本作为服务运行;任何时候都不要使用
    。您可以使用类似于
    monit
    的方法来确保脚本不会死机
  • 重构代码,在每次创建
    通知时触发新的后台作业。使用诸如或之类的工具来处理工作负载

  • 考虑到我的情况,我一直在围绕着代码工作

    def self.send_notifications
            expiry_time = Time.now + 59
            while (Time.now < expiry_time)
              Rails.logger.info "Triggered NotificationChecker"
              if RUN_SCHEDULER == "true" || RUN_SCHEDULER == true
                Rails.logger.info "Running Performer"
                Note.uncached do
                  notes = Note.unprocessed.uncached
                  if notes.present?
                    notes.each do |note|
                      RealtimeNotificationChecker.performer(note.user_id,"note_created")
                      note_hash = {}
                      note_hash[:note_id] = note.id
                      url = URI.parse(PROCESS_NOTE_API_URL)
                      resp, data = Net::HTTP.post_form(url, note_hash)
                    end
                  end
                end
              end
              sleep 20 #seconds
            end
          end
    
    def self.send_通知
    到期时间=time.now+59
    while(Time.now<到期时间)
    Rails.logger.info“触发通知检查器”
    如果RUN_SCHEDULER==“true”| RUN_SCHEDULER==true
    Rails.logger.info“正在运行的执行者”
    Note.uncached do
    notes=Note.unprocessed.uncached
    如果你在场?
    注释。每个do |注释|
    RealtimeNotificationChecker.performer(note.user\u id,“note\u created”)
    注意_hash={}
    note\u hash[:note\u id]=note.id
    url=URI.parse(进程\注释\ API \ url)
    resp,data=Net::HTTP.post\u表单(url,note\u散列)
    结束
    结束
    结束
    结束
    睡眠20秒
    结束
    结束
    

    我正在运行一个循环,该循环每59秒过期一次,在循环中休眠20秒。

    考虑到我的情况,我已经处理了代码

    def self.send_notifications
            expiry_time = Time.now + 59
            while (Time.now < expiry_time)
              Rails.logger.info "Triggered NotificationChecker"
              if RUN_SCHEDULER == "true" || RUN_SCHEDULER == true
                Rails.logger.info "Running Performer"
                Note.uncached do
                  notes = Note.unprocessed.uncached
                  if notes.present?
                    notes.each do |note|
                      RealtimeNotificationChecker.performer(note.user_id,"note_created")
                      note_hash = {}
                      note_hash[:note_id] = note.id
                      url = URI.parse(PROCESS_NOTE_API_URL)
                      resp, data = Net::HTTP.post_form(url, note_hash)
                    end
                  end
                end
              end
              sleep 20 #seconds
            end
          end
    
    def self.send_通知
    到期时间=time.now+59
    while(Time.now<到期时间)
    Rails.logger.info“触发通知检查器”
    如果RUN_SCHEDULER==“true”| RUN_SCHEDULER==true
    Rails.logger.info“正在运行的执行者”
    Note.uncached do
    notes=Note.unprocessed.uncached
    如果你在场?
    注释。每个do |注释|
    RealtimeNotificationChecker.performer(note.user\u id,“note\u created”)
    注意_hash={}
    note\u hash[:note\u id]=note.id
    url=URI.parse(进程\注释\ API \ url)
    resp,data=Net::HTTP.post\u表单(url,note\u散列)
    结束
    结束
    结束
    结束
    睡眠20秒
    结束
    结束
    

    我正在运行一个循环,它每59秒过期一次,在循环中睡眠20秒。

    你能不能不使用
    每20秒一次,
    每当
    创建一个
    cron
    作业时,该作业只能按分钟进行配置。您能不能不每隔20秒使用一次
    每当
    创建一个
    cron
    作业时-只能按分钟进行配置。我在@tom周围找到了一个工作,请建议我的代码是否有任何缺点。我在@tom周围找到了一个工作,请建议我的代码是否有任何缺点。这是一个黑客行为。在这里,无论何时使用
    都有什么意义?您可以只触发它一次,并且永远不会使它“过期”。例如,如果突然出现巨大的工作负载(即数千个未处理的
    Note
    s),会发生什么情况
    无论何时
    都会在脚本过期之前重新触发脚本,导致潜在的重复,甚至系统崩溃。这是可行的,但这是一种黑客行为。在这里,无论何时使用
    都有什么意义?您可以只触发它一次,并且永远不会使它“过期”。例如,如果突然出现巨大的工作负载(即数千个未处理的
    Note
    s),会发生什么情况
    无论何时
    都会在脚本过期之前重新触发脚本,从而导致潜在的重复甚至系统崩溃。