Skip to content

Commit 8097bc8

Browse files
author
Pete Wildsmith
committed
update readme
1 parent d3e7ea9 commit 8097bc8

File tree

1 file changed

+50
-5
lines changed

1 file changed

+50
-5
lines changed

README.md

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,60 @@ Asynchronous tasks are run via a *job queue*. This system is designed to support
88

99
This is not yet production-ready, and the API is liable to change. Use at your own risk.
1010

11-
### Terminology
11+
## Getting Started
1212

13-
#### Job
13+
### Describe your job
14+
15+
In e.g. project.common.jobs:
16+
17+
```python
18+
import time
19+
20+
def my_task(job):
21+
logger.info("Working hard...")
22+
time.sleep(10)
23+
logger.info("Job's done!")
24+
```
25+
26+
### Set up your job
27+
28+
In project.settings:
29+
30+
```python
31+
JOBS = {
32+
'my_job': ['project.common.jobs.my_task'],
33+
}
34+
```
35+
36+
### Start the worker
37+
38+
In another terminal:
39+
40+
`python manage.py worker`
41+
42+
### Create a job
43+
44+
Using the name you configured for your job in your settings, create an instance of Job.
45+
46+
```python
47+
Job.objects.create(name='my_job')
48+
```
49+
50+
## Terminology
51+
52+
### Job
1453

1554
The top-level abstraction of a standalone piece of work. Jobs are stored in the database (ie they are represented as Django model instances).
1655

17-
#### Task
56+
### Task
1857

1958
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:
2059

2160
def my_task(job):
2261
logger.info("Doing some hard work")
2362
do_some_hard_work()
2463

25-
#### Workspace
64+
### Workspace
2665

2766
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:
2867

@@ -32,7 +71,13 @@ The *workspace* is an area that tasks within a single job can use to communicate
3271
def my_second_task(job):
3372
logger.info("Task 1 says: %s" % job.workspace['message'])
3473

35-
#### Worker process
74+
When creating a Job, the workspace is passed as a keyword argument:
75+
76+
```python
77+
Job.objects.create(name='my_job', workspace={'key': value})
78+
```
79+
80+
### Worker process
3681

3782
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`.
3883

0 commit comments

Comments
 (0)