django-admin-background-task 0.4.0__tar.gz → 0.4.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {django_admin_background_task-0.4.0 → django_admin_background_task-0.4.1}/PKG-INFO +2 -1
- {django_admin_background_task-0.4.0 → django_admin_background_task-0.4.1}/README.md +1 -0
- {django_admin_background_task-0.4.0 → django_admin_background_task-0.4.1}/bgtask/models.py +24 -3
- {django_admin_background_task-0.4.0 → django_admin_background_task-0.4.1}/bgtask/tests/test_bgtask.py +37 -2
- {django_admin_background_task-0.4.0 → django_admin_background_task-0.4.1}/pyproject.toml +1 -1
- {django_admin_background_task-0.4.0 → django_admin_background_task-0.4.1}/LICENSE +0 -0
- {django_admin_background_task-0.4.0 → django_admin_background_task-0.4.1}/bgtask/__init__.py +0 -0
- {django_admin_background_task-0.4.0 → django_admin_background_task-0.4.1}/bgtask/admin.py +0 -0
- {django_admin_background_task-0.4.0 → django_admin_background_task-0.4.1}/bgtask/apps.py +0 -0
- {django_admin_background_task-0.4.0 → django_admin_background_task-0.4.1}/bgtask/backends/__init__.py +0 -0
- {django_admin_background_task-0.4.0 → django_admin_background_task-0.4.1}/bgtask/backends/thread_pool.py +0 -0
- {django_admin_background_task-0.4.0 → django_admin_background_task-0.4.1}/bgtask/decorators.py +0 -0
- {django_admin_background_task-0.4.0 → django_admin_background_task-0.4.1}/bgtask/migrations/0001_initial.py +0 -0
- {django_admin_background_task-0.4.0 → django_admin_background_task-0.4.1}/bgtask/migrations/0002_backgroundtask_namespace_alter_backgroundtask_name.py +0 -0
- {django_admin_background_task-0.4.0 → django_admin_background_task-0.4.1}/bgtask/migrations/0003_backgroundtask_queued_at.py +0 -0
- {django_admin_background_task-0.4.0 → django_admin_background_task-0.4.1}/bgtask/migrations/__init__.py +0 -0
- {django_admin_background_task-0.4.0 → django_admin_background_task-0.4.1}/bgtask/model_admin.py +0 -0
- {django_admin_background_task-0.4.0 → django_admin_background_task-0.4.1}/bgtask/static/bgtask/js/bgtask-once.js +0 -0
- {django_admin_background_task-0.4.0 → django_admin_background_task-0.4.1}/bgtask/static/bgtask/js/bgtask.js +0 -0
- {django_admin_background_task-0.4.0 → django_admin_background_task-0.4.1}/bgtask/templates/bgtask/admin/change_list.html +0 -0
- {django_admin_background_task-0.4.0 → django_admin_background_task-0.4.1}/bgtask/templates/bgtask/bg_changelist_status_column.html +0 -0
- {django_admin_background_task-0.4.0 → django_admin_background_task-0.4.1}/bgtask/templates/bgtask/bg_completed_column.html +0 -0
- {django_admin_background_task-0.4.0 → django_admin_background_task-0.4.1}/bgtask/templates/bgtask/bgtask_templates.html +0 -0
- {django_admin_background_task-0.4.0 → django_admin_background_task-0.4.1}/bgtask/templates/bgtask/bgtask_view.html +0 -0
- {django_admin_background_task-0.4.0 → django_admin_background_task-0.4.1}/bgtask/templates/bgtask/progress.html +0 -0
- {django_admin_background_task-0.4.0 → django_admin_background_task-0.4.1}/bgtask/templatetags/__init__.py +0 -0
- {django_admin_background_task-0.4.0 → django_admin_background_task-0.4.1}/bgtask/templatetags/bgtask.py +0 -0
- {django_admin_background_task-0.4.0 → django_admin_background_task-0.4.1}/bgtask/urls.py +0 -0
- {django_admin_background_task-0.4.0 → django_admin_background_task-0.4.1}/bgtask/views.py +0 -0
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.1
|
2
2
|
Name: django-admin-background-task
|
3
|
-
Version: 0.4.
|
3
|
+
Version: 0.4.1
|
4
4
|
Summary: A set of tools for django apps to persist and monitor the status of background tasks
|
5
5
|
Home-page: https://github.com/daphtdazz/django-bgtask
|
6
6
|
License: Apache-2.0
|
@@ -58,4 +58,5 @@ urlpatterns = [
|
|
58
58
|
from bgtask.models import BackgroundTask
|
59
59
|
|
60
60
|
BackgroundTask.new(name)
|
61
|
+
```
|
61
62
|
|
@@ -38,9 +38,11 @@ def locked(meth):
|
|
38
38
|
return _locked_meth
|
39
39
|
|
40
40
|
|
41
|
-
def only_if_state(state):
|
41
|
+
def only_if_state(state, no_op_states=frozenset()):
|
42
42
|
def only_if_state_decorator(meth):
|
43
|
-
def only_if_state_wrapper(self, *args, **kwargs):
|
43
|
+
def only_if_state_wrapper(self, *args, no_op_if_already_in_state=False, **kwargs):
|
44
|
+
if self.state in no_op_states:
|
45
|
+
return
|
44
46
|
states = (state,) if isinstance(state, str) else tuple(state)
|
45
47
|
if self.state not in states:
|
46
48
|
raise RuntimeError(
|
@@ -143,6 +145,11 @@ class BackgroundTask(models.Model):
|
|
143
145
|
queued_at__lt=self.queued_at, state=self.STATES.queued
|
144
146
|
).count()
|
145
147
|
|
148
|
+
def set_steps_to_complete(self, steps_to_complete):
|
149
|
+
self.steps_to_complete = steps_to_complete
|
150
|
+
self.steps_completed = 0
|
151
|
+
self.save()
|
152
|
+
|
146
153
|
@contextmanager
|
147
154
|
def runs_single_step(self):
|
148
155
|
try:
|
@@ -161,6 +168,14 @@ class BackgroundTask(models.Model):
|
|
161
168
|
else:
|
162
169
|
self.succeed()
|
163
170
|
|
171
|
+
@contextmanager
|
172
|
+
def fails_if_exception(self):
|
173
|
+
try:
|
174
|
+
yield
|
175
|
+
except Exception as exc:
|
176
|
+
self.fail(exc)
|
177
|
+
raise
|
178
|
+
|
164
179
|
@locked
|
165
180
|
@only_if_state(STATES.not_started)
|
166
181
|
def queue(self):
|
@@ -170,7 +185,13 @@ class BackgroundTask(models.Model):
|
|
170
185
|
self.save()
|
171
186
|
|
172
187
|
@locked
|
173
|
-
@only_if_state(
|
188
|
+
@only_if_state(
|
189
|
+
(STATES.not_started, STATES.queued),
|
190
|
+
# Allow start() to be called while running so that if a task's subtasks are queued and
|
191
|
+
# asynchronous each can call .start() independently so the first one that is executed will
|
192
|
+
# start the task.
|
193
|
+
no_op_states=(STATES.running,),
|
194
|
+
)
|
174
195
|
def start(self):
|
175
196
|
log.info("Background Task starting: %s", self.id)
|
176
197
|
self.state = self.STATES.running
|
@@ -1,7 +1,11 @@
|
|
1
1
|
import pytest
|
2
2
|
|
3
|
+
from django.utils import timezone
|
4
|
+
|
3
5
|
from bgtask.models import BackgroundTask
|
4
6
|
|
7
|
+
pytestmark = pytest.mark.django_db
|
8
|
+
|
5
9
|
|
6
10
|
@pytest.fixture
|
7
11
|
def a_task():
|
@@ -30,6 +34,37 @@ def test_bgtask_immediate_failure(a_task):
|
|
30
34
|
assert a_task.state == BackgroundTask.STATES.failed
|
31
35
|
|
32
36
|
assert len(a_task.errors) == 1
|
33
|
-
assert a_task.errors[0]["datetime"] ==
|
37
|
+
assert a_task.errors[0]["datetime"] == a_task.completed_at.isoformat()
|
34
38
|
assert a_task.errors[0]["error_message"] == "Some global failure"
|
35
|
-
assert "test_bgtask_immediate_failure" in a_task.errors[0]["
|
39
|
+
assert "test_bgtask_immediate_failure" in a_task.errors[0]["traceback"]
|
40
|
+
|
41
|
+
|
42
|
+
def test_bgtask_start_again(a_task):
|
43
|
+
a_task.start()
|
44
|
+
import time
|
45
|
+
time.sleep(0.001)
|
46
|
+
|
47
|
+
curr_started_at = a_task.started_at
|
48
|
+
assert timezone.now() > curr_started_at
|
49
|
+
|
50
|
+
# Starting again is no-op
|
51
|
+
a_task.start()
|
52
|
+
assert a_task.started_at == curr_started_at
|
53
|
+
|
54
|
+
|
55
|
+
def test_fails_if_exception(a_task):
|
56
|
+
a_task.start()
|
57
|
+
|
58
|
+
with pytest.raises(Exception):
|
59
|
+
with a_task.fails_if_exception():
|
60
|
+
raise Exception("fail!")
|
61
|
+
|
62
|
+
assert a_task.state == BackgroundTask.STATES.failed
|
63
|
+
|
64
|
+
|
65
|
+
def test_set_steps(a_task):
|
66
|
+
a_task.steps_completed = 10
|
67
|
+
a_task.set_steps_to_complete(20)
|
68
|
+
a_task.refresh_from_db()
|
69
|
+
assert a_task.steps_to_complete == 20
|
70
|
+
assert a_task.steps_completed == 0
|
@@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api"
|
|
4
4
|
|
5
5
|
[tool.poetry]
|
6
6
|
name = "django-admin-background-task"
|
7
|
-
version = "0.4.
|
7
|
+
version = "0.4.1"
|
8
8
|
description = "A set of tools for django apps to persist and monitor the status of background tasks"
|
9
9
|
authors = [
|
10
10
|
"David Park <david@greenparksoftware.co.uk>",
|
File without changes
|
{django_admin_background_task-0.4.0 → django_admin_background_task-0.4.1}/bgtask/__init__.py
RENAMED
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
{django_admin_background_task-0.4.0 → django_admin_background_task-0.4.1}/bgtask/decorators.py
RENAMED
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
{django_admin_background_task-0.4.0 → django_admin_background_task-0.4.1}/bgtask/model_admin.py
RENAMED
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|