-
Notifications
You must be signed in to change notification settings - Fork 2
feat: auto-disable recurring task after 4 consecutive failures #221
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
200b7dd
feat: auto-disable recurring task after 4 consecutive failures
gagantrivedi 3bacecf
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] fdf59d5
fix: rename test to match three-part naming convention
gagantrivedi f358de0
test: drop unreachable cache.set in disabled-task test
gagantrivedi 3dff037
review: log auto-disable, collapse stub for coverage
gagantrivedi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| import os | ||
|
|
||
| from django.db import migrations, models | ||
|
|
||
| from common.migrations.helpers import PostgresOnlyRunSQL | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
|
|
||
| dependencies = [ | ||
| ("task_processor", "0014_add_trace_context"), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.AddField( | ||
| model_name="recurringtask", | ||
| name="is_disabled", | ||
| field=models.BooleanField(default=False), | ||
| ), | ||
| migrations.AddField( | ||
| model_name="recurringtask", | ||
| name="num_consecutive_failures", | ||
| field=models.IntegerField(default=0), | ||
| ), | ||
| PostgresOnlyRunSQL.from_sql_file( | ||
| os.path.join( | ||
| os.path.dirname(__file__), | ||
| "sql", | ||
| "0015_get_recurringtasks_to_process.sql", | ||
| ), | ||
| reverse_sql=os.path.join( | ||
| os.path.dirname(__file__), | ||
| "sql", | ||
| "0013_get_recurringtasks_to_process.sql", | ||
| ), | ||
| ), | ||
| ] |
33 changes: 33 additions & 0 deletions
33
src/task_processor/migrations/sql/0015_get_recurringtasks_to_process.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| CREATE OR REPLACE FUNCTION get_recurringtasks_to_process() | ||
| RETURNS SETOF task_processor_recurringtask AS $$ | ||
| DECLARE | ||
| row_to_return task_processor_recurringtask; | ||
| BEGIN | ||
| -- Select the tasks that needs to be processed | ||
| FOR row_to_return IN | ||
| SELECT * | ||
| FROM task_processor_recurringtask | ||
| -- Skip disabled tasks; add one minute to the timeout as a grace period for overhead | ||
| WHERE is_disabled = FALSE | ||
| AND (is_locked = FALSE OR (locked_at IS NOT NULL AND locked_at < NOW() - timeout + INTERVAL '1 minute')) | ||
| ORDER BY last_picked_at NULLS FIRST | ||
| LIMIT 1 | ||
| -- Select for update to ensure that no other workers can select these tasks while in this transaction block | ||
| FOR UPDATE SKIP LOCKED | ||
| LOOP | ||
| -- Lock every selected task(by updating `is_locked` to true) | ||
| UPDATE task_processor_recurringtask | ||
| -- Lock this row by setting is_locked True, so that no other workers can select these tasks after this | ||
| -- transaction is complete (but the tasks are still being executed by the current worker) | ||
| SET is_locked = TRUE, locked_at = NOW(), last_picked_at = NOW() | ||
| WHERE id = row_to_return.id; | ||
| -- If we don't explicitly update the columns here, the client will receive a row | ||
| -- that is locked but still shows `is_locked` as `False` and `locked_at` as `None`. | ||
| row_to_return.is_locked := TRUE; | ||
| row_to_return.locked_at := NOW(); | ||
| RETURN NEXT row_to_return; | ||
| END LOOP; | ||
|
|
||
| RETURN; | ||
| END; | ||
| $$ LANGUAGE plpgsql | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.