@lunora/scheduler 1.0.0-alpha.1 → 1.0.0-alpha.11

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.
@@ -1,56 +0,0 @@
1
- const callDO = async (options, path, body) => {
2
- const stub = options.namespace.get(options.namespace.idFromName(options.instanceName ?? "default"));
3
- const response = await stub.fetch(`https://scheduler.internal${path}`, {
4
- body: JSON.stringify(body),
5
- headers: { "content-type": "application/json" },
6
- method: "POST"
7
- });
8
- if (!response.ok) {
9
- const text = await response.text();
10
- throw new Error(`@lunora/scheduler: SchedulerDO ${path} failed (${String(response.status)}): ${text}`);
11
- }
12
- return await response.json();
13
- };
14
- const getDO = async (options, path) => {
15
- const stub = options.namespace.get(options.namespace.idFromName(options.instanceName ?? "default"));
16
- const response = await stub.fetch(`https://scheduler.internal${path}`, { method: "GET" });
17
- if (!response.ok) {
18
- const text = await response.text();
19
- throw new Error(`@lunora/scheduler: SchedulerDO ${path} failed (${String(response.status)}): ${text}`);
20
- }
21
- return await response.json();
22
- };
23
- const createWorkpool = (options) => {
24
- if (!options.namespace) {
25
- throw new Error("@lunora/scheduler: `namespace` (SchedulerDO binding) is required");
26
- }
27
- if (!options.originUrl) {
28
- throw new Error("@lunora/scheduler: `originUrl` is required so the DO can dispatch back to the Worker");
29
- }
30
- if (!Number.isInteger(options.maxConcurrency) || options.maxConcurrency <= 0) {
31
- throw new Error("@lunora/scheduler: `maxConcurrency` must be a positive integer");
32
- }
33
- const name = typeof options.name === "string" && options.name.length > 0 ? options.name : "default";
34
- const enqueue = async (function_, args, options_ = {}) => {
35
- const delayMs = options_.delayMs ?? 0;
36
- if (!Number.isFinite(delayMs) || delayMs < 0) {
37
- throw new Error("@lunora/scheduler: `delayMs` must be a non-negative finite number");
38
- }
39
- return callDO(options, "/schedule", {
40
- args,
41
- functionPath: function_.__lunoraRef,
42
- instanceName: options.instanceName ?? "default",
43
- maxConcurrency: options.maxConcurrency,
44
- originUrl: options.originUrl,
45
- pool: name,
46
- retry: options_.retry,
47
- scheduledFor: Date.now() + delayMs,
48
- shardKey: options_.shardKey
49
- });
50
- };
51
- const cancel = async (id) => callDO(options, "/cancel", { id });
52
- const status = async () => getDO(options, `/pool?name=${encodeURIComponent(name)}`);
53
- return { cancel, enqueue, name, status };
54
- };
55
-
56
- export { createWorkpool as default };