@owlmeans/queue 0.1.18-rc.40 → 0.1.18-rc.42
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 +17 -16
- package/agent-meta/manifest.json +2 -2
- package/agent-meta/skills/queue/SKILL.md +26 -24
- package/agent-meta/skills/scheduled-jobs/SKILL.md +1 -1
- package/build/bridge/types.d.ts +25 -0
- package/build/bridge/types.d.ts.map +1 -0
- package/build/bridge/types.js +2 -0
- package/build/bridge/types.js.map +1 -0
- package/build/bridge.d.ts +5 -28
- package/build/bridge.d.ts.map +1 -1
- package/build/bridge.js +33 -104
- package/build/bridge.js.map +1 -1
- package/build/config.d.ts +6 -22
- package/build/config.d.ts.map +1 -1
- package/build/config.js +10 -92
- package/build/config.js.map +1 -1
- package/build/entrypoint-jobs/types.d.ts +29 -0
- package/build/entrypoint-jobs/types.d.ts.map +1 -0
- package/build/entrypoint-jobs/types.js +2 -0
- package/build/entrypoint-jobs/types.js.map +1 -0
- package/build/entrypoint-jobs.d.ts +5 -0
- package/build/entrypoint-jobs.d.ts.map +1 -0
- package/build/entrypoint-jobs.js +84 -0
- package/build/entrypoint-jobs.js.map +1 -0
- package/build/index.d.ts +8 -0
- package/build/index.d.ts.map +1 -1
- package/build/index.js +3 -0
- package/build/index.js.map +1 -1
- package/build/protocol/types.d.ts +12 -0
- package/build/protocol/types.d.ts.map +1 -0
- package/build/protocol/types.js +2 -0
- package/build/protocol/types.js.map +1 -0
- package/build/protocol.d.ts +6 -3
- package/build/protocol.d.ts.map +1 -1
- package/build/protocol.js +57 -50
- package/build/protocol.js.map +1 -1
- package/build/queue-config/types.d.ts +29 -0
- package/build/queue-config/types.d.ts.map +1 -0
- package/build/queue-config/types.js +2 -0
- package/build/queue-config/types.js.map +1 -0
- package/build/queue-config.d.ts +9 -0
- package/build/queue-config.d.ts.map +1 -0
- package/build/queue-config.js +83 -0
- package/build/queue-config.js.map +1 -0
- package/build/queue-route/types.d.ts +14 -0
- package/build/queue-route/types.d.ts.map +1 -0
- package/build/queue-route/types.js +2 -0
- package/build/queue-route/types.js.map +1 -0
- package/build/queue-route.d.ts +4 -0
- package/build/queue-route.d.ts.map +1 -0
- package/build/queue-route.js +24 -0
- package/build/queue-route.js.map +1 -0
- package/build/route.d.ts +2 -2
- package/build/route.d.ts.map +1 -1
- package/build/route.js +5 -20
- package/build/route.js.map +1 -1
- package/build/transport.d.ts.map +1 -1
- package/build/transport.js +2 -2
- package/build/transport.js.map +1 -1
- package/build/types.d.ts +3 -0
- package/build/types.d.ts.map +1 -1
- package/build/worker.d.ts +0 -17
- package/build/worker.d.ts.map +1 -1
- package/build/worker.js +1 -36
- package/build/worker.js.map +1 -1
- package/package.json +8 -8
- package/src/bridge/types.ts +25 -0
- package/src/bridge.ts +38 -118
- package/src/config.ts +14 -105
- package/src/entrypoint-jobs/types.ts +29 -0
- package/src/entrypoint-jobs.ts +105 -0
- package/src/index.ts +8 -0
- package/src/protocol/types.ts +16 -0
- package/src/protocol.ts +74 -53
- package/src/queue-config/types.ts +29 -0
- package/src/queue-config.ts +102 -0
- package/src/queue-route/types.ts +14 -0
- package/src/queue-route.ts +33 -0
- package/src/route.ts +6 -23
- package/src/transport.ts +3 -3
- package/src/types.ts +3 -0
- package/src/worker.ts +2 -48
- package/tests/bridge.spec.ts +8 -8
- package/tests/config.spec.ts +11 -10
- package/tests/protocol.spec.ts +6 -6
- package/tests/schedule.spec.ts +13 -14
- package/build/types.local.d.ts +0 -3
- package/build/types.local.d.ts.map +0 -1
- package/build/types.local.js +0 -2
- package/build/types.local.js.map +0 -1
- package/src/types.local.ts +0 -3
package/build/config.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { queueConfigOf } from './queue-config.js';
|
|
2
2
|
/**
|
|
3
3
|
* Add a queue to the shared configuration. Every process in the monorepo loads the same
|
|
4
4
|
* declarations — a producer needs them to know a queue exists and what it accepts, a worker needs
|
|
@@ -26,78 +26,6 @@ export const listenQueues = (cfg, ...names) => {
|
|
|
26
26
|
cfg.queue = { ...cfg.queue, listen: [...listen] };
|
|
27
27
|
return cfg;
|
|
28
28
|
};
|
|
29
|
-
/**
|
|
30
|
-
* @throws {UnknownQueue}
|
|
31
|
-
*/
|
|
32
|
-
export const queueOf = (cfg, name) => {
|
|
33
|
-
const queue = cfg.queue?.queues?.find(declared => declared.name === name);
|
|
34
|
-
if (queue == null) {
|
|
35
|
-
throw new UnknownQueue(name);
|
|
36
|
-
}
|
|
37
|
-
return queue;
|
|
38
|
-
};
|
|
39
|
-
export const queueOfJob = (cfg, job) => cfg.queue?.queues?.find(declared => declared.jobs.includes(job));
|
|
40
|
-
export const isListening = (cfg, name) => cfg.queue?.listen?.includes(name) === true;
|
|
41
|
-
/** A date option the broker can read — a `Date`, epoch milliseconds or a parsable string. */
|
|
42
|
-
const isInstant = (value) => Number.isFinite((value instanceof Date ? value : new Date(value)).getTime());
|
|
43
|
-
/**
|
|
44
|
-
* Whether the broker can carry a schedule out as written. Nothing is recorded or changed.
|
|
45
|
-
*
|
|
46
|
-
* @throws {ScheduleMisdeclared} naming the schedule id and the reason.
|
|
47
|
-
* @throws {UnknownQueue} when the schedule's queue is not declared.
|
|
48
|
-
* @throws {UnknownJobName} when that queue does not accept the schedule's job name — a scheduled
|
|
49
|
-
* run of a name nothing declares would fail on every tick instead of once, at declaration.
|
|
50
|
-
*/
|
|
51
|
-
export const assertSchedule = (cfg, schedule) => {
|
|
52
|
-
const { id } = schedule;
|
|
53
|
-
if (typeof id !== 'string' || id.trim() === '') {
|
|
54
|
-
throw new ScheduleMisdeclared('(empty):id');
|
|
55
|
-
}
|
|
56
|
-
const queue = queueOf(cfg, schedule.queue);
|
|
57
|
-
if (!queue.jobs.includes(schedule.name)) {
|
|
58
|
-
throw new UnknownJobName(`${schedule.queue}:${schedule.name}`);
|
|
59
|
-
}
|
|
60
|
-
const every = schedule.every != null;
|
|
61
|
-
const pattern = schedule.pattern != null;
|
|
62
|
-
if (every === pattern) {
|
|
63
|
-
throw new ScheduleMisdeclared(`${id}:every-or-pattern`);
|
|
64
|
-
}
|
|
65
|
-
if (every && !(Number.isSafeInteger(schedule.every) && schedule.every > 0)) {
|
|
66
|
-
throw new ScheduleMisdeclared(`${id}:every`);
|
|
67
|
-
}
|
|
68
|
-
if (pattern && (typeof schedule.pattern !== 'string' || schedule.pattern.trim() === '')) {
|
|
69
|
-
throw new ScheduleMisdeclared(`${id}:pattern`);
|
|
70
|
-
}
|
|
71
|
-
// Both only mean something to a cron pattern: an interval has no wall clock to read in a time
|
|
72
|
-
// zone, and its first run is immediate already.
|
|
73
|
-
if (!pattern && schedule.tz != null) {
|
|
74
|
-
throw new ScheduleMisdeclared(`${id}:tz`);
|
|
75
|
-
}
|
|
76
|
-
if (!pattern && schedule.immediately === true) {
|
|
77
|
-
throw new ScheduleMisdeclared(`${id}:immediately`);
|
|
78
|
-
}
|
|
79
|
-
if (schedule.immediately === true && schedule.startDate != null) {
|
|
80
|
-
throw new ScheduleMisdeclared(`${id}:immediately-start-date`);
|
|
81
|
-
}
|
|
82
|
-
if (schedule.limit != null && !(Number.isSafeInteger(schedule.limit) && schedule.limit > 0)) {
|
|
83
|
-
throw new ScheduleMisdeclared(`${id}:limit`);
|
|
84
|
-
}
|
|
85
|
-
if (schedule.startDate != null && !isInstant(schedule.startDate)) {
|
|
86
|
-
throw new ScheduleMisdeclared(`${id}:start-date`);
|
|
87
|
-
}
|
|
88
|
-
if (schedule.endDate != null && !isInstant(schedule.endDate)) {
|
|
89
|
-
throw new ScheduleMisdeclared(`${id}:end-date`);
|
|
90
|
-
}
|
|
91
|
-
// The type already omits both; a declaration assembled at runtime still reaches this check. The
|
|
92
|
-
// broker names every run itself and places it on the schedule, so either would be overridden.
|
|
93
|
-
const opts = schedule.opts;
|
|
94
|
-
if (opts?.id != null) {
|
|
95
|
-
throw new ScheduleMisdeclared(`${id}:opts-id`);
|
|
96
|
-
}
|
|
97
|
-
if (opts?.delay != null) {
|
|
98
|
-
throw new ScheduleMisdeclared(`${id}:opts-delay`);
|
|
99
|
-
}
|
|
100
|
-
};
|
|
101
29
|
/**
|
|
102
30
|
* Add a recurring job to the shared configuration. Declare the queue first — a schedule is checked
|
|
103
31
|
* against it here. Declaring an id twice replaces, like `declareQueue`.
|
|
@@ -108,7 +36,7 @@ export const assertSchedule = (cfg, schedule) => {
|
|
|
108
36
|
* @throws {ScheduleMisdeclared} / {UnknownQueue} / {UnknownJobName} — see `assertSchedule`.
|
|
109
37
|
*/
|
|
110
38
|
export const declareSchedule = (cfg, schedule) => {
|
|
111
|
-
assertSchedule(
|
|
39
|
+
queueConfigOf(cfg).assertSchedule(schedule);
|
|
112
40
|
const schedules = cfg.queue?.schedules ?? [];
|
|
113
41
|
const existing = schedules.findIndex(declared => declared.id === schedule.id);
|
|
114
42
|
if (existing < 0) {
|
|
@@ -120,22 +48,12 @@ export const declareSchedule = (cfg, schedule) => {
|
|
|
120
48
|
cfg.queue = { ...cfg.queue, schedules };
|
|
121
49
|
return cfg;
|
|
122
50
|
};
|
|
123
|
-
/**
|
|
124
|
-
export const
|
|
125
|
-
/**
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
export const assertSchedules = (cfg) => {
|
|
132
|
-
const seen = new Set();
|
|
133
|
-
for (const schedule of schedulesOf(cfg)) {
|
|
134
|
-
assertSchedule(cfg, schedule);
|
|
135
|
-
if (seen.has(schedule.id)) {
|
|
136
|
-
throw new ScheduleMisdeclared(`${schedule.id}:duplicate`);
|
|
137
|
-
}
|
|
138
|
-
seen.add(schedule.id);
|
|
139
|
-
}
|
|
140
|
-
};
|
|
51
|
+
/** @deprecated compat:factory-refactor — use `queueConfigOf(cfg).queueOfJob(…)` */
|
|
52
|
+
export const queueOfJob = (cfg, job) => queueConfigOf(cfg).queueOfJob(job);
|
|
53
|
+
/** @deprecated compat:factory-refactor — use `queueConfigOf(cfg).isListening(…)` */
|
|
54
|
+
export const isListening = (cfg, name) => queueConfigOf(cfg).isListening(name);
|
|
55
|
+
/** @deprecated compat:factory-refactor — use `queueConfigOf(cfg).schedulesOf(…)` */
|
|
56
|
+
export const schedulesOf = (cfg, queue) => queueConfigOf(cfg).schedulesOf(queue);
|
|
57
|
+
/** @deprecated compat:factory-refactor — use `queueConfigOf(cfg).assertSchedules()` */
|
|
58
|
+
export const assertSchedules = (cfg) => queueConfigOf(cfg).assertSchedules();
|
|
141
59
|
//# sourceMappingURL=config.js.map
|
package/build/config.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"config.js","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"config.js","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAA;AAEjD;;;;GAIG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG,CAC1B,GAAM,EAAE,IAAY,EAAE,IAAc,EAAE,IAA6D,EAChG,EAAE;IACL,MAAM,KAAK,GAAqB,EAAE,IAAI,EAAE,IAAI,EAAE,GAAG,IAAI,EAAE,CAAA;IACvD,MAAM,MAAM,GAAG,GAAG,CAAC,KAAK,EAAE,MAAM,IAAI,EAAE,CAAA;IACtC,MAAM,QAAQ,GAAG,MAAM,CAAC,SAAS,CAAC,QAAQ,CAAC,EAAE,CAAC,QAAQ,CAAC,IAAI,KAAK,IAAI,CAAC,CAAA;IAErE,IAAI,QAAQ,GAAG,CAAC,EAAE,CAAC;QACjB,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;IACpB,CAAC;SAAM,CAAC;QACN,MAAM,CAAC,QAAQ,CAAC,GAAG,KAAK,CAAA;IAC1B,CAAC;IAED,GAAG,CAAC,KAAK,GAAG,EAAE,GAAG,GAAG,CAAC,KAAK,EAAE,MAAM,EAAE,CAAA;IAEpC,OAAO,GAAG,CA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,CAAC,CAAA;AAEpC,oFAAoF;AACpF,MAAM,CAAC,MAAM,WAAW,GAAG,CAAmB,GAAM,EAAE,IAAY,EAAW,EAAE,CAC7E,aAAa,CAAC,GAAG,CAAC,CAAC,WAAW,CAAC,IAAI,CAAC,CAAA;AAEtC,oFAAoF;AACpF,MAAM,CAAC,MAAM,WAAW,GAAG,CAAmB,GAAM,EAAE,KAAc,EAAyB,EAAE,CAC7F,aAAa,CAAC,GAAG,CAAC,CAAC,WAAW,CAAC,KAAK,CAAC,CAAA;AAEvC,uFAAuF;AACvF,MAAM,CAAC,MAAM,eAAe,GAAG,CAAmB,GAAM,EAAQ,EAAE,CAAC,aAAa,CAAC,GAAG,CAAC,CAAC,eAAe,EAAE,CAAA"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { CommonEntrypoint } from '@owlmeans/entrypoint';
|
|
2
|
+
import type { JobContext, JobEnvelope, JobProcessor, JobReply } from '../types.js';
|
|
3
|
+
/** The queued entrypoints one context serves as a worker, and how a job of theirs is run. */
|
|
4
|
+
export interface EntrypointJobsHelper {
|
|
5
|
+
/**
|
|
6
|
+
* Every queued entrypoint this process both SERVES and LISTENS to, grouped by queue.
|
|
7
|
+
*
|
|
8
|
+
* Both halves are required and they answer different questions. Serving is about code — the alias
|
|
9
|
+
* was bound here, so a handler exists. Listening is about deployment — this process was
|
|
10
|
+
* configured to consume that queue. A worker that bound queues by what it can serve would make
|
|
11
|
+
* every deployment of the same binary a worker for everything it happens to import.
|
|
12
|
+
*/
|
|
13
|
+
served: () => Map<string, CommonEntrypoint[]>;
|
|
14
|
+
/**
|
|
15
|
+
* A processor that runs a queued entrypoint call. The driver dispatches by job name, and an
|
|
16
|
+
* entrypoint job's name IS its alias — which is what lets one worker carry both entrypoint jobs
|
|
17
|
+
* and the internal steps an application registers with `process()`.
|
|
18
|
+
*/
|
|
19
|
+
processor: () => JobProcessor<JobEnvelope, unknown>;
|
|
20
|
+
/**
|
|
21
|
+
* Run one queued entrypoint call and describe the outcome.
|
|
22
|
+
*
|
|
23
|
+
* It never throws for a DOMAIN failure — the error is marshalled into the returned reply so the
|
|
24
|
+
* producer can rebuild it as its own class, and so the broker does not count a legitimate refusal
|
|
25
|
+
* as a job to retry. Infrastructure failures are left to propagate: those are what retries are for.
|
|
26
|
+
*/
|
|
27
|
+
handle: (job: JobContext<JobEnvelope>) => Promise<JobReply>;
|
|
28
|
+
}
|
|
29
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/entrypoint-jobs/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAA;AAC5D,OAAO,KAAK,EAAE,UAAU,EAAE,WAAW,EAAE,YAAY,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAA;AAElF,6FAA6F;AAC7F,MAAM,WAAW,oBAAoB;IACnC;;;;;;;OAOG;IACH,MAAM,EAAE,MAAM,GAAG,CAAC,MAAM,EAAE,gBAAgB,EAAE,CAAC,CAAA;IAC7C;;;;OAIG;IACH,SAAS,EAAE,MAAM,YAAY,CAAC,WAAW,EAAE,OAAO,CAAC,CAAA;IACnD;;;;;;OAMG;IACH,MAAM,EAAE,CAAC,GAAG,EAAE,UAAU,CAAC,WAAW,CAAC,KAAK,OAAO,CAAC,QAAQ,CAAC,CAAA;CAC5D"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/entrypoint-jobs/types.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { Config, Context } from './types.js';
|
|
2
|
+
import type { EntrypointJobsHelper } from './entrypoint-jobs/types.js';
|
|
3
|
+
export declare const makeEntrypointJobsHelper: (context: Context<Config>) => EntrypointJobsHelper;
|
|
4
|
+
export declare const entrypointJobsOf: (target: Context<Config>) => EntrypointJobsHelper;
|
|
5
|
+
//# sourceMappingURL=entrypoint-jobs.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"entrypoint-jobs.d.ts","sourceRoot":"","sources":["../src/entrypoint-jobs.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,MAAM,EAAE,OAAO,EAAmD,MAAM,YAAY,CAAA;AAClG,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,4BAA4B,CAAA;AAMtE,eAAO,MAAM,wBAAwB,YAAa,OAAO,CAAC,MAAM,CAAC,KAAG,oBAyFnE,CAAA;AAED,eAAO,MAAM,gBAAgB,mDAA+C,CAAA"}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { AppType, memoHelper } from '@owlmeans/context';
|
|
2
|
+
import { EntrypointOutcome, provideResponse } from '@owlmeans/entrypoint';
|
|
3
|
+
import { AuthorizationError } from '@owlmeans/auth';
|
|
4
|
+
import { ResilientError } from '@owlmeans/error';
|
|
5
|
+
import { JobNotServed } from './errors.js';
|
|
6
|
+
import { queueBridgeHelper } from './bridge.js';
|
|
7
|
+
import { queueRouteHelper } from './queue-route.js';
|
|
8
|
+
import { makeEntityScope } from '@owlmeans/auth-common';
|
|
9
|
+
export const makeEntrypointJobsHelper = (context) => {
|
|
10
|
+
const served = () => {
|
|
11
|
+
const listen = context.cfg.queue?.listen ?? [];
|
|
12
|
+
const served = new Map();
|
|
13
|
+
context.entrypoints().forEach(entrypoint => {
|
|
14
|
+
const route = entrypoint.route.route;
|
|
15
|
+
if (route.type !== AppType.Backend || !queueRouteHelper.isQueueRoute(route)) {
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
if (route.service != null && route.service !== context.cfg.service) {
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
const { queue } = queueRouteHelper.queueRouteOptions(route);
|
|
22
|
+
if (entrypoint.handle == null || !listen.includes(queue)) {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
served.set(queue, [...(served.get(queue) ?? []), entrypoint]);
|
|
26
|
+
});
|
|
27
|
+
return served;
|
|
28
|
+
};
|
|
29
|
+
const handle = async (job) => {
|
|
30
|
+
const envelope = job.data;
|
|
31
|
+
try {
|
|
32
|
+
queueBridgeHelper.assertFresh(envelope, context.cfg.queue?.envelopeTtl);
|
|
33
|
+
const entrypoint = context.entrypoint(envelope.alias);
|
|
34
|
+
if (entrypoint.handle == null) {
|
|
35
|
+
throw new JobNotServed(envelope.alias);
|
|
36
|
+
}
|
|
37
|
+
const request = queueBridgeHelper.requestOf(envelope, entrypoint.path(), context, {
|
|
38
|
+
id: job.id, name: job.name, queue: job.queue, attempt: job.attempt, touch: job.touch,
|
|
39
|
+
});
|
|
40
|
+
const response = provideResponse();
|
|
41
|
+
const guards = entrypoint.getGuards();
|
|
42
|
+
if (guards.length > 0) {
|
|
43
|
+
let matched;
|
|
44
|
+
for (const alias of guards) {
|
|
45
|
+
const guard = context.service(alias);
|
|
46
|
+
if (await guard.match(request, response)) {
|
|
47
|
+
matched = guard;
|
|
48
|
+
break;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
if (matched == null) {
|
|
52
|
+
throw new AuthorizationError(`queue:${envelope.alias}`);
|
|
53
|
+
}
|
|
54
|
+
const auth = provideResponse();
|
|
55
|
+
if (!await matched.handle(request, auth)) {
|
|
56
|
+
throw new AuthorizationError(`queue:${envelope.alias}:${matched.alias}`);
|
|
57
|
+
}
|
|
58
|
+
request.auth = auth.value;
|
|
59
|
+
const entity = await makeEntityScope(request).attachEntity(context);
|
|
60
|
+
if (entity != null) {
|
|
61
|
+
request.entity = entity;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
await entrypoint.handle(request, response);
|
|
65
|
+
if (response.error != null) {
|
|
66
|
+
return { error: ResilientError.marshal(response.error).message };
|
|
67
|
+
}
|
|
68
|
+
return { value: response.value, outcome: response.outcome ?? EntrypointOutcome.Ok };
|
|
69
|
+
}
|
|
70
|
+
catch (e) {
|
|
71
|
+
// A refusal the caller must see as its own class — `ContentRefused`, `AgentLocked` — travels
|
|
72
|
+
// in the reply. Only a broker- or connection-level fault should ever reach the retry logic:
|
|
73
|
+
// retrying a refusal just spends the work again to be refused identically.
|
|
74
|
+
if (e instanceof ResilientError) {
|
|
75
|
+
return { error: ResilientError.marshal(e).message };
|
|
76
|
+
}
|
|
77
|
+
throw e;
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
const processor = () => async (job) => await handle(job);
|
|
81
|
+
return { served, processor, handle };
|
|
82
|
+
};
|
|
83
|
+
export const entrypointJobsOf = memoHelper.oncePer(makeEntrypointJobsHelper);
|
|
84
|
+
//# sourceMappingURL=entrypoint-jobs.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"entrypoint-jobs.js","sourceRoot":"","sources":["../src/entrypoint-jobs.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAA;AAEvD,OAAO,EAAE,iBAAiB,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAA;AAEzE,OAAO,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAA;AACnD,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAA;AAGhD,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAA;AAC1C,OAAO,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAA;AAC/C,OAAO,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAA;AACnD,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAA;AAEvD,MAAM,CAAC,MAAM,wBAAwB,GAAG,CAAC,OAAwB,EAAwB,EAAE;IACzF,MAAM,MAAM,GAAG,GAAoC,EAAE;QACnD,MAAM,MAAM,GAAG,OAAO,CAAC,GAAG,CAAC,KAAK,EAAE,MAAM,IAAI,EAAE,CAAA;QAC9C,MAAM,MAAM,GAAG,IAAI,GAAG,EAA8B,CA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bridge/types.js';
|
|
4
|
+
export type * from './queue-route/types.js';
|
|
5
|
+
export type * from './protocol/types.js';
|
|
6
|
+
export type * from './entrypoint-jobs/types.js';
|
|
2
7
|
export * from './consts.js';
|
|
3
8
|
export * from './utils.js';
|
|
4
9
|
export * from './errors.js';
|
|
5
10
|
export * from './config.js';
|
|
11
|
+
export * from './queue-config.js';
|
|
6
12
|
export * from './transport.js';
|
|
7
13
|
export * from './bridge.js';
|
|
14
|
+
export * from './entrypoint-jobs.js';
|
|
8
15
|
export * from './worker.js';
|
|
9
16
|
export * from './protocol.js';
|
|
10
17
|
export * from './route.js';
|
|
18
|
+
export * from './queue-route.js';
|
|
11
19
|
//# sourceMappingURL=index.d.ts.map
|
package/build/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAA;AAC1B,cAAc,aAAa,CAAA;AAC3B,cAAc,YAAY,CAAA;AAC1B,cAAc,aAAa,CAAA;AAC3B,cAAc,aAAa,CAAA;AAC3B,cAAc,gBAAgB,CAAA;AAC9B,cAAc,aAAa,CAAA;AAC3B,cAAc,aAAa,CAAA;AAC3B,cAAc,eAAe,CAAA;AAC7B,cAAc,YAAY,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAA;AAC1B,mBAAmB,yBAAyB,CAAA;AAC5C,mBAAmB,mBAAmB,CAAA;AACtC,mBAAmB,wBAAwB,CAAA;AAC3C,mBAAmB,qBAAqB,CAAA;AACxC,mBAAmB,4BAA4B,CAAA;AAC/C,cAAc,aAAa,CAAA;AAC3B,cAAc,YAAY,CAAA;AAC1B,cAAc,aAAa,CAAA;AAC3B,cAAc,aAAa,CAAA;AAC3B,cAAc,mBAAmB,CAAA;AACjC,cAAc,gBAAgB,CAAA;AAC9B,cAAc,aAAa,CAAA;AAC3B,cAAc,sBAAsB,CAAA;AACpC,cAAc,aAAa,CAAA;AAC3B,cAAc,eAAe,CAAA;AAC7B,cAAc,YAAY,CAAA;AAC1B,cAAc,kBAAkB,CAAA"}
|
package/build/index.js
CHANGED
|
@@ -3,9 +3,12 @@ export * from './consts.js';
|
|
|
3
3
|
export * from './utils.js';
|
|
4
4
|
export * from './errors.js';
|
|
5
5
|
export * from './config.js';
|
|
6
|
+
export * from './queue-config.js';
|
|
6
7
|
export * from './transport.js';
|
|
7
8
|
export * from './bridge.js';
|
|
9
|
+
export * from './entrypoint-jobs.js';
|
|
8
10
|
export * from './worker.js';
|
|
9
11
|
export * from './protocol.js';
|
|
10
12
|
export * from './route.js';
|
|
13
|
+
export * from './queue-route.js';
|
|
11
14
|
//# sourceMappingURL=index.js.map
|
package/build/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAA;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAA;AAM1B,cAAc,aAAa,CAAA;AAC3B,cAAc,YAAY,CAAA;AAC1B,cAAc,aAAa,CAAA;AAC3B,cAAc,aAAa,CAAA;AAC3B,cAAc,mBAAmB,CAAA;AACjC,cAAc,gBAAgB,CAAA;AAC9B,cAAc,aAAa,CAAA;AAC3B,cAAc,sBAAsB,CAAA;AACpC,cAAc,aAAa,CAAA;AAC3B,cAAc,eAAe,CAAA;AAC7B,cAAc,YAAY,CAAA;AAC1B,cAAc,kBAAkB,CAAA"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { EntrypointProtocolDeclaration, RequestOf, ResponseOf } from '@owlmeans/entrypoint';
|
|
2
|
+
import type { JobEnvelope, JobOptions, JobRecord, JobReply } from '../types.js';
|
|
3
|
+
/** Enqueueing and awaiting QUEUE protocols in one context, with their exact types kept. */
|
|
4
|
+
export interface QueueProtocolHelper {
|
|
5
|
+
/** Enqueue an immutable QUEUE protocol while preserving its exact request and response types. */
|
|
6
|
+
enqueue: <Protocol extends EntrypointProtocolDeclaration>(declaration: Protocol, request: RequestOf<Protocol>, options?: JobOptions) => Promise<JobRecord<JobEnvelope, JobReply<ResponseOf<Protocol>>>>;
|
|
7
|
+
/** Wait for a protocol job and unwrap the typed entrypoint reply. */
|
|
8
|
+
waitFor: <Protocol extends EntrypointProtocolDeclaration>(declaration: Protocol, job: string | JobRecord<JobEnvelope, JobReply<ResponseOf<Protocol>>>, options?: {
|
|
9
|
+
timeout?: number;
|
|
10
|
+
}) => Promise<ResponseOf<Protocol>>;
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/protocol/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,6BAA6B,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAA;AAChG,OAAO,KAAK,EAAE,WAAW,EAAE,UAAU,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAA;AAE/E,2FAA2F;AAC3F,MAAM,WAAW,mBAAmB;IAClC,iGAAiG;IACjG,OAAO,EAAE,CAAC,QAAQ,SAAS,6BAA6B,EACtD,WAAW,EAAE,QAAQ,EAAE,OAAO,EAAE,SAAS,CAAC,QAAQ,CAAC,EAAE,OAAO,CAAC,EAAE,UAAU,KACtE,OAAO,CAAC,SAAS,CAAC,WAAW,EAAE,QAAQ,CAAC,UAAU,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAA;IACpE,qEAAqE;IACrE,OAAO,EAAE,CAAC,QAAQ,SAAS,6BAA6B,EACtD,WAAW,EAAE,QAAQ,EACrB,GAAG,EAAE,MAAM,GAAG,SAAS,CAAC,WAAW,EAAE,QAAQ,CAAC,UAAU,CAAC,QAAQ,CAAC,CAAC,CAAC,EACpE,OAAO,CAAC,EAAE;QAAE,OAAO,CAAC,EAAE,MAAM,CAAA;KAAE,KAC3B,OAAO,CAAC,UAAU,CAAC,QAAQ,CAAC,CAAC,CAAA;CACnC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/protocol/types.ts"],"names":[],"mappings":""}
|
package/build/protocol.d.ts
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
import { type EntrypointProtocolDeclaration, type RequestOf, type ResponseOf } from '@owlmeans/entrypoint';
|
|
2
2
|
import type { JobEnvelope, JobOptions, JobRecord, JobReply } from './types.js';
|
|
3
|
-
import type { QueueContext } from './types.
|
|
4
|
-
|
|
3
|
+
import type { QueueContext } from './types.js';
|
|
4
|
+
import type { QueueProtocolHelper } from './protocol/types.js';
|
|
5
|
+
export declare const makeQueueProtocolHelper: (context: QueueContext) => QueueProtocolHelper;
|
|
6
|
+
export declare const queueProtocolOf: (target: QueueContext) => QueueProtocolHelper;
|
|
7
|
+
/** @deprecated compat:factory-refactor — use `queueProtocolOf(ctx).enqueue(…)` */
|
|
5
8
|
export declare const enqueueProtocol: <Protocol extends EntrypointProtocolDeclaration>(context: QueueContext, declaration: Protocol, request: RequestOf<Protocol>, options?: JobOptions) => Promise<JobRecord<JobEnvelope, JobReply<ResponseOf<Protocol>>>>;
|
|
6
|
-
/**
|
|
9
|
+
/** @deprecated compat:factory-refactor — use `queueProtocolOf(ctx).waitFor(…)` */
|
|
7
10
|
export declare const waitForProtocol: <Protocol extends EntrypointProtocolDeclaration>(context: QueueContext, declaration: Protocol, job: string | JobRecord<JobEnvelope, JobReply<ResponseOf<Protocol>>>, options?: {
|
|
8
11
|
timeout?: number;
|
|
9
12
|
}) => Promise<ResponseOf<Protocol>>;
|
package/build/protocol.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"protocol.d.ts","sourceRoot":"","sources":["../src/protocol.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,6BAA6B,EAAE,KAAK,SAAS,EAAE,KAAK,UAAU,EAAwB,MAAM,sBAAsB,CAAA;
|
|
1
|
+
{"version":3,"file":"protocol.d.ts","sourceRoot":"","sources":["../src/protocol.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,6BAA6B,EAAE,KAAK,SAAS,EAAE,KAAK,UAAU,EAAwB,MAAM,sBAAsB,CAAA;AAMhI,OAAO,KAAK,EAAE,WAAW,EAAE,UAAU,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAA;AAC9E,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,YAAY,CAAA;AAC9C,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,qBAAqB,CAAA;AAE9D,eAAO,MAAM,uBAAuB,YAAa,YAAY,KAAG,mBAiE/D,CAAA;AAED,eAAO,MAAM,eAAe,+CAA8C,CAAA;AAE1E,kFAAkF;AAClF,eAAO,MAAM,eAAe,GAAU,QAAQ,SAAS,6BAA6B,WACzE,YAAY,eACR,QAAQ,WACZ,SAAS,CAAC,QAAQ,CAAC,YAClB,UAAU,KACnB,OAAO,CAAC,SAAS,CAAC,WAAW,EAAE,QAAQ,CAAC,UAAU,CAAC,QAAQ,CAAC,CAAC,CAAC,CACM,CAAA;AAEvE,kFAAkF;AAClF,eAAO,MAAM,eAAe,GAAU,QAAQ,SAAS,6BAA6B,WACzE,YAAY,eACR,QAAQ,OAChB,MAAM,GAAG,SAAS,CAAC,WAAW,EAAE,QAAQ,CAAC,UAAU,CAAC,QAAQ,CAAC,CAAC,CAAC,YAC1D;IAAE,OAAO,CAAC,EAAE,MAAM,CAAA;CAAE,KAC7B,OAAO,CAAC,UAAU,CAAC,QAAQ,CAAC,CAAsE,CAAA"}
|
package/build/protocol.js
CHANGED
|
@@ -1,56 +1,63 @@
|
|
|
1
1
|
import { isEntrypointProtocol } from '@owlmeans/entrypoint';
|
|
2
|
+
import { memoHelper } from '@owlmeans/context';
|
|
2
3
|
import { ResilientError } from '@owlmeans/error';
|
|
3
|
-
import {
|
|
4
|
+
import { queueConfigOf } from './queue-config.js';
|
|
4
5
|
import { UnknownQueue } from './errors.js';
|
|
5
|
-
import {
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
};
|
|
20
|
-
/** Enqueue an immutable QUEUE protocol while preserving its exact request and response types. */
|
|
21
|
-
export const enqueueProtocol = async (context, declaration, request, options) => {
|
|
22
|
-
const { protocol, queue } = queueFor(context, declaration);
|
|
23
|
-
const shaped = (request ?? {});
|
|
24
|
-
const envelope = {
|
|
25
|
-
alias: protocol.alias,
|
|
26
|
-
params: shaped.params,
|
|
27
|
-
body: shaped.body,
|
|
28
|
-
query: shaped.query,
|
|
29
|
-
headers: shaped.headers,
|
|
30
|
-
enqueuedAt: new Date().toISOString(),
|
|
6
|
+
import { queueRouteHelper } from './queue-route.js';
|
|
7
|
+
export const makeQueueProtocolHelper = (context) => {
|
|
8
|
+
const queueFor = (declaration) => {
|
|
9
|
+
if (declaration == null || typeof declaration !== 'object' || !isEntrypointProtocol(declaration)) {
|
|
10
|
+
throw new UnknownQueue('protocol declaration required');
|
|
11
|
+
}
|
|
12
|
+
if (!queueRouteHelper.isQueueRoute(declaration.route)) {
|
|
13
|
+
throw new UnknownQueue(`${declaration.alias}: not a queue protocol`);
|
|
14
|
+
}
|
|
15
|
+
const { queue } = queueRouteHelper.queueRouteOptions(declaration.route);
|
|
16
|
+
const configured = queueConfigOf(context.cfg).queueOf(queue);
|
|
17
|
+
if (!configured.jobs.includes(declaration.alias)) {
|
|
18
|
+
throw new UnknownQueue(`${queue}:${declaration.alias}`);
|
|
19
|
+
}
|
|
20
|
+
return { protocol: declaration, queue };
|
|
31
21
|
};
|
|
32
|
-
|
|
33
|
-
queue
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
22
|
+
const enqueue = async (declaration, request, options) => {
|
|
23
|
+
const { protocol, queue } = queueFor(declaration);
|
|
24
|
+
const shaped = (request ?? {});
|
|
25
|
+
const envelope = {
|
|
26
|
+
alias: protocol.alias,
|
|
27
|
+
params: shaped.params,
|
|
28
|
+
body: shaped.body,
|
|
29
|
+
query: shaped.query,
|
|
30
|
+
headers: shaped.headers,
|
|
31
|
+
enqueuedAt: new Date().toISOString(),
|
|
32
|
+
};
|
|
33
|
+
return await context.jobs(queue).create({
|
|
34
|
+
queue,
|
|
35
|
+
name: protocol.alias,
|
|
36
|
+
data: envelope,
|
|
37
|
+
opts: options,
|
|
38
|
+
});
|
|
39
|
+
};
|
|
40
|
+
const waitFor = async (declaration, job, options) => {
|
|
41
|
+
const { protocol, queue } = queueFor(declaration);
|
|
42
|
+
if (typeof job !== 'string' && (job.queue !== queue || job.name !== protocol.alias)) {
|
|
43
|
+
throw new UnknownQueue(`${job.queue}:${job.name}`);
|
|
44
|
+
}
|
|
45
|
+
const id = typeof job === 'string' ? job : job.id;
|
|
46
|
+
if (id == null) {
|
|
47
|
+
throw new UnknownQueue(`${queue}:${protocol.alias}: missing job id`);
|
|
48
|
+
}
|
|
49
|
+
const reply = await context.jobs(queue)
|
|
50
|
+
.wait(id, options);
|
|
51
|
+
if (reply.error != null) {
|
|
52
|
+
throw ResilientError.ensure(reply.error);
|
|
53
|
+
}
|
|
54
|
+
return reply.value;
|
|
55
|
+
};
|
|
56
|
+
return { enqueue, waitFor };
|
|
55
57
|
};
|
|
58
|
+
export const queueProtocolOf = memoHelper.oncePer(makeQueueProtocolHelper);
|
|
59
|
+
/** @deprecated compat:factory-refactor — use `queueProtocolOf(ctx).enqueue(…)` */
|
|
60
|
+
export const enqueueProtocol = async (context, declaration, request, options) => await queueProtocolOf(context).enqueue(declaration, request, options);
|
|
61
|
+
/** @deprecated compat:factory-refactor — use `queueProtocolOf(ctx).waitFor(…)` */
|
|
62
|
+
export const waitForProtocol = async (context, declaration, job, options) => await queueProtocolOf(context).waitFor(declaration, job, options);
|
|
56
63
|
//# sourceMappingURL=protocol.js.map
|
package/build/protocol.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"protocol.js","sourceRoot":"","sources":["../src/protocol.ts"],"names":[],"mappings":"AAAA,OAAO,EAAuE,oBAAoB,EAAE,MAAM,sBAAsB,CAAA;AAChI,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAA;AAChD,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"protocol.js","sourceRoot":"","sources":["../src/protocol.ts"],"names":[],"mappings":"AAAA,OAAO,EAAuE,oBAAoB,EAAE,MAAM,sBAAsB,CAAA;AAChI,OAAO,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAA;AAC9C,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAA;AAChD,OAAO,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAA;AACjD,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAA;AAC1C,OAAO,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAA;AAKnD,MAAM,CAAC,MAAM,uBAAuB,GAAG,CAAC,OAAqB,EAAuB,EAAE;IACpF,MAAM,QAAQ,GAAG,CAAC,WAAoB,EAGpC,EAAE;QACF,IAAI,WAAW,IAAI,IAAI,IAAI,OAAO,WAAW,KAAK,QAAQ,IAAI,CAAC,oBAAoB,CAAC,WAAW,CAAC,EAAE,CAAC;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,GAAG,UAAU,CAAC,OAAO,CAAC,uBAAuB,CAAC,CAAA;AAE1E,kFAAkF;AAClF,MAAM,CAAC,MAAM,eAAe,GAAG,KAAK,EAClC,OAAqB,EACrB,WAAqB,EACrB,OAA4B,EAC5B,OAAoB,EAC6C,EAAE,CACnE,MAAM,eAAe,CAAC,OAAO,CAAC,CAAC,OAAO,CAAC,WAAW,EAAE,OAAO,EAAE,OAAO,CAAC,CAAA;AAEvE,kFAAkF;AAClF,MAAM,CAAC,MAAM,eAAe,GAAG,KAAK,EAClC,OAAqB,EACrB,WAAqB,EACrB,GAAoE,EACpE,OAA8B,EACC,EAAE,CAAC,MAAM,eAAe,CAAC,OAAO,CAAC,CAAC,OAAO,CAAC,WAAW,EAAE,GAAG,EAAE,OAAO,CAAC,CAAA"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { QueueDeclaration, ScheduleDeclaration } from '../types.js';
|
|
2
|
+
/** What one configuration declares about its queues and schedules, read and checked. */
|
|
3
|
+
export interface QueueConfigHelper {
|
|
4
|
+
/**
|
|
5
|
+
* @throws {UnknownQueue}
|
|
6
|
+
*/
|
|
7
|
+
queueOf: (name: string) => QueueDeclaration;
|
|
8
|
+
queueOfJob: (job: string) => QueueDeclaration | undefined;
|
|
9
|
+
isListening: (name: string) => boolean;
|
|
10
|
+
/**
|
|
11
|
+
* Whether the broker can carry a schedule out as written. Nothing is recorded or changed.
|
|
12
|
+
*
|
|
13
|
+
* @throws {ScheduleMisdeclared} naming the schedule id and the reason.
|
|
14
|
+
* @throws {UnknownQueue} when the schedule's queue is not declared.
|
|
15
|
+
* @throws {UnknownJobName} when that queue does not accept the schedule's job name — a scheduled
|
|
16
|
+
* run of a name nothing declares would fail on every tick instead of once, at declaration.
|
|
17
|
+
*/
|
|
18
|
+
assertSchedule: (schedule: ScheduleDeclaration) => void;
|
|
19
|
+
/** The declared schedules, of one queue when it is named. */
|
|
20
|
+
schedulesOf: (queue?: string) => ScheduleDeclaration[];
|
|
21
|
+
/**
|
|
22
|
+
* Check every declared schedule, including any written into the configuration without
|
|
23
|
+
* `declareSchedule`, and refuse two under one id.
|
|
24
|
+
*
|
|
25
|
+
* @throws {ScheduleMisdeclared} / {UnknownQueue} / {UnknownJobName} for the first one that fails.
|
|
26
|
+
*/
|
|
27
|
+
assertSchedules: () => void;
|
|
28
|
+
}
|
|
29
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/queue-config/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAA;AAExE,wFAAwF;AACxF,MAAM,WAAW,iBAAiB;IAChC;;OAEG;IACH,OAAO,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,gBAAgB,CAAA;IAC3C,UAAU,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,gBAAgB,GAAG,SAAS,CAAA;IACzD,WAAW,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,OAAO,CAAA;IACtC;;;;;;;OAOG;IACH,cAAc,EAAE,CAAC,QAAQ,EAAE,mBAAmB,KAAK,IAAI,CAAA;IACvD,6DAA6D;IAC7D,WAAW,EAAE,CAAC,KAAK,CAAC,EAAE,MAAM,KAAK,mBAAmB,EAAE,CAAA;IACtD;;;;;OAKG;IACH,eAAe,EAAE,MAAM,IAAI,CAAA;CAC5B"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/queue-config/types.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Config } from './types.js';
|
|
2
|
+
import type { QueueConfigHelper } from './queue-config/types.js';
|
|
3
|
+
/**
|
|
4
|
+
* The queue configuration of one `cfg`. It reads `cfg.queue` on every call, so declarations made
|
|
5
|
+
* after it was built are seen.
|
|
6
|
+
*/
|
|
7
|
+
export declare const makeQueueConfigHelper: (cfg: Config) => QueueConfigHelper;
|
|
8
|
+
export declare const queueConfigOf: (target: Config) => QueueConfigHelper;
|
|
9
|
+
//# sourceMappingURL=queue-config.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"queue-config.d.ts","sourceRoot":"","sources":["../src/queue-config.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,MAAM,EAAqD,MAAM,YAAY,CAAA;AAC3F,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAA;AAGhE;;;GAGG;AACH,eAAO,MAAM,qBAAqB,QAAS,MAAM,KAAG,iBA0FnD,CAAA;AAED,eAAO,MAAM,aAAa,uCAA4C,CAAA"}
|