@colabo-flow/s-go 0.1.2
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.
- package/DEVELOPMENT.md +38 -0
- package/README.md +110 -0
- package/__init__.py +0 -0
- package/__pycache__/__init__.cpython-39.pyc +0 -0
- package/create_client.py +84 -0
- package/create_host.py +91 -0
- package/create_host_terra.py +103 -0
- package/demo/__init__.py +0 -0
- package/demo/create-client.ts +51 -0
- package/demo/create-host.ts +136 -0
- package/demo/send-simple-recommended.py +26 -0
- package/demo/send-simple.py +50 -0
- package/demo/send.py +352 -0
- package/demo/send.ts +40 -0
- package/demo/threading/README.md +14 -0
- package/demo/threading/basic_consumer_connection_threaded.png +0 -0
- package/demo/threading/basic_consumer_connection_threaded.py +135 -0
- package/demo/threading/basic_consumer_threaded.py +100 -0
- package/demo/threading/long-running-publisher.py +68 -0
- package/docs/ColaboFlow-infrastructure-diagram.png +0 -0
- package/docs/workflow.md +35 -0
- package/index.ts +4 -0
- package/jest.config.ts +172 -0
- package/lib/__init__.py +0 -0
- package/lib/__pycache__/__init__.cpython-39.pyc +0 -0
- package/lib/__pycache__/config.cpython-39.pyc +0 -0
- package/lib/__pycache__/executor_operators.cpython-39.pyc +0 -0
- package/lib/__pycache__/executor_vos.cpython-39.pyc +0 -0
- package/lib/__pycache__/go_amqp_client.cpython-39.pyc +0 -0
- package/lib/__pycache__/go_amqp_client_threaded.cpython-39.pyc +0 -0
- package/lib/__pycache__/go_amqp_host.cpython-39.pyc +0 -0
- package/lib/__pycache__/operators.cpython-39.pyc +0 -0
- package/lib/__pycache__/vos.cpython-39.pyc +0 -0
- package/lib/config.py +21 -0
- package/lib/config.ts +22 -0
- package/lib/executor_operators.py +23 -0
- package/lib/executor_vos.py +318 -0
- package/lib/go.amqp.client-old-basic.py +50 -0
- package/lib/go.amqp.client.ts +100 -0
- package/lib/go.amqp.host-old-basic.py +48 -0
- package/lib/go.amqp.host.ts +269 -0
- package/lib/go.amqp.host_status.ts +81 -0
- package/lib/go.amqp.service.ts +272 -0
- package/lib/go_amqp_client.py +263 -0
- package/lib/go_amqp_client_non_threaded.py +152 -0
- package/lib/go_amqp_host.py +348 -0
- package/lib/operators.py +16 -0
- package/lib/operators.ts +17 -0
- package/lib/params.ts +1 -0
- package/lib/vos.py +232 -0
- package/lib/vos.ts +212 -0
- package/package.json +36 -0
- package/requirements.txt +4 -0
- package/tests/units/README.md +12 -0
- package/tests/units/chalk/index.ts +41 -0
- package/tests/units/go.amqp.host.test.ts +116 -0
- package/tests/units/tests-vos/tasks-and-flows.vos.ts +128 -0
- package/tsconfig.json +45 -0
- package/v1 - GRPC/README.md +18 -0
- package/v1 - GRPC/node/README.md +13 -0
- package/v1 - GRPC/node/colaboflow_go_actionhost_client.ts +91 -0
- package/v1 - GRPC/node/colaboflow_go_actionhost_server.ts +84 -0
- package/v1 - GRPC/node/colaboflow_go_client.ts +82 -0
- package/v1 - GRPC/node/colaboflow_go_server.ts +84 -0
- package/v1 - GRPC/node/demos/demo-colaboflow_go_actionhost_client.ts +21 -0
- package/v1 - GRPC/node/index.ts +3 -0
- package/v1 - GRPC/node/package-lock.json +770 -0
- package/v1 - GRPC/node/package.json +30 -0
- package/v1 - GRPC/node/tsconfig.json +32 -0
- package/v1 - GRPC/node/yarn.lock +642 -0
- package/v1 - GRPC/protos/colabo/flow/go/go.proto +135 -0
- package/v1 - GRPC/python/DEVELOPMENT.md +86 -0
- package/v1 - GRPC/python/DISCUSSION.md +8 -0
- package/v1 - GRPC/python/LICENSE.txt +21 -0
- package/v1 - GRPC/python/README.md +45 -0
- package/v1 - GRPC/python/__init__.py +1 -0
- package/v1 - GRPC/python/colabo/__init__.py +3 -0
- package/v1 - GRPC/python/colabo/flow/__init__.py +3 -0
- package/v1 - GRPC/python/colabo/flow/go/__init__.py +12 -0
- package/v1 - GRPC/python/colabo/flow/go/basic/PuzzleImporter.py +77 -0
- package/v1 - GRPC/python/colabo/flow/go/basic/__init__.py +5 -0
- package/v1 - GRPC/python/colabo/flow/go/basic/colaboflow_go_basic.py +66 -0
- package/v1 - GRPC/python/colabo/flow/go/basic/parseFlowJSON.py +32 -0
- package/v1 - GRPC/python/colabo/flow/go/colaboflow_go_actionhost.py +44 -0
- package/v1 - GRPC/python/colabo/flow/go/colaboflow_go_client.py +48 -0
- package/v1 - GRPC/python/colabo/flow/go/colaboflow_go_demo.py +168 -0
- package/v1 - GRPC/python/colabo/flow/go/go_pb2.py +976 -0
- package/v1 - GRPC/python/colabo/flow/go/go_pb2_grpc.py +139 -0
- package/v1 - GRPC/python/colabo.flow.go.egg-info/PKG-INFO +58 -0
- package/v1 - GRPC/python/colabo.flow.go.egg-info/SOURCES.txt +16 -0
- package/v1 - GRPC/python/colabo.flow.go.egg-info/dependency_links.txt +1 -0
- package/v1 - GRPC/python/colabo.flow.go.egg-info/requires.txt +3 -0
- package/v1 - GRPC/python/colabo.flow.go.egg-info/top_level.txt +2 -0
- package/v1 - GRPC/python/demo-0.py +69 -0
- package/v1 - GRPC/python/demo-1.py +146 -0
- package/v1 - GRPC/python/demo-actionhost.py +47 -0
- package/v1 - GRPC/python/demo-basic.md +13 -0
- package/v1 - GRPC/python/demo-basic.py +38 -0
- package/v1 - GRPC/python/demo-executeActionSync.py +32 -0
- package/v1 - GRPC/python/demo-flows/flow-demo.json +36 -0
- package/v1 - GRPC/python/demo-puzzles/puzzles/puzzle-a.py +19 -0
- package/v1 - GRPC/python/demo-puzzles/puzzles/puzzle-b.py +21 -0
- package/v1 - GRPC/python/demo-puzzles/puzzles/puzzle-c.py +20 -0
- package/v1 - GRPC/python/requirements.txt +7 -0
- package/v1 - GRPC/python/setup.py +39 -0
- package/v1 - GRPC/python/use-cases/__init__.py +0 -0
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
// https://www.npmjs.com/package/amqplib
|
|
2
|
+
import { connect } from "amqplib";
|
|
3
|
+
import { ChannelModel, Channel } from "amqplib/lib/channel_model";
|
|
4
|
+
// import {chalk} from "../tests/units/chalk";
|
|
5
|
+
import chalk from "chalk";
|
|
6
|
+
// https://stackoverflow.com/a/62841516/257561
|
|
7
|
+
// import uuidv1 from "uuid/v1";
|
|
8
|
+
import { v1 as uuidv1 } from "uuid";
|
|
9
|
+
|
|
10
|
+
import { connectConfig, reportsConfig } from "./config";
|
|
11
|
+
|
|
12
|
+
import { EExecutingEnvironment, EFlowPlaceType, EFlowTaskExecutionStatus, ETokenStatusInFlowPlaceInstance, IFlowTask, IFlowTaskInstance, ITaskExecutor } from "@colabo-flow/i-core/vos";
|
|
13
|
+
|
|
14
|
+
import { createTaskInstanceForTaskWithoutFlow } from "@colabo-flow/i-core/lib/flow-instance-engine";
|
|
15
|
+
import { EColaboFlowGoMsgType, EProgrammingLanguageSupported, ETaskExecute_Progress_Status, IColaboFlowGoMsg, IColaboFlowGoMsgExtension_Heartbeat, IColaboFlowGoMsgExtension_TaskExecute_Progress, IColaboFlowGoMsgExtension_TaskExecute_Request, IColaboFlowGoMsgExtension_TaskExecute_Response, IColaboFlowGoMsgExtension_TaskInstancesReport_Request, IColaboFlowGoMsgExtension_TaskInstancesReport_Response, IColaboFlowGoTaskInstanceInfo, IColaboFlowHostData } from "./vos";
|
|
16
|
+
import { createGoMsg } from "./operators";
|
|
17
|
+
|
|
18
|
+
const LANGUAGE = EProgrammingLanguageSupported.JAVASCRIPT;
|
|
19
|
+
|
|
20
|
+
let appNameText: string = "host-node";
|
|
21
|
+
|
|
22
|
+
export const hostSetTitles = (host: IColaboFlowHostData): void => {
|
|
23
|
+
process.title = `ColaboFlow.Go-${host.hostname}`;
|
|
24
|
+
host.appNameText = `[${chalk.red.bold("Colabo")}${chalk.blue.bold("Flow")}${chalk.green.bold(".Go")}-${chalk.green.yellow(host.hostname)}]`;
|
|
25
|
+
// console.log(`${host.appNameText} title is set`);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export const hostRegisterTaskExecutor = (host: IColaboFlowHostData, executor: ITaskExecutor): void => {
|
|
29
|
+
console.log(`${host.appNameText} Registering task executor, executor.id "${chalk.greenBright(executor.id)}"`);
|
|
30
|
+
host.taskExecutorsAvailable[executor.id] = executor;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
const getTaskExecutor = (host: IColaboFlowHostData, executorId: string): ITaskExecutor => {
|
|
34
|
+
return host.taskExecutorsAvailable[executorId];
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
export const hostRegisterTask = (host: IColaboFlowHostData, task: IFlowTask): void => {
|
|
38
|
+
host.tasksAvailable[task.id] = task;
|
|
39
|
+
console.log(`${host.appNameText} Registered task with task.id "${chalk.greenBright(task.id)}"`);
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
const connectToServer = async (host: IColaboFlowHostData): Promise<ChannelModel> => {
|
|
43
|
+
console.log(`${host.appNameText} Connecting to the server: ${chalk.greenBright(host.amqpUrl)}`);
|
|
44
|
+
let colaboFlowServiceConnection: ChannelModel;
|
|
45
|
+
try {
|
|
46
|
+
colaboFlowServiceConnection = await connect(host.amqpUrl);
|
|
47
|
+
return colaboFlowServiceConnection;
|
|
48
|
+
} catch (err) {
|
|
49
|
+
console.error(`Error connecting to the "${chalk.greenBright(host.amqpUrl)}" server: ${err}`);
|
|
50
|
+
throw err;
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
const connectToHost = async (host: IColaboFlowHostData, colaboFlowServiceConnection: ChannelModel): Promise<Channel> => {
|
|
55
|
+
console.log(`${host.appNameText} Opening the ${chalk.bold.italic("node host")} channel as a listener (consumer): ${chalk.greenBright(host.queueHost)}`);
|
|
56
|
+
let chHost: Channel;
|
|
57
|
+
try {
|
|
58
|
+
chHost = await colaboFlowServiceConnection.createChannel();
|
|
59
|
+
return chHost;
|
|
60
|
+
} catch (err) {
|
|
61
|
+
console.error(`${host.appNameText} Error creating the "${chalk.greenBright(host.queueHost)}" queue: ${err}`);
|
|
62
|
+
throw err;
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
const connectToServiceHeartbeat = async (host: IColaboFlowHostData, colaboFlowServiceConnection: ChannelModel): Promise<Channel> => {
|
|
67
|
+
console.log(`${host.appNameText} Opening the ${chalk.bold.italic("service heartbeat")} channel as a produced (emitter): ${chalk.greenBright(host.queueHostHeartbeat)}`);
|
|
68
|
+
try {
|
|
69
|
+
host.chHostHeartbeat = await colaboFlowServiceConnection.createChannel();
|
|
70
|
+
return host.chHostHeartbeat;
|
|
71
|
+
} catch (err) {
|
|
72
|
+
console.error(`${host.appNameText} Error creating the "${chalk.greenBright(host.queueHostHeartbeat)}" queue: ${err}`);
|
|
73
|
+
throw err;
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* the service heartbeat counter
|
|
79
|
+
*/
|
|
80
|
+
let heartbeatId: number = 0;
|
|
81
|
+
/**
|
|
82
|
+
* sends heartbeat messages back to the ColaboFlow service
|
|
83
|
+
* to
|
|
84
|
+
* 1. know the host exists
|
|
85
|
+
* 2. know or update (not implemented yet) the available task executors
|
|
86
|
+
*/
|
|
87
|
+
|
|
88
|
+
const sendHeartbeatToHost = async (host: IColaboFlowHostData) => {
|
|
89
|
+
// TODO: support listening for the heartbeat reply and set the timeout after we should send a warning/error on no response
|
|
90
|
+
const heartbeat: IColaboFlowGoMsg = {
|
|
91
|
+
...createGoMsg(host.hostname, EColaboFlowGoMsgType.HEARTBEAT),
|
|
92
|
+
extension: {
|
|
93
|
+
id: heartbeatId++,
|
|
94
|
+
queueHost: host.queueHost,
|
|
95
|
+
language: EProgrammingLanguageSupported.JAVASCRIPT,
|
|
96
|
+
taskIds: Object.keys(host.taskExecutorsAvailable),
|
|
97
|
+
} as IColaboFlowGoMsgExtension_Heartbeat,
|
|
98
|
+
msg: `node host with tasks: ${JSON.stringify(Object.keys(host.taskExecutorsAvailable))}`,
|
|
99
|
+
};
|
|
100
|
+
if (reportsConfig.showHeartbeatMsgs) {
|
|
101
|
+
console.log(`${host.appNameText} Sending heartbeat [queue: "${chalk.blueBright(host.queueHostHeartbeat)}"]: ${JSON.stringify(heartbeat)}`);
|
|
102
|
+
}
|
|
103
|
+
// https://www.w3schools.com/nodejs/met_buffer_from.asp
|
|
104
|
+
host.chHostHeartbeat.sendToQueue(host.queueHostHeartbeat, Buffer.from(JSON.stringify(heartbeat)));
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
const asyncProcessColaboFlowMsg_TaskExecute = async (host: IColaboFlowHostData, msgServiceReqObj: IColaboFlowGoMsg): Promise<void> => {
|
|
108
|
+
const msgServiceTaskExecuteReqObjExtension: IColaboFlowGoMsgExtension_TaskExecute_Request = msgServiceReqObj.extension as IColaboFlowGoMsgExtension_TaskExecute_Request;
|
|
109
|
+
const taskId: string = msgServiceTaskExecuteReqObjExtension.taskId;
|
|
110
|
+
const taskInput: any = msgServiceTaskExecuteReqObjExtension.taskInput;
|
|
111
|
+
const queueReplyTo: string = msgServiceReqObj.replyTo ?? host.queueHostHeartbeat;
|
|
112
|
+
|
|
113
|
+
const task: IFlowTask = host.tasksAvailable[taskId];
|
|
114
|
+
|
|
115
|
+
if (!task) {
|
|
116
|
+
console.error(`${host.appNameText} [processColaboFlowMsg_TaskExecute] task is missing for the id: ${taskId}`);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
console.log(`${host.appNameText} [processColaboFlowMsg_TaskExecute] Executing id: ${chalk.blueBright(taskId)}, task.description: ${task.description}`);
|
|
120
|
+
|
|
121
|
+
const taskInstanceId: string = `taskId:lang-${LANGUAGE}:host-${host.hostname}:${uuidv1()}`;
|
|
122
|
+
const taskInstance: IFlowTaskInstance = createTaskInstanceForTaskWithoutFlow(task);
|
|
123
|
+
taskInstance.id = taskInstanceId;
|
|
124
|
+
host.taskInstances[taskId] = taskInstance;
|
|
125
|
+
host.runningTaskInstancesInfos[taskId] = {
|
|
126
|
+
id: taskInstanceId,
|
|
127
|
+
taskInstance,
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
if (typeof task.executorRef?.id === "undefined") {
|
|
131
|
+
console.error(`Executor ID is not provided for the task: ${taskId}`);
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
const taskExecutor: ITaskExecutor = getTaskExecutor(host, task.executorRef?.id);
|
|
135
|
+
|
|
136
|
+
/*************************
|
|
137
|
+
* REPLYING THE TASK PROGRESS (task instance id)
|
|
138
|
+
*************************/
|
|
139
|
+
const msgTaskExecutionProgress: IColaboFlowGoMsg = {
|
|
140
|
+
...createGoMsg(host.hostname, EColaboFlowGoMsgType.TASK_EXECUTE_PROGRESS),
|
|
141
|
+
requestId: msgServiceReqObj.requestId,
|
|
142
|
+
msgReplyChainIds: [msgServiceReqObj.id],
|
|
143
|
+
extension: {
|
|
144
|
+
taskInstanceId,
|
|
145
|
+
status: ETaskExecute_Progress_Status.INITIALIZED,
|
|
146
|
+
progress: 0,
|
|
147
|
+
} as IColaboFlowGoMsgExtension_TaskExecute_Progress,
|
|
148
|
+
msg: `Returning task execution for the taskId: '${taskId}' over queue: '${queueReplyTo}'}`,
|
|
149
|
+
};
|
|
150
|
+
if (reportsConfig.showExecutionMsgs) {
|
|
151
|
+
console.log(`${host.appNameText} Sending PROGRESS to the task execution requester (service) [queue: "${chalk.blueBright(queueReplyTo)}"]: ${JSON.stringify(msgTaskExecutionProgress)}`);
|
|
152
|
+
}
|
|
153
|
+
// https://www.w3schools.com/nodejs/met_buffer_from.asp
|
|
154
|
+
host.chHostHeartbeat.sendToQueue(queueReplyTo, Buffer.from(JSON.stringify(msgTaskExecutionProgress)));
|
|
155
|
+
|
|
156
|
+
/*************************
|
|
157
|
+
* EXECUTING THE TASK
|
|
158
|
+
*************************/
|
|
159
|
+
/** task execution output */
|
|
160
|
+
const taskOutput: any = await taskExecutor.reference?.method(taskInput);
|
|
161
|
+
console.log(`${host.appNameText} [processColaboFlowMsg_TaskExecute] task executed. Result is: ${chalk.blueBright(JSON.stringify(taskOutput, null, 4))}`);
|
|
162
|
+
|
|
163
|
+
const msgTaskExecutionReply: IColaboFlowGoMsg = {
|
|
164
|
+
...createGoMsg(host.hostname, EColaboFlowGoMsgType.TASK_EXECUTE_RESPONSE),
|
|
165
|
+
requestId: msgServiceReqObj.requestId,
|
|
166
|
+
msgReplyChainIds: [msgServiceReqObj.id],
|
|
167
|
+
extension: {
|
|
168
|
+
taskOutput,
|
|
169
|
+
} as IColaboFlowGoMsgExtension_TaskExecute_Response,
|
|
170
|
+
msg: `Returning task execution for the taskId: '${taskId}' over queue: '${queueReplyTo}'}`,
|
|
171
|
+
};
|
|
172
|
+
if (reportsConfig.showExecutionMsgs) {
|
|
173
|
+
console.log(`${host.appNameText} Sending RESPONSE to the task execution requester (service) [queue: "${chalk.blueBright(queueReplyTo)}"]: ${JSON.stringify(msgTaskExecutionReply)}`);
|
|
174
|
+
}
|
|
175
|
+
// https://www.w3schools.com/nodejs/met_buffer_from.asp
|
|
176
|
+
host.chHostHeartbeat.sendToQueue(queueReplyTo, Buffer.from(JSON.stringify(msgTaskExecutionReply)));
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Process a msg with request to send report on the internal tasks instances.
|
|
181
|
+
*
|
|
182
|
+
* Based on the request (should provide running or finalized task instances, etc) it will assemble report on the internal task instances.
|
|
183
|
+
*
|
|
184
|
+
* It sends back a message with the report
|
|
185
|
+
* @param host
|
|
186
|
+
* @param msgObj
|
|
187
|
+
*/
|
|
188
|
+
const processColaboFlowMsg_TaskInstancesReport = (host: IColaboFlowHostData, msgObj: IColaboFlowGoMsg): void => {
|
|
189
|
+
const msgExtension: IColaboFlowGoMsgExtension_TaskInstancesReport_Request = msgObj.extension as IColaboFlowGoMsgExtension_TaskInstancesReport_Request;
|
|
190
|
+
const reportFinalizedTaskInstances: boolean = msgExtension.reportFinalizedTaskInstances;
|
|
191
|
+
const reportRunningTaskInstances: boolean = msgExtension.reportRunningTaskInstances;
|
|
192
|
+
|
|
193
|
+
console.log(`${host.appNameText} [processColaboFlowMsg_TaskInstancesReport] Sending task instances report to the requester: ${chalk.blueBright(msgObj.sender)}`);
|
|
194
|
+
|
|
195
|
+
const tasksInstanceInfos: Record<string, IColaboFlowGoTaskInstanceInfo> = {};
|
|
196
|
+
if (reportRunningTaskInstances) {
|
|
197
|
+
Object.assign(tasksInstanceInfos, host.runningTaskInstancesInfos);
|
|
198
|
+
}
|
|
199
|
+
if (reportFinalizedTaskInstances) {
|
|
200
|
+
Object.assign(tasksInstanceInfos, host.finalizedTaskInstancesInfos);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
const msgReply: IColaboFlowGoMsg = {
|
|
204
|
+
...createGoMsg(host.hostname, EColaboFlowGoMsgType.TASK_INSTANCES_REPORT__RESPONSE),
|
|
205
|
+
requestId: msgObj.requestId,
|
|
206
|
+
msgReplyChainIds: [msgObj.id],
|
|
207
|
+
extension: {
|
|
208
|
+
tasksInstanceInfos,
|
|
209
|
+
} as IColaboFlowGoMsgExtension_TaskInstancesReport_Response,
|
|
210
|
+
msg: `node host with tasks: ${JSON.stringify(Object.keys(host.taskExecutorsAvailable))}`,
|
|
211
|
+
};
|
|
212
|
+
const queueReplyTo: string = msgObj.replyTo ?? host.queueHostHeartbeat;
|
|
213
|
+
if (reportsConfig.showReportsMsgs) {
|
|
214
|
+
console.log(`${host.appNameText} Sending response to the task instances report [queue: "${chalk.blueBright(queueReplyTo)}"]: ${JSON.stringify(msgReply)}`);
|
|
215
|
+
}
|
|
216
|
+
// https://www.w3schools.com/nodejs/met_buffer_from.asp
|
|
217
|
+
host.chHostHeartbeat.sendToQueue(queueReplyTo, Buffer.from(JSON.stringify(msgReply)));
|
|
218
|
+
};
|
|
219
|
+
|
|
220
|
+
export const hostRun = async (host: IColaboFlowHostData): Promise<void> => {
|
|
221
|
+
process.title = `ColaboFlow.Go-${host.hostname}`;
|
|
222
|
+
appNameText = `[${chalk.red.bold("Colabo")}${chalk.blue.bold("Flow")}${chalk.green.bold(".Go")}-${chalk.green.yellow(host.hostname)}]`;
|
|
223
|
+
|
|
224
|
+
const colaboFlowServiceConnection: ChannelModel = await connectToServer(host);
|
|
225
|
+
|
|
226
|
+
const chHost: Channel = await connectToHost(host, colaboFlowServiceConnection);
|
|
227
|
+
|
|
228
|
+
await connectToServiceHeartbeat(host, colaboFlowServiceConnection);
|
|
229
|
+
|
|
230
|
+
console.log(`${host.appNameText} Hosting tasks: "${chalk.greenBright(Object.keys(host.tasksAvailable))}"`);
|
|
231
|
+
|
|
232
|
+
setInterval(() => {
|
|
233
|
+
sendHeartbeatToHost(host);
|
|
234
|
+
}, 3000);
|
|
235
|
+
|
|
236
|
+
// Listener (consumer)
|
|
237
|
+
console.log(`${host.appNameText} Listening on the queue: "${chalk.blueBright(host.queueHost)}"`);
|
|
238
|
+
// https://amqp-node.github.io/amqplib/channel_api.html#channel_assertQueue
|
|
239
|
+
await chHost.assertQueue(host.queueHost, {
|
|
240
|
+
exclusive: true,
|
|
241
|
+
});
|
|
242
|
+
console.log(`${host.appNameText} [*] Waiting for messages in %s. To exit press "${chalk.blueBright("CTRL+C")}"`);
|
|
243
|
+
chHost.consume(host.queueHost, (msg) => {
|
|
244
|
+
if (msg !== null) {
|
|
245
|
+
let msgObj: IColaboFlowGoMsg;
|
|
246
|
+
try {
|
|
247
|
+
msgObj = JSON.parse(msg.content);
|
|
248
|
+
|
|
249
|
+
console.log(`${host.appNameText} Received: "${chalk.blueBright(JSON.stringify(msgObj))}"`);
|
|
250
|
+
console.log(`${host.appNameText} msgObj.sender: "${chalk.blueBright(msgObj.sender)}", msgObj.type: "${chalk.blueBright(msgObj.type)}, msgObj.msg: "${chalk.blueBright(msgObj.msg)}"`);
|
|
251
|
+
} catch (err) {
|
|
252
|
+
console.error(`Error parsing the message: ${msg.content}`);
|
|
253
|
+
return;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
switch (msgObj.type) {
|
|
257
|
+
case EColaboFlowGoMsgType.TASK_EXECUTE_REQUEST:
|
|
258
|
+
asyncProcessColaboFlowMsg_TaskExecute(host, msgObj);
|
|
259
|
+
break;
|
|
260
|
+
case EColaboFlowGoMsgType.TASK_INSTANCES_REPORT__REQUEST:
|
|
261
|
+
processColaboFlowMsg_TaskInstancesReport(host, msgObj);
|
|
262
|
+
break;
|
|
263
|
+
}
|
|
264
|
+
chHost.ack(msg);
|
|
265
|
+
} else {
|
|
266
|
+
console.log("${host.appNameText} Consumer cancelled by server");
|
|
267
|
+
}
|
|
268
|
+
});
|
|
269
|
+
};
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
// https://www.npmjs.com/package/amqplib
|
|
2
|
+
import { connect } from "amqplib";
|
|
3
|
+
import { ChannelModel, Channel } from "amqplib/lib/channel_model";
|
|
4
|
+
import chalk from "chalk";
|
|
5
|
+
import { v1 as uuidv1 } from "uuid";
|
|
6
|
+
|
|
7
|
+
import { connectConfig } from "./config";
|
|
8
|
+
import { EColaboFlowGoMsgType, IColaboFlowGoMsg, IColaboFlowGoMsgExtension_TaskExecute_Request, IColaboFlowGoMsgExtension_TaskInstancesReport_Request } from "./vos";
|
|
9
|
+
import { createGoMsg } from "./operators";
|
|
10
|
+
|
|
11
|
+
process.title = "ColaboFlow.Go.Client-Node";
|
|
12
|
+
const appNameText = `[${chalk.red.bold("Colabo")}${chalk.blue.bold("Flow")}${chalk.green.bold(".Go")}${chalk.green.yellow(".Client-Node")}]`;
|
|
13
|
+
|
|
14
|
+
(async () => {
|
|
15
|
+
const queue = connectConfig.HOST_NODE_QUEUE;
|
|
16
|
+
console.log(`${appNameText} Connecting to the ColaboFlow node host: ${chalk.greenBright(connectConfig.AMQP_URL)}`);
|
|
17
|
+
let colaboFlowService: ChannelModel;
|
|
18
|
+
try {
|
|
19
|
+
colaboFlowService = await connect(connectConfig.AMQP_URL);
|
|
20
|
+
} catch (err) {
|
|
21
|
+
console.error(`${appNameText} Error connecting to the "${chalk.greenBright(connectConfig.AMQP_URL)}" ColaboFlow service: ${err}`);
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
let colaboFlowServiceChannel: Channel;
|
|
26
|
+
try {
|
|
27
|
+
colaboFlowServiceChannel = await colaboFlowService.createChannel();
|
|
28
|
+
} catch (err) {
|
|
29
|
+
console.error(`${appNameText} Error creating the "${chalk.greenBright(connectConfig.HOST_NODE_QUEUE)}" queue: ${err}`);
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const hostStatusResponseQueue = `host_status_response-${uuidv1()}`;
|
|
34
|
+
const taskInstancesRequest: IColaboFlowGoMsg = {
|
|
35
|
+
...createGoMsg("send.ts", EColaboFlowGoMsgType.TASK_INSTANCES_REPORT__REQUEST),
|
|
36
|
+
replyTo: hostStatusResponseQueue,
|
|
37
|
+
extension: {
|
|
38
|
+
reportFinalizedTaskInstances: true,
|
|
39
|
+
reportRunningTaskInstances: true,
|
|
40
|
+
} as IColaboFlowGoMsgExtension_TaskInstancesReport_Request,
|
|
41
|
+
msg: "get the tasks' reports",
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
// https://amqp-node.github.io/amqplib/channel_api.html#channel_assertQueue
|
|
45
|
+
await colaboFlowServiceChannel.assertQueue(hostStatusResponseQueue, {
|
|
46
|
+
exclusive: true,
|
|
47
|
+
});
|
|
48
|
+
colaboFlowServiceChannel.consume(
|
|
49
|
+
hostStatusResponseQueue,
|
|
50
|
+
function (msg) {
|
|
51
|
+
// if (msg.properties.correlationId == correlationId) {
|
|
52
|
+
console.log("${appNameText} Task instances %s", JSON.parse(msg.content));
|
|
53
|
+
setTimeout(async () => {
|
|
54
|
+
console.log(`${appNameText} Closing the ${chalk.bold.italic("channel")} to the ColaboFlow service...`);
|
|
55
|
+
await colaboFlowServiceChannel.close();
|
|
56
|
+
|
|
57
|
+
console.log(`${appNameText} Closing the ${chalk.bold.italic("connection")} to the ColaboFlow service...`);
|
|
58
|
+
await colaboFlowService.close();
|
|
59
|
+
|
|
60
|
+
// colaboFlowService.close();
|
|
61
|
+
process.exit(0);
|
|
62
|
+
}, 500);
|
|
63
|
+
// }
|
|
64
|
+
},
|
|
65
|
+
{
|
|
66
|
+
noAck: true,
|
|
67
|
+
}
|
|
68
|
+
);
|
|
69
|
+
|
|
70
|
+
console.log(`${appNameText} Sending task request [queue: "${chalk.blueBright(queue)}"]: ${JSON.stringify(taskInstancesRequest)}`);
|
|
71
|
+
// https://www.w3schools.com/nodejs/met_buffer_from.asp
|
|
72
|
+
colaboFlowServiceChannel.sendToQueue(queue, Buffer.from(JSON.stringify(taskInstancesRequest)));
|
|
73
|
+
|
|
74
|
+
// console.log(`${appNameText} Closing the ${chalk.bold.italic("channel")} to the ColaboFlow service...`);
|
|
75
|
+
// await colaboFlowServiceChannel.close();
|
|
76
|
+
|
|
77
|
+
// console.log(`${appNameText} Closing the ${chalk.bold.italic("connection")} to the ColaboFlow service...`);
|
|
78
|
+
// await colaboFlowService.close();
|
|
79
|
+
|
|
80
|
+
// // colaboFlowService.close();
|
|
81
|
+
})();
|
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
// https://www.npmjs.com/package/amqplib
|
|
2
|
+
import { connect } from "amqplib";
|
|
3
|
+
import { ChannelModel, Channel } from "amqplib/lib/channel_model";
|
|
4
|
+
import chalk from "chalk";
|
|
5
|
+
import { v1 as uuidv1 } from "uuid";
|
|
6
|
+
|
|
7
|
+
import { connectConfig, reportsConfig } from "./config";
|
|
8
|
+
import { createGoMsg } from "./operators";
|
|
9
|
+
import { EColaboFlowGoMsgType, IColaboFlowGoMsg, IColaboFlowGoMsgExtension_TaskExecute_Request, IColaboFlowServiceData, IColaboFlowGoMsgExtension_Heartbeat, IColaboFlowGoMsgExtension_Heartbeat_Response, IColaboFlowServiceHostData, IColaboFlowGoMsgExtension_TaskExecute_Response, IColaboFlowGoMsgExtension_TaskExecute_Progress } from "./vos";
|
|
10
|
+
|
|
11
|
+
process.title = "ColaboFlow.Go.Orchestrator";
|
|
12
|
+
const appNameText = `[${chalk.red.bold("Colabo")}${chalk.blue.bold("Flow")}${chalk.green.bold(".Go")}${chalk.green.yellow(".Orchestrator")}]`;
|
|
13
|
+
|
|
14
|
+
const service: IColaboFlowServiceData = {
|
|
15
|
+
serviceName: `${connectConfig.APP_NAME}-service`,
|
|
16
|
+
amqpUrl: connectConfig.AMQP_URL,
|
|
17
|
+
queueHostHeartbeat: connectConfig.HOST_HEARTBEAT_QUEUE,
|
|
18
|
+
queueOrchestrator: connectConfig.ORCHESTRATOR_QUEUE,
|
|
19
|
+
finalizedTaskInstancesInfos: {},
|
|
20
|
+
runningTaskInstancesInfos: {},
|
|
21
|
+
hosts: {},
|
|
22
|
+
hostsForTaskIds: {},
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
const asyncProcessColaboFlowMsg_TaskExecute = async (service: IColaboFlowServiceData, msgClientReqObj: IColaboFlowGoMsg): Promise<void> => {
|
|
26
|
+
/**
|
|
27
|
+
* process the request from a client
|
|
28
|
+
* */
|
|
29
|
+
const msgClientReqExtension: IColaboFlowGoMsgExtension_TaskExecute_Request = msgClientReqObj.extension as IColaboFlowGoMsgExtension_TaskExecute_Request;
|
|
30
|
+
const taskId: string = msgClientReqExtension.taskId;
|
|
31
|
+
|
|
32
|
+
console.log(`${appNameText} Executing task taskId: "${chalk.blueBright(taskId)}`);
|
|
33
|
+
/** the name of the queue that the host should return the task execution result and we should listen in the service for it */
|
|
34
|
+
const taskExecuteResponseQueue = `service-task_execute_response-${uuidv1()}`;
|
|
35
|
+
|
|
36
|
+
const hostsForTaskId: string[] = service.hostsForTaskIds[taskId];
|
|
37
|
+
if (!hostsForTaskId || hostsForTaskId.length <= 0) {
|
|
38
|
+
console.error(`${appNameText} Error, there is no host implementing the task: "${taskId}"`);
|
|
39
|
+
// TODO: return the response
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// TODO: just a simple assumption of a single host implementing the task
|
|
44
|
+
const hostName: string = hostsForTaskId[0];
|
|
45
|
+
const hostData: IColaboFlowServiceHostData = service.hosts[hostName];
|
|
46
|
+
|
|
47
|
+
console.log(`${appNameText} Delivering the task "${chalk.blueBright(taskId)}" to host "${chalk.blueBright(hostName)}" via queue "${chalk.blueBright(hostData.queueHost)}" and listening for the response on the queue: "${chalk.blueBright(taskExecuteResponseQueue)}"`);
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* send the request to the host
|
|
51
|
+
* */
|
|
52
|
+
const msgHostObjRequest: IColaboFlowGoMsg = {
|
|
53
|
+
...createGoMsg(service.serviceName, EColaboFlowGoMsgType.TASK_EXECUTE_REQUEST),
|
|
54
|
+
replyTo: taskExecuteResponseQueue,
|
|
55
|
+
extension: msgClientReqExtension as IColaboFlowGoMsgExtension_TaskExecute_Request,
|
|
56
|
+
msg: "run a ffmepg test task",
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
console.log(`${appNameText} Creating the task execution response queue: "${chalk.blueBright(taskExecuteResponseQueue)}" ...`);
|
|
60
|
+
/**
|
|
61
|
+
* listen for the response from the host
|
|
62
|
+
* */
|
|
63
|
+
// https://amqp-node.github.io/amqplib/channel_api.html#channel_assertQueue
|
|
64
|
+
await service.hostsChannel.assertQueue(taskExecuteResponseQueue, {
|
|
65
|
+
exclusive: true,
|
|
66
|
+
});
|
|
67
|
+
// NOTE: very important to send the message AFTER activating listener (consumer) on the listening `taskExecuteResponseQueue` queue as otherwise the response from the host could have arrived back BEFORE the listener (consumer) is activated. This was a REAL rat-racing issue!!! (probably due to the long time with handshaking and enabling a new `taskExecuteResponseQueue` with RabbitMQ)
|
|
68
|
+
service.hostsChannel.consume(
|
|
69
|
+
taskExecuteResponseQueue,
|
|
70
|
+
function (msgHostRes: any) {
|
|
71
|
+
console.log(`${appNameText} Task Execution reply on the queue: "${chalk.blueBright(taskExecuteResponseQueue)}" for the taskId: "${chalk.blueBright(taskId)}"`);
|
|
72
|
+
let msgHostResObj: IColaboFlowGoMsg;
|
|
73
|
+
try {
|
|
74
|
+
msgHostResObj = JSON.parse(msgHostRes.content);
|
|
75
|
+
|
|
76
|
+
// console.log(`${appNameText} Received: "${chalk.blueBright(JSON.stringify(msgHostResObj))}"`);
|
|
77
|
+
// console.log(`${appNameText} msgObj.sender: "${chalk.blueBright(msgHostResObj.sender)}", msgObj.type: "${chalk.blueBright(msgHostResObj.type)}, msgObj.msg: "${chalk.blueBright(msgHostResObj.msg)}"`);
|
|
78
|
+
} catch (err) {
|
|
79
|
+
console.error(`Error parsing the message: ${msgHostRes.content}`);
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
switch (msgHostResObj.type) {
|
|
84
|
+
case EColaboFlowGoMsgType.TASK_EXECUTE_PROGRESS:
|
|
85
|
+
const msgHostProgressObjExtension: IColaboFlowGoMsgExtension_TaskExecute_Progress = msgHostResObj.extension as IColaboFlowGoMsgExtension_TaskExecute_Progress;
|
|
86
|
+
console.log(`${appNameText} Task Execution PROGRESS: ${JSON.stringify(msgHostProgressObjExtension)}`);
|
|
87
|
+
break;
|
|
88
|
+
|
|
89
|
+
case EColaboFlowGoMsgType.TASK_EXECUTE_RESPONSE:
|
|
90
|
+
const msgHostResObjExtension: IColaboFlowGoMsgExtension_TaskExecute_Response = msgHostResObj.extension as IColaboFlowGoMsgExtension_TaskExecute_Response;
|
|
91
|
+
|
|
92
|
+
console.log(`${appNameText} Task Execution RESPONSE: ${JSON.stringify(msgHostResObjExtension.taskOutput)}`);
|
|
93
|
+
|
|
94
|
+
if (!msgClientReqObj.replyTo) {
|
|
95
|
+
console.error(`${appNameText} replyTo is missing in the msgClientReqObj [msgClientReqObj.id: "${chalk.blueBright(msgClientReqObj.id)}"]`);
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
const queueReplyTo: string = msgClientReqObj.replyTo;
|
|
99
|
+
const msgClientRes: IColaboFlowGoMsg = {
|
|
100
|
+
...createGoMsg(service.serviceName, EColaboFlowGoMsgType.TASK_EXECUTE_RESPONSE),
|
|
101
|
+
requestId: msgClientReqObj.requestId,
|
|
102
|
+
msgReplyChainIds: [msgClientReqObj.id],
|
|
103
|
+
extension: msgHostResObjExtension,
|
|
104
|
+
msg: `Host with tasks`,
|
|
105
|
+
};
|
|
106
|
+
console.log(`${appNameText} Sending the task Execution response back to the client "${chalk.blueBright(msgClientReqObj.sender)}" through queue: "${chalk.blueBright(queueReplyTo)}"`);
|
|
107
|
+
// https://www.w3schools.com/nodejs/met_buffer_from.asp
|
|
108
|
+
service.clientsChannel.sendToQueue(queueReplyTo, Buffer.from(JSON.stringify(msgClientRes)));
|
|
109
|
+
|
|
110
|
+
break;
|
|
111
|
+
}
|
|
112
|
+
// no need due to `noAck: true,`
|
|
113
|
+
// service.hostsChannel.ack(msgHostRes);
|
|
114
|
+
},
|
|
115
|
+
{
|
|
116
|
+
// check if it is good to have it
|
|
117
|
+
noAck: true,
|
|
118
|
+
}
|
|
119
|
+
);
|
|
120
|
+
|
|
121
|
+
console.log(`${appNameText} Listening on the queue: "${chalk.blueBright(taskExecuteResponseQueue)}" for the execution response for the task "${chalk.blueBright(taskId)}"`);
|
|
122
|
+
|
|
123
|
+
// https://www.w3schools.com/nodejs/met_buffer_from.asp
|
|
124
|
+
// NOTE: very important to send the message AFTER activating listener (consumer) on the listening `taskExecuteResponseQueue` queue as otherwise the response from the host could have arrived back BEFORE the listener (consumer) is activated. This was a REAL rat-racing issue!!! (probably due to the long time with handshaking and enabling a new `taskExecuteResponseQueue` with RabbitMQ)
|
|
125
|
+
service.hostsChannel.sendToQueue(hostData.queueHost, Buffer.from(JSON.stringify(msgHostObjRequest)));
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
const asyncProcessColaboFlowMsg_Heartbeat = async (service: IColaboFlowServiceData, msgObj: IColaboFlowGoMsg): Promise<void> => {
|
|
129
|
+
const msgExtension: IColaboFlowGoMsgExtension_Heartbeat = msgObj.extension as IColaboFlowGoMsgExtension_Heartbeat;
|
|
130
|
+
|
|
131
|
+
const hostName: string = msgObj.sender;
|
|
132
|
+
// console.log(`${appNameText} [processColaboFlowMsg_TaskExecute] Executing id: ${chalk.blueBright(taskId)}, task.description: ${task.description}`);
|
|
133
|
+
|
|
134
|
+
// we got a new host
|
|
135
|
+
if (!(hostName in service.hosts)) {
|
|
136
|
+
console.log(`${appNameText} Discovered a new host: "${chalk.blueBright(hostName)}" with the tasks available: ${chalk.blueBright(JSON.stringify(msgExtension.taskIds))}`);
|
|
137
|
+
service.hosts[hostName] = {
|
|
138
|
+
hostName,
|
|
139
|
+
queueHost: msgExtension.queueHost,
|
|
140
|
+
taskIdsAvailable: msgExtension.taskIds,
|
|
141
|
+
taskInstanceIds: [],
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
const hostData: IColaboFlowServiceHostData = service.hosts[hostName];
|
|
146
|
+
// update the available tasks on each heartbeat as they can evolve
|
|
147
|
+
hostData.taskIdsAvailable = msgExtension.taskIds;
|
|
148
|
+
|
|
149
|
+
for (const taskId of msgExtension.taskIds) {
|
|
150
|
+
if (!service.hostsForTaskIds[taskId]) service.hostsForTaskIds[taskId] = [];
|
|
151
|
+
if (service.hostsForTaskIds[taskId].indexOf(hostName) < 0) {
|
|
152
|
+
service.hostsForTaskIds[taskId].push(hostName);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
if (reportsConfig.showHeartbeatMsgs) {
|
|
157
|
+
console.log(`${appNameText} Received heartbeat: "${chalk.blueBright(msgObj)}"`);
|
|
158
|
+
console.log(`${appNameText} [${msgObj.id}] hostName: "${chalk.blueBright(hostName)}", msgObj.type: "${chalk.blueBright(msgObj.type)}, msgExtension.language: "${chalk.blueBright(msgExtension.language)}, msgExtension.tasks: "${chalk.blueBright(msgExtension.taskIds)}, msgObj.msg: "${chalk.blueBright(msgObj.msg)}"`);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// TODO: Send heartbeat reply
|
|
162
|
+
// We want the host know that we (service) are there listening
|
|
163
|
+
// const msgHeartbeatReply: IColaboFlowGoMsg = {
|
|
164
|
+
// ...createGoMsg(service.serviceName, EColaboFlowGoMsgType.TASK_INSTANCES_REPORT__RESPONSE),
|
|
165
|
+
// requestId: msgObj.requestId,
|
|
166
|
+
// msgReplyChainIds: [msgObj.id],
|
|
167
|
+
// extension: {
|
|
168
|
+
// id: msgExtension.id,
|
|
169
|
+
// serviceName: service.serviceName,
|
|
170
|
+
// } as IColaboFlowGoMsgExtension_Heartbeat_Response,
|
|
171
|
+
// msg: `"${service.serviceName}" service received the heartbeat from the "${hostName}" host`,
|
|
172
|
+
// };
|
|
173
|
+
|
|
174
|
+
// const queueReplyTo: string = msgObj.replyTo ?? "";
|
|
175
|
+
// if (reportsConfig.showReportsMsgs) {
|
|
176
|
+
// console.log(`${appNameText} Sending response to the task instances report [queue: "${chalk.blueBright(queueReplyTo)}"]: ${JSON.stringify(msgHeartbeatReply)}`);
|
|
177
|
+
// }
|
|
178
|
+
// // https://www.w3schools.com/nodejs/met_buffer_from.asp
|
|
179
|
+
// service.service.hostsChannel.sendToQueue(queueReplyTo, Buffer.from(JSON.stringify(msgHeartbeatReply)));
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
(async () => {
|
|
183
|
+
console.log(`${appNameText} Connecting to the server: ${chalk.greenBright(connectConfig.AMQP_URL)}`);
|
|
184
|
+
let colaboFlowServiceConnection: ChannelModel;
|
|
185
|
+
try {
|
|
186
|
+
colaboFlowServiceConnection = await connect(connectConfig.AMQP_URL);
|
|
187
|
+
} catch (err) {
|
|
188
|
+
console.error(`Error connecting to the "${chalk.greenBright(connectConfig.AMQP_URL)}" server: ${err}`);
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
console.log(`${appNameText} Opening the ${chalk.bold.italic("clients")} channel`);
|
|
193
|
+
try {
|
|
194
|
+
service.clientsChannel = await colaboFlowServiceConnection.createChannel();
|
|
195
|
+
} catch (err) {
|
|
196
|
+
console.error(`Error creating the ${chalk.greenBright("clients")} queue: ${err}`);
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
console.log(`${appNameText} Opening the ${chalk.bold.italic("hosts")} channel`);
|
|
200
|
+
try {
|
|
201
|
+
service.hostsChannel = await colaboFlowServiceConnection.createChannel();
|
|
202
|
+
} catch (err) {
|
|
203
|
+
console.error(`Error creating the ${chalk.greenBright("hosts")} queue: ${err}`);
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
console.log(`${appNameText} Opening the ${chalk.bold.italic("orchestrator")} queue as a listener: ${chalk.greenBright(service.queueOrchestrator)}`);
|
|
208
|
+
// https://amqp-node.github.io/amqplib/channel_api.html#channel_assertQueue
|
|
209
|
+
await service.clientsChannel.assertQueue(service.queueOrchestrator, {
|
|
210
|
+
durable: true,
|
|
211
|
+
exclusive: false,
|
|
212
|
+
autoDelete: false,
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
console.log(`${appNameText} Opening the ${chalk.bold.italic("heartbeat")} queue as a listener: ${chalk.greenBright(service.queueHostHeartbeat)}`);
|
|
216
|
+
// https://amqp-node.github.io/amqplib/channel_api.html#channel_assertQueue
|
|
217
|
+
await service.hostsChannel.assertQueue(service.queueHostHeartbeat, {
|
|
218
|
+
durable: true,
|
|
219
|
+
exclusive: false,
|
|
220
|
+
autoDelete: false,
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
// Orchestrator (tasks) Listener
|
|
224
|
+
console.log(`${appNameText} Listening on the orchestrator queue: "${chalk.blueBright(service.queueOrchestrator)}"`);
|
|
225
|
+
console.log(`${appNameText} [*] Waiting for orchestrator messages. To exit press "${chalk.blueBright("CTRL+C")}"`);
|
|
226
|
+
service.clientsChannel.consume(service.queueOrchestrator, (msg) => {
|
|
227
|
+
if (msg !== null) {
|
|
228
|
+
let msgObj: IColaboFlowGoMsg;
|
|
229
|
+
try {
|
|
230
|
+
msgObj = JSON.parse(msg.content);
|
|
231
|
+
|
|
232
|
+
// console.log(`${appNameText} Received: "${chalk.blueBright(JSON.stringify(msgObj))}"`);
|
|
233
|
+
console.log(`${appNameText} msgObj.sender: "${chalk.blueBright(msgObj.sender)}", msgObj.msg: "${chalk.blueBright(msgObj.msg)}"`);
|
|
234
|
+
|
|
235
|
+
switch (msgObj.type) {
|
|
236
|
+
case EColaboFlowGoMsgType.TASK_EXECUTE_REQUEST:
|
|
237
|
+
asyncProcessColaboFlowMsg_TaskExecute(service, msgObj);
|
|
238
|
+
break;
|
|
239
|
+
}
|
|
240
|
+
} catch (err) {
|
|
241
|
+
console.error(`Error parsing the message: ${msg.content}`);
|
|
242
|
+
}
|
|
243
|
+
service.clientsChannel.ack(msg);
|
|
244
|
+
} else {
|
|
245
|
+
console.log("${appNameText} Consumer cancelled by server");
|
|
246
|
+
}
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
// Heartbeat Listener
|
|
250
|
+
console.log(`${appNameText} Listening on the heartbeat queue: "${chalk.blueBright(service.queueHostHeartbeat)}"`);
|
|
251
|
+
console.log(`${appNameText} [*] Waiting for heartbeat messages. To exit press "${chalk.blueBright("CTRL+C")}"`);
|
|
252
|
+
service.clientsChannel.consume(service.queueHostHeartbeat, (msg) => {
|
|
253
|
+
if (msg !== null) {
|
|
254
|
+
let msgObj: IColaboFlowGoMsg;
|
|
255
|
+
try {
|
|
256
|
+
// console.log(`${appNameText} msgHostRes.content: ${msg.content}`);
|
|
257
|
+
msgObj = JSON.parse(msg.content);
|
|
258
|
+
|
|
259
|
+
switch (msgObj.type) {
|
|
260
|
+
case EColaboFlowGoMsgType.HEARTBEAT:
|
|
261
|
+
asyncProcessColaboFlowMsg_Heartbeat(service, msgObj);
|
|
262
|
+
break;
|
|
263
|
+
}
|
|
264
|
+
} catch (err) {
|
|
265
|
+
console.error(`Error parsing the message: ${msg.content}`);
|
|
266
|
+
}
|
|
267
|
+
service.clientsChannel.ack(msg);
|
|
268
|
+
} else {
|
|
269
|
+
console.log("${appNameText} Consumer cancelled by server");
|
|
270
|
+
}
|
|
271
|
+
});
|
|
272
|
+
})();
|