Skip to content

Commit da50d39

Browse files
committed
Blacken docs and reword slightly
1 parent 893c984 commit da50d39

File tree

2 files changed

+32
-25
lines changed

2 files changed

+32
-25
lines changed

Diff for: README.md

+31-24
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ In e.g. project.common.jobs:
4040
```python
4141
import time
4242

43+
4344
def my_task(job):
4445
logger.info("Working hard...")
4546
time.sleep(10)
@@ -52,8 +53,8 @@ In project.settings:
5253

5354
```python
5455
JOBS = {
55-
'my_job': {
56-
'tasks': ['project.common.jobs.my_task']
56+
"my_job": {
57+
"tasks": ["project.common.jobs.my_task"],
5758
},
5859
}
5960
```
@@ -69,16 +70,16 @@ A failure hook receives the failed `Job` instance along with the unhandled excep
6970

7071
```python
7172
def my_task_failure_hook(job, e):
72-
# delete some temporary files on the filesystem
73+
... # clean up after failed job
7374
```
7475

7576
To ensure this hook gets run, simply add a `failure_hook` key to your job config like so:
7677

7778
```python
7879
JOBS = {
79-
'my_job': {
80-
'tasks': ['project.common.jobs.my_task'],
81-
'failure_hook': 'project.common.jobs.my_task_failure_hook'
80+
"my_job": {
81+
"tasks": ["project.common.jobs.my_task"],
82+
"failure_hook": "project.common.jobs.my_task_failure_hook",
8283
},
8384
}
8485
```
@@ -91,16 +92,16 @@ A creation hook receives your `Job` instance as its only argument. Here's an exa
9192

9293
```python
9394
def my_task_creation_hook(job):
94-
# configure something before running your job
95+
... # configure something before running your job
9596
```
9697

9798
To ensure this hook gets run, simply add a `creation_hook` key to your job config like so:
9899

99100
```python
100101
JOBS = {
101-
'my_job': {
102-
'tasks': ['project.common.jobs.my_task'],
103-
'creation_hook': 'project.common.jobs.my_task_creation_hook'
102+
"my_job": {
103+
"tasks": ["project.common.jobs.my_task"],
104+
"creation_hook": "project.common.jobs.my_task_creation_hook",
104105
},
105106
}
106107
```
@@ -116,7 +117,7 @@ In another terminal:
116117
Using the name you configured for your job in your settings, create an instance of Job.
117118

118119
```python
119-
Job.objects.create(name='my_job')
120+
Job.objects.create(name="my_job")
120121
```
121122

122123
### Prioritising jobs
@@ -125,10 +126,11 @@ important emails to users. However, once an hour, you may need to run a _really_
125126
of emails to be dispatched before it can begin.
126127

127128
In order to make sure that an important job is run before others, you can set the `priority` field to an integer higher than `0` (the default). For example:
129+
128130
```python
129-
Job.objects.create(name='normal_job')
130-
Job.objects.create(name='important_job', priority=1)
131-
Job.objects.create(name='critical_job', priority=2)
131+
Job.objects.create(name="normal_job")
132+
Job.objects.create(name="important_job", priority=1)
133+
Job.objects.create(name="critical_job", priority=2)
132134
```
133135

134136
Jobs will be ordered by their `priority` (highest to lowest) and then the time which they were created (oldest to newest) and processed in that order.
@@ -137,7 +139,10 @@ Jobs will be ordered by their `priority` (highest to lowest) and then the time w
137139
If you'd like to create a job but have it run at some time in the future, you can use the `run_after` field on the Job model:
138140

139141
```python
140-
Job.objects.create(name='scheduled_job', run_after=timezone.now() + timedelta(minutes=10))
142+
Job.objects.create(
143+
name="scheduled_job",
144+
run_after=(timezone.now() + timedelta(minutes=10)),
145+
)
141146
```
142147

143148
Of course, the scheduled job will only be run if your `python manage.py worker` process is running at the time when the job is scheduled to run. Otherwise, it will run the next time you start your worker process after that time has passed.
@@ -154,25 +159,27 @@ The top-level abstraction of a standalone piece of work. Jobs are stored in the
154159

155160
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:
156161

157-
def my_task(job):
158-
logger.info("Doing some hard work")
159-
do_some_hard_work()
162+
```python
163+
def my_task(job):
164+
logger.info("Doing some hard work")
165+
do_some_hard_work()
166+
```
160167

161168
### Workspace
162169

163-
The *workspace* is an area that can be used 1) to provide additional arg/kwargs to task functions, and 2) to categorize jobs with additional metadata. 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).
170+
The *workspace* is an area that can be used 1) to provide additional arguments to task functions, and 2) to categorize jobs with additional metadata. 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).
164171

165172
When creating a Job, the workspace is passed as a keyword argument:
166173

167174
```python
168-
Job.objects.create(name='my_job', workspace={'key': value})
175+
Job.objects.create(name="my_job", workspace={"key": value})
169176
```
170177

171178
Then, the task function can access the workspace to get the data it needs to perform its task:
172179

173180
```python
174181
def my_task(job):
175-
cats_import = CatsImport.objects.get(pk=job.workspace['cats_import_id']
182+
cats_import = CatsImport.objects.get(pk=job.workspace["cats_import_id"])
176183
```
177184

178185
Tasks within a single job can use the workspace to communicate with each other. A single task can edit the workspace, and the modified workspace will be passed on to the next task in the sequence. For example:
@@ -186,7 +193,7 @@ Tasks within a single job can use the workspace to communicate with each other.
186193
The workspace can be queried like any [JSONField](https://docs.djangoproject.com/en/3.2/topics/db/queries/#querying-jsonfield). For instance, if you wanted to display a list of jobs that a certain user had initiated, add `user_id` to the workspace when creating the job:
187194

188195
```python
189-
Job.objects.create(name='foo', workspace={'user_id': request.user.id})
196+
Job.objects.create(name="foo", workspace={"user_id": request.user.id})
190197
```
191198

192199
Then filter the query with it in the view that renders the list:
@@ -228,8 +235,8 @@ from django_dbq.models import Job
228235

229236
...
230237

231-
Job.objects.create(name='do_work', workspace={})
232-
Job.objects.create(name='do_other_work', queue_name='other_queue', workspace={})
238+
Job.objects.create(name="do_work", workspace={})
239+
Job.objects.create(name="do_other_work", queue_name="other_queue", workspace={})
233240

234241
queue_depths = Job.get_queue_depths()
235242
print(queue_depths) # {"default": 1, "other_queue": 1}

Diff for: test-requirements.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ freezegun==0.3.12
33
mock==3.0.5
44
dj-database-url==0.5.0
55
psycopg2==2.8.4
6-
black==19.10b0
6+
black==21.12b0

0 commit comments

Comments
 (0)