Schedule job with Sidekiq and Redis

Tram Ho

I have just done a task related to this part, and found out there are quite a few documents related to it, although I find it has been applied quite a lot in my life, from the smallest thing I do every day. is setting an alarm, to more important jobs like opening a football ticket, for example =))

First, because I work with rails, I use Sidekiq gem, if you do not know, Sidekiq is a gem that is often used to handle background jobs, using threads to handle multiple jobs at once, and it is running in the background, so it will not affect the operation of the user. More specifically, you can read more here . In this article I will focus on how to use the schedule job only.

Sidekiq provides two methods that we can use: perform_in(interval, *args) and perform_at(timestamp, *args) . For example:

Looking at this example, it is easy to understand the difference between the two methods, perform_in is to perform an action after how long from the present time, and with perform_at will be passed into it at a specific time. we want, like day a month b time xyz, and it will execute the job at that time.

In a worker, we would write the following:

quere here is the thread name for us to group jobs together, jobs of different threads can run in parallel, that’s the multi-threaded mechanism of Sidekeq, this I will confict in the file sidekiq.yml

Sidekiq also provides us with a web interface for tracking jobs We can find the job we just created in the Scheduled tab.

Basically, creating a schedule job is just that, simple =)) But if you stop here, you will encounter a problem, that is how if you want to change the job execution schedule, Or cancel the job?

As usual, we want to update or destroy a certain record, we use id to find it and handle it, sidekiq is similar, it also generates a so-called jid (job id) of the job. , but also can not save it into the database, because after the job is executed, jid has no meaning. Now that we need Redis, Redis is a very popular key-value storage system on RAM, the operation is quite simple.

Now a bit revised, when creating the job:

More expiration time, you probably do not want it to last forever in redis ? ) In this case, I will set the expiration time as the time the job is done, note that the time you pass must be in seconds, redis will automatically delete that key-value pair after the number of seconds you pass. .

Now when I want to update or cancel a job, I just need to get the jid from Redis

Then find that job in Sidekiq

There is another way to use it

But this way is quite slow, especially when the amount of your job has become too large.

Now if I want to update my job:

Then don’t forget to update the key-value expiration time in Redis, just like when we set the expiration time for it.

If you want to cancel a job, it’s even simpler:

That’s it then hopefully this article will be useful to you! ^^

Share the news now

Source : Viblo