Search code examples
ruby-on-railsrubydelayed-job

ruby on rails background application to run jobs automaically at a time dynamically defined by users?


I have a use case where user schedules a 'command' from the web interface. The user also specifies the date and time the command needs to be triggred.

This is sequence of steps:

1.User schedules a command 'Restart Device' at May 31, 3pm.

2.This is saved in a database table called Command.

3.Now there needs to be a background job that needs to be triggered at this specified time to do something (make an api call, send email etc.)

4.Once job is executed, It is removed or marked done, until a new command is issued.

There could be multpile users concurrently performing the above sequence of steps.

Is delayed_job a good choice for above? I couldnt find an example as how to implement above using delayed job.

EDIT: the reason I was looking at delayed_job is because eventually I would need to leverage existing relational database


Solution

  • I would advise to use Sidekiq. With it you can use scheduled jobs to tell sidekiq when to perform the jobs.

    Example : MyWorker.perform_at(3.hours.from_now, 'mike', 1)

    EDIT : worker example

    #app/workers/restart_device_worker.rb
    class RestartDeviceWorker
      include Sidekiq::Worker
    
      def perform(params)
        # Do the job
        # ...
        # update in DB
      end
    end