Skip to content

Commit 893c984

Browse files
authored
Merge pull request #49 from johncmacy/patch-1
Clarify workspace docs
2 parents 6cd92cd + 485e5b5 commit 893c984

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

Diff for: README.md

+24-3
Original file line numberDiff line numberDiff line change
@@ -160,18 +160,39 @@ Jobs are processed to completion by *tasks*. These are simply Python functions,
160160

161161
### Workspace
162162

163-
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:
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).
164+
165+
When creating a Job, the workspace is passed as a keyword argument:
166+
167+
```python
168+
Job.objects.create(name='my_job', workspace={'key': value})
169+
```
170+
171+
Then, the task function can access the workspace to get the data it needs to perform its task:
172+
173+
```python
174+
def my_task(job):
175+
cats_import = CatsImport.objects.get(pk=job.workspace['cats_import_id']
176+
```
177+
178+
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:
164179

165180
def my_first_task(job):
166181
job.workspace['message'] = 'Hello, task 2!'
167182

168183
def my_second_task(job):
169184
logger.info("Task 1 says: %s" % job.workspace['message'])
170185

171-
When creating a Job, the workspace is passed as a keyword argument:
186+
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:
172187

173188
```python
174-
Job.objects.create(name='my_job', workspace={'key': value})
189+
Job.objects.create(name='foo', workspace={'user_id': request.user.id})
190+
```
191+
192+
Then filter the query with it in the view that renders the list:
193+
194+
```python
195+
user_jobs = Job.objects.filter(workspace__user_id=request.user.id)
175196
```
176197

177198
### Worker process

0 commit comments

Comments
 (0)