Resque-Scheduler does not work with ActiveJob in Rails 4.2

Could anyone get the scheduled tasks to work in Rails 4.2?

I am using resque and I am trying to use resque-scheduler to schedule tasks. I have a schedule that scheduler loads and starts, and it even looks like it starts tasks, but does nothing.

resque-scheduler: [INFO] 2014-09-16T01:54:25-07:00: Starting resque-scheduler: [INFO] 2014-09-16T01:54:25-07:00: Loading Schedule resque-scheduler: [INFO] 2014-09-16T01:54:25-07:00: Scheduling friends resque-scheduler: [INFO] 2014-09-16T01:54:25-07:00: Schedules Loaded resque-scheduler: [INFO] 2014-09-16T01:54:55-07:00: queueing FriendsJob (friends) 

I can set tasks like this and they will be processed.

 TestJob.enqueue(params[:id]) 

and I get this conclusion in the work log

 got: (Job{default} | ActiveJob::QueueAdapters::ResqueAdapter::JobWrapper | ["TestJob", "98732ce5-17f7-4da3-9a03-a5d2f8f74e84", "8"]) ** [01:24:01 2014-09-16] 54841: resque-1.25.2: Processing default since 1410855841 [ActiveJob::QueueAdapters::ResqueAdapter::JobWrapper] ** [01:24:01 2014-09-16] 54841: Running before_fork hooks with [(Job{default} | ActiveJob::QueueAdapters::ResqueAdapter::JobWrapper | ["TestJob", "98732ce5-17f7-4da3-9a03-a5d2f8f74e84", "8"])] ** [01:24:01 2014-09-16] 54841: resque-1.25.2: Forked 54882 at 1410855841 ** [01:24:01 2014-09-16] 54882: Running after_fork hooks with [(Job{default} | ActiveJob::QueueAdapters::ResqueAdapter::JobWrapper | ["TestJob", "98732ce5-17f7-4da3-9a03-a5d2f8f74e84", "8"])] Hello World!! ** [01:24:01 2014-09-16] 54882: done: (Job{default} | ActiveJob::QueueAdapters::ResqueAdapter::JobWrapper | ["TestJob", "98732ce5-17f7-4da3-9a03-a5d2f8f74e84", "8"]) 

But when I try to schedule a task, it looks like they are getting a queue, but they are not doing anything.

Here is the .yml schedule

 friends: every: "30s" queue: "friends" class: "FriendsJob" args: 8 description: "Friends jobs scheduler" 

Here is the result of a scheduled task.

 ** [01:23:36 2014-09-16] 54841: got: (Job{friends} | FriendsJob | [8]) ** [01:23:36 2014-09-16] 54841: resque-1.25.2: Processing friends since 1410855816 [FriendsJob] ** [01:23:36 2014-09-16] 54841: Running before_fork hooks with [(Job{friends} | FriendsJob | [8])] ** [01:23:36 2014-09-16] 54841: resque-1.25.2: Forked 54880 at 1410855816 ** [01:23:36 2014-09-16] 54880: Running after_fork hooks with [(Job{friends} | FriendsJob | [8])] ** [01:23:36 2014-09-16] 54880: done: (Job{friends} | FriendsJob | [8]) 

After reading this http://dev.mikamai.com/post/96343027199/rails-4-2-new-gems-active-job-and-global-id I suspect this has something to do with ActiveJob and GlobalId .

Take a look at the difference in line

 ** [01:24:01 2014-09-16] 54841: Running before_fork hooks with [(Job{default} | ActiveJob::QueueAdapters::ResqueAdapter::JobWrapper | ["TestJob", "98732ce5-17f7-4da3-9a03-a5d2f8f74e84", "8"])] 

vs planned

 ** [01:23:36 2014-09-16] 54841: Running before_fork hooks with [(Job{friends} | FriendsJob | [8])] 

The tasks themselves were created through

  rails g job <JobName> 

They look like this:

 class TestJob < ActiveJob::Base queue_as :default def perform(friend_id) friend = Friend.find(friend_id) name = friend.name.swapcase puts "Hello World!!" end end class FriendsJob < ActiveJob::Base queue_as :friends def perform(friend_id) friend = Friend.find(friend_id) name = friend.name.swapcase puts "Hello World!!" end end 

Any help with this would be greatly appreciated and in advance.

********* UPDATE *********

I removed the action_job railtie and I use only Resque and Resque-Scheduler, and scheduled tasks now work. So this is similar to ActionJob / GlobalId. I open a problem in a rails project. I hope they fix it soon.

****** SECOND UPDATE ********* I received an update about this. This was stated by Cristianbica, who works in the ActiveJob code base. β€œWe have not thought about repetitive work, and we do not support it, since none of the adapters supports it without an external gem. However, this is a very nice feature, but I don’t think we will be in time 4.2 Also I’m not sure that it will be included in the rails "

+6
source share
3 answers

https://github.com/JustinAiken/active_scheduler is a gem that wraps it in another

+4
source

UPDATE: 4/4/16 - Although the answer below is still correct with the current version of Rails, I now use the active_scheduler stone created by Justin, as stated in the answer above: fooobar.com/questions/975347 / ...

ORIGINAL RESPONSE: Avoid using ActiveJob if you need to schedule recurring tasks.

Of the problem that Louis raised

Since we currently do not support recurring jobs with ActiveJob, we are going to close this. If it is possible to support repetitive tasks, I see it as a separate stone or rails. 5. Feature requests and conversations around them are usually discussed on the mailing list ( https://groups.google.com/forum/#!forum/rubyonrails-core ). As a solution to your @luismadrigal problem, I suggest you use the resque-scheduler method to do repetitive work.

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

The mailing list talked about creating a gem, but I cannot find any additional information about it.

+2
source

ActiveJob in Rails 4.2 does not seem to support a resc scheduler. Thus, the tasks are not scheduled correctly, which explains the difference in the log when the task was launched using the ActiveJob API and the scheduler.

To fix this, we need to find a way to schedule work in the ActiveJob shell:

 ActiveJob::QueueAdapters::ResqueAdapter::JobWrapper 

Resque-scheduler provides a way to support extensions that are not supported by default. To do this, we must extend our own job class to support the #scheduled method. Thus, we can manually complete the task manually using the ActiveJob API.

The easiest way is to write a general code method in the base task, and then extend all the tasks 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 each individual job extended from the Job class. This way, jobs will be placed in the ActiveJob shell. The result will be the same as calling MyJob.perform_later(*args) .

0
source

Source: https://habr.com/ru/post/975347/


All Articles