-
-
Notifications
You must be signed in to change notification settings - Fork 200
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
Use unique index on [cron_key, cron_at] columns to prevent duplicate cron jobs from being enqueued #423
Conversation
745b290
to
43ceab4
Compare
This is what shows up in the logs from the expected failed inserts:
Unfortunately it's not until Rails 7 that there will be a way for the Adapter to cleanly reject an enqueue (rails/rails#41191), which is why the |
43ceab4
to
c144467
Compare
Should the migrations all use |
Good catch, but I don't think it matters. GoodJob's migrations should be compatible with the oldest supported Rails version. I think I was originally comforted by ActiveStorage, which hardcodes versions in its migrations: https://github.com/rails/rails/blob/826f947ce7078a66e93276c8102dd235bb629911/activestorage/db/migrate/20170806125915_create_active_storage_tables.rb#L1 |
I have to admit I don't know exactly how migration compatibility works in Rails, but it seems that migrations might have different results depending on the version, see this comment and their tests. My assumption is that people would rather have migrations works the way they do on their current Rails version?
Interesting, I wonder why. I went back to the very first commit and it seems it's just accepted like that. Then again, the maintainers probably know exactly why they did this. |
c144467
to
163e418
Compare
163e418
to
b8e1a4f
Compare
I've updated the migrations to include |
b8e1a4f
to
e976a3f
Compare
…cron jobs from being enqueued
e976a3f
to
9b9df34
Compare
It seems rails 7 still has this problem
I have two process running in docker swarm, with rails 7.0.1 |
@zeevy thanks for the reminder! Rails has the capacity, but GoodJob hasn't been updated to take advantage of it. I'll write an Issue. |
Connects to #392.
cron_at
that stores the time for which the cronned job has been enqueued[cron_key, cron_at]
to ensure that only one job is enqueued for the given key and timeActiveRecord::RecordNotUnique
when multiple cron processes try to enqueue the jobI'm honestly not sure whether this is a huge improvement to Cron because it sidesteps some of the tight race conditions of
GoodJob::ActiveJobExtensions::Concurrency
... or a terrible idea because of some as yet identified database performance pressure this will generate.One bit of complexity here is that only one
good_jobs
record (the initialGoodJob::Execution
) will have thecron_at
value. So if the job is retried, the subsequent Executions in that retry chain will not have thecron_at
value. Operationally it won't affect things, but is an inconsistency in job state across executions that disappoints me.Note: I believe this should be a backwards-compatible/safe change; the unique indexes should be generatable because NULL values are not considered unique by PG, and the code will not try to fill-in the column values unless both the columns and indexes exist.