简体   繁体   English

服务器重启后使rufus-scheduler作业仍然存在

[英]Making rufus-scheduler jobs persist after a server reboot

The Application 应用程序

I am writing an application that runs the scheduler and fetches the records after someone saves an entry into the database. 我正在编写一个运行调度程序并在有人将条目保存到数据库后获取记录的应用程序。

initializers/scheduler.rb 初始值设定项/scheduler.rb

require 'rufus-scheduler' 


SCHEDULER = Rufus::Scheduler.new(:lockfile => ".rufus-scheduler.lock")
unless SCHEDULER.down?

  # SCHEDULER.every '3m', :tags => 121 do
  #   Rails.logger.info "Ping!!"
  #     Rails.logger.flush
  # end

end

controllers/abc_controller.rb controllers / abc_controller.rb

class AbcController < ApplicationController

  def new
    obj = ModelClass.new(params)
    if obj.save
      SCHEDULER.every obj.frequency do
        # run a job
      end
    end
  end

end

Problem 问题

The job is started from the controller, but the problem is when a server is restarted the job is lost. 该作业是从控制器启动的,但问题是重新启动服务器时,该作业丢失了。

I have read the rufus-scheduler's readme file and it is mentioned that it is a non-feature. 我已经阅读了rufus-scheduler的自述文件,并且提到它是非功能性文件。 But still I want to know if there is any solution to this (persisting the jobs after a server reboot) or do I have to change my approach? 但是我仍然想知道是否对此有任何解决方案(服务器重新启动后仍然存在作业)还是我必须更改方法?

PS I searched for it on stack overflow but I am sorry if I missed anything or missed a similar question. PS我在堆栈溢出时进行搜索,但是如果我错过了任何事情或错过了类似的问题,我感到抱歉。

EDIT 编辑

For all those who want to run the scheduler from the controller itself using params, think of a strategy to save the data in the db(temp table or something) if possible and then bind the create event with scheduling. 对于所有想要使用参数从控制器本身运行调度程序的用户,请考虑一种策略,如果可能的话,将数据保存在db(临时表或其他内容)中,然后将create事件与调度绑定。 Re-schedule the jobs on scheduler initialization, ie, server reboot as mentioned in the answer by @jmettraux. 按照@jmettraux的答案在调度程序初始化时重新调度作业,即服务器重新启动。 Hope this helps. 希望这可以帮助。

You are right, rufus-scheduler is not tied to any persistence. 没错,rufus-scheduler与持久性无关。

Since you're already tying records to schedules (by scheduling a job for each object getting created), why don't you simply reschedule when your application restarts? 由于您已经将记录与时间表绑定在一起(通过为要创建的每个对象安排一个作业),为什么不重新启动应用程序时重新安排时间表呢?

Something like (pseudo-code aligned on yours): 类似于(伪代码与您的伪代码对齐):

require 'rufus-scheduler'


SCHEDULER = Rufus::Scheduler.new(:lockfile => ".rufus-scheduler.lock")

unless SCHEDULER.down?

  ModelClass.all.each do |obj|
    SCHEDULER.every obj.frequency do
      # the job
    end
  end
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM