How to enqueue Sidekiq jobs and execute them only upon manual launch?

47 Views Asked by At

I'm working on a Ruby on Rails application where I need to enqueue Sidekiq jobs for sending SMS messages. However, I want these jobs to be enqueued but not executed immediately. Instead, I want to have control over when these jobs are executed manually on Sidekiq.

class Worker
  include Sidekiq::Worker
  def perform(method_name, *args)
    Services.send(method_name, *args)
  end
end

I'm able to enqueue the jobs successfully, but I'm unsure how to prevent them from being executed immediately. I want them to be executed only when I manually run them on Sidekiq or any other tool that i can make

Is there a way to enqueue the jobs in such a way that they remain in the queue without being processed until I manually launch Sidekiq?

1

There are 1 best solutions below

0
Merouane Amqor On BEST ANSWER

To achieve your goal of pausing SMS notifications and manually controlling their execution, i had to modify my Worker class to check for a flag (e.g., pause_notifications) that determines whether the notifications should be paused. If the flag is set, the job can be rescheduled for a later time.

Here's my current implementation:

class Worker
  include Sidekiq::Worker

  def perform(method_name, *args)
    if pause_notifications
      # Reschedule the job to be performed 10 minutes later
      self.class.perform_in(10.minutes, method_name, *args)
    else
      Services.send(method_name, *args)
    end
  end
end

In this implementation, when perform is called, it checks the pause_notifications method If it returns true, the job is rescheduled to be performed 10 minutes later using perform_in. Otherwise, it executes the Services.send method as usual