verk icon indicating copy to clipboard operation
verk copied to clipboard

[question] is it possible to configure a queue to allow to schedule a specific job only once?

Open fcevado opened this issue 7 years ago • 2 comments

i want to allow a job(same class, args and queue) to be schedule only once... is that possible? i found out that there isn't any easi api to get scheduled jobs or to verify running jobs... i kinda use Verk.SortedSet.range/4 for that but idk if that is consistent.... i've seen duplicate jobs scheduling nevertheless

fcevado avatar Jan 13 '18 00:01 fcevado

Even if you implement something with Verk.SortedSet there's always a chance for a race condition:

Suppose that the job just came out of the scheduled set.

  • Check if this job does not exist on the schedule set
  • Job is not scheduled but is running
  • Add new job to the schedule set

In this case your code will add a new job to the scheduled set.

I'm not sure what's the best approach here 🤔

edgurgel avatar Jan 14 '18 19:01 edgurgel

If the goal is simply to prevent rescheduling, one possible solution:

  1. create a new set scheduled
  2. serialize every Job struct to a string
  3. before scheduling, attempt to write the serialized Job to the scheduled set using an atomic read/update command, possibly HSETNX
  4. only schedule the Job if HSETNX returns 1, otherwise we discard it as a duplicate

The main issue that arises: how we do garbage collect the set? The maximum set size is ~14mil (2^32), so if you are doing normal-ish production volume then you can't persist the set forever. How much of a problem this is depends on the requirements you had in mind @fcevado.

keyan avatar Feb 08 '18 18:02 keyan