Sidekiq jobs tags

RSpec

Sidekiq

Publié le par

Mathieu EUSTACHY

Mathieu EUSTACHY

2 minutes de lecture

I recently had to do some cleaning on jobs on a client project. We had to set up priority queues but we also wanted to be able to quickly tell to which API call the jobs belong. That is the perfect use case for this feature introduced by Sidekiq, let's have a look!


Implement your tags


Tags are an array of strings within the job payload, you can pass as many tags as you want with that option, however always keep in mind that you should keep your code as simple as possible. On my project, I used tags for API names, e.g. Facebook, etc.


Tags can be implemented on the job class or set on a specific job:


class SomeApiWorker
 sidekiq_options queue: "critical", tags: ["Facebook"]

 ...
end

# or
SomeApiWorker.set(tags: ["Facebook"]).perform_async(...)



Watch your tags in action


Now that we have implemented this "Facebook" tag, I'll be able to see it in Sidekiq web UI if I launch a bunch of jobs that have this tag:

Using Tags with Sidekiq Jobs | Mike Perham



If you use Sidekiq Pro, you will also have the ability to click on a tag, it will then display all the jobs related to that tag.


It is also worth noting that this is a good way to document your code since tags options generally come on top of your job class, so you will quickly be able to tell which is the job purpose.



The last step for use is, like always, to test these freshly implemented tags with Rspec. It is quite easy to test actually:


describe "Sidekiq options" do
 it "has 'Facebook' tag" do
  expect(SomeJob.sidekiq_options_hash["tags"]).to include("Facebook")
 end
end




Resources:

- Sidekiq changelog: https://github.com/mperham/sidekiq/blob/main/Changes.md#601

- Sidekiq Merge Request for that feature: https://github.com/mperham/sidekiq/pull/4280

- Mike Perham article on Sidekiq tags: https://www.mikeperham.com/2021/06/23/using-tags-with-sidekiq-jobs


Mes derniers articles