@kumologica/sdk 3.6.4-beta4 → 3.6.4-beta6

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.
@@ -54,6 +54,12 @@ exports.builder = (yargs) => {
54
54
  alias: "t",
55
55
  nargs: 1,
56
56
  });
57
+ yargs.option(`taskTimeout`, {
58
+ describe: "The timeout in seconds for task execution.",
59
+ type: "number",
60
+ alias: "o",
61
+ nargs: 1,
62
+ });
57
63
  yargs.option(`args`, {
58
64
  describe: "The list of arguments, either '-a one two three' or '-a one -a two -a three' ",
59
65
  type: "array",
@@ -67,13 +73,15 @@ exports.handler = async ({ project_directory, loglevel, port, taskName, args })
67
73
  // project_directory can point to a directory or a flow, so lets make sure that first the directory exists
68
74
  let absProjectDirectory;
69
75
  let exitCode = 1;
76
+
70
77
  try {
71
78
  absProjectDirectory = fs.realpathSync(projectDirectory);
72
79
  } catch (err) {
73
80
  logFatal(`Project not found: ${projectDirectory}`);
74
81
  process.exit(exitCode);
75
82
  }
76
-
83
+
84
+ let server;
77
85
  try {
78
86
  // Resolve the path to the flow path
79
87
  const [projectFlowDirname, projectFlowFullPath] = resolveProjectFlowPath(absProjectDirectory);
@@ -87,38 +95,63 @@ exports.handler = async ({ project_directory, loglevel, port, taskName, args })
87
95
  };
88
96
 
89
97
  // Start a server
90
- let server = new TaskServer({
91
- flowPath: projectFlowFullPath,
92
- cliParams,
93
- });
98
+ server = new TaskServer(projectFlowFullPath, cliParams);
94
99
 
95
100
  await server.listen();
96
101
 
102
+ const got = require('got');
103
+
104
+ // Timeout in ms (default 10 minutes); make configurable via env or param if needed
105
+ const TIMEOUT_MS = process.env.TIMEOUT_MS || 1000000; // 10 minutes
106
+
97
107
  const tName = taskName || process.env.taskName;
98
- const req = {
99
- method: 'POST',
100
- headers: {
101
- },
102
- body: JSON.stringify({args: args})
103
- };
108
+ const url = `http://127.0.0.1:${port || 1880}/__task__/${tName}`;
109
+
110
+ try {
111
+ const response = await got(url, {
112
+ method: 'POST',
113
+ headers: {
114
+ // Add any specific headers here if needed (e.g., 'Content-Type': 'application/json')
115
+ },
116
+ json: { args: args }, // Automatically stringifies to JSON and sets Content-Type
117
+ timeout: { request: TIMEOUT_MS } // Full request timeout (headers + body)
118
+ });
119
+
120
+ // Log success and response details
121
+ logInfo('Request succeeded!');
122
+ logInfo('Status:', response.statusCode);
123
+ logInfo('Response body:', response.body); // Assuming JSON response; parse if needed via response.json
124
+
125
+ exitCode = 0;
126
+
127
+ } catch (error) {
128
+ // Log failure details
129
+ logFatal('Task Error:', error.message);
130
+
131
+ // if (error.response) {
132
+ // logFatal('Status:', error.response.statusCode);
133
+ // logFatal('Response body:', error.response.body);
134
+ // }
135
+ // if (error.code === 'ETIMEDOUT') {
136
+ // logFatal('Timeout exceeded (', TIMEOUT_MS / 1000, 's)');
137
+ // }
138
+ }
139
+ } catch (error) {
140
+ logFatal('Task failed!');
141
+ logFatal('Error:', error.message);
104
142
 
105
- if (tName) {
106
- const url = `http://127.0.0.1:${port||1880}/__task__/${tName}`;
107
-
108
- const response = await fetch(url, req);
109
- const data = await response.text();
110
-
111
- logInfo("Status: " + response.status);
112
- logInfo("Message: " + data);
143
+ }
113
144
 
145
+ try {
146
+ if (server) {
114
147
  await server.stop();
115
-
116
- exitCode = response.status >= 200 && response.status < 300 ? 0 : 1;
117
- process.exit(exitCode); // this is task, terminate after completion
118
- }
119
- } catch (e) {
120
- logFatal(e.message);
148
+ }
149
+ } catch (error) {
150
+ logError('Error stopping server:', error.message);
121
151
  }
152
+ logInfo('HTTP Server stopped.');
153
+
154
+ process.exit(exitCode); // Error exit
122
155
  };
123
156
 
124
157
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kumologica/sdk",
3
- "version": "3.6.4-beta4",
3
+ "version": "3.6.4-beta6",
4
4
  "productName": "Kumologica Designer",
5
5
  "copyright": "Copyright 2020 Kumologica Pty Ltd, All Rights Reserved.",
6
6
  "author": "Kumologica Pty Ltd <contact@kumologica.com>",
@@ -83,9 +83,9 @@
83
83
  "@aws-sdk/credential-providers": "^3.556.0",
84
84
  "@aws-sdk/lib-dynamodb": "^3.549.0",
85
85
  "@electron/remote": "^2.0.8",
86
- "@kumologica/builder": "3.6.4-beta4",
87
- "@kumologica/devkit": "3.6.4-beta4",
88
- "@kumologica/runtime": "3.6.4-beta4",
86
+ "@kumologica/builder": "3.6.4-beta6",
87
+ "@kumologica/devkit": "3.6.4-beta6",
88
+ "@kumologica/runtime": "3.6.4-beta6",
89
89
  "ajv": "8.10.0",
90
90
  "archive-type": "^4.0.0",
91
91
  "basic-auth": "2.0.1",
@@ -5,6 +5,7 @@ const {
5
5
  PLATFORMS,
6
6
  } = require("@kumologica/runtime");
7
7
 
8
+ const tcpPortUsed = require("tcp-port-used");
8
9
 
9
10
  /**
10
11
  * This class will encapsulate the adminApp and FlowApp into two servers.
@@ -32,12 +33,26 @@ class TaskServer {
32
33
  { noadmin: true }
33
34
  );
34
35
 
35
- this.config = { ...baseConfig, ...params };
36
+ this.config = { ...baseConfig, ...params };
36
37
 
37
- this.flowServer = new AbstractServerfulServer(this.config);
38
+ console.log(`Runtime configuration:${JSON.stringify(this.config)}`);
38
39
  }
39
40
 
41
+ async allocatePort(port) {
42
+ let inUse = await tcpPortUsed.check(port);
43
+ if (inUse) {
44
+ let newPort = port + 1;
45
+ console.log(`> Port: ${port} in use. Trying with port: ${newPort}...`);
46
+ return await this.allocatePort(newPort);
47
+ } else {
48
+ return port;
49
+ }
50
+ }
51
+
40
52
  async listen() {
53
+ this.config.port = await this.allocatePort(this.config.port || 1880);
54
+ this.flowServer = new AbstractServerfulServer(this.config);
55
+
41
56
  await this.flowServer.start();
42
57
  this.flowServer.log.debug(`Runtime configuration:${JSON.stringify(this.config)}`);
43
58
  }