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 2 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
60 changes: 60 additions & 0 deletions
60
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,60 @@ | ||
# 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="task", | ||
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(default=datetime.timedelta(minutes=1)), | ||
), | ||
PostgresOnlyRunSQL.from_sql_file( | ||
os.path.join( | ||
os.path.dirname(__file__), | ||
"sql", | ||
"0012_get_recurringtasks_to_process.sql", | ||
), | ||
reverse_sql=os.path.join( | ||
os.path.dirname(__file__), | ||
"sql", | ||
"0008_get_recurringtasks_to_process.sql", | ||
), | ||
), | ||
PostgresOnlyRunSQL.from_sql_file( | ||
os.path.join( | ||
os.path.dirname(__file__), | ||
"sql", | ||
"0012_get_tasks_to_process.sql", | ||
), | ||
reverse_sql=os.path.join( | ||
os.path.dirname(__file__), | ||
"sql", | ||
"0011_get_tasks_to_process.sql", | ||
), | ||
), | ||
] |
32 changes: 32 additions & 0 deletions
32
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,32 @@ | ||
CREATE OR REPLACE FUNCTION get_recurringtasks_to_process(num_tasks integer) | ||
matthewelwell marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 | ||
WHERE is_locked = FALSE OR (locked_at IS NOT NULL AND locked_at < NOW() - timeout) | ||
ORDER BY id | ||
LIMIT num_tasks | ||
-- 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 | ||
|
31 changes: 31 additions & 0 deletions
31
task_processor/migrations/sql/0012_get_tasks_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,31 @@ | ||
CREATE OR REPLACE FUNCTION get_tasks_to_process(num_tasks integer) | ||
RETURNS SETOF task_processor_task AS $$ | ||
DECLARE | ||
row_to_return task_processor_task; | ||
BEGIN | ||
-- Select the tasks that needs to be processed | ||
FOR row_to_return IN | ||
SELECT * | ||
FROM task_processor_task | ||
WHERE num_failures < 3 AND scheduled_for < NOW() AND completed = FALSE AND (is_locked = FALSE OR (locked_at IS NOT NULL AND locked_at < NOW() - timeout)) | ||
ORDER BY priority ASC, scheduled_for ASC, created_at ASC | ||
LIMIT num_tasks | ||
-- 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_task | ||
-- 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; | ||
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
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.