Skip to content

Commit

Permalink
Ensure enqueued_at is updated when the task is enqueued
Browse files Browse the repository at this point in the history
  • Loading branch information
RealOrangeOne committed Sep 13, 2024
1 parent e147532 commit 8dba0b0
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 9 deletions.
1 change: 1 addition & 0 deletions django_tasks/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"finished_at",
"started_at",
"status",
"enqueued_at",
}


Expand Down
20 changes: 14 additions & 6 deletions tests/tests/test_database_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,20 +260,23 @@ def test_priority_range_check(self) -> None:
}
}
)
@skipIf(connection.vendor == "sqlite", "SQLite locks the entire database")
def test_wait_until_transaction_commit(self) -> None:
self.assertTrue(default_task_backend.enqueue_on_commit)
self.assertTrue(
default_task_backend._get_enqueue_on_commit_for_task(test_tasks.noop_task)
)

with transaction.atomic():
test_tasks.noop_task.enqueue()
result = test_tasks.noop_task.enqueue()

self.assertIsNone(result.enqueued_at)

self.assertEqual(DBTaskResult.objects.count(), 0)
self.assertEqual(self.get_task_count_in_new_connection(), 0)

self.assertEqual(self.get_task_count_in_new_connection(), 1)
result.refresh()
self.assertIsNotNone(result.enqueued_at)

@override_settings(
TASKS={
Expand All @@ -283,20 +286,25 @@ def test_wait_until_transaction_commit(self) -> None:
}
}
)
@skipIf(connection.vendor == "sqlite", "SQLite locks the entire database")
def test_doesnt_wait_until_transaction_commit(self) -> None:
self.assertFalse(default_task_backend.enqueue_on_commit)
self.assertFalse(
default_task_backend._get_enqueue_on_commit_for_task(test_tasks.noop_task)
)

with transaction.atomic():
test_tasks.noop_task.enqueue()
result = test_tasks.noop_task.enqueue()

self.assertIsNotNone(result.enqueued_at)

self.assertEqual(DBTaskResult.objects.count(), 1)
self.assertEqual(self.get_task_count_in_new_connection(), 0)

self.assertEqual(self.get_task_count_in_new_connection(), 1)
# SQLite locks the table during this transaction
if connection.vendor != "sqlite":
self.assertEqual(self.get_task_count_in_new_connection(), 0)

if connection.vendor != "sqlite":
self.assertEqual(self.get_task_count_in_new_connection(), 1)

@override_settings(
TASKS={
Expand Down
18 changes: 15 additions & 3 deletions tests/tests/test_dummy_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,9 @@ def test_doesnt_wait_until_transaction_commit(self) -> None:
)

with transaction.atomic():
test_tasks.noop_task.enqueue()
result = test_tasks.noop_task.enqueue()

self.assertIsNotNone(result.enqueued_at)

self.assertEqual(len(default_task_backend.results), 1) # type:ignore[attr-defined]

Expand All @@ -204,11 +206,16 @@ def test_wait_until_transaction_by_default(self) -> None:
)

with transaction.atomic():
test_tasks.noop_task.enqueue()
result = test_tasks.noop_task.enqueue()

self.assertIsNone(result.enqueued_at)

self.assertEqual(len(default_task_backend.results), 0) # type:ignore[attr-defined]

self.assertEqual(len(default_task_backend.results), 1) # type:ignore[attr-defined]
self.assertIsNone(result.enqueued_at)
result.refresh()
self.assertIsNotNone(result.enqueued_at)

@override_settings(
TASKS={
Expand All @@ -228,8 +235,13 @@ def test_task_specific_enqueue_on_commit(self) -> None:
)

with transaction.atomic():
test_tasks.enqueue_on_commit_task.enqueue()
result = test_tasks.enqueue_on_commit_task.enqueue()

self.assertIsNone(result.enqueued_at)

self.assertEqual(len(default_task_backend.results), 0) # type:ignore[attr-defined]

self.assertEqual(len(default_task_backend.results), 1) # type:ignore[attr-defined]
self.assertIsNone(result.enqueued_at)
result.refresh()
self.assertIsNotNone(result.enqueued_at)
6 changes: 6 additions & 0 deletions tests/tests/test_immediate_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,11 @@ def test_wait_until_transaction_commit(self) -> None:
with transaction.atomic():
result = test_tasks.noop_task.enqueue()

self.assertIsNone(result.enqueued_at)
self.assertEqual(result.status, ResultStatus.NEW)

self.assertEqual(result.status, ResultStatus.COMPLETE)
self.assertIsNotNone(result.enqueued_at)

@override_settings(
TASKS={
Expand All @@ -251,6 +253,8 @@ def test_doesnt_wait_until_transaction_commit(self) -> None:
with transaction.atomic():
result = test_tasks.noop_task.enqueue()

self.assertIsNotNone(result.enqueued_at)

self.assertEqual(result.status, ResultStatus.COMPLETE)

self.assertEqual(result.status, ResultStatus.COMPLETE)
Expand All @@ -271,6 +275,7 @@ def test_wait_until_transaction_by_default(self) -> None:
with transaction.atomic():
result = test_tasks.noop_task.enqueue()

self.assertIsNone(result.enqueued_at)
self.assertEqual(result.status, ResultStatus.NEW)

self.assertEqual(result.status, ResultStatus.COMPLETE)
Expand All @@ -295,6 +300,7 @@ def test_task_specific_enqueue_on_commit(self) -> None:
with transaction.atomic():
result = test_tasks.enqueue_on_commit_task.enqueue()

self.assertIsNone(result.enqueued_at)
self.assertEqual(result.status, ResultStatus.NEW)

self.assertEqual(result.status, ResultStatus.COMPLETE)

0 comments on commit 8dba0b0

Please sign in to comment.