Resque-Scheduler not working with ActiveJob in Rails 4.2

前端 未结 3 1441
时光说笑
时光说笑 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条回答
  • 2021-01-02 14:48

    https://github.com/JustinAiken/active_scheduler is a gem that wraps the one into the other

    0 讨论(0)
  • 2021-01-02 14:55

    UPDATE: 4/4/16 - Whilst the below answer is still correct with the current version of Rails, I now use the active_scheduler gem created by Justin as mentioned in the answer above: https://stackoverflow.com/a/29155372/1299792

    ORIGINAL ANSWER: Avoid using ActiveJob if you need to schedule recurring jobs.

    From the issue that Luis raised

    As we are not currently supporting recurring jobs with ActiveJob we're going to close this. If it's possible to support recurring jobs I'm seeing this as a separate gem or in rails 5. Feature requests and talks around them are usually talked in the mailing list (https://groups.google.com/forum/#!forum/rubyonrails-core). As a solution to your problem @luismadrigal I suggest you use the resque-scheduler way to do recurring job.

    https://github.com/rails/rails/issues/16933#issuecomment-58945932

    There was talk about creating a gem in the mailing list but I can't find any further information on it.

    0 讨论(0)
  • 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).

    0 讨论(0)
提交回复
热议问题