|
8 | 8 | Human
|
9 | 9 | -----
|
10 | 10 |
|
| 11 | +Human tasks are represented by a Django :class:`View<django.views.generic.base.View>`. |
| 12 | + |
| 13 | +A user can change the processes state via a Django form or a JSON API. |
| 14 | +Anything you can do in a view you can do in a human task. They only |
| 15 | +difference to machine tasks is that they require some kind of interaction. |
| 16 | + |
| 17 | +You can use view mixins like the |
| 18 | +:class:`PermissionRequiredMixin<django.contrib.auth.mixins.PermissionRequiredMixin>` |
| 19 | +or |
| 20 | +:class:`LoginRequiredMixin<django.contrib.auth.mixins.LoginRequiredMixin>` |
| 21 | +to create your own tasks that are only available to certain users. |
| 22 | + |
| 23 | +Generic human tasks |
| 24 | +~~~~~~~~~~~~~~~~~~~ |
| 25 | + |
11 | 26 | .. automodule:: galahad.tasks.human
|
12 | 27 | :members:
|
13 | 28 |
|
14 | 29 | Machine
|
15 | 30 | -------
|
16 | 31 |
|
| 32 | +Machine tasks are represented by simple methods on the `Process` class. |
| 33 | + |
| 34 | +They can change the state and perform any action you can think of. They can |
| 35 | +decide which task to execute next (exclusive gateway) but also start or wait |
| 36 | +for multiple other tasks (split/join gateways). |
| 37 | + |
| 38 | +Furthermore tasks can implement things like sending emails or fetching data |
| 39 | +from an 3rd party API. All tasks are executed asynchronously to avoid blocking |
| 40 | +IO and locked to prevent raise conditions. |
| 41 | + |
| 42 | +Return values |
| 43 | +~~~~~~~~~~~~~ |
| 44 | + |
| 45 | +Machine tasks have three different allowed return values all of which will |
| 46 | +cause the process to behave differently: |
| 47 | + |
| 48 | +``None``: |
| 49 | + If a task returns ``None`` or anything at all the process will just |
| 50 | + proceed as planed and follow all outgoing edges and execute the nexttasks. |
| 51 | + |
| 52 | +``Iterable``: |
| 53 | + A task can return also an explicit list of tasks that should be executed |
| 54 | + next. This can be used to create exclusive gateways:: |
| 55 | + |
| 56 | + from django.utils import timezone |
| 57 | + from galahad.models import Process |
| 58 | + from galahad import tasks |
| 59 | + |
| 60 | + |
| 61 | + class ExclusiveProcess(Process): |
| 62 | + start = tasks.Start() |
| 63 | + |
| 64 | + def is_workday(self): |
| 65 | + if timezone.localtime().weekday() < 5: |
| 66 | + return [self.work] |
| 67 | + else: |
| 68 | + return [self.chill] |
| 69 | + |
| 70 | + def work(self): |
| 71 | + # pass time at the water cooler |
| 72 | + pass |
| 73 | + |
| 74 | + def chill(self): |
| 75 | + # enjoy life |
| 76 | + pass |
| 77 | + |
| 78 | + edges = ( |
| 79 | + (start, is_workday), |
| 80 | + (is_workday, work), |
| 81 | + (is_workday, chill), |
| 82 | + ) |
| 83 | + |
| 84 | + A task can also return am empty list. This will cause the process branch |
| 85 | + to come to a halt and no further stats will be started. |
| 86 | + |
| 87 | +``False``: |
| 88 | + A task can also return a boolean. Should a task return ``False`` the |
| 89 | + process will wait until the condition changes to ``True`` (or anything but |
| 90 | + ``False``):: |
| 91 | + |
| 92 | + from galahad.models import Process |
| 93 | + from galahad import tasks |
| 94 | + from django.utils import timezone |
| 95 | + |
| 96 | + |
| 97 | + class WaitingProcess(Process): |
| 98 | + start = tasks.Start() |
| 99 | + |
| 100 | + def wait_for_weekend(self): |
| 101 | + return timezone.now().weekday() >= 5 |
| 102 | + |
| 103 | + def go_home(self): |
| 104 | + # enjoy life |
| 105 | + pass |
| 106 | + |
| 107 | + edges = ( |
| 108 | + (start, wait_for_weekend), |
| 109 | + (wait_for_weekend, go_home), |
| 110 | + ) |
| 111 | + |
| 112 | +Exceptions |
| 113 | +~~~~~~~~~~ |
| 114 | + |
| 115 | +Should a task raise an exception the tasks will change it status to failed. |
| 116 | +The exception that caused the task to fail will be recorded on the task |
| 117 | +itself and further propagated. You can find and rerun failed tasks form |
| 118 | +Django's admin interface. |
| 119 | + |
| 120 | +Generic machine tasks |
| 121 | +~~~~~~~~~~~~~~~~~~~~~ |
| 122 | + |
17 | 123 | .. automodule:: galahad.tasks.machine
|
18 | 124 | :members:
|
0 commit comments