|
3 | 3 | Creating and using tasks
|
4 | 4 | ========================
|
5 | 5 |
|
6 |
| -TODO: I've written SO MUCH already and need to get this into production. This week. |
| 6 | +Creating tasks |
| 7 | +-------------- |
| 8 | +A task is created by subclassing one of the ``OnDemandTask``, ``PeriodicTask``, or ``SubscriberTask`` classes. In each |
| 9 | +task, the ``run`` method must be overridden and any relevant class variables set. See the `full example implementation |
| 10 | +here <https://github.com/octue/django-gcp/tree/main/tests/server>`_ to see some example tasks. |
7 | 11 |
|
8 |
| -I'll come back and explain this, I promise. |
| 12 | +Registering tasks |
| 13 | +----------------- |
| 14 | +For tasks to be registered, they must be imported in the app's ``AppConfig.ready`` method. For example, if the classes |
| 15 | +are defined in modules in a subpackage of the app called ``tasks``, the app config would look like this: |
9 | 16 |
|
10 |
| -~~ Tom ~~ |
| 17 | +.. code-block:: python |
11 | 18 |
|
12 |
| -IN THE MEANTIME: |
| 19 | + class ExampleAppConfig(AppConfig): |
| 20 | + """Example (test server) app showing how you would use django-gcp within your own django server""" |
13 | 21 |
|
14 |
| -Look at management commands available (both in django gcp and the example app), and look at the `full example implementation here <https://github.com/octue/django-gcp/tree/main/tests/server>`_ to pick up how to define and use tasks :) |
| 22 | + ... |
15 | 23 |
|
16 |
| -If you need to use this library and can't figure it out, get in touch by raising an issue on GitHub and we'll help you configure your app (and write the docs at the same time). |
| 24 | + def ready(self): |
| 25 | + # Import the tasks only once the app is ready, in order to register them. |
| 26 | + from . import tasks |
17 | 27 |
|
| 28 | +Note that this requires the task classes to be imported in ``tasks/__init__.py``. |
| 29 | + |
| 30 | +Scheduling periodic tasks |
| 31 | +------------------------- |
| 32 | +Periodic tasks are triggered by cronjobs in Google Cloud Scheduler. To create these jobs, the ``create_scheduler_jobs`` |
| 33 | +management command must be run. |
| 34 | + |
| 35 | +Setting up subscriber tasks |
| 36 | +--------------------------- |
| 37 | +Subscriber tasks are triggered by Pub/Sub messages received by subscriptions. To create these subscriptions, the |
| 38 | +``create_pubsub_subscriptions`` management command must be run. |
| 39 | + |
| 40 | +More information |
| 41 | +---------------- |
| 42 | +Have a look at the management commands available (both in ``django-gcp`` and the example app). If you are having |
| 43 | +problems, get in touch by raising an issue on GitHub and we'll help you configure your app. |
18 | 44 |
|
19 | 45 | Deduplicating tasks
|
20 | 46 | -------------------
|
|
0 commit comments