Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't reuse existing job when spawning a scheduled job #43

Closed
wants to merge 1 commit into from
Closed

Don't reuse existing job when spawning a scheduled job #43

wants to merge 1 commit into from

Conversation

extesy
Copy link

@extesy extesy commented May 24, 2014

Since any job with non-inifinte result_ttl will be eventually cleaned up by RQ, it makes it impossible to use periodic jobs with finite or zero ttl. This change changes saved job into a template to store args, timeout and ttl, but actual executed job will be different so that the template will not be affected.

Fixes #42

Since any job with non-inifinte result_ttl will be eventually cleaned up by RQ, it makes it impossible to use periodic jobs with finite or zero ttl. This change changes saved job into a template to store args, timeout and ttl, but actual executed job will be different so that the template will not be affected.
@selwin
Copy link
Collaborator

selwin commented May 30, 2014

Thanks for the PR :) I'm closing this for now because of reasons explained in #42 (comment)

@selwin selwin closed this May 30, 2014
@lost-theory
Copy link
Contributor

I am testing different failure scenarios in rq-scheduler and found it fairly easy to get it into weird states because of the re-using of job IDs. Here's an example of it silently & permanently deleting a scheduled_job under what I'd consider normal operations:

1415304441.912919 [0 [::1]:62617] "ZRANGEBYSCORE" "rq:scheduler:scheduled_jobs" "0" "1415304441"
1415304441.914764 [0 [::1]:62617] "ZREM" "rq:scheduler:scheduled_jobs" "2b8badc2-efb3-4833-9c22-1bcb74959ba7"
1415304441.914991 [0 [::1]:62617] "ZADD" "rq:scheduler:scheduled_jobs" "1415304501" "2b8badc2-efb3-4833-9c22-1bcb74959ba7"
1415304501.918851 [0 [::1]:62617] "ZRANGEBYSCORE" "rq:scheduler:scheduled_jobs" "0" "1415304501"
1415304501.920557 [0 [::1]:62617] "ZREM" "rq:scheduler:scheduled_jobs" "2b8badc2-efb3-4833-9c22-1bcb74959ba7"
1415304501.920802 [0 [::1]:62617] "ZADD" "rq:scheduler:scheduled_jobs" "1415304561" "2b8badc2-efb3-4833-9c22-1bcb74959ba7"
1415304561.923648 [0 [::1]:62617] "ZRANGEBYSCORE" "rq:scheduler:scheduled_jobs" "0" "1415304561"
1415304561.926773 [0 [::1]:62617] "ZREM" "rq:scheduler:scheduled_jobs" "2b8badc2-efb3-4833-9c22-1bcb74959ba7"
1415304621.935821 [0 [::1]:62617] "ZRANGEBYSCORE" "rq:scheduler:scheduled_jobs" "0" "1415304621"
1415304681.940967 [0 [::1]:62617] "ZRANGEBYSCORE" "rq:scheduler:scheduled_jobs" "0" "1415304681"
1415304741.943468 [0 [::1]:62617] "ZRANGEBYSCORE" "rq:scheduler:scheduled_jobs" "0" "1415304741"
1415304801.949408 [0 [::1]:62617] "ZRANGEBYSCORE" "rq:scheduler:scheduled_jobs" "0" "1415304801"
1415304861.953504 [0 [::1]:62617] "ZRANGEBYSCORE" "rq:scheduler:scheduled_jobs" "0" "1415304861"
1415304921.958572 [0 [::1]:62617] "ZRANGEBYSCORE" "rq:scheduler:scheduled_jobs" "0" "1415304921"
1415304981.961450 [0 [::1]:62617] "ZRANGEBYSCORE" "rq:scheduler:scheduled_jobs" "0" "1415304981"
1415305041.966961 [0 [::1]:62617] "ZRANGEBYSCORE" "rq:scheduler:scheduled_jobs" "0" "1415305041"
1415305080.351153 [0 [::1]:57088] "ZRANGEBYSCORE" "rq:scheduler:scheduled_jobs" "0" "1415305080"
1415305140.354161 [0 [::1]:57088] "ZRANGEBYSCORE" "rq:scheduler:scheduled_jobs" "0" "1415305140"
...the job was permanently removed at this point without my input...

I was able to do this by scheduling a job once per minute, running two workers, having the job sleep for longer than 60 seconds, then waiting 2-3 minutes so the jobs overlapped.

I'm concerned that sharing the same job ID for each run leads to race conditions & edge cases that aren't being handled. These would be solved by following @extesy's idea of using the schedule as a template and enqueueing a separate job for each run. I think that the basic correctness of running & scheduling the jobs outweighs the reasons given in #42 (e.g. that it's nicer for monitoring).

@lost-theory
Copy link
Contributor

Here's how to subclass rq_scheduler.Scheduler and override enqueue_job with @extesy's change:

https://gist.github.com/lost-theory/2e94ae20643ac32a6773

That way you don't need to maintain a fork. Just run that file instead of rqscheduler.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Periodic jobs don't work when result_ttl is not None
3 participants