@colabo-flow/s-go 0.1.2 → 0.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (141) hide show
  1. package/dist/cjs/demo/create-client.d.ts +1 -0
  2. package/dist/cjs/demo/create-host.d.ts +1 -0
  3. package/dist/cjs/demo/send.d.ts +1 -0
  4. package/{index.ts → dist/cjs/index.d.ts} +3 -4
  5. package/dist/cjs/index.js +8 -0
  6. package/dist/cjs/index.js.map +1 -0
  7. package/dist/cjs/jest.config.d.ts +3 -0
  8. package/dist/cjs/lib/config.d.ts +13 -0
  9. package/dist/cjs/lib/go.amqp.client.d.ts +4 -0
  10. package/dist/cjs/lib/go.amqp.host.d.ts +6 -0
  11. package/dist/cjs/lib/go.amqp.host_status.d.ts +1 -0
  12. package/dist/cjs/lib/go.amqp.service.d.ts +1 -0
  13. package/dist/cjs/lib/operators.d.ts +2 -0
  14. package/dist/cjs/lib/params.d.ts +1 -0
  15. package/dist/cjs/lib/vos.d.ts +173 -0
  16. package/dist/cjs/test_ts.d.ts +1 -0
  17. package/dist/cjs/tests/units/chalk/index.d.ts +21 -0
  18. package/dist/cjs/tests/units/tests-vos/tasks-and-flows.vos.d.ts +20 -0
  19. package/dist/esm/demo/create-client.d.ts +1 -0
  20. package/dist/esm/demo/create-host.d.ts +1 -0
  21. package/dist/esm/demo/send.d.ts +1 -0
  22. package/dist/esm/index.d.ts +3 -0
  23. package/dist/esm/index.js +8 -0
  24. package/dist/esm/index.js.map +1 -0
  25. package/dist/esm/jest.config.d.ts +3 -0
  26. package/dist/esm/lib/config.d.ts +13 -0
  27. package/dist/esm/lib/go.amqp.client.d.ts +4 -0
  28. package/dist/esm/lib/go.amqp.host.d.ts +6 -0
  29. package/dist/esm/lib/go.amqp.host_status.d.ts +1 -0
  30. package/dist/esm/lib/go.amqp.service.d.ts +1 -0
  31. package/dist/esm/lib/operators.d.ts +2 -0
  32. package/dist/esm/lib/params.d.ts +1 -0
  33. package/dist/esm/lib/vos.d.ts +173 -0
  34. package/dist/esm/test_ts.d.ts +1 -0
  35. package/dist/esm/tests/units/chalk/index.d.ts +21 -0
  36. package/dist/esm/tests/units/tests-vos/tasks-and-flows.vos.d.ts +20 -0
  37. package/dist/index.d.ts +184 -0
  38. package/package.json +43 -6
  39. package/DEVELOPMENT.md +0 -38
  40. package/__init__.py +0 -0
  41. package/__pycache__/__init__.cpython-39.pyc +0 -0
  42. package/create_client.py +0 -84
  43. package/create_host.py +0 -91
  44. package/create_host_terra.py +0 -103
  45. package/demo/__init__.py +0 -0
  46. package/demo/create-client.ts +0 -51
  47. package/demo/create-host.ts +0 -136
  48. package/demo/send-simple-recommended.py +0 -26
  49. package/demo/send-simple.py +0 -50
  50. package/demo/send.py +0 -352
  51. package/demo/send.ts +0 -40
  52. package/demo/threading/README.md +0 -14
  53. package/demo/threading/basic_consumer_connection_threaded.png +0 -0
  54. package/demo/threading/basic_consumer_connection_threaded.py +0 -135
  55. package/demo/threading/basic_consumer_threaded.py +0 -100
  56. package/demo/threading/long-running-publisher.py +0 -68
  57. package/docs/ColaboFlow-infrastructure-diagram.png +0 -0
  58. package/docs/workflow.md +0 -35
  59. package/jest.config.ts +0 -172
  60. package/lib/__init__.py +0 -0
  61. package/lib/__pycache__/__init__.cpython-39.pyc +0 -0
  62. package/lib/__pycache__/config.cpython-39.pyc +0 -0
  63. package/lib/__pycache__/executor_operators.cpython-39.pyc +0 -0
  64. package/lib/__pycache__/executor_vos.cpython-39.pyc +0 -0
  65. package/lib/__pycache__/go_amqp_client.cpython-39.pyc +0 -0
  66. package/lib/__pycache__/go_amqp_client_threaded.cpython-39.pyc +0 -0
  67. package/lib/__pycache__/go_amqp_host.cpython-39.pyc +0 -0
  68. package/lib/__pycache__/operators.cpython-39.pyc +0 -0
  69. package/lib/__pycache__/vos.cpython-39.pyc +0 -0
  70. package/lib/config.py +0 -21
  71. package/lib/config.ts +0 -22
  72. package/lib/executor_operators.py +0 -23
  73. package/lib/executor_vos.py +0 -318
  74. package/lib/go.amqp.client-old-basic.py +0 -50
  75. package/lib/go.amqp.client.ts +0 -100
  76. package/lib/go.amqp.host-old-basic.py +0 -48
  77. package/lib/go.amqp.host.ts +0 -269
  78. package/lib/go.amqp.host_status.ts +0 -81
  79. package/lib/go.amqp.service.ts +0 -272
  80. package/lib/go_amqp_client.py +0 -263
  81. package/lib/go_amqp_client_non_threaded.py +0 -152
  82. package/lib/go_amqp_host.py +0 -348
  83. package/lib/operators.py +0 -16
  84. package/lib/operators.ts +0 -17
  85. package/lib/params.ts +0 -1
  86. package/lib/vos.py +0 -232
  87. package/lib/vos.ts +0 -212
  88. package/requirements.txt +0 -4
  89. package/tests/units/README.md +0 -12
  90. package/tests/units/chalk/index.ts +0 -41
  91. package/tests/units/go.amqp.host.test.ts +0 -116
  92. package/tests/units/tests-vos/tasks-and-flows.vos.ts +0 -128
  93. package/tsconfig.json +0 -45
  94. package/v1 - GRPC/README.md +0 -18
  95. package/v1 - GRPC/node/README.md +0 -13
  96. package/v1 - GRPC/node/colaboflow_go_actionhost_client.ts +0 -91
  97. package/v1 - GRPC/node/colaboflow_go_actionhost_server.ts +0 -84
  98. package/v1 - GRPC/node/colaboflow_go_client.ts +0 -82
  99. package/v1 - GRPC/node/colaboflow_go_server.ts +0 -84
  100. package/v1 - GRPC/node/demos/demo-colaboflow_go_actionhost_client.ts +0 -21
  101. package/v1 - GRPC/node/index.ts +0 -3
  102. package/v1 - GRPC/node/package-lock.json +0 -770
  103. package/v1 - GRPC/node/package.json +0 -30
  104. package/v1 - GRPC/node/tsconfig.json +0 -32
  105. package/v1 - GRPC/node/yarn.lock +0 -642
  106. package/v1 - GRPC/protos/colabo/flow/go/go.proto +0 -135
  107. package/v1 - GRPC/python/DEVELOPMENT.md +0 -86
  108. package/v1 - GRPC/python/DISCUSSION.md +0 -8
  109. package/v1 - GRPC/python/LICENSE.txt +0 -21
  110. package/v1 - GRPC/python/README.md +0 -45
  111. package/v1 - GRPC/python/__init__.py +0 -1
  112. package/v1 - GRPC/python/colabo/__init__.py +0 -3
  113. package/v1 - GRPC/python/colabo/flow/__init__.py +0 -3
  114. package/v1 - GRPC/python/colabo/flow/go/__init__.py +0 -12
  115. package/v1 - GRPC/python/colabo/flow/go/basic/PuzzleImporter.py +0 -77
  116. package/v1 - GRPC/python/colabo/flow/go/basic/__init__.py +0 -5
  117. package/v1 - GRPC/python/colabo/flow/go/basic/colaboflow_go_basic.py +0 -66
  118. package/v1 - GRPC/python/colabo/flow/go/basic/parseFlowJSON.py +0 -32
  119. package/v1 - GRPC/python/colabo/flow/go/colaboflow_go_actionhost.py +0 -44
  120. package/v1 - GRPC/python/colabo/flow/go/colaboflow_go_client.py +0 -48
  121. package/v1 - GRPC/python/colabo/flow/go/colaboflow_go_demo.py +0 -168
  122. package/v1 - GRPC/python/colabo/flow/go/go_pb2.py +0 -976
  123. package/v1 - GRPC/python/colabo/flow/go/go_pb2_grpc.py +0 -139
  124. package/v1 - GRPC/python/colabo.flow.go.egg-info/PKG-INFO +0 -58
  125. package/v1 - GRPC/python/colabo.flow.go.egg-info/SOURCES.txt +0 -16
  126. package/v1 - GRPC/python/colabo.flow.go.egg-info/dependency_links.txt +0 -1
  127. package/v1 - GRPC/python/colabo.flow.go.egg-info/requires.txt +0 -3
  128. package/v1 - GRPC/python/colabo.flow.go.egg-info/top_level.txt +0 -2
  129. package/v1 - GRPC/python/demo-0.py +0 -69
  130. package/v1 - GRPC/python/demo-1.py +0 -146
  131. package/v1 - GRPC/python/demo-actionhost.py +0 -47
  132. package/v1 - GRPC/python/demo-basic.md +0 -13
  133. package/v1 - GRPC/python/demo-basic.py +0 -38
  134. package/v1 - GRPC/python/demo-executeActionSync.py +0 -32
  135. package/v1 - GRPC/python/demo-flows/flow-demo.json +0 -36
  136. package/v1 - GRPC/python/demo-puzzles/puzzles/puzzle-a.py +0 -19
  137. package/v1 - GRPC/python/demo-puzzles/puzzles/puzzle-b.py +0 -21
  138. package/v1 - GRPC/python/demo-puzzles/puzzles/puzzle-c.py +0 -20
  139. package/v1 - GRPC/python/requirements.txt +0 -7
  140. package/v1 - GRPC/python/setup.py +0 -39
  141. package/v1 - GRPC/python/use-cases/__init__.py +0 -0
@@ -1,263 +0,0 @@
1
- #!/usr/bin/env python
2
- # https://pypi.org/project/pika/
3
- import pika
4
- from pika.connection import Connection
5
- import json
6
- import time
7
- from yachalk import chalk
8
- from dataclasses import replace
9
- # https://docs.python.org/3/library/uuid.html
10
- import uuid
11
- from typing import Any
12
- import asyncio
13
- from asyncio import Future
14
- import threading
15
- import dataclasses
16
-
17
- # https://pypi.org/project/setproctitle/
18
- # https://github.com/dvarrazzo/py-setproctitle
19
- # https://stackoverflow.com/a/1866700/257561
20
- from setproctitle import setproctitle
21
-
22
- from lib.config import connectConfig
23
- from lib.vos import (
24
- IColaboFlowGoMsg,
25
- EColaboFlowGoMsgType,
26
- IColaboFlowClientData,
27
- IColaboFlowGoMsgExtension_TaskExecute_Request
28
- )
29
- from lib.operators import createGoMsg
30
-
31
- EXCHANGE = ""
32
- DEBUG_TICKS = False
33
-
34
- async def run(client: IColaboFlowClientData):
35
-
36
- # TODO: make more advanced mechanism, like thread-safe singleton
37
- # https://medium.com/analytics-vidhya/how-to-create-a-thread-safe-singleton-class-in-python-822e1170a7f6
38
- if(client.running):
39
- print(f"{client.appNameTextWithContext('run')} Client already running, returning")
40
- return
41
-
42
- proc_title = f"ColaboFlow.Go.Client-{client.clientName}"
43
- setproctitle(proc_title)
44
-
45
- connect(client)
46
- # start_consuming(client)
47
- process_data_events(client)
48
-
49
- print(f"{client.appNameTextWithContext('run')} Connection established, returning")
50
-
51
- def connect(client: IColaboFlowClientData):
52
- """
53
- connects to the ColaboFlow service through the RabbitMQ broker
54
- """
55
- print(f"{client.appNameTextWithContext('connect')} Connecting to the server...")
56
- parameters = (
57
- pika.ConnectionParameters(
58
- host=client.amqpUrl,
59
- port=5672,
60
- connection_attempts=5,
61
- retry_delay=1
62
- )
63
- )
64
-
65
- # https://pika.readthedocs.io/en/stable/modules/adapters/blocking.html
66
- client.connection = pika.BlockingConnection(parameters)
67
- print(f"{client.appNameTextWithContext('connect')} Creating a channel...")
68
- client.channel = client.connection.channel()
69
- # https://github.com/MassTransit/MassTransit/issues/370
70
- print(f"{client.appNameTextWithContext('connect')} Declaring the orchestrator queue ...")
71
- client.channel.queue_declare(queue=client.queueServiceOrchestrator, durable=True, exclusive=False, auto_delete=False)
72
-
73
- client.connection.add_callback_threadsafe(lambda: _noop(client))
74
-
75
- client.running = True
76
-
77
- def _noop(client: IColaboFlowClientData):
78
- print(f"{client.appNameTextWithContext('_noop')} launched with: `client.connection.add_callback_threadsafe(lambda: _noop(client))`")
79
-
80
- def _start_consuming(client: IColaboFlowClientData):
81
- """
82
- Starts consuming messages from the ColaboFlow service through RabbitMQ broker
83
-
84
- NOTE: It runs in the context of a new connection-thread (`client.connectionThread`)
85
- it is necessary as the `client.channel.start_consuming()` function is **blocking**
86
- """
87
-
88
- while client.running:
89
- print(f"{client.appNameTextWithContext('_start_consuming')} started")
90
- # https://pika.readthedocs.io/en/stable/modules/adapters/blocking.html#pika.adapters.blocking_connection.BlockingChannel.start_consuming
91
- client.channel.start_consuming()
92
- print(f'{client.appNameTextWithContext("_start_consuming")} finished')
93
- # time.sleep(0.001)
94
- # NOTE: this will delay stopping when the stop_consuming() is triggered. We can avoid it with more clever algorithm
95
- time.sleep(0.1)
96
- # time.sleep(1)
97
- # time.sleep(2)
98
-
99
- def start_consuming(client: IColaboFlowClientData):
100
- print(f"{client.appNameTextWithContext('start_consuming')} starting '_start_consuming' as a separate thread to avoid blocking the current ({chalk.blue('the message handling')}) thread")
101
- client.consumingThread = threading.Thread(target=_start_consuming, args=(client, ))
102
- client.consumingThread.start()
103
- native_id = client.consumingThread.native_id
104
- print(f"{client.appNameTextWithContext('start_consuming')} OK, the '_start_consuming' thread (with the native native_id: {chalk.blue.bold(native_id)} ) is spawned")
105
-
106
- def _process_data_events(client: IColaboFlowClientData):
107
- """
108
- Starts consuming messages from the ColaboFlow service through RabbitMQ broker
109
-
110
- NOTE: It runs in the context of a new connection-thread (`client.connectionThread`)
111
- it is necessary as the `client.channel.process_data_events()` function is **blocking**
112
- """
113
-
114
- while client.running:
115
- if DEBUG_TICKS:
116
- print(f"{client.appNameTextWithContext('_process_data_events')} started")
117
- # https://pika.readthedocs.io/en/stable/modules/adapters/blocking.html#pika.adapters.blocking_connection.BlockingChannel.process_data_events
118
- client.connection.process_data_events(1)
119
- if DEBUG_TICKS:
120
- print(f'{client.appNameTextWithContext("_process_data_events")} finished')
121
- # time.sleep(0.001)
122
- # NOTE: this will delay stopping when the stop_consuming() is triggered. We can avoid it with more clever algorithm
123
- time.sleep(0.1)
124
- # time.sleep(1)
125
- # time.sleep(2)
126
-
127
- def process_data_events(client: IColaboFlowClientData):
128
- print(f"{client.appNameTextWithContext('process_data_events')} starting '_process_data_events' as a separate thread to avoid blocking the current ({chalk.blue('the message handling')}) thread")
129
- client.eventsThread = threading.Thread(target=_process_data_events, args=(client, ))
130
- client.eventsThread.start()
131
- native_id = client.eventsThread.native_id
132
- print(f"{client.appNameTextWithContext('process_data_events')} OK, the '_process_data_events' thread (with the native native_id: {chalk.blue.bold(native_id)} ) is spawned")
133
-
134
- def _stop_consuming(client: IColaboFlowClientData):
135
- print(f"{client.appNameTextWithContext('_stop_consuming')} stopping consuming")
136
- client.channel.stop_consuming()
137
- client.running = False
138
- print(f"{client.appNameTextWithContext('_stop_consuming')} stopped consuming")
139
-
140
- def stop_consuming(client: IColaboFlowClientData):
141
- print(f"{client.appNameTextWithContext('stop_consuming')} launching _stop_consuming in the connection-thread")
142
- client.connection.add_callback_threadsafe(lambda: _stop_consuming(client))
143
-
144
- async def async_callback_threadsafe(client: IColaboFlowClientData, _lambda)->bool:
145
-
146
- # Get the current event loop.
147
- loop = asyncio.get_running_loop()
148
-
149
- # Create a new Future object.
150
- futLambdaExecuted = loop.create_future()
151
-
152
- def _runLambdaInContext():
153
- print(f"{client.appNameTextWithContext('_runLambdaInContext')} start, futLambdaExecuted: {futLambdaExecuted}")
154
- _lambda()
155
- # futLambdaExecuted.set_result(True)
156
- loop.call_soon_threadsafe(futLambdaExecuted.set_result, True)
157
- print(f"{client.appNameTextWithContext('_runLambdaInContext')} end, futLambdaExecuted: {futLambdaExecuted}")
158
-
159
- print(f"{client.appNameTextWithContext('async_callback_threadsafe')} before add_callback_threadsafe, futLambdaExecuted: {futLambdaExecuted}")
160
- client.connection.add_callback_threadsafe(lambda: _runLambdaInContext())
161
- print(f"{client.appNameTextWithContext('async_callback_threadsafe')} awaiting futLambdaExecuted: {futLambdaExecuted}")
162
- await futLambdaExecuted
163
- print(f"{client.appNameTextWithContext('async_callback_threadsafe')} after add_callback_threadsafe, futLambdaExecuted: {futLambdaExecuted}")
164
-
165
- async def executeTask(client: IColaboFlowClientData, taskId: str, taskInput: Any):
166
- """
167
- Asks a ColaboFlow infrastructure to execute a specific task
168
-
169
- :param IColaboFlowClientData client: client we want to use to execute task through
170
- :param str taskId: the id of the task we want to execute
171
- :param Any taskInput: the input we want to provide to the task while executing
172
- """
173
-
174
- print(f'{client.appNameTextWithContext("executeTask")} taskId: "{chalk.blue_bright(taskId)}"')
175
- # Get the current event loop.
176
- loop = asyncio.get_running_loop()
177
-
178
- # Create a new Future object.
179
- futTaskExecute = loop.create_future()
180
-
181
- taskExecuteResponseQueue = f"client-task_execute_response-{uuid.uuid1()}";
182
- print(f'{client.appNameTextWithContext("executeTask")} futTaskExecute: "{futTaskExecute}"')
183
- print(f'{client.appNameTextWithContext("executeTask")} taskExecuteResponseQueue: "{taskExecuteResponseQueue}"')
184
-
185
- await async_callback_threadsafe(client, lambda: client.channel.queue_declare(queue=taskExecuteResponseQueue, exclusive=True))
186
-
187
- print(f'{client.appNameTextWithContext("executeTask")} queue "{taskExecuteResponseQueue}" declared')
188
-
189
- msgTaskExecuteReq = createGoMsg(client.clientName, EColaboFlowGoMsgType.TASK_EXECUTE_REQUEST)
190
-
191
- # https://stackoverflow.com/a/50319825/257561
192
- # msg.replyTo = "test-client",
193
- msgTaskExecuteReq.replyTo = taskExecuteResponseQueue
194
- msgTaskExecuteReq.msg = f"executing task request from python, taskId: {taskId}"
195
- msgTaskExecuteReq.extension = IColaboFlowGoMsgExtension_TaskExecute_Request(
196
- taskId = taskId,
197
- taskInput = taskInput
198
- )
199
-
200
- msgTaskExecuteReqDict: Any = dataclasses.asdict(msgTaskExecuteReq)
201
- msgTaskExecuteReqStr = json.dumps(msgTaskExecuteReqDict)
202
-
203
- print(f'{client.appNameTextWithContext("executeTask")} Sending [queue: "{chalk.blue_bright(client.queueServiceOrchestrator)}"]: {msgTaskExecuteReqDict}')
204
- # https://pika.readthedocs.io/en/stable/modules/channel.html#pika.channel.Channel.basic_publish
205
- await async_callback_threadsafe(client, lambda: client.channel.basic_publish(exchange='', routing_key=client.queueServiceOrchestrator, body=msgTaskExecuteReqStr))
206
-
207
- print(f"{client.appNameTextWithContext('executeTask')} [x] Sent {msgTaskExecuteReq}")
208
-
209
- def callback_task_execution_response_set_result(futTaskExecute, result):
210
- """
211
- Sets result IMMEDIATELY, instead of calling it with `loop.call_soon_threadsafe()` from the `callback_task_execution_response` function, which is useful so we can printout the correct, new, future status.
212
-
213
- NOTE: It runs in the context of the main thread
214
- """
215
- futTaskExecute.set_result(result)
216
- print(f'{client.appNameTextWithContext("callback_task_execution_response_set_result")} taskId: "{chalk.blue_bright(taskId)}", futTaskExecute: "{futTaskExecute}"')
217
-
218
- def callback_task_execution_response(ch, method, properties, body):
219
- """
220
- A callback invited when a message with task execution response is received
221
-
222
- NOTE: It runs in the context of a new connection-thread (`client.consumingThread`)
223
- """
224
- print(f'{client.appNameTextWithContext("callback_task_execution_response")} taskId: "{chalk.blue_bright(taskId)}" Received {body}')
225
- msg = json.loads(body)
226
- print(f'{client.appNameTextWithContext("callback_task_execution_response")} taskOutput: {msg["extension"]["taskOutput"]}')
227
- print(f'{client.appNameTextWithContext("callback_task_execution_response")} futTaskExecute: "{futTaskExecute}"')
228
- # set the `futTaskExecute` result in the context of the main-thread
229
- loop.call_soon_threadsafe(callback_task_execution_response_set_result, futTaskExecute, msg["extension"]["taskOutput"])
230
-
231
- # https://pika.readthedocs.io/en/stable/modules/channel.html#pika.channel.Channel.basic_consume
232
- # https://stackoverflow.com/questions/50404273/python-tutorial-code-from-rabbitmq-failing-to-run
233
- # TODO: client.consumerTag is a PoC, it has to be archived for each separate task that is executed
234
- client.consumerTag = client.channel.basic_consume(on_message_callback=callback_task_execution_response, queue=taskExecuteResponseQueue, auto_ack=True)
235
-
236
- print(f"{client.appNameTextWithContext('executeTask')} [*] Waiting for task execution response message. To exit press CTRL+C")
237
-
238
- # client.channel.start_consuming()
239
- # await start_consuming(client)
240
- # loop.create_task(
241
- # start_consuming(client)
242
- # )
243
-
244
- print(f'{client.appNameTextWithContext("executeTask")} Awaiting the taskId: "{chalk.blue_bright(taskId)}", futTaskExecute: {futTaskExecute}')
245
- result = await futTaskExecute
246
- print(f'{client.appNameTextWithContext("executeTask")} Leaving the taskId: "{chalk.blue_bright(taskId)}", futTaskExecute: {futTaskExecute}')
247
- return result
248
-
249
- async def close(client: IColaboFlowClientData):
250
- print(f'{client.appNameTextWithContext("close")} Closing the connection')
251
- client.running = False
252
- print(f'{client.appNameTextWithContext("close")} Waiting on all the connection-related threads to close')
253
- if(client.consumerTag):
254
- client.connection.add_callback_threadsafe(lambda: client.channel.basic_cancel(client.consumerTag))
255
- if client.consumingThread:
256
- client.consumingThread.join()
257
- if client.eventsThread:
258
- client.eventsThread.join()
259
- # print(f"{client.appNameTextWithContext('close')} Closing the {chalk.bold.italic('channel')} to the ColaboFlow service...")
260
- # client.channel.close()
261
- # print(f"{client.appNameTextWithContext('close')} Closing the {chalk.bold.italic('connection')} to the ColaboFlow service...")
262
- # client.connection.close()
263
- print(f'{client.appNameTextWithContext("close")} Connection closed')
@@ -1,152 +0,0 @@
1
- #!/usr/bin/env python
2
- # https://pypi.org/project/pika/
3
- import pika
4
- import json
5
- from yachalk import chalk
6
- from dataclasses import replace
7
- # https://docs.python.org/3/library/uuid.html
8
- import uuid
9
- from typing import Any
10
- import asyncio
11
- from asyncio import Future
12
-
13
- # https://pypi.org/project/setproctitle/
14
- # https://github.com/dvarrazzo/py-setproctitle
15
- # https://stackoverflow.com/a/1866700/257561
16
- from setproctitle import setproctitle
17
-
18
- from lib.config import connectConfig
19
- from lib.vos import (
20
- IColaboFlowGoMsg,
21
- EColaboFlowGoMsgType,
22
- IColaboFlowClientData
23
- )
24
- from lib.operators import createGoMsg
25
-
26
- EXCHANGE = ""
27
-
28
- def run(client: IColaboFlowClientData):
29
- proc_title = f"ColaboFlow.Go.Client-{client.clientName}"
30
- setproctitle(proc_title)
31
-
32
- print(f"{client.appNameTextWithContext('run')} Connecting to the server: {client.amqpUrl}");
33
- parameters = (
34
- pika.ConnectionParameters(
35
- host=client.amqpUrl,
36
- port=5672,
37
- connection_attempts=5,
38
- retry_delay=1
39
- )
40
- )
41
-
42
- client.connection = pika.BlockingConnection(parameters)
43
- client.channel = client.connection.channel()
44
- # https://github.com/MassTransit/MassTransit/issues/370
45
- client.channel.queue_declare(queue=client.queueServiceOrchestrator, durable=True, exclusive=False, auto_delete=False)
46
-
47
- # def open_channel(channel: pika.channel.Channel):
48
- # client.connection.channel(on_open_callback=on_channel_open)
49
-
50
- # def on_channel_open(channel: pika.channel.Channel):
51
- # """This method is invoked by pika when the channel has been opened.
52
- # The channel object is passed in so we can make use of it.
53
- # Since the channel is now open, we'll declare the exchange to use.
54
- # :param pika.channel.Channel channel: The channel object
55
- # """
56
- # self.setup_exchange(EXCHANGE)
57
- # client.channel = channel
58
- # client.channel.add_on_close_callback(on_channel_closed)
59
-
60
- # def on_connection_open(_unused_connection: pika.SelectConnection):
61
- # """This method is called by pika once the connection to RabbitMQ has
62
- # been established. It passes the handle to the connection object in
63
- # case we need it, but in this case, we'll just mark it unused.
64
- # :param pika.SelectConnection _unused_connection: The connection
65
- # """
66
- # client.connection = _unused_connection
67
- # open_channel()
68
-
69
- # def on_channel_closed(self, channel, reason):
70
- # """Invoked by pika when RabbitMQ unexpectedly closes the channel.
71
- # Channels are usually closed if you attempt to do something that
72
- # violates the protocol, such as re-declare an exchange or queue with
73
- # different parameters. In this case, we'll close the connection
74
- # to shutdown the object.
75
- # :param pika.channel.Channel channel: The closed channel
76
- # :param Exception reason: why the channel was closed
77
- # """
78
- # printf('Channel %i was closed: %s', channel, reason)
79
- # client.channel = None
80
- # # if not self._stopping:
81
- # # self._connection.close()
82
-
83
- # pika.SelectConnection(
84
- # parameters,
85
- # on_open_callback=on_connection_open,
86
- # on_open_error_callback=on_connection_open_error,
87
- # on_close_callback=on_connection_closed)
88
-
89
- async def start_consuming(client: IColaboFlowClientData):
90
- print(f'{client.appNameTextWithContext("start_consuming")} starting ...');
91
- # https://pika.readthedocs.io/en/stable/modules/adapters/blocking.html#pika.adapters.blocking_connection.BlockingChannel.start_consuming
92
- client.channel.start_consuming()
93
- print(f'{client.appNameTextWithContext("start_consuming")} finished');
94
-
95
- async def executeTask(futTaskExecute: Future, client: IColaboFlowClientData, taskId: str, task1Input: Any):
96
- # # Get the current event loop.
97
- loop = asyncio.get_running_loop()
98
-
99
- # # Create a new Future object.
100
- # futTaskExecute = loop.create_future()
101
-
102
- taskExecuteResponseQueue = f"client-task_execute_response-{uuid.uuid1()}";
103
- print(f'{client.appNameTextWithContext("executeTask")} futTaskExecute: "{futTaskExecute}"');
104
- print(f'{client.appNameTextWithContext("executeTask")} taskExecuteResponseQueue: "{taskExecuteResponseQueue}"');
105
- client.channel.queue_declare(queue=taskExecuteResponseQueue, exclusive=True)
106
-
107
- msg = createGoMsg(connectConfig.APP_NAME, EColaboFlowGoMsgType.TASK_EXECUTE_REQUEST);
108
-
109
- # https://stackoverflow.com/a/50319825/257561
110
- # msg.replyTo = "test-client",
111
- msg = replace(msg, replyTo = taskExecuteResponseQueue)
112
- # msg.msg = "executing task request from python",
113
- msg = replace(msg, msg = "executing task request from python")
114
- msg = replace(msg, extension = {
115
- "taskId": "com.welcometerra.video.ffmpeg-combine",
116
- "taskInput": {
117
- "a": 5,
118
- "b": 2,
119
- }
120
- })
121
-
122
- print(f'{client.appNameTextWithContext("executeTask")} Sending [queue: "{chalk.blue_bright(client.queueServiceOrchestrator)}"]: {msg.to_json()}');
123
- client.channel.basic_publish(exchange='', routing_key=client.queueServiceOrchestrator, body=msg.to_json())
124
-
125
- print(f"{client.appNameTextWithContext('executeTask')} [x] Sent {msg}")
126
-
127
- def callback_task_execution_response(ch, method, properties, body):
128
- print(f"{client.appNameTextWithContext('callback_task_execution_response')} Received {body}")
129
- msg = json.loads(body)
130
- print(f'{client.appNameTextWithContext("callback_task_execution_response")} taskOutput: {msg["extension"]["taskOutput"]}')
131
- print(f'{client.appNameTextWithContext("callback_task_execution_response")} futTaskExecute: "{futTaskExecute}"');
132
- futTaskExecute.set_result(msg["extension"]["taskOutput"])
133
- print(f'{client.appNameTextWithContext("callback_task_execution_response")} futTaskExecute: "{futTaskExecute}"');
134
-
135
- # https://pika.readthedocs.io/en/stable/modules/channel.html#pika.channel.Channel.basic_consume
136
- # https://stackoverflow.com/questions/50404273/python-tutorial-code-from-rabbitmq-failing-to-run
137
- client.channel.basic_consume(on_message_callback=callback_task_execution_response, queue=taskExecuteResponseQueue, auto_ack=True)
138
-
139
- print(f"{client.appNameTextWithContext('executeTask')} [*] Waiting for task execution response message. To exit press CTRL+C")
140
-
141
- # client.channel.start_consuming()
142
- # await start_consuming(client)
143
- loop.create_task(
144
- start_consuming(client)
145
- )
146
-
147
- print(f"{client.appNameTextWithContext('executeTask')} Leaving the executeTask")
148
- # return futTaskExecute
149
-
150
- def close():
151
- print(f"{client.appNameTextWithContext('close')} Closing the {chalk.bold.italic('connection')} to the ColaboFlow service...")
152
- connection.close()