This repository was archived by the owner on Mar 28, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
fix(17): Add timeout to auto unlock recurring tasks #16
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
39 changes: 39 additions & 0 deletions
39
task_processor/migrations/0012_add_locked_at_and_timeout.py
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,39 @@ | ||
# Generated by Django 3.2.23 on 2025-01-06 04:51 | ||
|
||
from task_processor.migrations.helpers import PostgresOnlyRunSQL | ||
import datetime | ||
from django.db import migrations, models | ||
import os | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("task_processor", "0011_add_priority_to_get_tasks_to_process"), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name="recurringtask", | ||
name="locked_at", | ||
field=models.DateTimeField(blank=True, null=True), | ||
), | ||
migrations.AddField( | ||
model_name="recurringtask", | ||
name="timeout", | ||
field=models.DurationField(default=datetime.timedelta(minutes=30)), | ||
), | ||
migrations.AddField( | ||
model_name="task", | ||
name="timeout", | ||
field=models.DurationField(blank=True, null=True), | ||
matthewelwell marked this conversation as resolved.
Show resolved
Hide resolved
|
||
), | ||
PostgresOnlyRunSQL.from_sql_file( | ||
os.path.join( | ||
os.path.dirname(__file__), | ||
"sql", | ||
"0012_get_recurringtasks_to_process.sql", | ||
), | ||
reverse_sql="DROP FUNCTION IF EXISTS get_recurringtasks_to_process()", | ||
matthewelwell marked this conversation as resolved.
Show resolved
Hide resolved
|
||
), | ||
] |
33 changes: 33 additions & 0 deletions
33
task_processor/migrations/sql/0012_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 | ||
-- Add one minute to the timeout as a grace period for overhead | ||
WHERE is_locked = FALSE OR (locked_at IS NOT NULL AND locked_at < NOW() - timeout + INTERVAL '1 minute') | ||
ORDER BY id | ||
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() | ||
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(); | ||
matthewelwell marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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
Oops, something went wrong.
Oops, something went wrong.
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.