-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmanage_tasks.py
More file actions
63 lines (51 loc) · 2.07 KB
/
manage_tasks.py
File metadata and controls
63 lines (51 loc) · 2.07 KB
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
63
import aiohttp
import asyncio
from camunda.external_task.external_task import ExternalTask, Variables
from camunda.external_task.external_task_worker import ExternalTaskWorker
from camunda.external_task.external_task_result import ExternalTaskResult
class Worker:
def __init__(self):
self.worker = None
self.loop = None
def start(self):
"""Run the worker and block forever"""
self.loop = asyncio.get_event_loop()
self.loop.run_until_complete(self._run())
async def _run(self):
async with aiohttp.ClientSession() as session:
self.worker = ExternalTaskWorker(
worker_id=4, base_url="http://localhost:8080/engine-rest", session=session
)
# dispatch the first subscription
self.loop.create_task(
self.worker.subscribe(topic_names="NumberCheckTask", action=number_check)
)
# and block the current task with the second subscription again
await self.worker.subscribe(topic_names="EchoTask", action=echo)
def stop(self):
self.loop.run_until_complete(self.worker.cancel())
async def number_check(task: ExternalTask) -> ExternalTaskResult:
try:
number = task.context_variables["number"]
print(f"We received {number} for checking...")
task.local_variables.set_variable(
"result", "true" if int(number) % 2 != 0 else "false", Variables.ValueType.STRING
)
return task.complete()
except Exception as err:
print(f"Oh no! Something went wrong: {err}")
return task.failure()
async def echo(task: ExternalTask) -> ExternalTaskResult:
print(f"Camunda wants to say: {task.context_variables['text']}")
await asyncio.sleep(1000)
return task.complete()
# run the main task
try:
worker = Worker()
worker.start()
except KeyboardInterrupt:
# Stopping workers might take a while.
# How long it will take depends on the chosen asyncResponseTimeout (default is 30000)
print(f"Stopping workers...")
worker.stop()
print(f"All done!")