-
Notifications
You must be signed in to change notification settings - Fork 8
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
feat: rewrite cron to use async system #2407
Conversation
4a36b47
to
27c5c20
Compare
backend/controller/sql/schema/20240815164808_async_calls_cron_job_key.sql
Outdated
Show resolved
Hide resolved
c78af2e
to
7810fc8
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code LGTM but I don't have a ton of context in this area so it's probably best to get another set of eyes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome work @safeer , it feels really good to get this done.
FROM cron_jobs j | ||
INNER JOIN deployments d on j.deployment_id = d.id | ||
WHERE j.key = sqlc.arg('key')::cron_job_key | ||
FOR UPDATE SKIP LOCKED; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you want FOR UPDATE SKIP LOCKED
here? Above it makes sense to me, but in this case it will not return a cron job even if it exists, in the case when it is locked.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
GetCronJobByKey
is used to schedule the next execution of a completed job. Theoretically, there could be a race condition with another controller that's scheduling all cron jobs (via GetUnscheduledCronJobs
), and two dupe jobs will be scheduled, so the lock is needed here to deconflict
backend/controller/sql/schema/20240815164808_async_calls_cron_job_key.sql
Show resolved
Hide resolved
fafcba4
to
cfe16aa
Compare
4d38d19
to
fe429b8
Compare
also made AsyncOriginCron type safe
This refactor removes the cron job system's state management, cron execution, and hashring management, in favor of the async call system.
Data tables changed as follows:
cron_jobs
is still used to maintain the job liststate
, addslast_execution::timestampz
andlast_async_call_id::bigint
async_calls
is inserted to by the cron systemcron_jobs.last_async_call_id = async_calls.id
cron
origin inasync_calls.origin
After a deployment, all valid unscheduled cron jobs are scheduled; a row is added to
async_calls
with apending
state andscheduled_at
is set to the job's next execution time. The corresponding row incron_jobs
is also updated with the scheduled async call, the computed next execution time, and the inserted async call ID.On completion of a cron async call, the next execution of that job is scheduled. Effectively, every cron job will have exactly one scheduled execution.
Closes #2197