Resque-Scheduler not working with ActiveJob in Rails 4.2

前端 未结 3 1455
时光说笑
时光说笑 2021-01-02 14:29

Has anyone been able to get scheduled jobs to work in Rails 4.2?

I am using resque, and I am attempting to use resque-scheduler to schedule jobs. I have a schedule

3条回答
  •  -上瘾入骨i
    2021-01-02 14:56

    It seems that ActiveJob in Rails 4.2 is not supported by resque-scheduler. Therefore, jobs are not scheduled correctly, that explains the difference in the log when a job is enqueued using ActiveJob API and resque-scheduler.

    To fix this, we should find a way to schedule job within ActiveJob wrapper:

    ActiveJob::QueueAdapters::ResqueAdapter::JobWrapper
    

    Resque-scheduler provides the way to support extensions that are not supported by default. To do this, we should extend custom job class to support the #scheduled method. This way we can enqueue job manually using ActiveJob API.

    The easiest way is to write general code method in the base job, and then extend all jobs from it:

    # custom base job
    class Job < ActiveJob::Base
    
        # method called by resque-scheduler to enqueue job in a queue
        def self.scheduled(queue, klass, *args)
    
            # create the job instance and pass the arguments
            job = self.job_or_instantiate(*args)
    
            # set correct queue
            job.queue_name = queue
    
            # enqueue job using ActiveJob API
            job.enqueue
        end
    end
    

    Reque-scheduler will call this method to schedule every single job extended from Job class. This way jobs are going to be enqueued within the ActiveJob wrapper. Result will be same as calling MyJob.perform_later(*args).

提交回复
热议问题