-
Notifications
You must be signed in to change notification settings - Fork 15
/
03-waiting-for-events.py
58 lines (38 loc) · 1.35 KB
/
03-waiting-for-events.py
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
import time
from liteflow.core import *
from liteflow.providers.mongo import MongoPersistenceProvider
# mongo = MongoPersistenceProvider('mongodb://localhost:27017/', 'liteflow')
class Hello(StepBody):
def run(self, context: StepExecutionContext) -> ExecutionResult:
print("Hello")
return ExecutionResult.next()
class PrintMessage(StepBody):
def __init__(self):
self.message = ""
def run(self, context: StepExecutionContext) -> ExecutionResult:
print(self.message)
return ExecutionResult.next()
class MyData:
def __init__(self):
self.event_result = None
class MyWorkflow(Workflow):
def id(self):
return "MyWorkflow"
def version(self):
return 1
def build(self, builder: WorkflowBuilder):
builder\
.start_with(Hello) \
.wait_for('event1', lambda data, context: 'key1') \
.output('event_result', lambda step: step.event_data) \
.then(PrintMessage) \
.input('message', lambda data, context: "The response is %s" % data.event_result)
host = configure_workflow_host()
host.register_workflow(MyWorkflow())
host.start()
wid = host.start_workflow("MyWorkflow", 1, MyData())
time.sleep(1)
event_data = input("Enter value to publish: ")
host.publish_event('event1', 'key1', event_data)
input()
host.stop()