Search code examples
ruby-on-railsruby-on-rails-4rake-task

Are rake tasks suitable for long running processes in production?


I'm planing in using a rake task to develop a long running background process for my rails application. Are rake tasks appropriate for this kind of processes? Ideally, I would like wrap it inside a linux daemon to be able to start and end the process easily.

If it's not the best option, which are the alternatives? I'm trying to avoid using a cron-based solution to avoid having to worry about the schedule and the posibility of having diferent running instances of the same process overlapping between them.

Thanks!


Solution

  • You can try delayed job with this extension.

    class MyJob
    
      include Delayed::ScheduledJob
    
      run_every 1.day
    
      def display_name
        "MyJob"
      end
    
      def perform
        # code to run ...
      end
    end
    

    Or manually enqueue another job with Time.now + 5.minutes for example after current job is finished inside perform method.