📅  最后修改于: 2023-12-03 15:05:12.648000             🧑  作者: Mango
Sidekiq is a Ruby application for handling background jobs. With its Perform At feature, we can schedule jobs to be executed at a specific time in the future. This feature is handy in scenarios where we want to execute a job at a particular time or schedule multiple jobs to run at different times.
The syntax for scheduling a job at a specific time in the future is as follows:
SomeWorker.perform_at(1.hour.from_now, arg1, arg2)
This tells Sidekiq to execute the SomeWorker
job with arg1
and arg2
parameters one hour from the current time.
We can also use the perform_in
method to schedule jobs to run after a specific duration:
SomeWorker.perform_in(30.minutes, arg1, arg2)
This instructs Sidekiq to execute the SomeWorker
job with arg1
and arg2
parameters after 30 minutes from the current time.
Sidekiq also provides functionality to cancel scheduled jobs. We can do this using the cancel_scheduled_job
method:
job = SomeWorker.perform_at(1.hour.from_now, arg1, arg2)
Sidekiq::Status.cancel(job.jid)
This code schedules the SomeWorker
job and stores its job ID in the job
variable. We can use the job.jid
value to cancel the job if required.
In summary, Sidekiq Perform At is an excellent feature to schedule jobs to be executed at a specific time. With its simple syntax, cancelation functionality, and flexibility, it is an indispensable tool for handling background jobs in our Ruby applications.