@vouchington/worker-runtime 0.0.0
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/LICENSE +21 -0
- package/README.md +50 -0
- package/dist/index.d.mts +3 -0
- package/dist/index.mjs +3 -0
- package/dist/queue-selection.d.mts +17 -0
- package/dist/queue-selection.mjs +41 -0
- package/dist/schedules.d.mts +7 -0
- package/dist/schedules.mjs +10 -0
- package/dist/workers.d.mts +9 -0
- package/dist/workers.mjs +19 -0
- package/package.json +47 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Jonathan Ong
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# @vouchington/worker-runtime
|
|
2
|
+
|
|
3
|
+
Generic queue selection, [GlideMQ](https://www.npmjs.com/package/glide-mq) worker loading, and
|
|
4
|
+
schedule registration. It accepts an application-owned list of worker and schedule definitions;
|
|
5
|
+
the package does not name queues, choose deployment topology, or define queue-placement policy.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
pnpm add @vouchington/worker-runtime glide-mq
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
GlideMQ is a peer dependency so loaded workers share the application's GlideMQ instance.
|
|
14
|
+
|
|
15
|
+
## Queue selection and workers
|
|
16
|
+
|
|
17
|
+
`parseQueueSelection` reads a comma-separated `QUEUES` value. A blank value selects every queue;
|
|
18
|
+
an all-positive list includes only those names, and an all-negative list excludes those names.
|
|
19
|
+
Mixed signs and empty entries are invalid. Includes are strict by default, while callers handling
|
|
20
|
+
deployment skew can opt into dropping and observing unknown include names.
|
|
21
|
+
|
|
22
|
+
```ts
|
|
23
|
+
import { loadWorkers, type WorkerDefinition } from '@vouchington/worker-runtime'
|
|
24
|
+
|
|
25
|
+
const definitions: WorkerDefinition[] = [
|
|
26
|
+
{
|
|
27
|
+
queueName: 'send-email',
|
|
28
|
+
load: () => import('./email-worker.mjs').then((module) => module.worker),
|
|
29
|
+
},
|
|
30
|
+
]
|
|
31
|
+
|
|
32
|
+
const workers = await loadWorkers(definitions, process.env.QUEUES)
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Definitions marked `requiresExplicitInclusion` run only when an include list names them. Pass
|
|
36
|
+
`queueNamesOwnedByOtherRuntimes` when the same `QUEUES` value also selects queues consumed by a
|
|
37
|
+
sibling runtime; those names are recognized but never loaded by these definitions.
|
|
38
|
+
|
|
39
|
+
## Schedules
|
|
40
|
+
|
|
41
|
+
`upsertSchedules` loads and runs schedules for selected queues. A definition may set `alwaysRun`
|
|
42
|
+
when an application intentionally needs it to bypass selection; which schedules may do that is an
|
|
43
|
+
application policy, not a package rule.
|
|
44
|
+
|
|
45
|
+
## Application-owned concerns
|
|
46
|
+
|
|
47
|
+
This package deliberately does not implement SQS polling, process lifecycle or shutdown,
|
|
48
|
+
observability, warmups, worker inventories, or queue placement. Cloudflare routing and CSP also
|
|
49
|
+
remain application-owned: they encode each application's routes, origins, authentication, cache,
|
|
50
|
+
and security policy.
|
package/dist/index.d.mts
ADDED
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export type QueueSelection = {
|
|
2
|
+
mode: 'all';
|
|
3
|
+
queueNames: ReadonlySet<string>;
|
|
4
|
+
} | {
|
|
5
|
+
mode: 'include';
|
|
6
|
+
queueNames: ReadonlySet<string>;
|
|
7
|
+
} | {
|
|
8
|
+
mode: 'exclude';
|
|
9
|
+
queueNames: ReadonlySet<string>;
|
|
10
|
+
};
|
|
11
|
+
export interface QueueSelectionOptions {
|
|
12
|
+
dropUnknownIncludes?: boolean;
|
|
13
|
+
/** Called with dropped include-mode queue names when tolerant selection is enabled. */
|
|
14
|
+
onUnknownIncludes?: (unknownQueueNames: readonly string[]) => void;
|
|
15
|
+
}
|
|
16
|
+
export declare function parseQueueSelection(value: string | undefined, knownQueueNames: readonly string[], { dropUnknownIncludes, onUnknownIncludes }?: QueueSelectionOptions): QueueSelection;
|
|
17
|
+
export declare function isQueueSelected(selection: QueueSelection, queueName: string): boolean;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
export function parseQueueSelection(value, knownQueueNames, { dropUnknownIncludes = false, onUnknownIncludes } = {}) {
|
|
2
|
+
const known = new Set(knownQueueNames);
|
|
3
|
+
const trimmed = value?.trim();
|
|
4
|
+
if (!trimmed)
|
|
5
|
+
return { mode: 'all', queueNames: new Set() };
|
|
6
|
+
const parts = trimmed.split(',').map((part) => part.trim());
|
|
7
|
+
if (parts.some((part) => part.length === 0)) {
|
|
8
|
+
throw new Error('QUEUES must be a comma-separated list without empty entries');
|
|
9
|
+
}
|
|
10
|
+
const excludes = parts.map((part) => part.startsWith('-'));
|
|
11
|
+
const isExclude = excludes.every(Boolean);
|
|
12
|
+
if (!isExclude && !excludes.every((exclude) => !exclude)) {
|
|
13
|
+
throw new Error('QUEUES must use either all include entries or all exclude entries, not both');
|
|
14
|
+
}
|
|
15
|
+
const parsedNames = parts.map((part) => (isExclude ? part.slice(1) : part));
|
|
16
|
+
if (parsedNames.includes('')) {
|
|
17
|
+
throw new Error('QUEUES exclude entries must include a queue name after "-"');
|
|
18
|
+
}
|
|
19
|
+
const unknown = parsedNames.filter((queueName) => !known.has(queueName));
|
|
20
|
+
if (!isExclude && !dropUnknownIncludes && unknown.length > 0) {
|
|
21
|
+
throw new Error(`QUEUES contains unknown queue name(s): ${unknown.join(', ')}`);
|
|
22
|
+
}
|
|
23
|
+
if (!isExclude && dropUnknownIncludes && unknown.length > 0)
|
|
24
|
+
onUnknownIncludes?.(unknown);
|
|
25
|
+
return {
|
|
26
|
+
mode: isExclude ? 'exclude' : 'include',
|
|
27
|
+
queueNames: new Set(isExclude || dropUnknownIncludes
|
|
28
|
+
? parsedNames.filter((name) => known.has(name))
|
|
29
|
+
: parsedNames),
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
export function isQueueSelected(selection, queueName) {
|
|
33
|
+
switch (selection.mode) {
|
|
34
|
+
case 'all':
|
|
35
|
+
return true;
|
|
36
|
+
case 'include':
|
|
37
|
+
return selection.queueNames.has(queueName);
|
|
38
|
+
case 'exclude':
|
|
39
|
+
return !selection.queueNames.has(queueName);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export interface ScheduleDefinition {
|
|
2
|
+
queueName: string;
|
|
3
|
+
load: () => Promise<() => Promise<void>>;
|
|
4
|
+
/** Register this schedule even when queue selection excludes it. */
|
|
5
|
+
alwaysRun?: boolean;
|
|
6
|
+
}
|
|
7
|
+
export declare function upsertSchedules(definitions: readonly ScheduleDefinition[], queues?: string | undefined): Promise<void>;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { isQueueSelected, parseQueueSelection } from './queue-selection.mjs';
|
|
2
|
+
export async function upsertSchedules(definitions, queues = process.env.QUEUES) {
|
|
3
|
+
const selection = parseQueueSelection(queues, definitions.map((definition) => definition.queueName), { dropUnknownIncludes: true });
|
|
4
|
+
await Promise.all(definitions
|
|
5
|
+
.filter((definition) => definition.alwaysRun || isQueueSelected(selection, definition.queueName))
|
|
6
|
+
.map(async (definition) => {
|
|
7
|
+
const upsert = await definition.load();
|
|
8
|
+
await upsert();
|
|
9
|
+
}));
|
|
10
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Worker } from 'glide-mq';
|
|
2
|
+
export interface WorkerDefinition {
|
|
3
|
+
queueName: string;
|
|
4
|
+
load: () => Promise<Worker>;
|
|
5
|
+
requiresExplicitInclusion?: boolean;
|
|
6
|
+
}
|
|
7
|
+
export type UnknownQueueReporter = (unknownQueueNames: readonly string[]) => void;
|
|
8
|
+
export declare function selectedWorkerDefinitions(definitions: readonly WorkerDefinition[], queues?: string | undefined, onUnknownIncludes?: UnknownQueueReporter, queueNamesOwnedByOtherRuntimes?: readonly string[]): WorkerDefinition[];
|
|
9
|
+
export declare function loadWorkers(definitions: readonly WorkerDefinition[], queues?: string | undefined, onUnknownIncludes?: UnknownQueueReporter, queueNamesOwnedByOtherRuntimes?: readonly string[]): Promise<Worker[]>;
|
package/dist/workers.mjs
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { isQueueSelected, parseQueueSelection } from './queue-selection.mjs';
|
|
2
|
+
export function selectedWorkerDefinitions(definitions, queues = process.env.QUEUES, onUnknownIncludes = () => undefined, queueNamesOwnedByOtherRuntimes = []) {
|
|
3
|
+
const knownQueueNames = [
|
|
4
|
+
...definitions.map((definition) => definition.queueName),
|
|
5
|
+
...queueNamesOwnedByOtherRuntimes,
|
|
6
|
+
];
|
|
7
|
+
const selection = parseQueueSelection(queues, knownQueueNames, {
|
|
8
|
+
dropUnknownIncludes: true,
|
|
9
|
+
onUnknownIncludes,
|
|
10
|
+
});
|
|
11
|
+
return definitions.filter((definition) => {
|
|
12
|
+
if (definition.requiresExplicitInclusion && selection.mode !== 'include')
|
|
13
|
+
return false;
|
|
14
|
+
return isQueueSelected(selection, definition.queueName);
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
export function loadWorkers(definitions, queues = process.env.QUEUES, onUnknownIncludes = () => undefined, queueNamesOwnedByOtherRuntimes = []) {
|
|
18
|
+
return Promise.all(selectedWorkerDefinitions(definitions, queues, onUnknownIncludes, queueNamesOwnedByOtherRuntimes).map((definition) => definition.load()));
|
|
19
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@vouchington/worker-runtime",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"description": "Queue selection, GlideMQ worker loading, and schedule registration.",
|
|
5
|
+
"homepage": "https://github.com/vouchington/vouchington-platform/tree/main/packages/worker-runtime#readme",
|
|
6
|
+
"bugs": {
|
|
7
|
+
"url": "https://github.com/vouchington/vouchington-platform/issues"
|
|
8
|
+
},
|
|
9
|
+
"license": "MIT",
|
|
10
|
+
"author": "Jonathan Ong",
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "git+https://github.com/vouchington/vouchington-platform.git",
|
|
14
|
+
"directory": "packages/worker-runtime"
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist",
|
|
18
|
+
"README.md",
|
|
19
|
+
"LICENSE"
|
|
20
|
+
],
|
|
21
|
+
"type": "module",
|
|
22
|
+
"main": "./dist/index.mjs",
|
|
23
|
+
"types": "./dist/index.d.mts",
|
|
24
|
+
"exports": {
|
|
25
|
+
".": {
|
|
26
|
+
"types": "./dist/index.d.mts",
|
|
27
|
+
"import": "./dist/index.mjs",
|
|
28
|
+
"default": "./dist/index.mjs"
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
"publishConfig": {
|
|
32
|
+
"access": "public"
|
|
33
|
+
},
|
|
34
|
+
"scripts": {
|
|
35
|
+
"build": "tsc --project tsconfig.build.json",
|
|
36
|
+
"prepack": "pnpm run build"
|
|
37
|
+
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"glide-mq": "^0.15.3"
|
|
40
|
+
},
|
|
41
|
+
"peerDependencies": {
|
|
42
|
+
"glide-mq": "^0.15.3"
|
|
43
|
+
},
|
|
44
|
+
"engines": {
|
|
45
|
+
"node": ">=24.0.0"
|
|
46
|
+
}
|
|
47
|
+
}
|