0

I have a rake task in a rails 4 application that is called from my controller and runs in background, like this:

system "rake fetch_news SOURCE=#{src} CATS=#{cat} C_ID=#{c_id} S_ID=#{s_id} FEED_ID=#{f_id} &"

It runs perfect in background, does it's job, but I need to start is again with same parameters, after a delay of 1.5 -- 2 minutes.

Informally, I see this like:

desc "Fetch news for specific category"
task :fetch_news => :environment do
    ... code of my background task ...
   sleep 120
   << insert here some code to restart the task >>
end

but I don't know how to implement this :(. Initially I thought I can define a function into my task, call the function, sleep, call it again, but the task doesn't permit to define a function into.

I searched on Google for solutions, but I don't find anything to help me. Can you give me an idea?

Thanks!

1 Answer 1

0

Instead of rake tasks you should use:

  • CRON - in case if parameters is fixed. Look at whenever if you want to simplify work with CRON.
  • Sidekiq - in case if you want to change parameters in runtime. Sidekiq has ability to define recurring jobs.
1
  • you can use both a rake task and cron -- execute the rake task with cron.You can also use ActiveJob in Rails4 (what the OP is already using) to set a job to be performed in the future -- you can't do reoccuring jobs, but you can possibly have a job that executes, and then sets another job to be performed in the future. Only some ActiveJob adapters support future scheduled jobs. edgeguides.rubyonrails.org/active_job_basics.html And you could even have a cron job which runs every X minutes to executes a rake task to schedule an ActiveJob. I'd prob do it that way.
    – jrochkind
    Feb 27, 2015 at 15:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.