You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Simple databased-backed job queue. Jobs are defined in your settings, and are processed by management commands.
6
7
7
8
Asynchronous tasks are run via a *job queue*. This system is designed to support multi-step job workflows.
8
9
9
10
This is not yet production-ready, and the API is liable to change. Use at your own risk.
10
11
11
-
### Terminology
12
+
##Getting Started
12
13
13
-
#### Job
14
+
### Describe your job
15
+
16
+
In e.g. project.common.jobs:
17
+
18
+
```python
19
+
import time
20
+
21
+
defmy_task(job):
22
+
logger.info("Working hard...")
23
+
time.sleep(10)
24
+
logger.info("Job's done!")
25
+
```
26
+
27
+
### Set up your job
28
+
29
+
In project.settings:
30
+
31
+
```python
32
+
JOBS= {
33
+
'my_job': ['project.common.jobs.my_task'],
34
+
}
35
+
```
36
+
37
+
### Start the worker
38
+
39
+
In another terminal:
40
+
41
+
`python manage.py worker`
42
+
43
+
### Create a job
44
+
45
+
Using the name you configured for your job in your settings, create an instance of Job.
46
+
47
+
```python
48
+
Job.objects.create(name='my_job')
49
+
```
50
+
51
+
## Terminology
52
+
53
+
### Job
14
54
15
55
The top-level abstraction of a standalone piece of work. Jobs are stored in the database (ie they are represented as Django model instances).
16
56
17
-
####Task
57
+
### Task
18
58
19
59
Jobs are processed to completion by *tasks*. These are simply Python functions, which must take a single argument - the `Job` instance being processed. A single job will often require processing by more than one task to be completed fully. Creating the task functions is the responsibility of the developer. For example:
20
60
21
61
def my_task(job):
22
62
logger.info("Doing some hard work")
23
63
do_some_hard_work()
24
64
25
-
####Workspace
65
+
### Workspace
26
66
27
67
The *workspace* is an area that tasks within a single job can use to communicate with each other. It is implemented as a Python dictionary, available on the `job` instance passed to tasks as `job.workspace`. The initial workspace of a job can be empty, or can contain some parameters that the tasks require (for example, API access tokens, account IDs etc). A single task can edit the workspace, and the modified workspace will be passed on to the next task in the sequence. For example:
28
68
@@ -32,16 +72,22 @@ The *workspace* is an area that tasks within a single job can use to communicate
A *worker process* is a long-running process, implemented as a Django management command, which is responsible for executing the tasks associated with a job. There may be many worker processes running concurrently in the final system. Worker processes wait for a new job to be created in the database, and call the each associated task in the correct sequeunce.. A worker can be started using `python manage.py worker`, and a single worker instance is included in the development `procfile`.
38
84
39
85
### Configuration
40
86
41
-
Jobs are configured in the Django `settings.py` file. The `JOBS` setting is a dictionary mapping a *job name* (eg `import_hats`) to a *list* of one or more task function paths. For example:
87
+
Jobs are configured in the Django `settings.py` file. The `JOBS` setting is a dictionary mapping a *job name* (eg `import_cats`) to a *list* of one or more task function paths. For example:
0 commit comments