summaryrefslogtreecommitdiff
path: root/1 Projects/Resumable tasks goba/Prototype resumable task.md
blob: fb58a4ebfa70e81fbf1068a2e9ba0edcbcc4d080 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
---
tags:
  - taskflow
  - "#openstack"
---
```python
import time
import contextlib

from taskflow.patterns import linear_flow as lf
from taskflow import task, engines
from taskflow.persistence import backends, models
from taskflow.exceptions import NotFound


class PrintAndWait(task.Task):
    def execute(self, *args, **kwargs):
        print(self.name)
        time.sleep(10)
        return self.name


def logbook_and_flow_detail(backend, id):
    conn = backend.get_connection()

    try:
        logbook = conn.get_logbook(id)
        flow_detail = logbook.find(id)
    except NotFound:
        logbook = models.LogBook("printer_log", uuid=id)
        flow_detail = models.FlowDetail("cool flow", uuid=id)
        logbook.add(flow_detail)

        with contextlib.closing(backend.get_connection()) as conn:
            conn.upgrade()
            conn.save_logbook(logbook)

    return logbook, flow_detail


def create_flow():
    main_flow = lf.Flow("main_flow")

    for i in range(10):
        main_flow.add(PrintAndWait(name=f"print_n_wait_{i}"))

    return main_flow


def main():
    backend = backends.fetch(conf=dict(connection="sqlite:///test.db"))
    logbook, flow_detail = logbook_and_flow_detail(backend, "1")

    flow = create_flow()
    eng = engines.load(flow, flow_detail=flow_detail, backend=backend, book=logbook)

    eng.run()


if __name__ == "__main__":
    main()
```