appmesh 3.1.6 → 3.1.7

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/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.fetch_task();
70
+ * const payload = await worker.fetchTask();
73
71
  * const result = processPayload(payload);
74
- * await worker.send_task_result(result);
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.fetch_task();
80
+ * const payload = await worker.fetchTask();
83
81
  * const result = processPayload(payload);
84
- * await worker.send_task_result(result);
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 (`fetch_task`/`send_task_result`) roles.
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.set_bearer_token(process.env.APPMESH_BEARER_TOKEN);
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 fetch_task retry loop (safe to call at any time);
142
- * a pending fetch_task resolves `null` at its next retry iteration.
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 fetch_task () {
182
+ async fetchTask() {
189
183
  const { processKey, appName } = this._getRuntimeEnv()
190
184
  const path = `/appmesh/app/${appName}/task`
191
185
 
192
- const RETRY_DELAY = 100
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(`fetch_task permanently rejected: ${error.message}`)
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(`fetch_task error: ${error.message}, retrying...`)
229
+ this._logger.warn(`fetchTask error: ${error.message}, retrying...`)
242
230
  }
243
231
 
244
- const remainingDelay = RETRY_DELAY - (Date.now() - attemptStart)
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 send_task_result (result) {
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(`send_task_result failed with status ${error.statusCode}: ${error.message}`)
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 = `send_task_result failed with status ${response.status}: ${response.data}`
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.fetch_task();
297
+ * const payload = await worker.fetchTask();
310
298
  * const result = await processPayload(payload);
311
- * await worker.send_task_result(result);
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); when set, sslConfig/tcpAddress are ignored.
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.set_bearer_token(process.env.APPMESH_BEARER_TOKEN);
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 fetch_task () {
372
+ async fetchTask() {
384
373
  await this._ensureClient()
385
- return super.fetch_task()
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 send_task_result (result) {
380
+ async sendTaskResult(result) {
392
381
  await this._ensureClient()
393
- return super.send_task_result(result)
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.fetch_task();
410
+ * const payload = await worker.fetchTask();
422
411
  * const result = process(payload);
423
- * await worker.send_task_result(result);
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
+