sparkle
sparkle

Reputation: 7390

Active Job : How to check if same job has already been scheduled

I am using Rails Active Job with Resque and I don't want to schedule twice the same job (same arguments). Job is triggered by user.

How can check this before performing the job?

Upvotes: 4

Views: 2136

Answers (2)

johansenja
johansenja

Reputation: 678

There is a likely a reason that this is not super straightforward - it is worth making jobs idempotent, so that they can be run multiple times. If a job fails or restarts, it may end up running again.

https://github.com/mperham/sidekiq/wiki/Best-Practices#2-make-your-job-idempotent-and-transactional (I am more used to sidekiq than resque, but the principles are the same)

A better way of structuring it might be for the job to store a value somewhere in the database, and for the job to read that value and determine whether it should perform an action or exit early.

Upvotes: 2

Clemens Kofler
Clemens Kofler

Reputation: 1968

Have you tried https://github.com/neighborland/resque_solo? It claims to be a rewrite of the once more or less official resque-loner.

Alternatively, you could always consider switching to Sidekiq which has official support for unique jobs in its paid enterprise version (https://github.com/mperham/sidekiq/wiki/Ent-Unique-Jobs) as well as a separate (unofficial) gem (https://github.com/mhenrixon/sidekiq-unique-jobs).

Upvotes: 0

Related Questions