appmesh 3.1.6 → 3.1.8
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/README.md +27 -23
- package/dist/appmesh.esm.js +1 -1
- package/package.json +2 -2
- package/src/appmesh.js +108 -106
- package/src/appmesh_tcp.js +306 -210
- package/src/worker.js +34 -44
- package/dist/894.appmesh.esm.js +0 -1
package/src/worker.js
CHANGED
|
@@ -3,9 +3,7 @@
|
|
|
3
3
|
|
|
4
4
|
// Environment check - only works in Node.js
|
|
5
5
|
if (typeof window !== 'undefined' && typeof window.document !== 'undefined') {
|
|
6
|
-
throw new Error(
|
|
7
|
-
'AppMeshWorker is only available in Node.js environment. Use AppMeshClient for browser applications.'
|
|
8
|
-
)
|
|
6
|
+
throw new Error('AppMeshWorker is only available in Node.js environment. Use AppMeshClient for browser applications.')
|
|
9
7
|
}
|
|
10
8
|
|
|
11
9
|
import AppMeshClient, { AppMeshError } from './appmesh.js'
|
|
@@ -69,9 +67,9 @@ const _DEFER_CLIENT = Symbol('appmesh.deferClient')
|
|
|
69
67
|
* import { AppMeshWorker } from 'appmesh/worker';
|
|
70
68
|
*
|
|
71
69
|
* const worker = new AppMeshWorker();
|
|
72
|
-
* const payload = await worker.
|
|
70
|
+
* const payload = await worker.fetchTask();
|
|
73
71
|
* const result = processPayload(payload);
|
|
74
|
-
* await worker.
|
|
72
|
+
* await worker.sendTaskResult(result);
|
|
75
73
|
*
|
|
76
74
|
* @example
|
|
77
75
|
* // TCP Worker Example
|
|
@@ -79,9 +77,9 @@ const _DEFER_CLIENT = Symbol('appmesh.deferClient')
|
|
|
79
77
|
*
|
|
80
78
|
* const worker = new AppMeshWorkerTCP();
|
|
81
79
|
* try {
|
|
82
|
-
* const payload = await worker.
|
|
80
|
+
* const payload = await worker.fetchTask();
|
|
83
81
|
* const result = processPayload(payload);
|
|
84
|
-
* await worker.
|
|
82
|
+
* await worker.sendTaskResult(result);
|
|
85
83
|
* } finally {
|
|
86
84
|
* worker.close();
|
|
87
85
|
* }
|
|
@@ -101,7 +99,7 @@ class AppMeshWorker {
|
|
|
101
99
|
* @param {AppMeshClient} [options.client] - Optional pre-built `AppMeshClient` instance to reuse.
|
|
102
100
|
* When supplied, `baseURL` and `sslConfig` are ignored and the worker shares the caller's
|
|
103
101
|
* client (and therefore its caller-owned bearer). Use this when a single process needs both
|
|
104
|
-
* client (outbound calls like `AddApp`) and worker (`
|
|
102
|
+
* client (outbound calls like `AddApp`) and worker (`fetchTask`/`sendTaskResult`) roles.
|
|
105
103
|
*
|
|
106
104
|
* @example
|
|
107
105
|
* import fs from 'fs';
|
|
@@ -116,7 +114,7 @@ class AppMeshWorker {
|
|
|
116
114
|
* @example
|
|
117
115
|
* // Share a single client between client-role and worker-role usage:
|
|
118
116
|
* const client = new AppMeshClient('https://127.0.0.1:6060', sslConfig);
|
|
119
|
-
* client.
|
|
117
|
+
* client.setBearerToken(process.env.APPMESH_BEARER_TOKEN);
|
|
120
118
|
* const worker = new AppMeshWorker('https://127.0.0.1:6060', sslConfig, { client });
|
|
121
119
|
*/
|
|
122
120
|
constructor (
|
|
@@ -138,8 +136,8 @@ class AppMeshWorker {
|
|
|
138
136
|
}
|
|
139
137
|
|
|
140
138
|
/**
|
|
141
|
-
* Request cancellation of the
|
|
142
|
-
* a pending
|
|
139
|
+
* Request cancellation of the fetchTask retry loop (safe to call at any time);
|
|
140
|
+
* a pending fetchTask resolves `null` at its next retry iteration.
|
|
143
141
|
*/
|
|
144
142
|
stop () {
|
|
145
143
|
this._stopped = true
|
|
@@ -156,14 +154,10 @@ class AppMeshWorker {
|
|
|
156
154
|
const appName = process.env.APP_MESH_APPLICATION_NAME
|
|
157
155
|
|
|
158
156
|
if (!processKey) {
|
|
159
|
-
throw new Error(
|
|
160
|
-
'Missing environment variable: APP_MESH_PROCESS_KEY. This must be set by App Mesh service.'
|
|
161
|
-
)
|
|
157
|
+
throw new Error('Missing environment variable: APP_MESH_PROCESS_KEY. This must be set by App Mesh service.')
|
|
162
158
|
}
|
|
163
159
|
if (!appName) {
|
|
164
|
-
throw new Error(
|
|
165
|
-
'Missing environment variable: APP_MESH_APPLICATION_NAME. This must be set by App Mesh service.'
|
|
166
|
-
)
|
|
160
|
+
throw new Error('Missing environment variable: APP_MESH_APPLICATION_NAME. This must be set by App Mesh service.')
|
|
167
161
|
}
|
|
168
162
|
|
|
169
163
|
return { processKey, appName }
|
|
@@ -185,11 +179,11 @@ class AppMeshWorker {
|
|
|
185
179
|
* @throws {WorkerRejectedError} The daemon reported a permanent HTTP 400; an app entry
|
|
186
180
|
* point should stop serving and exit.
|
|
187
181
|
*/
|
|
188
|
-
async
|
|
182
|
+
async fetchTask() {
|
|
189
183
|
const { processKey, appName } = this._getRuntimeEnv()
|
|
190
184
|
const path = `/appmesh/app/${appName}/task`
|
|
191
185
|
|
|
192
|
-
const
|
|
186
|
+
const retryDelay = 100
|
|
193
187
|
|
|
194
188
|
while (!this._stopped) {
|
|
195
189
|
const attemptStart = Date.now()
|
|
@@ -211,15 +205,10 @@ class AppMeshWorker {
|
|
|
211
205
|
)
|
|
212
206
|
}
|
|
213
207
|
if (response.status === 400) {
|
|
214
|
-
throw new WorkerRejectedError(
|
|
215
|
-
'fetch_task permanently rejected with status 400',
|
|
216
|
-
response.data
|
|
217
|
-
)
|
|
208
|
+
throw new WorkerRejectedError('fetchTask permanently rejected with status 400', response.data)
|
|
218
209
|
}
|
|
219
210
|
|
|
220
|
-
this._logger.warn(
|
|
221
|
-
`fetch_task failed with status ${response.status}: ${response.data}, retrying...`
|
|
222
|
-
)
|
|
211
|
+
this._logger.warn(`fetchTask failed with status ${response.status}: ${response.data}, retrying...`)
|
|
223
212
|
} catch (error) {
|
|
224
213
|
// ProcessSupersededError fixes statusCode to 412, so this covers both the
|
|
225
214
|
// branch above and 412 AppMeshErrors thrown by request().
|
|
@@ -227,21 +216,20 @@ class AppMeshWorker {
|
|
|
227
216
|
this._logger.error('Process key mismatch (412): this process has been superseded')
|
|
228
217
|
throw error instanceof ProcessSupersededError
|
|
229
218
|
? error
|
|
230
|
-
: new ProcessSupersededError(
|
|
231
|
-
'Process key mismatch (412): this process has been superseded by a newer instance',
|
|
219
|
+
: new ProcessSupersededError('Process key mismatch (412): this process has been superseded by a newer instance',
|
|
232
220
|
error.responseData
|
|
233
221
|
)
|
|
234
222
|
}
|
|
235
223
|
if (error.statusCode === 400) {
|
|
236
|
-
this._logger.error(`
|
|
224
|
+
this._logger.error(`fetchTask permanently rejected: ${error.message}`)
|
|
237
225
|
throw error instanceof WorkerRejectedError
|
|
238
226
|
? error
|
|
239
227
|
: new WorkerRejectedError(error.message, error.responseData)
|
|
240
228
|
}
|
|
241
|
-
this._logger.warn(`
|
|
229
|
+
this._logger.warn(`fetchTask error: ${error.message}, retrying...`)
|
|
242
230
|
}
|
|
243
231
|
|
|
244
|
-
const remainingDelay =
|
|
232
|
+
const remainingDelay = retryDelay - (Date.now() - attemptStart)
|
|
245
233
|
if (remainingDelay > 0) {
|
|
246
234
|
await this._sleep(remainingDelay)
|
|
247
235
|
}
|
|
@@ -260,7 +248,7 @@ class AppMeshWorker {
|
|
|
260
248
|
* @returns {Promise<void>}
|
|
261
249
|
* @throws {Error} If the task return fails
|
|
262
250
|
*/
|
|
263
|
-
async
|
|
251
|
+
async sendTaskResult(result) {
|
|
264
252
|
const { processKey, appName } = this._getRuntimeEnv()
|
|
265
253
|
const path = `/appmesh/app/${appName}/task`
|
|
266
254
|
|
|
@@ -271,12 +259,12 @@ class AppMeshWorker {
|
|
|
271
259
|
})
|
|
272
260
|
} catch (error) {
|
|
273
261
|
// request() throws AppMeshError on non-200
|
|
274
|
-
this._logger.error(`
|
|
262
|
+
this._logger.error(`sendTaskResult failed with status ${error.statusCode}: ${error.message}`)
|
|
275
263
|
throw error
|
|
276
264
|
}
|
|
277
265
|
|
|
278
266
|
if (response.status !== 200) {
|
|
279
|
-
const msg = `
|
|
267
|
+
const msg = `sendTaskResult failed with status ${response.status}: ${response.data}`
|
|
280
268
|
this._logger.error(msg)
|
|
281
269
|
throw new Error(msg)
|
|
282
270
|
}
|
|
@@ -306,9 +294,9 @@ class AppMeshWorker {
|
|
|
306
294
|
*
|
|
307
295
|
* const worker = new AppMeshWorkerTCP();
|
|
308
296
|
* try {
|
|
309
|
-
* const payload = await worker.
|
|
297
|
+
* const payload = await worker.fetchTask();
|
|
310
298
|
* const result = await processPayload(payload);
|
|
311
|
-
* await worker.
|
|
299
|
+
* await worker.sendTaskResult(result);
|
|
312
300
|
* } finally {
|
|
313
301
|
* worker.close();
|
|
314
302
|
* }
|
|
@@ -329,7 +317,8 @@ class AppMeshWorkerTCP extends AppMeshWorker {
|
|
|
329
317
|
* @param {Array<string, number>|{host: string, port: number}} [tcpAddress=['127.0.0.1', 6059]] - TCP server address
|
|
330
318
|
* @param {Object} [options={}] - Additional options
|
|
331
319
|
* @param {Object} [options.logger=console] - Logger instance
|
|
332
|
-
* @param {AppMeshClientTCP} [options.client] - Pre-built client to reuse (shares its caller-owned bearer);
|
|
320
|
+
* @param {AppMeshClientTCP} [options.client] - Pre-built client to reuse (shares its caller-owned bearer);
|
|
321
|
+
* when set, sslConfig/tcpAddress are ignored.
|
|
333
322
|
*
|
|
334
323
|
* @example
|
|
335
324
|
* import fs from 'fs';
|
|
@@ -344,7 +333,7 @@ class AppMeshWorkerTCP extends AppMeshWorker {
|
|
|
344
333
|
* // Share a single TCP client between client-role and worker-role usage:
|
|
345
334
|
* import { AppMeshClientTCP } from 'appmesh/tcp';
|
|
346
335
|
* const client = new AppMeshClientTCP(sslConfig, ['127.0.0.1', 6059]);
|
|
347
|
-
* client.
|
|
336
|
+
* client.setBearerToken(process.env.APPMESH_BEARER_TOKEN);
|
|
348
337
|
* const worker = new AppMeshWorkerTCP(sslConfig, ['127.0.0.1', 6059], { client });
|
|
349
338
|
*/
|
|
350
339
|
constructor (
|
|
@@ -380,17 +369,17 @@ class AppMeshWorkerTCP extends AppMeshWorker {
|
|
|
380
369
|
/**
|
|
381
370
|
* Fetch task data (overrides parent to ensure TCP client is initialized)
|
|
382
371
|
*/
|
|
383
|
-
async
|
|
372
|
+
async fetchTask() {
|
|
384
373
|
await this._ensureClient()
|
|
385
|
-
return super.
|
|
374
|
+
return super.fetchTask()
|
|
386
375
|
}
|
|
387
376
|
|
|
388
377
|
/**
|
|
389
378
|
* Return task result (overrides parent to ensure TCP client is initialized)
|
|
390
379
|
*/
|
|
391
|
-
async
|
|
380
|
+
async sendTaskResult(result) {
|
|
392
381
|
await this._ensureClient()
|
|
393
|
-
return super.
|
|
382
|
+
return super.sendTaskResult(result)
|
|
394
383
|
}
|
|
395
384
|
|
|
396
385
|
/**
|
|
@@ -418,9 +407,9 @@ class AppMeshWorkerTCP extends AppMeshWorker {
|
|
|
418
407
|
* await withServer(
|
|
419
408
|
* () => new AppMeshWorkerTCP(),
|
|
420
409
|
* async (worker) => {
|
|
421
|
-
* const payload = await worker.
|
|
410
|
+
* const payload = await worker.fetchTask();
|
|
422
411
|
* const result = process(payload);
|
|
423
|
-
* await worker.
|
|
412
|
+
* await worker.sendTaskResult(result);
|
|
424
413
|
* }
|
|
425
414
|
* );
|
|
426
415
|
*/
|
|
@@ -443,3 +432,4 @@ export {
|
|
|
443
432
|
withServer
|
|
444
433
|
}
|
|
445
434
|
export default AppMeshWorker
|
|
435
|
+
|
package/dist/894.appmesh.esm.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export const __webpack_esm_ids__=[894];export const __webpack_esm_modules__={894(e){e.exports="object"==typeof self?self.FormData:window.FormData}};
|