@webpieces/cloudtasks-client 0.4.962 → 0.4.964
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 +9 -9
- package/package.json +4 -4
- package/src/GcpTaskRegistry.d.ts +5 -5
- package/src/GcpTaskRegistry.js +7 -7
- package/src/GcpTaskRegistry.js.map +1 -1
- package/src/GrpcErrorMapper.d.ts +1 -1
- package/src/GrpcErrorMapper.js +1 -1
- package/src/GrpcErrorMapper.js.map +1 -1
- package/src/TaskProxyClient.d.ts +2 -2
- package/src/TaskProxyClient.js +4 -4
- package/src/TaskProxyClient.js.map +1 -1
- package/src/TaskTypes.d.ts +1 -1
- package/src/TaskTypes.js +1 -1
- package/src/TaskTypes.js.map +1 -1
package/README.md
CHANGED
|
@@ -9,7 +9,7 @@ where it runs through the full server filter chain.
|
|
|
9
9
|
// one shared contract
|
|
10
10
|
@PubSub() @ApiPath('/email')
|
|
11
11
|
abstract class EmailApi {
|
|
12
|
-
@Endpoint(POST, '/send', WRITE, CLOUDTASKS
|
|
12
|
+
@Endpoint(POST, '/send', WRITE, CLOUDTASKS) // Promise<void> by kind: delivered as 204, 0 bytes
|
|
13
13
|
@WpAuth([oidcSameProject()])
|
|
14
14
|
@WpAuthorization({ authType: AuthorizationType.SERVICE_ONLY })
|
|
15
15
|
abstract sendEmail(r: SendEmailRequest): Promise<void>;
|
|
@@ -43,7 +43,7 @@ await scheduler.schedule(() => emailTasks.sendEmail(req), Date.now() + 60_000, n
|
|
|
43
43
|
## Enqueue retries (this is ENQUEUE, not delivery)
|
|
44
44
|
|
|
45
45
|
The `createTask` gRPC call is retried by webpieces' shared `RetryStrategy` — the same
|
|
46
|
-
`
|
|
46
|
+
`ClientRegistry.execute` the RPC clients use, with the enqueue transport default
|
|
47
47
|
`RetryStrategy(5_000, 3, 500, 2, 4_000)`: 5 s per attempt, 3 retries, backoff 500 ms ×2 capped at
|
|
48
48
|
4 s. Only a transient failure is retried (gRPC UNAVAILABLE, DEADLINE_EXCEEDED, RESOURCE_EXHAUSTED →
|
|
49
49
|
`GrpcErrorMapper`); giving up throws a 502 `ApiRetriesExhaustedError`. Each retried attempt logs
|
|
@@ -68,13 +68,13 @@ for about an hour after its task runs or is deleted.
|
|
|
68
68
|
new ScheduleOptions(taskName, taskTimeoutSeconds?, customizeTask?, gax?)
|
|
69
69
|
```
|
|
70
70
|
|
|
71
|
-
- **`customizeTask`** (or `GcpTaskRegistry.setCustomizer(c,
|
|
71
|
+
- **`customizeTask`** (or `GcpTaskRegistry.setCustomizer(c, scope)`) — the Google
|
|
72
72
|
`ICreateTaskRequest`, run LAST on every attempt's freshly built request. Request only, never timing.
|
|
73
73
|
Call site wins over the registry (method → API → ALL).
|
|
74
|
-
- **`gax`** (or `GcpTaskRegistry.setGax(options,
|
|
74
|
+
- **`gax`** (or `GcpTaskRegistry.setGax(options, scope)`) — `CallOptions` for the one `createTask`
|
|
75
75
|
RPC. **Set ⇒ you own timing:** no webpieces strategy, ONE attempt, `createTask(request, yourGax)`
|
|
76
|
-
as given, under a safety cap from `
|
|
77
|
-
the cap with `
|
|
76
|
+
as given, under a safety cap from `ClientRegistry`'s timeout ladder (method → API → ALL → 30 s). Raise
|
|
77
|
+
the cap with `ClientRegistry.setTimeout(60_000, CallScope.api(EmailApi))` to give gax retries room. Exactly one retry
|
|
78
78
|
layer per enqueue — ours or gax's, never both.
|
|
79
79
|
- **The client** — override `CloudTasksClientProvider` for channel/client settings:
|
|
80
80
|
|
|
@@ -95,11 +95,11 @@ construct any unbound class token, `@optional()` included, and fails on `CloudTa
|
|
|
95
95
|
// you own timing AND retries (gax retries UNAVAILABLE); raise the safety cap
|
|
96
96
|
await scheduler.addToQueue(() => api.sendEmail(req),
|
|
97
97
|
new ScheduleOptions(TaskName.dedup(id), undefined, undefined, { timeout: 10_000, retry: { retryCodes: [14] } }));
|
|
98
|
-
|
|
98
|
+
ClientRegistry.setTimeout(60_000, CallScope.api(EmailApi));
|
|
99
99
|
|
|
100
100
|
// process-wide defaults, no call-site edits
|
|
101
|
-
GcpTaskRegistry.setCustomizer((request) => { request.task!.dispatchDeadline = { seconds: 900 }; return request; },
|
|
102
|
-
|
|
101
|
+
GcpTaskRegistry.setCustomizer((request) => { request.task!.dispatchDeadline = { seconds: 900 }; return request; }, CallScope.all());
|
|
102
|
+
ClientRegistry.setStrategy(new RetryStrategy(2_000, 5, 200, 2, 2_000).run, CallScope.api(WhatsAppProcessApi)); // tune OUR retries
|
|
103
103
|
```
|
|
104
104
|
|
|
105
105
|
`createTask`'s public promise exposes no `cancel()`; on the default path the gRPC deadline equals the
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webpieces/cloudtasks-client",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.964",
|
|
4
4
|
"description": "Cloud Tasks enqueue client generated from a shared @PubSub API contract (twin of http-client)",
|
|
5
5
|
"type": "commonjs",
|
|
6
6
|
"main": "./src/index.js",
|
|
@@ -23,9 +23,9 @@
|
|
|
23
23
|
"access": "public"
|
|
24
24
|
},
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"@webpieces/core-context": "0.4.
|
|
27
|
-
"@webpieces/core-util": "0.4.
|
|
28
|
-
"@webpieces/gcp-identity": "0.4.
|
|
26
|
+
"@webpieces/core-context": "0.4.964",
|
|
27
|
+
"@webpieces/core-util": "0.4.964",
|
|
28
|
+
"@webpieces/gcp-identity": "0.4.964",
|
|
29
29
|
"@google-cloud/tasks": "6.3.0",
|
|
30
30
|
"google-gax": "5.0.8",
|
|
31
31
|
"inversify": "7.10.4",
|
package/src/GcpTaskRegistry.d.ts
CHANGED
|
@@ -3,15 +3,15 @@ import type { CallScope } from '@webpieces/core-util';
|
|
|
3
3
|
import { TaskCustomizer } from './TaskTypes';
|
|
4
4
|
/**
|
|
5
5
|
* Process-wide Cloud Tasks enqueue defaults, set at startup and resolved method -> API -> ALL — the
|
|
6
|
-
* same {@link CallScope} ladder as `
|
|
6
|
+
* same {@link CallScope} ladder as `ClientRegistry`. A call site's `ScheduleOptions` wins over both.
|
|
7
7
|
*
|
|
8
8
|
* - {@link GcpTaskRegistry.setCustomizer} — a {@link TaskCustomizer} applied to the Google
|
|
9
9
|
* `ICreateTaskRequest` of every matching enqueue whose call site set none. It changes the REQUEST
|
|
10
10
|
* only (headers, dispatch deadline, ...); webpieces still owns timing.
|
|
11
11
|
* - {@link GcpTaskRegistry.setGax} — gax `CallOptions` for matching enqueues. Setting them means
|
|
12
12
|
* YOU own timing for that scope: no webpieces retry strategy runs, there is exactly one attempt,
|
|
13
|
-
* `createTask(request, yourGax)` is sent as given, and `
|
|
14
|
-
* API -> ALL -> 30 s) is a safety cap only. Raise the cap with `
|
|
13
|
+
* `createTask(request, yourGax)` is sent as given, and `ClientRegistry`'s timeout ladder (method ->
|
|
14
|
+
* API -> ALL -> 30 s) is a safety cap only. Raise the cap with `ClientRegistry.setTimeout(...)` to
|
|
15
15
|
* give gax retries room.
|
|
16
16
|
*
|
|
17
17
|
* Read only by `TaskProxyClient` (the customizer is applied by `GcpTaskInvoker`; the in-memory
|
|
@@ -19,8 +19,8 @@ import { TaskCustomizer } from './TaskTypes';
|
|
|
19
19
|
*/
|
|
20
20
|
export declare class GcpTaskRegistry {
|
|
21
21
|
private static readonly ladder;
|
|
22
|
-
static setCustomizer(customizer: TaskCustomizer | undefined,
|
|
23
|
-
static setGax(gax: CallOptions | undefined,
|
|
22
|
+
static setCustomizer(customizer: TaskCustomizer | undefined, scope: CallScope): void;
|
|
23
|
+
static setGax(gax: CallOptions | undefined, scope: CallScope): void;
|
|
24
24
|
/** The registered customizer, method -> API -> ALL, or undefined. */
|
|
25
25
|
static customizerFor(api: Function, methodName: string): TaskCustomizer | undefined;
|
|
26
26
|
/** The registered gax CallOptions, method -> API -> ALL, or undefined. */
|
package/src/GcpTaskRegistry.js
CHANGED
|
@@ -8,15 +8,15 @@ class GcpTaskPolicy {
|
|
|
8
8
|
}
|
|
9
9
|
/**
|
|
10
10
|
* Process-wide Cloud Tasks enqueue defaults, set at startup and resolved method -> API -> ALL — the
|
|
11
|
-
* same {@link CallScope} ladder as `
|
|
11
|
+
* same {@link CallScope} ladder as `ClientRegistry`. A call site's `ScheduleOptions` wins over both.
|
|
12
12
|
*
|
|
13
13
|
* - {@link GcpTaskRegistry.setCustomizer} — a {@link TaskCustomizer} applied to the Google
|
|
14
14
|
* `ICreateTaskRequest` of every matching enqueue whose call site set none. It changes the REQUEST
|
|
15
15
|
* only (headers, dispatch deadline, ...); webpieces still owns timing.
|
|
16
16
|
* - {@link GcpTaskRegistry.setGax} — gax `CallOptions` for matching enqueues. Setting them means
|
|
17
17
|
* YOU own timing for that scope: no webpieces retry strategy runs, there is exactly one attempt,
|
|
18
|
-
* `createTask(request, yourGax)` is sent as given, and `
|
|
19
|
-
* API -> ALL -> 30 s) is a safety cap only. Raise the cap with `
|
|
18
|
+
* `createTask(request, yourGax)` is sent as given, and `ClientRegistry`'s timeout ladder (method ->
|
|
19
|
+
* API -> ALL -> 30 s) is a safety cap only. Raise the cap with `ClientRegistry.setTimeout(...)` to
|
|
20
20
|
* give gax retries room.
|
|
21
21
|
*
|
|
22
22
|
* Read only by `TaskProxyClient` (the customizer is applied by `GcpTaskInvoker`; the in-memory
|
|
@@ -25,12 +25,12 @@ class GcpTaskPolicy {
|
|
|
25
25
|
class GcpTaskRegistry {
|
|
26
26
|
static ladder = new core_util_1.CallScopeLadder(() => new GcpTaskPolicy());
|
|
27
27
|
// webpieces-disable no-function-outside-class -- process-global startup registry
|
|
28
|
-
static setCustomizer(customizer,
|
|
29
|
-
GcpTaskRegistry.ladder.at(
|
|
28
|
+
static setCustomizer(customizer, scope) {
|
|
29
|
+
GcpTaskRegistry.ladder.at(scope).customizer = customizer;
|
|
30
30
|
}
|
|
31
31
|
// webpieces-disable no-function-outside-class -- process-global startup registry
|
|
32
|
-
static setGax(gax,
|
|
33
|
-
GcpTaskRegistry.ladder.at(
|
|
32
|
+
static setGax(gax, scope) {
|
|
33
|
+
GcpTaskRegistry.ladder.at(scope).gax = gax;
|
|
34
34
|
}
|
|
35
35
|
/** The registered customizer, method -> API -> ALL, or undefined. */
|
|
36
36
|
// webpieces-disable no-function-outside-class -- process-global registry lookup
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"GcpTaskRegistry.js","sourceRoot":"","sources":["../../../../../packages/cloud/cloudtasks-client/src/GcpTaskRegistry.ts"],"names":[],"mappings":";;;AACA,oDAAuD;AAIvD,MAAM,aAAa;IACf,UAAU,CAAkB;IAC5B,GAAG,CAAe;CACrB;AAED;;;;;;;;;;;;;;;GAeG;AACH,MAAa,eAAe;IAChB,MAAM,CAAU,MAAM,GAAG,IAAI,2BAAe,CAAgB,GAAG,EAAE,CAAC,IAAI,aAAa,EAAE,CAAC,CAAC;IAE/F,iFAAiF;IACjF,MAAM,CAAC,aAAa,CAAC,UAAsC,EAAE,
|
|
1
|
+
{"version":3,"file":"GcpTaskRegistry.js","sourceRoot":"","sources":["../../../../../packages/cloud/cloudtasks-client/src/GcpTaskRegistry.ts"],"names":[],"mappings":";;;AACA,oDAAuD;AAIvD,MAAM,aAAa;IACf,UAAU,CAAkB;IAC5B,GAAG,CAAe;CACrB;AAED;;;;;;;;;;;;;;;GAeG;AACH,MAAa,eAAe;IAChB,MAAM,CAAU,MAAM,GAAG,IAAI,2BAAe,CAAgB,GAAG,EAAE,CAAC,IAAI,aAAa,EAAE,CAAC,CAAC;IAE/F,iFAAiF;IACjF,MAAM,CAAC,aAAa,CAAC,UAAsC,EAAE,KAAgB;QACzE,eAAe,CAAC,MAAM,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,UAAU,GAAG,UAAU,CAAC;IAC7D,CAAC;IAED,iFAAiF;IACjF,MAAM,CAAC,MAAM,CAAC,GAA4B,EAAE,KAAgB;QACxD,eAAe,CAAC,MAAM,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,GAAG,GAAG,GAAG,CAAC;IAC/C,CAAC;IAED,qEAAqE;IACrE,gFAAgF;IAChF,MAAM,CAAC,aAAa,CAAC,GAAa,EAAE,UAAkB;QAClD,OAAO,eAAe,CAAC,MAAM;aACxB,KAAK,CAAC,GAAG,EAAE,UAAU,CAAC;aACtB,IAAI,CAAC,CAAC,CAAgB,EAAE,EAAE,CAAC,CAAC,CAAC,UAAU,KAAK,SAAS,CAAC,EAAE,UAAU,CAAC;IAC5E,CAAC;IAED,0EAA0E;IAC1E,gFAAgF;IAChF,MAAM,CAAC,MAAM,CAAC,GAAa,EAAE,UAAkB;QAC3C,OAAO,eAAe,CAAC,MAAM,CAAC,KAAK,CAAC,GAAG,EAAE,UAAU,CAAC,CAAC,IAAI,CAAC,CAAC,CAAgB,EAAE,EAAE,CAAC,CAAC,CAAC,GAAG,KAAK,SAAS,CAAC,EAAE,GAAG,CAAC;IAC9G,CAAC;IAED,yFAAyF;IACzF,MAAM,CAAC,KAAK;QACR,eAAe,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC;IACnC,CAAC;;AA9BL,0CA+BC","sourcesContent":["import type { CallOptions } from 'google-gax';\nimport { CallScopeLadder } from '@webpieces/core-util';\nimport type { CallScope } from '@webpieces/core-util';\nimport { TaskCustomizer } from './TaskTypes';\n\nclass GcpTaskPolicy {\n customizer?: TaskCustomizer;\n gax?: CallOptions;\n}\n\n/**\n * Process-wide Cloud Tasks enqueue defaults, set at startup and resolved method -> API -> ALL — the\n * same {@link CallScope} ladder as `ClientRegistry`. A call site's `ScheduleOptions` wins over both.\n *\n * - {@link GcpTaskRegistry.setCustomizer} — a {@link TaskCustomizer} applied to the Google\n * `ICreateTaskRequest` of every matching enqueue whose call site set none. It changes the REQUEST\n * only (headers, dispatch deadline, ...); webpieces still owns timing.\n * - {@link GcpTaskRegistry.setGax} — gax `CallOptions` for matching enqueues. Setting them means\n * YOU own timing for that scope: no webpieces retry strategy runs, there is exactly one attempt,\n * `createTask(request, yourGax)` is sent as given, and `ClientRegistry`'s timeout ladder (method ->\n * API -> ALL -> 30 s) is a safety cap only. Raise the cap with `ClientRegistry.setTimeout(...)` to\n * give gax retries room.\n *\n * Read only by `TaskProxyClient` (the customizer is applied by `GcpTaskInvoker`; the in-memory\n * invoker has no Google request and ignores both).\n */\nexport class GcpTaskRegistry {\n private static readonly ladder = new CallScopeLadder<GcpTaskPolicy>(() => new GcpTaskPolicy());\n\n // webpieces-disable no-function-outside-class -- process-global startup registry\n static setCustomizer(customizer: TaskCustomizer | undefined, scope: CallScope): void {\n GcpTaskRegistry.ladder.at(scope).customizer = customizer;\n }\n\n // webpieces-disable no-function-outside-class -- process-global startup registry\n static setGax(gax: CallOptions | undefined, scope: CallScope): void {\n GcpTaskRegistry.ladder.at(scope).gax = gax;\n }\n\n /** The registered customizer, method -> API -> ALL, or undefined. */\n // webpieces-disable no-function-outside-class -- process-global registry lookup\n static customizerFor(api: Function, methodName: string): TaskCustomizer | undefined {\n return GcpTaskRegistry.ladder\n .rungs(api, methodName)\n .find((p: GcpTaskPolicy) => p.customizer !== undefined)?.customizer;\n }\n\n /** The registered gax CallOptions, method -> API -> ALL, or undefined. */\n // webpieces-disable no-function-outside-class -- process-global registry lookup\n static gaxFor(api: Function, methodName: string): CallOptions | undefined {\n return GcpTaskRegistry.ladder.rungs(api, methodName).find((p: GcpTaskPolicy) => p.gax !== undefined)?.gax;\n }\n\n // webpieces-disable no-function-outside-class -- process-global registry reset for tests\n static clear(): void {\n GcpTaskRegistry.ladder.clear();\n }\n}\n"]}
|
package/src/GrpcErrorMapper.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Translates a gRPC failure from the Cloud Tasks API into the portable webpieces error taxonomy, so
|
|
3
|
-
* the ONE classifier (`
|
|
3
|
+
* the ONE classifier (`ApiRegistry.classify`) — and therefore `RetryStrategy` and `LogApiCall` —
|
|
4
4
|
* judges an enqueue exactly like any other call:
|
|
5
5
|
*
|
|
6
6
|
* | gRPC code | becomes | outcome |
|
package/src/GrpcErrorMapper.js
CHANGED
|
@@ -5,7 +5,7 @@ const core_util_1 = require("@webpieces/core-util");
|
|
|
5
5
|
const TaskAlreadyExistsError_1 = require("./TaskAlreadyExistsError");
|
|
6
6
|
/**
|
|
7
7
|
* Translates a gRPC failure from the Cloud Tasks API into the portable webpieces error taxonomy, so
|
|
8
|
-
* the ONE classifier (`
|
|
8
|
+
* the ONE classifier (`ApiRegistry.classify`) — and therefore `RetryStrategy` and `LogApiCall` —
|
|
9
9
|
* judges an enqueue exactly like any other call:
|
|
10
10
|
*
|
|
11
11
|
* | gRPC code | becomes | outcome |
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"GrpcErrorMapper.js","sourceRoot":"","sources":["../../../../../packages/cloud/cloudtasks-client/src/GrpcErrorMapper.ts"],"names":[],"mappings":";;;AAAA,oDAK8B;AAC9B,qEAAkE;AAElE;;;;;;;;;;;;;;;GAeG;AACH,MAAa,eAAe;IACxB,MAAM,CAAU,iBAAiB,GAAG,CAAC,CAAC;IACtC,MAAM,CAAU,cAAc,GAAG,CAAC,CAAC;IACnC,MAAM,CAAU,kBAAkB,GAAG,CAAC,CAAC;IACvC,MAAM,CAAU,WAAW,GAAG,EAAE,CAAC;IAEjC;;;OAGG;IACH,GAAG,CAAC,KAAY,EAAE,QAA4B;QAC1C,MAAM,IAAI,GAAG,MAAM,CAAC,wBAAwB,CAAC,KAAK,EAAE,MAAM,CAAC,EAAE,KAAK,CAAC;QACnE,IAAI,OAAO,IAAI,KAAK,QAAQ,EAAE,CAAC;YAC3B,OAAO,KAAK,CAAC;QACjB,CAAC;QACD,MAAM,OAAO,GAAG,2CAA2C,IAAI,KAAK,KAAK,CAAC,OAAO,EAAE,CAAC;QACpF,QAAQ,IAAI,EAAE,CAAC;YACX,KAAK,eAAe,CAAC,WAAW;gBAC5B,OAAO,IAAI,+BAAmB,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;YACnD,KAAK,eAAe,CAAC,iBAAiB;gBAClC,OAAO,IAAI,qCAAyB,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;YACzD,KAAK,eAAe,CAAC,kBAAkB;gBACnC,OAAO,IAAI,qCAAyB,CAAC,OAAO,EAAE,EAAE,EAAE,KAAK,CAAC,CAAC;YAC7D,KAAK,eAAe,CAAC,cAAc;gBAC/B,OAAO,IAAI,+CAAsB,CAAC,QAAQ,IAAI,SAAS,EAAE,KAAK,CAAC,CAAC;YACpE;gBACI,OAAO,IAAI,8BAAkB,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;QACtD,CAAC;IACL,CAAC;;AA5BL,0CA6BC","sourcesContent":["import {\n ApiDependencyBackoffError,\n ApiDependencyError,\n ApiDependencyTimeoutError,\n ApiUnavailableError,\n} from '@webpieces/core-util';\nimport { TaskAlreadyExistsError } from './TaskAlreadyExistsError';\n\n/**\n * Translates a gRPC failure from the Cloud Tasks API into the portable webpieces error taxonomy, so\n * the ONE classifier (`
|
|
1
|
+
{"version":3,"file":"GrpcErrorMapper.js","sourceRoot":"","sources":["../../../../../packages/cloud/cloudtasks-client/src/GrpcErrorMapper.ts"],"names":[],"mappings":";;;AAAA,oDAK8B;AAC9B,qEAAkE;AAElE;;;;;;;;;;;;;;;GAeG;AACH,MAAa,eAAe;IACxB,MAAM,CAAU,iBAAiB,GAAG,CAAC,CAAC;IACtC,MAAM,CAAU,cAAc,GAAG,CAAC,CAAC;IACnC,MAAM,CAAU,kBAAkB,GAAG,CAAC,CAAC;IACvC,MAAM,CAAU,WAAW,GAAG,EAAE,CAAC;IAEjC;;;OAGG;IACH,GAAG,CAAC,KAAY,EAAE,QAA4B;QAC1C,MAAM,IAAI,GAAG,MAAM,CAAC,wBAAwB,CAAC,KAAK,EAAE,MAAM,CAAC,EAAE,KAAK,CAAC;QACnE,IAAI,OAAO,IAAI,KAAK,QAAQ,EAAE,CAAC;YAC3B,OAAO,KAAK,CAAC;QACjB,CAAC;QACD,MAAM,OAAO,GAAG,2CAA2C,IAAI,KAAK,KAAK,CAAC,OAAO,EAAE,CAAC;QACpF,QAAQ,IAAI,EAAE,CAAC;YACX,KAAK,eAAe,CAAC,WAAW;gBAC5B,OAAO,IAAI,+BAAmB,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;YACnD,KAAK,eAAe,CAAC,iBAAiB;gBAClC,OAAO,IAAI,qCAAyB,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;YACzD,KAAK,eAAe,CAAC,kBAAkB;gBACnC,OAAO,IAAI,qCAAyB,CAAC,OAAO,EAAE,EAAE,EAAE,KAAK,CAAC,CAAC;YAC7D,KAAK,eAAe,CAAC,cAAc;gBAC/B,OAAO,IAAI,+CAAsB,CAAC,QAAQ,IAAI,SAAS,EAAE,KAAK,CAAC,CAAC;YACpE;gBACI,OAAO,IAAI,8BAAkB,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;QACtD,CAAC;IACL,CAAC;;AA5BL,0CA6BC","sourcesContent":["import {\n ApiDependencyBackoffError,\n ApiDependencyError,\n ApiDependencyTimeoutError,\n ApiUnavailableError,\n} from '@webpieces/core-util';\nimport { TaskAlreadyExistsError } from './TaskAlreadyExistsError';\n\n/**\n * Translates a gRPC failure from the Cloud Tasks API into the portable webpieces error taxonomy, so\n * the ONE classifier (`ApiRegistry.classify`) — and therefore `RetryStrategy` and `LogApiCall` —\n * judges an enqueue exactly like any other call:\n *\n * | gRPC code | becomes | outcome |\n * |---|---|---|\n * | 14 UNAVAILABLE (connection reset, channel down) | `ApiUnavailableError` | transient |\n * | 4 DEADLINE_EXCEEDED | `ApiDependencyTimeoutError` | transient |\n * | 8 RESOURCE_EXHAUSTED (quota, throttling) | `ApiDependencyBackoffError` | transient |\n * | 6 ALREADY_EXISTS | `TaskAlreadyExistsError` | success, see that class |\n * | anything else | `ApiDependencyError` | failure |\n *\n * The original gRPC error is kept as `cause`. An error with no numeric gRPC `code` (a local failure\n * before any RPC was sent) is returned unchanged.\n */\nexport class GrpcErrorMapper {\n static readonly DEADLINE_EXCEEDED = 4;\n static readonly ALREADY_EXISTS = 6;\n static readonly RESOURCE_EXHAUSTED = 8;\n static readonly UNAVAILABLE = 14;\n\n /**\n * @param error - the already-normalized error `createTask` rejected with\n * @param taskName - the full task name that was sent, reported by an ALREADY_EXISTS\n */\n map(error: Error, taskName: string | undefined): Error {\n const code = Object.getOwnPropertyDescriptor(error, 'code')?.value;\n if (typeof code !== 'number') {\n return error;\n }\n const message = `Cloud Tasks createTask failed with gRPC ${code}: ${error.message}`;\n switch (code) {\n case GrpcErrorMapper.UNAVAILABLE:\n return new ApiUnavailableError(message, error);\n case GrpcErrorMapper.DEADLINE_EXCEEDED:\n return new ApiDependencyTimeoutError(message, error);\n case GrpcErrorMapper.RESOURCE_EXHAUSTED:\n return new ApiDependencyBackoffError(message, 30, error);\n case GrpcErrorMapper.ALREADY_EXISTS:\n return new TaskAlreadyExistsError(taskName ?? 'unknown', error);\n default:\n return new ApiDependencyError(message, error);\n }\n }\n}\n"]}
|
package/src/TaskProxyClient.d.ts
CHANGED
|
@@ -25,7 +25,7 @@ export declare class TaskProxyClient {
|
|
|
25
25
|
private readonly invoker;
|
|
26
26
|
private readonly headers;
|
|
27
27
|
/**
|
|
28
|
-
* The enqueue transport's default for `
|
|
28
|
+
* The enqueue transport's default for `ClientRegistry.execute` (Path A): 30 s if a caller only
|
|
29
29
|
* registers a strategy-free timeout ladder, otherwise webpieces' shared `RetryStrategy` — 5 s per
|
|
30
30
|
* attempt, 3 retries, backoff 500 ms x2 capped at 4 s. RPC passes `new TransportDefault(30_000)`
|
|
31
31
|
* (no retry); this is the ONLY place the two transports differ.
|
|
@@ -59,7 +59,7 @@ export declare class TaskProxyClient {
|
|
|
59
59
|
* and the effective customizer and gax options (call site, else `GcpTaskRegistry` method -> API
|
|
60
60
|
* -> ALL). Then exactly ONE retry layer runs:
|
|
61
61
|
*
|
|
62
|
-
* - **Path A, no gax — webpieces owns timing.** `
|
|
62
|
+
* - **Path A, no gax — webpieces owns timing.** `ClientRegistry.execute` with
|
|
63
63
|
* {@link TaskProxyClient.ENQUEUE_TRANSPORT_DEFAULT}: a registered strategy (method -> API -> ALL), else a
|
|
64
64
|
* registered timeout, else `RetryStrategy(5_000, 3, 500, 2, 4_000)`. Each attempt sends
|
|
65
65
|
* `createTask(request, { timeout: attemptMs })`.
|
package/src/TaskProxyClient.js
CHANGED
|
@@ -50,7 +50,7 @@ let TaskProxyClient = class TaskProxyClient {
|
|
|
50
50
|
invoker;
|
|
51
51
|
headers;
|
|
52
52
|
/**
|
|
53
|
-
* The enqueue transport's default for `
|
|
53
|
+
* The enqueue transport's default for `ClientRegistry.execute` (Path A): 30 s if a caller only
|
|
54
54
|
* registers a strategy-free timeout ladder, otherwise webpieces' shared `RetryStrategy` — 5 s per
|
|
55
55
|
* attempt, 3 retries, backoff 500 ms x2 capped at 4 s. RPC passes `new TransportDefault(30_000)`
|
|
56
56
|
* (no retry); this is the ONLY place the two transports differ.
|
|
@@ -101,7 +101,7 @@ let TaskProxyClient = class TaskProxyClient {
|
|
|
101
101
|
* and the effective customizer and gax options (call site, else `GcpTaskRegistry` method -> API
|
|
102
102
|
* -> ALL). Then exactly ONE retry layer runs:
|
|
103
103
|
*
|
|
104
|
-
* - **Path A, no gax — webpieces owns timing.** `
|
|
104
|
+
* - **Path A, no gax — webpieces owns timing.** `ClientRegistry.execute` with
|
|
105
105
|
* {@link TaskProxyClient.ENQUEUE_TRANSPORT_DEFAULT}: a registered strategy (method -> API -> ALL), else a
|
|
106
106
|
* registered timeout, else `RetryStrategy(5_000, 3, 500, 2, 4_000)`. Each attempt sends
|
|
107
107
|
* `createTask(request, { timeout: attemptMs })`.
|
|
@@ -136,8 +136,8 @@ let TaskProxyClient = class TaskProxyClient {
|
|
|
136
136
|
const attempt = (timeoutMs) => core_util_1.CallDeadline.run(timeoutMs, callContext, (signal) => this.attempt(plan, enqueuePlan, frame.info, requestDto, timeoutMs, signal));
|
|
137
137
|
// Only the winning attempt may publish the reference. A late enqueue cannot overwrite it.
|
|
138
138
|
frame.jobRef = await this.dedupAware(methodName, () => enqueuePlan.gax === undefined
|
|
139
|
-
? core_util_1.
|
|
140
|
-
: attempt(core_util_1.
|
|
139
|
+
? core_util_1.ClientRegistry.execute(this.apiClass, callContext, attempt, TaskProxyClient_1.ENQUEUE_TRANSPORT_DEFAULT)
|
|
140
|
+
: attempt(core_util_1.ClientRegistry.resolveTimeout(this.apiClass, methodName, TaskProxyClient_1.ENQUEUE_SAFETY_CAP_MS)));
|
|
141
141
|
// Fire-and-forget: the enqueue ack is not a meaningful response, but it is non-null so
|
|
142
142
|
// LogApiCall's null-response guard passes; a recorded test asserts the enqueued REQUEST.
|
|
143
143
|
return frame.jobRef;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TaskProxyClient.js","sourceRoot":"","sources":["../../../../../packages/cloud/cloudtasks-client/src/TaskProxyClient.ts"],"names":[],"mappings":";;;;;AAAA,yCAAmC;AACnC,oDAuB8B;AAC9B,0DAIiC;AACjC,2CAAgG;AAChG,qEAAkE;AAClE,uDAAoD;AACpD,uDAAyD;AAGzD,MAAM,GAAG,GAAG,sBAAU,CAAC,SAAS,CAAC,iBAAiB,CAAC,CAAC;AAEpD,8EAA8E;AAC9E,MAAM,YAAY;IACd,IAAI,CAAS;IACb,SAAS,CAAS;IAClB,QAAQ,CAAW;IACnB;uGACmG;IACnG,IAAI,CAAY;IAEhB,YAAY,IAAY,EAAE,SAAiB,EAAE,QAAkB,EAAE,IAAe;QAC5E,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;QAC3B,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;QACzB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;IACrB,CAAC;CACJ;AAED;;;;;;;;;;;;;;;;;;GAkBG;AAEI,IAAM,eAAe,GAArB,MAAM,eAAe;;IA+BkB;IACU;IA/BpD;;;;;OAKG;IACH,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\n assertEveryEndpointHasAuthMode,\n assertNotInternalApi,\n AuthMode,\n DestinationTrust,\n MaskSpec,\n LogManager,\n LogApiCallImpl,\n ApiMethodInfo,\n CallRegistry,\n CallDeadline,\n CallContext,\n RetryStrategy,\n TransportDefault,\n WRITE_IDEMPOTENT,\n toError,\n} from '@webpieces/core-util';\nimport {\n RequestContextApiCallContext,\n RequestContextHeaders,\n provideFrameworkTransient,\n} from '@webpieces/core-context';\nimport { TaskInvoker, TaskRequest, ScheduleInfo, EnqueuePlan, JobReference } from './TaskTypes';\nimport { TaskAlreadyExistsError } from './TaskAlreadyExistsError';\nimport { GcpTaskRegistry } from './GcpTaskRegistry';\nimport { currentScheduleFrame } from './ScheduleContext';\nimport { ApiPrototype, TaskClientConfig } from './TaskClientConfig';\n\nconst log = LogManager.getLogger('TaskProxyClient');\n\n/** Per-endpoint routing plan resolved once from the contract's decorators. */\nclass EndpointPlan {\n path: string;\n queueName: string;\n authMode: AuthMode;\n /** @MaskLog spec for this endpoint's request DTO, read once here so the enqueue log path masks\n * a secret on the task payload without a per-enqueue reflection hit. Undefined = log verbatim. */\n mask?: MaskSpec;\n\n constructor(path: string, queueName: string, authMode: AuthMode, mask?: MaskSpec) {\n this.path = path;\n this.queueName = queueName;\n this.authMode = authMode;\n this.mask = mask;\n }\n}\n\n/**\n * TaskProxyClient - the enqueue engine behind one @PubSub API contract's client proxy.\n *\n * The fire-and-forget twin of http-client-core's ProxyClient, and TWO-PHASE for the same reason:\n * its COLLABORATORS (invoker, headers) come from the container, while the PER-CLIENT state (which\n * contract, which target) arrives on {@link init}. That is what lets {@link ClientCloudTasksFactory}\n * hold a `Provider<TaskProxyClient>` and hand out a fresh, independently-configured client per\n * contract.\n *\n * Calling an endpoint ENQUEUES a task (it does not call remotely); the task is later delivered to\n * the same endpoint's controller through the full server filter chain.\n *\n * It owns:\n * - @ApiPath / @PubSub convention validation + the endpoint plans from the contract's decorators\n * - Resolving the callee's base URL from svcName through `ClientRegistry.resolve` — the SAME chain the\n * RPC client runs: a registered mapping, else the installed deriver, else throw\n * - Context propagation onto the task headers (a credential is never a context key, so none can ride along)\n * - Handing a fully-built TaskRequest to the bound {@link TaskInvoker}\n */\n@provideFrameworkTransient()\nexport class TaskProxyClient {\n /**\n * The enqueue transport's default for `CallRegistry.execute` (Path A): 30 s if a caller only\n * registers a strategy-free timeout ladder, otherwise webpieces' shared `RetryStrategy` — 5 s per\n * attempt, 3 retries, backoff 500 ms x2 capped at 4 s. RPC passes `new TransportDefault(30_000)`\n * (no retry); this is the ONLY place the two transports differ.\n */\n static readonly ENQUEUE_TRANSPORT_DEFAULT = new TransportDefault(\n 30_000,\n new RetryStrategy(5_000, 3, 500, 2, 4_000).run,\n );\n\n /** Path B's safety cap when no timeout is registered: caller-owned gax timing still ends. */\n static readonly ENQUEUE_SAFETY_CAP_MS = 30_000;\n\n // Assigned by init(), which the factory calls immediately after construction.\n private plans!: Map<string, EndpointPlan>;\n private apiName!: string;\n private apiClass!: ApiPrototype<object>;\n private config!: TaskClientConfig;\n\n /**\n * The api-call log seam, built around this node-only package's OWN RequestContext-backed\n * ApiCallContext. Constructed here rather than installed at startup, so a plain NestJS/Express\n * host needs no webpieces STARTUP INSTALL to enqueue — it only has to run the call inside a\n * `RequestContext.run(...)` scope, which such a host already opens per request. That is the defect\n * that made every enqueue throw `ApiCallContext is not installed`.\n */\n private readonly logApiCall = new LogApiCallImpl(new RequestContextApiCallContext());\n\n constructor(\n @inject(TaskInvoker) private readonly invoker: TaskInvoker,\n @inject(RequestContextHeaders) private readonly headers: RequestContextHeaders,\n ) {}\n\n /** Bind this client to one @PubSub contract + target. */\n init(apiClass: ApiPrototype<object>, config: TaskClientConfig): void {\n assertNotInternalApi(apiClass, 'Cloud Tasks client');\n if (!isApiPath(apiClass)) {\n throw new Error(\n `Class ${apiClass.name || 'Unknown'} must be decorated with @ApiPath()`,\n );\n }\n assertPubSubConventions(apiClass);\n assertEveryEndpointHasAuthMode(apiClass);\n\n this.config = config;\n this.apiClass = apiClass;\n this.apiName = apiClass.name || 'UnknownApi';\n this.plans = this.buildPlans(apiClass);\n }\n\n /** Check whether the contract declares a @PubSub endpoint with this method name. */\n hasEndpoint(methodName: string): boolean {\n return this.plans.has(methodName);\n }\n\n /**\n * Enqueue one task for the named endpoint. Must run inside a CloudTaskScheduler lambda\n * (which supplies the ScheduleInfo) within an active RequestContext, e.g.:\n * scheduler.addToQueue(() => taskClient.foo(req), new ScheduleOptions(TaskName.dedup(id)));\n *\n * Before any attempt it resolves, ONCE: the task name (so every attempt carries the same one),\n * and the effective customizer and gax options (call site, else `GcpTaskRegistry` method -> API\n * -> ALL). Then exactly ONE retry layer runs:\n *\n * - **Path A, no gax — webpieces owns timing.** `CallRegistry.execute` with\n * {@link TaskProxyClient.ENQUEUE_TRANSPORT_DEFAULT}: a registered strategy (method -> API -> ALL), else a\n * registered timeout, else `RetryStrategy(5_000, 3, 500, 2, 4_000)`. Each attempt sends\n * `createTask(request, { timeout: attemptMs })`.\n * - **Path B, gax set — you own timing.** No strategy lookup and ONE attempt, sending\n * `createTask(request, yourGax)` under a safety cap from the timeout ladder (method -> API ->\n * ALL -> 30 s) so a hung channel cannot block forever.\n *\n * Either way the endpoint reports `WRITE_IDEMPOTENT`: one name for every attempt makes a repeat\n * safe (delivery is at-least-once anyway, and `TaskName.serverAssigned()` accepts duplicates).\n * A `TaskAlreadyExistsError` is caught HERE, above the retry layer: `WARN [ENQUEUE-DEDUP]`, then\n * success with `JobReference.alreadyExisted = true`.\n */\n // webpieces-disable no-any-unknown -- the request DTO's type is erased at the proxy boundary\n async enqueue(methodName: string, requestDto: unknown): Promise<void> {\n const plan = this.plans.get(methodName);\n if (!plan) {\n throw new Error(`No @PubSub endpoint '${methodName}' on ${this.apiName}`);\n }\n\n const frame = currentScheduleFrame();\n if (!frame) {\n throw new Error(\n 'Cloud task enqueue must run inside a CloudTaskScheduler lambda, e.g. ' +\n 'scheduler.addToQueue(() => taskClient.method(req), new ScheduleOptions(TaskName.dedup(id))).',\n );\n }\n const enqueuePlan = new EnqueuePlan(\n frame.info.taskName.resolve(),\n frame.info.customizeTask ?? GcpTaskRegistry.customizerFor(this.apiClass, methodName),\n frame.info.gax ?? GcpTaskRegistry.gaxFor(this.apiClass, methodName),\n );\n\n // Route through LogApiCall exactly like ProxyClient does for http. TaskProxyClient.enqueue is\n // the ONE seam above BOTH GcpTaskInvoker (prod) and InMemoryTaskInvoker (localhost) — whichever\n // TaskInvoker is bound, this method sits above it — so the enqueue edge gets structured\n // `jsonPayload.api.*` logging uniformly, without touching either invoker impl.\n const info = new ApiMethodInfo(\n 'client',\n this.apiName,\n methodName,\n this.apiClass,\n undefined,\n plan.mask,\n );\n await this.logApiCall.execute(info, requestDto, async () => {\n const callContext = new CallContext(this.apiName, methodName, WRITE_IDEMPOTENT, this.apiClass);\n const attempt = (timeoutMs: number): Promise<JobReference> =>\n CallDeadline.run(timeoutMs, callContext, (signal: AbortSignal) =>\n this.attempt(plan, enqueuePlan, frame.info, requestDto, timeoutMs, signal),\n );\n // Only the winning attempt may publish the reference. A late enqueue cannot overwrite it.\n frame.jobRef = await this.dedupAware(methodName, () =>\n enqueuePlan.gax === undefined\n ? CallRegistry.execute(this.apiClass, callContext, attempt, TaskProxyClient.ENQUEUE_TRANSPORT_DEFAULT)\n : attempt(CallRegistry.resolveTimeout(this.apiClass, methodName, TaskProxyClient.ENQUEUE_SAFETY_CAP_MS)),\n );\n // Fire-and-forget: the enqueue ack is not a meaningful response, but it is non-null so\n // LogApiCall's null-response guard passes; a recorded test asserts the enqueued REQUEST.\n return frame.jobRef;\n });\n }\n\n /** ALREADY_EXISTS is the task the caller asked for, already there: success, logged once. */\n private async dedupAware(\n methodName: string,\n enqueue: () => Promise<JobReference>,\n ): Promise<JobReference> {\n // webpieces-disable no-unmanaged-exceptions -- ALREADY_EXISTS is success; everything else propagates unchanged\n // eslint-disable-next-line @webpieces/no-unmanaged-exceptions\n try {\n return await enqueue();\n } catch (err: unknown) {\n const error = toError(err);\n if (!(error instanceof TaskAlreadyExistsError)) {\n // eslint-disable-next-line @webpieces/no-unmanaged-exceptions -- rethrows the enqueue's own failure unchanged\n throw err;\n }\n log.warn(\n `[ENQUEUE-DEDUP] ${this.apiName}.${methodName} task ${error.taskName} already exists ` +\n '(a retry whose earlier attempt landed, or a dedup name reused) — treated as success',\n );\n return new JobReference(error.taskName, true);\n }\n }\n\n /** ONE enqueue attempt: a fresh TaskRequest, handed to the bound invoker. */\n private async attempt(\n plan: EndpointPlan,\n enqueuePlan: EnqueuePlan,\n scheduleInfo: ScheduleInfo,\n // webpieces-disable no-any-unknown -- the request DTO's type is erased at the proxy boundary\n requestDto: unknown,\n timeoutMs: number,\n signal: AbortSignal,\n ): Promise<JobReference> {\n // Resolved lazily (not at client construction) so building a client stays synchronous.\n // Every metadata read beneath resolveUrl is memoized process-wide, so only the first\n // enqueue in the process pays a lookup.\n const targetUrl = await this.config.resolveUrl();\n CallDeadline.throwIfAborted(signal);\n\n const request = new TaskRequest(\n targetUrl,\n plan.path,\n plan.queueName,\n requestDto,\n this.buildContextHeaders(plan.authMode),\n plan.authMode,\n scheduleInfo,\n enqueuePlan,\n );\n\n // svcName, not the URL, is the stable name across demo/qa/prod.\n log.debug(`enqueue task ${plan.queueName} -> ${this.config.svcName}${plan.path}`);\n return this.invoker.enqueue(request, timeoutMs, signal);\n }\n\n /** Endpoint name -> its resolved path / queue / auth mode, read once from the decorators. */\n private buildPlans(apiClass: ApiPrototype<object>): Map<string, EndpointPlan> {\n const basePath = getApiPath(apiClass) ?? '';\n const endpoints = getEndpoints(apiClass) ?? {};\n const plans = new Map<string, EndpointPlan>();\n\n for (const methodName of Object.keys(endpoints)) {\n const authMeta = getAuthMeta(apiClass, methodName);\n const authMode = authMeta ? this.invoker.selectAuthentication(authMeta) : undefined;\n if (!authMode) {\n throw new Error(`Endpoint '${methodName}' on ${this.apiName} has no auth mode`);\n }\n const plan = new EndpointPlan(\n basePath + endpoints[methodName],\n getQueueName(apiClass, methodName),\n authMode,\n getMaskSpec(apiClass, methodName),\n );\n plans.set(methodName, plan);\n }\n return plans;\n }\n\n /**\n * Every transferred context key (txId/requestId/tenant…) the DELIVERY endpoint may receive,\n * request-id chained. Throws if there is no active RequestContext — an enqueue with no trace is\n * a bug.\n *\n * No credential can appear here: `authorization` is read off the inbound HttpRequest and is not\n * a ContextKey, so it never enters the RequestContext to be transferred. The invoker mints the\n * task's own delivery auth per the endpoint's oidcSameProject() / oidcSvcs(...) / sharedSecret(...) mode.\n *\n * TRUSTED keys DO flow on this path, and that is the point of deriving {@link DestinationTrust}\n * from `plan.authMode` rather than hardcoding it: a task is delivered under the OIDC token the\n * invoker mints, so the delivery endpoint authenticates its caller and its AuthFilter admits the\n * userId/orgId we vouched for. A @PubSub endpoint declared @WpAuthPublic or jwt() would not, and it\n * correctly stops receiving them — the enqueue keeps working, minus context the callee would\n * have 401'd on.\n */\n private buildContextHeaders(authMode: AuthMode): Map<string, string> {\n return this.headers.buildOutboundHeaders(DestinationTrust.forAuthMode(authMode));\n }\n}\n\n/**\n * DI token for the `Provider<TaskProxyClient>` that hands out enqueue clients — one per @PubSub\n * contract. `Provider<T>` is erased at runtime, so it cannot be its own token; this Symbol names T.\n *\n * Because TaskProxyClient is bound TRANSIENT, every `get()` constructs a new one. (Were it bound\n * `@provideFrameworkSingleton`, the very same Provider would hand back one lazily-created instance\n * instead — the provider caches nothing, so the target's scope decides.)\n */\n// webpieces-disable no-symbol-di-tokens -- Provider<T> is erased at runtime; the Symbol names T\nexport const TASK_PROXY_CLIENT_PROVIDER = Symbol.for('Provider<TaskProxyClient>');\n"]}
|
|
1
|
+
{"version":3,"file":"TaskProxyClient.js","sourceRoot":"","sources":["../../../../../packages/cloud/cloudtasks-client/src/TaskProxyClient.ts"],"names":[],"mappings":";;;;;AAAA,yCAAmC;AACnC,oDAuB8B;AAC9B,0DAIiC;AACjC,2CAAgG;AAChG,qEAAkE;AAClE,uDAAoD;AACpD,uDAAyD;AAGzD,MAAM,GA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,IAAI,CAAC,OAAO,CAAC,oBAAoB,CAAC,4BAAgB,CAAC,WAAW,CAAC,QAAQ,CAAC,CAAC,CAAC;IACrF,CAAC;;AAjOQ,0CAAe;0BAAf,eAAe;IAD3B,IAAA,wCAAyB,GAAE;IAgCnB,mBAAA,IAAA,kBAAM,EAAC,uBAAW,CAAC,CAAA;IACnB,mBAAA,IAAA,kBAAM,EAAC,oCAAqB,CAAC,CAAA;6CADiB,uBAAW;QACD,oCAAqB;GAhCzE,eAAe,CAkO3B;AAED;;;;;;;GAOG;AACH,gGAAgG;AACnF,QAAA,0BAA0B,GAAG,MAAM,CAAC,GAAG,CAAC,2BAA2B,CAAC,CAAC","sourcesContent":["import { inject } from 'inversify';\nimport {\n isApiPath,\n getApiPath,\n getEndpoints,\n getAuthMeta,\n getQueueName,\n getMaskSpec,\n assertPubSubConventions,\n assertEveryEndpointHasAuthMode,\n assertNotInternalApi,\n AuthMode,\n DestinationTrust,\n MaskSpec,\n LogManager,\n LogApiCallImpl,\n ApiMethodInfo,\n ClientRegistry,\n CallDeadline,\n CallContext,\n RetryStrategy,\n TransportDefault,\n WRITE_IDEMPOTENT,\n toError,\n} from '@webpieces/core-util';\nimport {\n RequestContextApiCallContext,\n RequestContextHeaders,\n provideFrameworkTransient,\n} from '@webpieces/core-context';\nimport { TaskInvoker, TaskRequest, ScheduleInfo, EnqueuePlan, JobReference } from './TaskTypes';\nimport { TaskAlreadyExistsError } from './TaskAlreadyExistsError';\nimport { GcpTaskRegistry } from './GcpTaskRegistry';\nimport { currentScheduleFrame } from './ScheduleContext';\nimport { ApiPrototype, TaskClientConfig } from './TaskClientConfig';\n\nconst log = LogManager.getLogger('TaskProxyClient');\n\n/** Per-endpoint routing plan resolved once from the contract's decorators. */\nclass EndpointPlan {\n path: string;\n queueName: string;\n authMode: AuthMode;\n /** @MaskLog spec for this endpoint's request DTO, read once here so the enqueue log path masks\n * a secret on the task payload without a per-enqueue reflection hit. Undefined = log verbatim. */\n mask?: MaskSpec;\n\n constructor(path: string, queueName: string, authMode: AuthMode, mask?: MaskSpec) {\n this.path = path;\n this.queueName = queueName;\n this.authMode = authMode;\n this.mask = mask;\n }\n}\n\n/**\n * TaskProxyClient - the enqueue engine behind one @PubSub API contract's client proxy.\n *\n * The fire-and-forget twin of http-client-core's ProxyClient, and TWO-PHASE for the same reason:\n * its COLLABORATORS (invoker, headers) come from the container, while the PER-CLIENT state (which\n * contract, which target) arrives on {@link init}. That is what lets {@link ClientCloudTasksFactory}\n * hold a `Provider<TaskProxyClient>` and hand out a fresh, independently-configured client per\n * contract.\n *\n * Calling an endpoint ENQUEUES a task (it does not call remotely); the task is later delivered to\n * the same endpoint's controller through the full server filter chain.\n *\n * It owns:\n * - @ApiPath / @PubSub convention validation + the endpoint plans from the contract's decorators\n * - Resolving the callee's base URL from svcName through `ClientRegistry.resolve` — the SAME chain the\n * RPC client runs: a registered mapping, else the installed deriver, else throw\n * - Context propagation onto the task headers (a credential is never a context key, so none can ride along)\n * - Handing a fully-built TaskRequest to the bound {@link TaskInvoker}\n */\n@provideFrameworkTransient()\nexport class TaskProxyClient {\n /**\n * The enqueue transport's default for `ClientRegistry.execute` (Path A): 30 s if a caller only\n * registers a strategy-free timeout ladder, otherwise webpieces' shared `RetryStrategy` — 5 s per\n * attempt, 3 retries, backoff 500 ms x2 capped at 4 s. RPC passes `new TransportDefault(30_000)`\n * (no retry); this is the ONLY place the two transports differ.\n */\n static readonly ENQUEUE_TRANSPORT_DEFAULT = new TransportDefault(\n 30_000,\n new RetryStrategy(5_000, 3, 500, 2, 4_000).run,\n );\n\n /** Path B's safety cap when no timeout is registered: caller-owned gax timing still ends. */\n static readonly ENQUEUE_SAFETY_CAP_MS = 30_000;\n\n // Assigned by init(), which the factory calls immediately after construction.\n private plans!: Map<string, EndpointPlan>;\n private apiName!: string;\n private apiClass!: ApiPrototype<object>;\n private config!: TaskClientConfig;\n\n /**\n * The api-call log seam, built around this node-only package's OWN RequestContext-backed\n * ApiCallContext. Constructed here rather than installed at startup, so a plain NestJS/Express\n * host needs no webpieces STARTUP INSTALL to enqueue — it only has to run the call inside a\n * `RequestContext.run(...)` scope, which such a host already opens per request. That is the defect\n * that made every enqueue throw `ApiCallContext is not installed`.\n */\n private readonly logApiCall = new LogApiCallImpl(new RequestContextApiCallContext());\n\n constructor(\n @inject(TaskInvoker) private readonly invoker: TaskInvoker,\n @inject(RequestContextHeaders) private readonly headers: RequestContextHeaders,\n ) {}\n\n /** Bind this client to one @PubSub contract + target. */\n init(apiClass: ApiPrototype<object>, config: TaskClientConfig): void {\n assertNotInternalApi(apiClass, 'Cloud Tasks client');\n if (!isApiPath(apiClass)) {\n throw new Error(\n `Class ${apiClass.name || 'Unknown'} must be decorated with @ApiPath()`,\n );\n }\n assertPubSubConventions(apiClass);\n assertEveryEndpointHasAuthMode(apiClass);\n\n this.config = config;\n this.apiClass = apiClass;\n this.apiName = apiClass.name || 'UnknownApi';\n this.plans = this.buildPlans(apiClass);\n }\n\n /** Check whether the contract declares a @PubSub endpoint with this method name. */\n hasEndpoint(methodName: string): boolean {\n return this.plans.has(methodName);\n }\n\n /**\n * Enqueue one task for the named endpoint. Must run inside a CloudTaskScheduler lambda\n * (which supplies the ScheduleInfo) within an active RequestContext, e.g.:\n * scheduler.addToQueue(() => taskClient.foo(req), new ScheduleOptions(TaskName.dedup(id)));\n *\n * Before any attempt it resolves, ONCE: the task name (so every attempt carries the same one),\n * and the effective customizer and gax options (call site, else `GcpTaskRegistry` method -> API\n * -> ALL). Then exactly ONE retry layer runs:\n *\n * - **Path A, no gax — webpieces owns timing.** `ClientRegistry.execute` with\n * {@link TaskProxyClient.ENQUEUE_TRANSPORT_DEFAULT}: a registered strategy (method -> API -> ALL), else a\n * registered timeout, else `RetryStrategy(5_000, 3, 500, 2, 4_000)`. Each attempt sends\n * `createTask(request, { timeout: attemptMs })`.\n * - **Path B, gax set — you own timing.** No strategy lookup and ONE attempt, sending\n * `createTask(request, yourGax)` under a safety cap from the timeout ladder (method -> API ->\n * ALL -> 30 s) so a hung channel cannot block forever.\n *\n * Either way the endpoint reports `WRITE_IDEMPOTENT`: one name for every attempt makes a repeat\n * safe (delivery is at-least-once anyway, and `TaskName.serverAssigned()` accepts duplicates).\n * A `TaskAlreadyExistsError` is caught HERE, above the retry layer: `WARN [ENQUEUE-DEDUP]`, then\n * success with `JobReference.alreadyExisted = true`.\n */\n // webpieces-disable no-any-unknown -- the request DTO's type is erased at the proxy boundary\n async enqueue(methodName: string, requestDto: unknown): Promise<void> {\n const plan = this.plans.get(methodName);\n if (!plan) {\n throw new Error(`No @PubSub endpoint '${methodName}' on ${this.apiName}`);\n }\n\n const frame = currentScheduleFrame();\n if (!frame) {\n throw new Error(\n 'Cloud task enqueue must run inside a CloudTaskScheduler lambda, e.g. ' +\n 'scheduler.addToQueue(() => taskClient.method(req), new ScheduleOptions(TaskName.dedup(id))).',\n );\n }\n const enqueuePlan = new EnqueuePlan(\n frame.info.taskName.resolve(),\n frame.info.customizeTask ?? GcpTaskRegistry.customizerFor(this.apiClass, methodName),\n frame.info.gax ?? GcpTaskRegistry.gaxFor(this.apiClass, methodName),\n );\n\n // Route through LogApiCall exactly like ProxyClient does for http. TaskProxyClient.enqueue is\n // the ONE seam above BOTH GcpTaskInvoker (prod) and InMemoryTaskInvoker (localhost) — whichever\n // TaskInvoker is bound, this method sits above it — so the enqueue edge gets structured\n // `jsonPayload.api.*` logging uniformly, without touching either invoker impl.\n const info = new ApiMethodInfo(\n 'client',\n this.apiName,\n methodName,\n this.apiClass,\n undefined,\n plan.mask,\n );\n await this.logApiCall.execute(info, requestDto, async () => {\n const callContext = new CallContext(this.apiName, methodName, WRITE_IDEMPOTENT, this.apiClass);\n const attempt = (timeoutMs: number): Promise<JobReference> =>\n CallDeadline.run(timeoutMs, callContext, (signal: AbortSignal) =>\n this.attempt(plan, enqueuePlan, frame.info, requestDto, timeoutMs, signal),\n );\n // Only the winning attempt may publish the reference. A late enqueue cannot overwrite it.\n frame.jobRef = await this.dedupAware(methodName, () =>\n enqueuePlan.gax === undefined\n ? ClientRegistry.execute(this.apiClass, callContext, attempt, TaskProxyClient.ENQUEUE_TRANSPORT_DEFAULT)\n : attempt(ClientRegistry.resolveTimeout(this.apiClass, methodName, TaskProxyClient.ENQUEUE_SAFETY_CAP_MS)),\n );\n // Fire-and-forget: the enqueue ack is not a meaningful response, but it is non-null so\n // LogApiCall's null-response guard passes; a recorded test asserts the enqueued REQUEST.\n return frame.jobRef;\n });\n }\n\n /** ALREADY_EXISTS is the task the caller asked for, already there: success, logged once. */\n private async dedupAware(\n methodName: string,\n enqueue: () => Promise<JobReference>,\n ): Promise<JobReference> {\n // webpieces-disable no-unmanaged-exceptions -- ALREADY_EXISTS is success; everything else propagates unchanged\n // eslint-disable-next-line @webpieces/no-unmanaged-exceptions\n try {\n return await enqueue();\n } catch (err: unknown) {\n const error = toError(err);\n if (!(error instanceof TaskAlreadyExistsError)) {\n // eslint-disable-next-line @webpieces/no-unmanaged-exceptions -- rethrows the enqueue's own failure unchanged\n throw err;\n }\n log.warn(\n `[ENQUEUE-DEDUP] ${this.apiName}.${methodName} task ${error.taskName} already exists ` +\n '(a retry whose earlier attempt landed, or a dedup name reused) — treated as success',\n );\n return new JobReference(error.taskName, true);\n }\n }\n\n /** ONE enqueue attempt: a fresh TaskRequest, handed to the bound invoker. */\n private async attempt(\n plan: EndpointPlan,\n enqueuePlan: EnqueuePlan,\n scheduleInfo: ScheduleInfo,\n // webpieces-disable no-any-unknown -- the request DTO's type is erased at the proxy boundary\n requestDto: unknown,\n timeoutMs: number,\n signal: AbortSignal,\n ): Promise<JobReference> {\n // Resolved lazily (not at client construction) so building a client stays synchronous.\n // Every metadata read beneath resolveUrl is memoized process-wide, so only the first\n // enqueue in the process pays a lookup.\n const targetUrl = await this.config.resolveUrl();\n CallDeadline.throwIfAborted(signal);\n\n const request = new TaskRequest(\n targetUrl,\n plan.path,\n plan.queueName,\n requestDto,\n this.buildContextHeaders(plan.authMode),\n plan.authMode,\n scheduleInfo,\n enqueuePlan,\n );\n\n // svcName, not the URL, is the stable name across demo/qa/prod.\n log.debug(`enqueue task ${plan.queueName} -> ${this.config.svcName}${plan.path}`);\n return this.invoker.enqueue(request, timeoutMs, signal);\n }\n\n /** Endpoint name -> its resolved path / queue / auth mode, read once from the decorators. */\n private buildPlans(apiClass: ApiPrototype<object>): Map<string, EndpointPlan> {\n const basePath = getApiPath(apiClass) ?? '';\n const endpoints = getEndpoints(apiClass) ?? {};\n const plans = new Map<string, EndpointPlan>();\n\n for (const methodName of Object.keys(endpoints)) {\n const authMeta = getAuthMeta(apiClass, methodName);\n const authMode = authMeta ? this.invoker.selectAuthentication(authMeta) : undefined;\n if (!authMode) {\n throw new Error(`Endpoint '${methodName}' on ${this.apiName} has no auth mode`);\n }\n const plan = new EndpointPlan(\n basePath + endpoints[methodName],\n getQueueName(apiClass, methodName),\n authMode,\n getMaskSpec(apiClass, methodName),\n );\n plans.set(methodName, plan);\n }\n return plans;\n }\n\n /**\n * Every transferred context key (txId/requestId/tenant…) the DELIVERY endpoint may receive,\n * request-id chained. Throws if there is no active RequestContext — an enqueue with no trace is\n * a bug.\n *\n * No credential can appear here: `authorization` is read off the inbound HttpRequest and is not\n * a ContextKey, so it never enters the RequestContext to be transferred. The invoker mints the\n * task's own delivery auth per the endpoint's oidcSameProject() / oidcSvcs(...) / sharedSecret(...) mode.\n *\n * TRUSTED keys DO flow on this path, and that is the point of deriving {@link DestinationTrust}\n * from `plan.authMode` rather than hardcoding it: a task is delivered under the OIDC token the\n * invoker mints, so the delivery endpoint authenticates its caller and its AuthFilter admits the\n * userId/orgId we vouched for. A @PubSub endpoint declared @WpAuthPublic or jwt() would not, and it\n * correctly stops receiving them — the enqueue keeps working, minus context the callee would\n * have 401'd on.\n */\n private buildContextHeaders(authMode: AuthMode): Map<string, string> {\n return this.headers.buildOutboundHeaders(DestinationTrust.forAuthMode(authMode));\n }\n}\n\n/**\n * DI token for the `Provider<TaskProxyClient>` that hands out enqueue clients — one per @PubSub\n * contract. `Provider<T>` is erased at runtime, so it cannot be its own token; this Symbol names T.\n *\n * Because TaskProxyClient is bound TRANSIENT, every `get()` constructs a new one. (Were it bound\n * `@provideFrameworkSingleton`, the very same Provider would hand back one lazily-created instance\n * instead — the provider caches nothing, so the target's scope decides.)\n */\n// webpieces-disable no-symbol-di-tokens -- Provider<T> is erased at runtime; the Symbol names T\nexport const TASK_PROXY_CLIENT_PROVIDER = Symbol.for('Provider<TaskProxyClient>');\n"]}
|
package/src/TaskTypes.d.ts
CHANGED
|
@@ -124,7 +124,7 @@ export declare class TaskRequest {
|
|
|
124
124
|
* HTTP POST to `targetUrl + path` — GcpTaskInvoker via Google Cloud Tasks, and
|
|
125
125
|
* InMemoryTaskInvoker via an in-process queue that fetches the target directly.
|
|
126
126
|
*
|
|
127
|
-
* An invoker performs ONE attempt and never retries: retries belong to `
|
|
127
|
+
* An invoker performs ONE attempt and never retries: retries belong to `ClientRegistry` (webpieces'
|
|
128
128
|
* `RetryStrategy` by default) or, when `plan.gax` is set, to gax. It throws
|
|
129
129
|
* `TaskAlreadyExistsError` for a name that already exists.
|
|
130
130
|
*/
|
package/src/TaskTypes.js
CHANGED
|
@@ -117,7 +117,7 @@ exports.TaskRequest = TaskRequest;
|
|
|
117
117
|
* HTTP POST to `targetUrl + path` — GcpTaskInvoker via Google Cloud Tasks, and
|
|
118
118
|
* InMemoryTaskInvoker via an in-process queue that fetches the target directly.
|
|
119
119
|
*
|
|
120
|
-
* An invoker performs ONE attempt and never retries: retries belong to `
|
|
120
|
+
* An invoker performs ONE attempt and never retries: retries belong to `ClientRegistry` (webpieces'
|
|
121
121
|
* `RetryStrategy` by default) or, when `plan.gax` is set, to gax. It throws
|
|
122
122
|
* `TaskAlreadyExistsError` for a name that already exists.
|
|
123
123
|
*/
|
package/src/TaskTypes.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TaskTypes.js","sourceRoot":"","sources":["../../../../../packages/cloud/cloudtasks-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AAT,SAAS,CAAQ;QAEjB,SAAI,GAAJ,IAAI,CAAQ;QAEZ,cAAS,GAAT,SAAS,CAAQ;QAGjB,SAAI,GAAJ,IAAI,CAAS;QAEb,mBAAc,GAAd,cAAc,CAAqB;QAEnC,aAAQ,GAAR,QAAQ,CAAU;QAElB,iBAAY,GAAZ,YAAY,CAAc;QAE1B,SAAI,GAAJ,IAAI,CAAa;IAC3B,CAAC;CACP;AApBD,kCAoBC;AAED;;;;;;;;;;GAUG;AACH,MAAsB,WAAW;IAC7B,oBAAoB,CAAC,IAAc;QAC/B,MAAM,QAAQ,GAAG,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,MAAgB,EAAE,EAAE,CAAC,MAAM,CAAC,IAAI,KAAK,MAAM,IAAI,MAAM,CAAC,IAAI,KAAK,QAAQ,CAAC,CAAC;QAC7G,IAAI,QAAQ;YAAE,OAAO,QAAQ,CAAC;QAC9B,IAAI,IAAI,CAAC,OAAO,CAAC,MAAM,KAAK,CAAC;YAAE,OAAO,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC;QACtD,MAAM,IAAI,KAAK,CAAC,2EAA2E,CAAC,CAAC;IACjG,CAAC;CAYJ;AAlBD,kCAkBC","sourcesContent":["import { AuthMeta, AuthMode } from '@webpieces/core-util';\nimport type { protos } from '@google-cloud/tasks';\nimport type { CallOptions } from 'google-gax';\nimport { TaskName } from './TaskName';\n\n/** The Google `CreateTask` request: `{ parent, task }`, built by webpieces on every attempt. */\nexport type ICreateTaskRequest = protos.google.cloud.tasks.v2.ICreateTaskRequest;\n\n/**\n * Changes the Google request of ONE enqueue attempt. It runs on every attempt, LAST, on a freshly\n * built `ICreateTaskRequest` (url, body, headers, auth, name, scheduleTime and dispatchDeadline are\n * already set) and returns the request to send — the same object mutated, or a new one.\n *\n * It is Google-request-only and never touches timing: gax `CallOptions` are\n * `ScheduleOptions.gax` / `GcpTaskRegistry.setGax`. If it changes `task.name`, it must stay under the\n * same queue path and return the same name on every attempt, or webpieces' own retries can create\n * duplicates. The in-memory invoker has no Google request and ignores it.\n */\nexport type TaskCustomizer = (request: ICreateTaskRequest) => ICreateTaskRequest;\n\n/**\n * Scheduling options for an enqueued task. Travels out-of-band (via the scheduler\n * frame in RequestContext) so the shared API method signature stays `foo(req)` on\n * both the client and the controller.\n */\nexport class ScheduleInfo {\n constructor(\n /** How the task is named; resolved ONCE per logical enqueue by `TaskProxyClient`. */\n readonly taskName: TaskName,\n /** Absolute epoch-millis to run at; omitted = run as soon as possible. */\n readonly epochMsToRunAt?: number,\n /** Per-task dispatch deadline in seconds (`task.dispatchDeadline`). */\n readonly taskTimeoutSeconds?: number,\n /** The call site's {@link TaskCustomizer}; wins over `GcpTaskRegistry.setCustomizer`. */\n readonly customizeTask?: TaskCustomizer,\n /** The call site's gax `CallOptions`; set ⇒ the caller owns timing for this enqueue. */\n readonly gax?: CallOptions,\n ) {}\n}\n\n/** Handle to an enqueued task (its Cloud Tasks name), returned by the scheduler. */\nexport class JobReference {\n constructor(\n /** The task's full resource name (`projects/.../tasks/<id>`), or the in-memory task id. */\n readonly taskId: string,\n /**\n * True when the task already existed (ALREADY_EXISTS): a retry whose earlier attempt landed,\n * or a `TaskName.dedup` collision. Reported as success, never as an error.\n */\n readonly alreadyExisted: boolean,\n ) {}\n}\n\n/**\n * What `TaskProxyClient` resolved ONCE for a logical enqueue, before any attempt: the task id every\n * attempt carries, and the EFFECTIVE customizer and gax options (call site, else `GcpTaskRegistry`\n * method -> API -> ALL).\n */\nexport class EnqueuePlan {\n constructor(\n /** The task id (not the full path); undefined = let Cloud Tasks assign one. */\n readonly taskId: string | undefined,\n readonly customizeTask?: TaskCustomizer,\n /** Set ⇒ the caller owns timing: one attempt, `createTask(request, gax)` as given. */\n readonly gax?: CallOptions,\n ) {}\n}\n\n/**\n * Everything an invoker needs to enqueue one task. Built by the enqueue proxy from\n * the shared @PubSub contract's decorators + the ambient RequestContext.\n */\nexport class TaskRequest {\n constructor(\n /** Callee base URL (e.g. https://email-svc-123.us-central1.run.app). */\n readonly targetUrl: string,\n /** Endpoint path the task is delivered to (basePath + endpoint, e.g. /email/send). */\n readonly path: string,\n /** Cloud Tasks queue name (getQueueName: @Queue override or `${Api}-${method}`). */\n readonly queueName: string,\n /** The request DTO (serialized to the POST body on delivery). */\n // webpieces-disable no-any-unknown -- request DTO type is erased at the task boundary\n readonly body: unknown,\n /** Context headers to propagate (txId/requestId/tenant…), already resolved. */\n readonly contextHeaders: Map<string, string>,\n /** The endpoint's auth mode — how the invoker authenticates delivery. */\n readonly authMode: AuthMode,\n /** Scheduling options (run-at, dispatch deadline). */\n readonly scheduleInfo: ScheduleInfo,\n /** The resolved name, customizer and gax for this logical enqueue. */\n readonly plan: EnqueuePlan,\n ) {}\n}\n\n/**\n * The transport that actually enqueues a task. Abstract class so it doubles as the\n * inversify DI token (inject by type, no Symbol). Bind GcpTaskInvoker in prod or\n * InMemoryTaskInvoker in tests/local dev. Both deliver a task the same way — a plain\n * HTTP POST to `targetUrl + path` — GcpTaskInvoker via Google Cloud Tasks, and\n * InMemoryTaskInvoker via an in-process queue that fetches the target directly.\n *\n * An invoker performs ONE attempt and never retries: retries belong to `
|
|
1
|
+
{"version":3,"file":"TaskTypes.js","sourceRoot":"","sources":["../../../../../packages/cloud/cloudtasks-client/src/TaskTypes.ts"],"names":[],"mappings":";;;AAoBA;;;;GAIG;AACH,MAAa,YAAY;IAGR;IAEA;IAEA;IAEA;IAEA;IAVb;IACI,qFAAqF;IAC5E,QAAkB;IAC3B,0EAA0E;IACjE,cAAuB;IAChC,uEAAuE;IAC9D,kBAA2B;IACpC,yFAAyF;IAChF,aAA8B;IACvC,wFAAwF;IAC/E,GAAiB;QARjB,aAAQ,GAAR,QAAQ,CAAU;QAElB,mBAAc,GAAd,cAAc,CAAS;QAEvB,uBAAkB,GAAlB,kBAAkB,CAAS;QAE3B,kBAAa,GAAb,aAAa,CAAiB;QAE9B,QAAG,GAAH,GAAG,CAAc;IAC3B,CAAC;CACP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EAAE,CAAC,MAAM,CAAC,IAAI,KAAK,MAAM,IAAI,MAAM,CAAC,IAAI,KAAK,QAAQ,CAAC,CAAC;QAC7G,IAAI,QAAQ;YAAE,OAAO,QAAQ,CAAC;QAC9B,IAAI,IAAI,CAAC,OAAO,CAAC,MAAM,KAAK,CAAC;YAAE,OAAO,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC;QACtD,MAAM,IAAI,KAAK,CAAC,2EAA2E,CAAC,CAAC;IACjG,CAAC;CAYJ;AAlBD,kCAkBC","sourcesContent":["import { AuthMeta, AuthMode } from '@webpieces/core-util';\nimport type { protos } from '@google-cloud/tasks';\nimport type { CallOptions } from 'google-gax';\nimport { TaskName } from './TaskName';\n\n/** The Google `CreateTask` request: `{ parent, task }`, built by webpieces on every attempt. */\nexport type ICreateTaskRequest = protos.google.cloud.tasks.v2.ICreateTaskRequest;\n\n/**\n * Changes the Google request of ONE enqueue attempt. It runs on every attempt, LAST, on a freshly\n * built `ICreateTaskRequest` (url, body, headers, auth, name, scheduleTime and dispatchDeadline are\n * already set) and returns the request to send — the same object mutated, or a new one.\n *\n * It is Google-request-only and never touches timing: gax `CallOptions` are\n * `ScheduleOptions.gax` / `GcpTaskRegistry.setGax`. If it changes `task.name`, it must stay under the\n * same queue path and return the same name on every attempt, or webpieces' own retries can create\n * duplicates. The in-memory invoker has no Google request and ignores it.\n */\nexport type TaskCustomizer = (request: ICreateTaskRequest) => ICreateTaskRequest;\n\n/**\n * Scheduling options for an enqueued task. Travels out-of-band (via the scheduler\n * frame in RequestContext) so the shared API method signature stays `foo(req)` on\n * both the client and the controller.\n */\nexport class ScheduleInfo {\n constructor(\n /** How the task is named; resolved ONCE per logical enqueue by `TaskProxyClient`. */\n readonly taskName: TaskName,\n /** Absolute epoch-millis to run at; omitted = run as soon as possible. */\n readonly epochMsToRunAt?: number,\n /** Per-task dispatch deadline in seconds (`task.dispatchDeadline`). */\n readonly taskTimeoutSeconds?: number,\n /** The call site's {@link TaskCustomizer}; wins over `GcpTaskRegistry.setCustomizer`. */\n readonly customizeTask?: TaskCustomizer,\n /** The call site's gax `CallOptions`; set ⇒ the caller owns timing for this enqueue. */\n readonly gax?: CallOptions,\n ) {}\n}\n\n/** Handle to an enqueued task (its Cloud Tasks name), returned by the scheduler. */\nexport class JobReference {\n constructor(\n /** The task's full resource name (`projects/.../tasks/<id>`), or the in-memory task id. */\n readonly taskId: string,\n /**\n * True when the task already existed (ALREADY_EXISTS): a retry whose earlier attempt landed,\n * or a `TaskName.dedup` collision. Reported as success, never as an error.\n */\n readonly alreadyExisted: boolean,\n ) {}\n}\n\n/**\n * What `TaskProxyClient` resolved ONCE for a logical enqueue, before any attempt: the task id every\n * attempt carries, and the EFFECTIVE customizer and gax options (call site, else `GcpTaskRegistry`\n * method -> API -> ALL).\n */\nexport class EnqueuePlan {\n constructor(\n /** The task id (not the full path); undefined = let Cloud Tasks assign one. */\n readonly taskId: string | undefined,\n readonly customizeTask?: TaskCustomizer,\n /** Set ⇒ the caller owns timing: one attempt, `createTask(request, gax)` as given. */\n readonly gax?: CallOptions,\n ) {}\n}\n\n/**\n * Everything an invoker needs to enqueue one task. Built by the enqueue proxy from\n * the shared @PubSub contract's decorators + the ambient RequestContext.\n */\nexport class TaskRequest {\n constructor(\n /** Callee base URL (e.g. https://email-svc-123.us-central1.run.app). */\n readonly targetUrl: string,\n /** Endpoint path the task is delivered to (basePath + endpoint, e.g. /email/send). */\n readonly path: string,\n /** Cloud Tasks queue name (getQueueName: @Queue override or `${Api}-${method}`). */\n readonly queueName: string,\n /** The request DTO (serialized to the POST body on delivery). */\n // webpieces-disable no-any-unknown -- request DTO type is erased at the task boundary\n readonly body: unknown,\n /** Context headers to propagate (txId/requestId/tenant…), already resolved. */\n readonly contextHeaders: Map<string, string>,\n /** The endpoint's auth mode — how the invoker authenticates delivery. */\n readonly authMode: AuthMode,\n /** Scheduling options (run-at, dispatch deadline). */\n readonly scheduleInfo: ScheduleInfo,\n /** The resolved name, customizer and gax for this logical enqueue. */\n readonly plan: EnqueuePlan,\n ) {}\n}\n\n/**\n * The transport that actually enqueues a task. Abstract class so it doubles as the\n * inversify DI token (inject by type, no Symbol). Bind GcpTaskInvoker in prod or\n * InMemoryTaskInvoker in tests/local dev. Both deliver a task the same way — a plain\n * HTTP POST to `targetUrl + path` — GcpTaskInvoker via Google Cloud Tasks, and\n * InMemoryTaskInvoker via an in-process queue that fetches the target directly.\n *\n * An invoker performs ONE attempt and never retries: retries belong to `ClientRegistry` (webpieces'\n * `RetryStrategy` by default) or, when `plan.gax` is set, to gax. It throws\n * `TaskAlreadyExistsError` for a name that already exists.\n */\nexport abstract class TaskInvoker {\n selectAuthentication(meta: AuthMeta): AuthMode {\n const selected = meta.methods.find((method: AuthMode) => method.kind === 'oidc' || method.kind === 'public');\n if (selected) return selected;\n if (meta.methods.length === 1) return meta.methods[0];\n throw new Error('No configured Cloud Tasks credential satisfies the declared alternatives.');\n }\n\n /**\n * Enqueue ONE attempt.\n *\n * @param request - the task, including the resolved {@link EnqueuePlan}\n * @param timeoutMs - this attempt's deadline (webpieces' attempt timeout, or the safety cap when\n * the caller owns timing); the GCP invoker sends it as the gRPC deadline\n * @param signal - aborted when the attempt's deadline fires\n */\n abstract enqueue(request: TaskRequest, timeoutMs: number, signal: AbortSignal): Promise<JobReference>;\n abstract delete(ref: JobReference): Promise<void>;\n}\n"]}
|