@lunora/scheduler 1.0.0-alpha.163 → 1.0.0-alpha.165
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/dist/index.d.mts +32 -1
- package/dist/index.d.ts +32 -1
- package/dist/index.mjs +1 -1
- package/dist/packem_shared/MAX_RETRY_ATTEMPTS-DWQhFLsT.mjs +1 -0
- package/dist/packem_shared/{assertScheduleDelay-CH6-R3I0.mjs → assertScheduleDelay-DjJk5EFP.mjs} +1 -1
- package/dist/packem_shared/{assertScheduleInstant-BRQWdkcF.mjs → assertScheduleInstant-DmCFfey6.mjs} +1 -1
- package/dist/packem_shared/{createScheduler-gJy27c_Q.mjs → createScheduler-Cn8jThvJ.mjs} +1 -1
- package/dist/packem_shared/{createSchedulerHost-Dicjj_iQ.mjs → createSchedulerHost-4IT2KkFo.mjs} +1 -1
- package/dist/packem_shared/{createWorkpool-CdedzPRj.mjs → createWorkpool-DAXkkOrW.mjs} +1 -1
- package/package.json +2 -2
- package/dist/packem_shared/MAX_RETRY_ATTEMPTS-B0v-qzSa.mjs +0 -1
package/dist/index.d.mts
CHANGED
|
@@ -112,6 +112,22 @@ interface RetryPolicy {
|
|
|
112
112
|
backoff?: "exponential" | "linear";
|
|
113
113
|
/** Base delay in milliseconds for the first retry. Default `30_000`. */
|
|
114
114
|
baseMs?: number;
|
|
115
|
+
/**
|
|
116
|
+
* Keep retrying failed deliveries instead of dead-lettering. Overrides
|
|
117
|
+
* `maxAttempts`, and logs once at `warn` at the attempt that would have
|
|
118
|
+
* dead-lettered. The delay between attempts is clamped to between 1 second
|
|
119
|
+
* and 30 minutes: the default backoff climbs to the 30-minute ceiling, but a
|
|
120
|
+
* caller-supplied `maxMs` below it keeps a job that never succeeds retrying
|
|
121
|
+
* that often.
|
|
122
|
+
*
|
|
123
|
+
* `cancel` applies once SchedulerDO has accepted the job. A job still in a
|
|
124
|
+
* shard's outbox custody (its mutation committed, the hand-off has not
|
|
125
|
+
* landed) is dropped from the shard's `dropOutboxEntry` admin RPC instead. A
|
|
126
|
+
* stored call the scheduler refuses as invalid is parked and logged rather
|
|
127
|
+
* than retried. Honoured by SchedulerDO; a `SchedulerHost` without unbounded
|
|
128
|
+
* retry, such as the Node one, refuses it.
|
|
129
|
+
*/
|
|
130
|
+
forever?: boolean;
|
|
115
131
|
/**
|
|
116
132
|
* Maximum number of **retries** after the initial dispatch. Default `5`, so
|
|
117
133
|
* a job that keeps failing is dispatched 6 times in total before it is
|
|
@@ -176,6 +192,14 @@ interface ScheduleRecord {
|
|
|
176
192
|
enqueuedAt: number;
|
|
177
193
|
/** Why the job was dead-lettered. Set only on a parked (`/dead`) record. */
|
|
178
194
|
error?: string;
|
|
195
|
+
/**
|
|
196
|
+
* The due time a pooled job had when its full pool first turned it away.
|
|
197
|
+
* Each backpressure re-arm moves {@link ScheduleRecord.scheduledFor}, so the
|
|
198
|
+
* dispatch reports this one as the job's due time and the lag metric counts
|
|
199
|
+
* the wait for a slot. Absent until the first re-arm; cleared by a retry,
|
|
200
|
+
* which measures its own attempt's lateness.
|
|
201
|
+
*/
|
|
202
|
+
firstDueAt?: number;
|
|
179
203
|
/**
|
|
180
204
|
* The `ns:fn` path of the function to dispatch on fire. Absent when the job
|
|
181
205
|
* targets a durable workflow/agent instead — see {@link ScheduleRecord.workflow}.
|
|
@@ -963,7 +987,9 @@ declare class SchedulerDO {
|
|
|
963
987
|
* Resolve the effective retry parameters for a record: its per-job
|
|
964
988
|
* {@link RetryPolicy} merged over the DO's built-in defaults. Callers that
|
|
965
989
|
* never set `record.retry` get today's behaviour verbatim
|
|
966
|
-
* (`maxAttempts: 5`, exponential, `baseMs: 30_000`, no ceiling).
|
|
990
|
+
* (`maxAttempts: 5`, exponential, `baseMs: 30_000`, no ceiling). A
|
|
991
|
+
* `forever` policy has its delay clamped into
|
|
992
|
+
* [{@link FOREVER_RETRY_MIN_DELAY_MS}, {@link FOREVER_RETRY_MAX_DELAY_MS}].
|
|
967
993
|
*/
|
|
968
994
|
private static resolveRetry;
|
|
969
995
|
/** Clamp an untrusted `maxConcurrency` to a positive integer, else fall back. */
|
|
@@ -1285,6 +1311,11 @@ declare class SchedulerDO {
|
|
|
1285
1311
|
* hangs therefore costs at most that long in declines before the ordinary
|
|
1286
1312
|
* budget applies again. Without that ceiling, not charging would let one hung
|
|
1287
1313
|
* handler keep its record retrying and never dead-letter.
|
|
1314
|
+
*
|
|
1315
|
+
* A {@link RetryPolicy.forever} record never parks on its budget. Its
|
|
1316
|
+
* attempt count saturates at `Number.MAX_SAFE_INTEGER` so the stored count
|
|
1317
|
+
* and the backoff exponent stay finite; the delay ceiling in
|
|
1318
|
+
* {@link SchedulerDO.resolveRetry} is what keeps the backoff indexable.
|
|
1288
1319
|
*/
|
|
1289
1320
|
private recordRetry;
|
|
1290
1321
|
/**
|
package/dist/index.d.ts
CHANGED
|
@@ -112,6 +112,22 @@ interface RetryPolicy {
|
|
|
112
112
|
backoff?: "exponential" | "linear";
|
|
113
113
|
/** Base delay in milliseconds for the first retry. Default `30_000`. */
|
|
114
114
|
baseMs?: number;
|
|
115
|
+
/**
|
|
116
|
+
* Keep retrying failed deliveries instead of dead-lettering. Overrides
|
|
117
|
+
* `maxAttempts`, and logs once at `warn` at the attempt that would have
|
|
118
|
+
* dead-lettered. The delay between attempts is clamped to between 1 second
|
|
119
|
+
* and 30 minutes: the default backoff climbs to the 30-minute ceiling, but a
|
|
120
|
+
* caller-supplied `maxMs` below it keeps a job that never succeeds retrying
|
|
121
|
+
* that often.
|
|
122
|
+
*
|
|
123
|
+
* `cancel` applies once SchedulerDO has accepted the job. A job still in a
|
|
124
|
+
* shard's outbox custody (its mutation committed, the hand-off has not
|
|
125
|
+
* landed) is dropped from the shard's `dropOutboxEntry` admin RPC instead. A
|
|
126
|
+
* stored call the scheduler refuses as invalid is parked and logged rather
|
|
127
|
+
* than retried. Honoured by SchedulerDO; a `SchedulerHost` without unbounded
|
|
128
|
+
* retry, such as the Node one, refuses it.
|
|
129
|
+
*/
|
|
130
|
+
forever?: boolean;
|
|
115
131
|
/**
|
|
116
132
|
* Maximum number of **retries** after the initial dispatch. Default `5`, so
|
|
117
133
|
* a job that keeps failing is dispatched 6 times in total before it is
|
|
@@ -176,6 +192,14 @@ interface ScheduleRecord {
|
|
|
176
192
|
enqueuedAt: number;
|
|
177
193
|
/** Why the job was dead-lettered. Set only on a parked (`/dead`) record. */
|
|
178
194
|
error?: string;
|
|
195
|
+
/**
|
|
196
|
+
* The due time a pooled job had when its full pool first turned it away.
|
|
197
|
+
* Each backpressure re-arm moves {@link ScheduleRecord.scheduledFor}, so the
|
|
198
|
+
* dispatch reports this one as the job's due time and the lag metric counts
|
|
199
|
+
* the wait for a slot. Absent until the first re-arm; cleared by a retry,
|
|
200
|
+
* which measures its own attempt's lateness.
|
|
201
|
+
*/
|
|
202
|
+
firstDueAt?: number;
|
|
179
203
|
/**
|
|
180
204
|
* The `ns:fn` path of the function to dispatch on fire. Absent when the job
|
|
181
205
|
* targets a durable workflow/agent instead — see {@link ScheduleRecord.workflow}.
|
|
@@ -963,7 +987,9 @@ declare class SchedulerDO {
|
|
|
963
987
|
* Resolve the effective retry parameters for a record: its per-job
|
|
964
988
|
* {@link RetryPolicy} merged over the DO's built-in defaults. Callers that
|
|
965
989
|
* never set `record.retry` get today's behaviour verbatim
|
|
966
|
-
* (`maxAttempts: 5`, exponential, `baseMs: 30_000`, no ceiling).
|
|
990
|
+
* (`maxAttempts: 5`, exponential, `baseMs: 30_000`, no ceiling). A
|
|
991
|
+
* `forever` policy has its delay clamped into
|
|
992
|
+
* [{@link FOREVER_RETRY_MIN_DELAY_MS}, {@link FOREVER_RETRY_MAX_DELAY_MS}].
|
|
967
993
|
*/
|
|
968
994
|
private static resolveRetry;
|
|
969
995
|
/** Clamp an untrusted `maxConcurrency` to a positive integer, else fall back. */
|
|
@@ -1285,6 +1311,11 @@ declare class SchedulerDO {
|
|
|
1285
1311
|
* hangs therefore costs at most that long in declines before the ordinary
|
|
1286
1312
|
* budget applies again. Without that ceiling, not charging would let one hung
|
|
1287
1313
|
* handler keep its record retrying and never dead-letter.
|
|
1314
|
+
*
|
|
1315
|
+
* A {@link RetryPolicy.forever} record never parks on its budget. Its
|
|
1316
|
+
* attempt count saturates at `Number.MAX_SAFE_INTEGER` so the stored count
|
|
1317
|
+
* and the backoff exponent stay finite; the delay ceiling in
|
|
1318
|
+
* {@link SchedulerDO.resolveRetry} is what keeps the backoff indexable.
|
|
1288
1319
|
*/
|
|
1289
1320
|
private recordRetry;
|
|
1290
1321
|
/**
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{default as o}from"./packem_shared/createScheduler-
|
|
1
|
+
import{default as o}from"./packem_shared/createScheduler-Cn8jThvJ.mjs";import{default as a}from"./packem_shared/createWorkpool-DAXkkOrW.mjs";import{createCronTrigger as f}from"./packem_shared/createCronTrigger-cg32QNgj.mjs";import{CRON_SCHEDULE_KINDS as p,compileCronSchedule as c,cronJobs as d}from"./packem_shared/CRON_SCHEDULE_KINDS-AS5GtmnL.mjs";import{createQueueConsumer as m,createQueueWorkpool as n,httpDispatcher as x}from"./packem_shared/createQueueConsumer-CYwmsmvq.mjs";import{r as E}from"./packem_shared/resolve-schedule-id-B2ghxgFm.mjs";import{MAX_RETRY_ATTEMPTS as h,RETRY_BASE_DELAY_MS as T,SchedulerDO as _,TERMINAL_RETENTION_MS as R}from"./packem_shared/MAX_RETRY_ATTEMPTS-DWQhFLsT.mjs";import{createSchedulerHost as D}from"./packem_shared/createSchedulerHost-4IT2KkFo.mjs";import{isWorkflowReference as A}from"./packem_shared/isWorkflowReference-CT3tdefh.mjs";import{assertValidCronExpression as N,isValidCronExpression as L,warnIfSecondsLeading as g}from"./packem_shared/assertValidCronExpression-fiNfNiWf.mjs";import{default as O}from"./packem_shared/assertScheduleDelay-DjJk5EFP.mjs";import{default as Y}from"./packem_shared/assertScheduleInstant-DmCFfey6.mjs";export{p as CRON_SCHEDULE_KINDS,h as MAX_RETRY_ATTEMPTS,T as RETRY_BASE_DELAY_MS,_ as SchedulerDO,R as TERMINAL_RETENTION_MS,O as assertScheduleDelay,Y as assertScheduleInstant,N as assertValidCronExpression,c as compileCronSchedule,f as createCronTrigger,m as createQueueConsumer,n as createQueueWorkpool,o as createScheduler,D as createSchedulerHost,a as createWorkpool,d as cronJobs,x as httpDispatcher,L as isValidCronExpression,A as isWorkflowReference,E as resolveScheduleId,g as warnIfSecondsLeading};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{t as z,r as L}from"./resolve-schedule-id-B2ghxgFm.mjs";const B="DISPATCH_IN_PROGRESS",J="x-lunora-dispatch-declined",Q=(c,t)=>{if(c.size<t)return;const e=c.keys().next().value;e!==void 0&&c.delete(e)},D=(c,t,e,s)=>{const a=c.get(t);if(a!==void 0)return a;Q(c,s);const n=e().catch(r=>{throw c.get(t)===n&&c.delete(t),r});return c.set(t,n),n},Z=new TextEncoder,O=64,tt=new Map,et=async c=>D(tt,c,async()=>crypto.subtle.importKey("raw",Z.encode(c),{hash:"SHA-256",name:"HMAC"},!1,["sign","verify"]),O),st=new TextEncoder,at=Array.from({length:32},(c,t)=>t);new RegExp(`[${at.map(c=>String.fromCodePoint(c)).join("")}]`,"u");const it=async(c,t)=>{const e=await et(c),s=await crypto.subtle.sign("HMAC",e,st.encode(t));return z(new Uint8Array(s))},K=(c,t=200,e)=>{const s=new Headers({"content-type":"application/json"});for(const[a,n]of Object.entries({}))s.set(a,n);return Response.json(c,{headers:s,status:t})},nt="lunora-ping",rt="lunora-pong",u="id:",I="retry:",f="dead:",R="pool:",A="pq:",p="done:",C="dt:",P="repair:",S="inst:",$="cl:",E="cr:",F=50,y=128,_=c=>typeof c=="string"&&c.length>0?c:void 0,m=c=>Array.from({length:c.length},(t,e)=>c.charCodeAt(e).toString(16).padStart(4,"0")).join(""),ot=c=>{let t="";for(let e=0;e<c.length;e+=4)t+=String.fromCharCode(Number.parseInt(c.slice(e,e+4),16));return t},G=c=>`${A}${m(c)}:`,N="meta:clean",k="meta:paused",v=100,dt=10,ct=5,H=3e4,W=1e3,X=18e5,lt=1e3,ht=6,V=9e5,q=6048e5,Y=64,ut=4,j=999999999999999,gt=15,T=c=>String(c).padStart(gt,"0"),M=c=>Number.isInteger(c)&&c>0&&c<=j;class i{static indexKey(t,e){return`t:${T(t)}:${e}`}static terminalIndexKey(t,e){return`${C}${T(j-t)}:${e}`}static cronRunKey(t){return`${E}${m(t.name)}:${T(j-t.endedAt)}:${t.runId}`}static leaseIsLive(t,e){return(t.renewedAt??t.startedAt)>e-V}static cronName(t){const e=t?.name;return typeof e=="string"&&e.length>0&&e.length<=256?e:void 0}static poolQueueKey(t){return t.pool===void 0?void 0:`${G(t.pool)}${t.id}`}static pendingRows(t){const e=i.poolQueueKey(t);return{[`${u}${t.id}`]:t,...e===void 0?{}:{[e]:t.id}}}static pendingKeys(t){return Object.keys(i.pendingRows(t))}static json(t,e=200){return K(t,e)}static error(t,e,s){return i.json({error:{code:e,message:s}},t)}static resolveRetry(t){const e=t.retry,s=typeof e?.maxAttempts=="number"&&Number.isInteger(e.maxAttempts)&&e.maxAttempts>0?e.maxAttempts:ct,a=typeof e?.baseMs=="number"&&Number.isFinite(e.baseMs)&&e.baseMs>=0?e.baseMs:H,n=e?.backoff==="linear"?"linear":"exponential",r=typeof e?.maxMs=="number"&&Number.isFinite(e.maxMs)&&e.maxMs>=0?e.maxMs:void 0,d=e?.forever===!0;return d?{backoff:n,baseMs:Math.max(W,a),forever:d,maxAttempts:s,maxMs:Math.min(X,Math.max(W,r??X))}:{backoff:n,baseMs:a,forever:d,maxAttempts:s,maxMs:r}}static normalizeConcurrency(t,e){return typeof t=="number"&&Number.isInteger(t)&&t>0?t:e}static normalizeRetry(t){if(typeof t!="object"||t===null)return;const e=t,s={};return typeof e.maxAttempts=="number"&&Number.isInteger(e.maxAttempts)&&e.maxAttempts>0&&(s.maxAttempts=e.maxAttempts),typeof e.baseMs=="number"&&Number.isFinite(e.baseMs)&&e.baseMs>=0&&(s.baseMs=e.baseMs),(e.backoff==="exponential"||e.backoff==="linear")&&(s.backoff=e.backoff),typeof e.maxMs=="number"&&Number.isFinite(e.maxMs)&&e.maxMs>=0&&(s.maxMs=e.maxMs),e.forever===!0&&(s.forever=!0),Object.keys(s).length===0?void 0:s}static releaseSlot(t,e){if(t.inFlightIds===void 0)return{...t,inFlight:Math.max(0,t.inFlight-1)};const s=t.inFlightIds.filter(a=>a!==e);return{...t,inFlight:s.length,inFlightIds:s}}static releaseFirstSlot(t){if(t.inFlightIds===void 0)return{...t,inFlight:Math.max(0,t.inFlight-1)};const e=t.inFlightIds.slice(0,Math.max(0,t.inFlightIds.length-1));return{...t,inFlight:e.length,inFlightIds:e}}static resolveScheduleTarget(t){const e=typeof t?.functionPath=="string"&&t.functionPath.length>0?t.functionPath:void 0,s=typeof t?.workflow=="string"&&t.workflow.length>0?t.workflow:void 0;if(!(e===void 0&&s===void 0))return{functionPath:e,workflow:s}}state;env;recovery;recovered=!1;dirty=!1;poolLock=Promise.resolve();activeLeases=new Map;cancelledLeases=new Set;constructor(t,e){this.state=t,this.env=e,this.armWebSocketKeepalive()}async fetch(t){await this.reindexOrphanedRecords();const e=new URL(t.url);if(e.pathname==="/ws"&&t.headers.get("Upgrade")==="websocket")return this.handleWebSocketUpgrade();switch(`${t.method} ${e.pathname}`){case"GET /cron/runs":return this.handleCronRuns();case"GET /dead":return this.handleDeadList(e);case"GET /get":return this.handleGet(e);case"GET /history":return this.handleHistory(e);case"GET /instances":return this.handleListInstances();case"GET /list":return this.handleList(e);case"GET /pause":return i.json(await this.pauseState());case"GET /pool":return this.handlePoolStatus(e);case"GET /status":return this.handleStatus();case"POST /cancel":return this.handleCancel(t);case"POST /complete":return this.handleComplete(t);case"POST /cron/begin":return this.handleCronBegin(t);case"POST /cron/end":return this.handleCronEnd(t);case"POST /cron/renew":return this.handleCronRenew(t);case"POST /dead/cancel":return this.handleDeadCancel(t);case"POST /dead/retry":return this.handleDeadRetry(t);case"POST /instances/register":return this.handleRegisterInstance(t);case"POST /pause":return this.handleSetPause(t);case"POST /schedule":return this.handleSchedule(t)}return K({error:{code:"NOT_FOUND"}},404)}async alarm(){if(await this.reindexOrphanedRecords(),(await this.pauseState()).paused){await this.state.storage.deleteAlarm();return}const e=Date.now(),s=[],a=new Set,n=await this.state.storage.list({end:`t:${T(e)}:~`,limit:100,prefix:"t:"}),r=[];for(const[l,h]of n.entries()){const w=Number.parseInt(l.slice(2,l.indexOf(":",2)),10);Number.isFinite(w)&&w<=e&&r.push({indexKey:l,recordId:h})}const d=await Promise.all(r.map(l=>this.state.storage.get(`${u}${l.recordId}`)));for(const[l,{indexKey:h,recordId:w}]of r.entries()){const g=d[l];g&&a.has(w)?await this.state.storage.delete(h):g?(a.add(w),s.push({claimKey:h,record:g})):await this.state.storage.delete(h)}let o=!1;try{s.length>0&&(await this.state.storage.delete(N),o=!0),await this.drainDue(s)}finally{await this.rescheduleAlarm(),o&&this.recovered&&!this.dirty&&await this.state.storage.put(N,!0)}await this.sweepTerminal().catch(()=>{}),s.length>0&&await this.broadcastChange()}async dispatch(t){const e=typeof this.env.LUNORA_ORIGIN_URL=="string"&&this.env.LUNORA_ORIGIN_URL.length>0?this.env.LUNORA_ORIGIN_URL:void 0;if(!e)return!1;const s=JSON.stringify({args:t.args,functionPath:t.functionPath,id:t.id,instanceName:t.instanceName,pool:t.pool,scheduledFor:t.firstDueAt??t.scheduledFor,shardKey:t.shardKey,workflow:t.workflow});try{const a={"content-type":"application/json"},n=await this.signDispatch(s);n!==void 0?a["x-lunora-scheduler-signature"]=n:typeof this.env.LUNORA_ADMIN_TOKEN=="string"&&this.env.LUNORA_ADMIN_TOKEN.length>0&&(a.authorization=`Bearer ${this.env.LUNORA_ADMIN_TOKEN}`);const r=await fetch(`${e}/_lunora/scheduler/dispatch`,{body:s,headers:a,method:"POST"});return r.status===409&&r.headers.get(J)==="1"&&await r.json().then(o=>o?.error?.code===B,()=>!1)?"in-progress":r.ok}catch{return!1}}armWebSocketKeepalive(){const t=this.state.setWebSocketAutoResponse;typeof t!="function"||typeof WebSocketRequestResponsePair>"u"||t.call(this.state,new WebSocketRequestResponsePair(nt,rt))}async drainDue(t){const e=[...t],s=[],a=Math.min(ht,e.length);for(let n=0;n<a;n+=1)s.push((async()=>{for(let r=e.shift();r!==void 0;r=e.shift())await this.drainRecordGuarded(r.record,r.claimKey)})());await Promise.all(s)}async withPoolLock(t){const e=this.poolLock.then(t,t);return this.poolLock=e.then(()=>{},()=>{}),e}async drainRecordGuarded(t,e){const s=i.indexKey(Date.now()+V,t.id);try{const a=await this.state.storage.get(`${u}${t.id}`);if(a===void 0){await this.state.storage.delete(e);return}if(JSON.stringify(a)!==JSON.stringify(t)||await this.isPaused())return;await this.state.storage.delete(e),await this.state.storage.put(s,t.id),this.activeLeases.set(t.id,s),await this.drainRecord(t)}catch{try{if(this.cancelledLeases.has(t.id)){await this.state.storage.delete(s);return}if(await this.state.storage.get(`${f}${t.id}`)!==void 0){await this.state.storage.delete(s),await this.state.storage.delete([`${I}${t.id}`,...i.pendingKeys(t)]);return}await this.state.storage.put(e,t.id),await this.state.storage.delete(s)}catch{this.dirty=!0}return}finally{this.activeLeases.delete(t.id),this.cancelledLeases.delete(t.id)}try{await this.state.storage.delete(s)}catch{}}async drainRecord(t){if(!await this.reservePoolSlot(t))return!1;const s=await this.dispatch(t),a=s===!0,n=t.pool;if(!a&&n!==void 0&&await this.withPoolLock(async()=>{const r=await this.loadPool(n);await this.savePool(n,i.releaseSlot(r,t.id))}),this.cancelledLeases.has(t.id))return a&&await this.correctCanceledToSucceeded(t).catch(()=>{}),a;if(a){const r=Date.now();try{await this.writeTerminal(t,"succeeded",r)}catch{this.dirty=!0,await this.state.storage.put(`${P}${t.id}`,{...t,completedAt:r,state:"succeeded"}).catch(()=>{})}try{await this.state.storage.delete([...i.pendingKeys(t),`${I}${t.id}`])}catch{this.dirty=!0}return!0}return await this.recordRetry(t,s==="in-progress"),!1}async reservePoolSlot(t){const e=t.pool;if(e===void 0)return!0;const s=await this.withPoolLock(async()=>{const a=await this.loadPool(e);if(a.inFlight>=a.maxConcurrency)return!1;const n=a.inFlightIds??[];return n.includes(t.id)||n.push(t.id),a.inFlightIds=n,a.inFlight=n.length,await this.savePool(e,a),!0});return s||await this.requeuePooled(t),s}async handleWebSocketUpgrade(){if(this.state.acceptWebSocket===void 0)return i.error(501,"WS_UNSUPPORTED","WebSocket subscriptions are not supported in this runtime");const t=new WebSocketPair,e=t[0],s=t[1];this.state.acceptWebSocket(s);const a=await this.listPage(u,v);return s.send(JSON.stringify({records:a.records,truncated:a.truncated,type:"jobs"})),new Response(null,{status:101,webSocket:e})}async broadcastChange(){const t=this.state.getWebSockets?.();if(t===void 0||t.length===0)return;const{records:e,truncated:s}=await this.listPage(u,v),a=JSON.stringify({records:e,truncated:s,type:"jobs"});for(const n of t)try{n.send(a)}catch{}}async listPage(t,e,s){const a=await this.state.storage.list({limit:e+1,prefix:t,...s===void 0?{}:{startAfter:s}}),n=[...a.keys()],r=[...a.values()],d=r.length>e;return d?{cursor:n[e-1],records:r.slice(0,e),truncated:d}:{records:r,truncated:d}}async forEachPage(t,e,s=v){let a;for(;;){const n=await this.state.storage.list(a===void 0?{limit:s,prefix:t}:{limit:s,prefix:t,startAfter:a});if(n.size===0)break;for(const[d,o]of n.entries())e(o,d);if(a=[...n.keys()].at(-1),n.size<s)break}}async signDispatch(t){const e=typeof this.env.LUNORA_SCHEDULER_SECRET=="string"?this.env.LUNORA_SCHEDULER_SECRET:void 0;if(!(!e||e.length===0))return it(e,t)}async recordRetry(t,e=!1){if(this.cancelledLeases.has(t.id))return;const{backoff:s,baseMs:a,forever:n,maxAttempts:r,maxMs:d}=i.resolveRetry(t),o=t.attempts??0,l=Math.min(Number.MAX_SAFE_INTEGER,o+(e?0:1)),h=Math.min(Number.MAX_SAFE_INTEGER,o+1),w=s==="linear"?a*h:a*2**(h-1),g=d===void 0?w:Math.min(w,d),x=e?Math.max(g,H):g,b=Math.round(Date.now()+x);if(!n&&l>r){await this.parkDead(t,l,`after ${String(l)} attempts`);return}if(n&&!e&&l===r+1&&console.warn(`@lunora/scheduler: job "${t.id}" (${t.functionPath??t.workflow??"unknown"}) has failed ${String(l)} attempts and keeps retrying (retry.forever)`),!M(b)){await this.parkDead(t,l,`at attempt ${String(l)}: the retry backoff exceeded the largest schedulable time`);return}const U={...t,attempts:l,firstDueAt:void 0,scheduledFor:b};await this.state.storage.put({...i.pendingRows(U),[`${I}${t.id}`]:U,[i.indexKey(b,t.id)]:t.id})}async parkDead(t,e,s){const a=Date.now();await this.state.storage.put({[`${f}${t.id}`]:{...t,attempts:e,completedAt:a,error:`dead-lettered ${s}`},[i.terminalIndexKey(a,t.id)]:t.id}),await this.state.storage.delete([`${I}${t.id}`,...i.pendingKeys(t)]),console.warn(`@lunora/scheduler: job "${t.id}" (${t.functionPath??t.workflow??"unknown"}) parked in dead-letter ${s}`)}async loadPool(t,e){const s=await this.state.storage.get(`${R}${t}`);return s!==void 0?Array.isArray(s.inFlightIds)?{inFlight:s.inFlightIds.length,inFlightIds:[...s.inFlightIds],maxConcurrency:s.maxConcurrency}:{inFlight:Math.max(0,s.inFlight),maxConcurrency:s.maxConcurrency}:{inFlight:0,inFlightIds:[],maxConcurrency:i.normalizeConcurrency(e,1)}}async savePool(t,e){await this.state.storage.put(`${R}${t}`,e)}async requeuePooled(t){if(this.cancelledLeases.has(t.id))return;const e=Date.now()+lt,s={...t,firstDueAt:t.firstDueAt??t.scheduledFor,scheduledFor:e};await this.state.storage.put({...i.pendingRows(s),[i.indexKey(e,t.id)]:t.id})}async handleComplete(t){const e=await t.json().catch(()=>{}),s=typeof e?.pool=="string"&&e.pool.length>0?e.pool:void 0,a=typeof e?.id=="string"&&e.id.length>0?e.id:void 0;if(s===void 0)return i.error(400,"INVALID_INPUT","pool is required");const n=await this.withPoolLock(async()=>{const r=await this.loadPool(s),d=a===void 0?i.releaseFirstSlot(r):i.releaseSlot(r,a);return await this.savePool(s,d),d});return await this.armAlarmIfEarlier(Date.now()),i.json({inFlight:n.inFlight})}async handlePoolStatus(t){const e=t.searchParams.get("name");if(e===null||e.length===0)return i.error(400,"INVALID_INPUT","name is required");const s=await this.loadPool(e);let a=0;return await this.forEachPage(G(e),()=>{a+=1}),i.json({inFlight:s.inFlight,maxConcurrency:s.maxConcurrency,queued:a})}async handleStatus(){const t=new Map;await this.forEachPage(A,(r,d)=>{const o=ot(d.slice(A.length,d.indexOf(":",A.length)));t.set(o,(t.get(o)??0)+1)});const e=[];let s=0,a=0;await this.forEachPage(R,(r,d)=>{const o=d.slice(R.length),l=Math.max(0,r.inFlight),h=t.get(o)??0;e.push({inFlight:l,maxConcurrency:r.maxConcurrency,name:o,queued:h}),s+=h,a+=l});const n={backlog:s,inFlight:a,pools:e};return i.json(n)}async persistPoolCap(t,e){const s=await this.loadPool(t,e);await this.savePool(t,{inFlight:s.inFlight,...s.inFlightIds===void 0?{}:{inFlightIds:s.inFlightIds},maxConcurrency:i.normalizeConcurrency(e,s.maxConcurrency)})}async idConflict(t){if(await this.state.storage.get(`${u}${t}`)!==void 0)return i.error(409,"DUPLICATE_SCHEDULE_ID",`a job with id "${t}" is already scheduled — cancel it first, or schedule under a different id`);if(await this.state.storage.get(`${f}${t}`)!==void 0)return i.error(409,"DUPLICATE_SCHEDULE_ID",`id "${t}" is held by a dead-letter record — retry or cancel it (POST /dead/retry, POST /dead/cancel) first, or schedule under a different id`)}async resolveId(t){let e;try{e=L(t)}catch(s){return i.error(400,"INVALID_SCHEDULE_ID",s instanceof Error?s.message:"invalid `id`")}return t===void 0?e:await this.idConflict(e)??e}async handleSchedule(t){const e=await t.json().catch(()=>{}),s=i.resolveScheduleTarget(e);if(!e||s===void 0)return i.error(400,"INVALID_INPUT","functionPath or workflow is required");const{functionPath:a,workflow:n}=s;if(typeof e.scheduledFor!="number"||!M(e.scheduledFor))return i.error(400,"INVALID_INPUT","scheduledFor must be a positive integer epoch-millisecond number no greater than 999999999999999");if(typeof this.env.LUNORA_ORIGIN_URL!="string"||this.env.LUNORA_ORIGIN_URL.length===0)return i.error(500,"ORIGIN_NOT_CONFIGURED","LUNORA_ORIGIN_URL env binding must be set on the SchedulerDO");const r=_(e.pool),d=_(e.instanceName),o=i.normalizeRetry(e.retry),l=await this.resolveId(e.id);if(l instanceof Response)return l;const h=l;r!==void 0&&await this.persistPoolCap(r,e.maxConcurrency);const w=e.id===void 0?void 0:this.state.storage.delete([`${p}${h}`,`${P}${h}`]),g={args:e.args??{},enqueuedAt:Date.now(),id:h,...a===void 0?{}:{functionPath:a},...d===void 0?{}:{instanceName:d},...r===void 0?{}:{pool:r},...o===void 0?{}:{retry:o},scheduledFor:e.scheduledFor,shardKey:e.shardKey,...n===void 0?{}:{workflow:n}},x=this.state.storage.put({...i.pendingRows(g),[i.indexKey(g.scheduledFor,h)]:h});return await Promise.all([w,x]),await this.armAlarmIfEarlier(g.scheduledFor),await this.broadcastChange(),i.json({id:h,scheduledFor:g.scheduledFor})}async handleCancel(t){const e=await t.json().catch(()=>{});if(!e?.id)return i.error(400,"INVALID_INPUT","id is required");const s=await this.state.storage.get(`${u}${e.id}`);return s?await this.state.storage.get(`${p}${e.id}`)!==void 0?(await this.removeRecord(s),await this.rescheduleAlarm(),i.json({cancelled:!1})):(await this.removeRecord(s),await this.writeTerminal(s,"canceled").catch(()=>{}),await this.sweepTerminal().catch(()=>{}),await this.rescheduleAlarm(),await this.broadcastChange(),i.json({cancelled:!0})):i.json({cancelled:!1})}async handleList(t){const{cursor:e,records:s,truncated:a}=await this.listPage(u,v,t.searchParams.get("cursor")??void 0);return i.json({cursor:e,records:s,truncated:a})}async handleDeadList(t){const{cursor:e,records:s,truncated:a}=await this.listPage(f,v,t.searchParams.get("cursor")??void 0);return i.json({cursor:e,records:s,truncated:a})}async handleDeadRetry(t){const e=await t.json().catch(()=>{});if(typeof e?.id!="string"||e.id.length===0)return i.error(400,"INVALID_INPUT","id is required");const s=await this.state.storage.get(`${f}${e.id}`);if(s===void 0)return i.json({retried:!1});const a=Date.now(),n={...s,attempts:0,firstDueAt:void 0,scheduledFor:a};delete n.error,delete n.completedAt,await this.state.storage.delete(`${p}${s.id}`);const r=this.state.storage.put({...i.pendingRows(n),[i.indexKey(a,s.id)]:s.id}),d=this.state.storage.delete(s.completedAt===void 0?[`${f}${s.id}`]:[`${f}${s.id}`,i.terminalIndexKey(s.completedAt,s.id)]);return await Promise.all([r,d]),await this.armAlarmIfEarlier(a),await this.broadcastChange(),i.json({id:s.id,retried:!0,scheduledFor:a})}async handleDeadCancel(t){const e=await t.json().catch(()=>{});if(typeof e?.id!="string"||e.id.length===0)return i.error(400,"INVALID_INPUT","id is required");const s=await this.state.storage.get(`${f}${e.id}`);return s===void 0?i.json({removed:!1}):(await this.writeTerminal(s,"failed",s.completedAt),await this.state.storage.delete(`${f}${e.id}`),await this.sweepTerminal().catch(()=>{}),i.json({removed:!0}))}async handleGet(t){const e=t.searchParams.get("id");if(e===null||e.length===0)return i.error(400,"INVALID_INPUT","id is required");const s=await this.readTerminal(e);if(s!==void 0)return i.json({record:s});const a=await this.state.storage.get(`${u}${e}`);if(a!==void 0){const r={...a,state:this.activeLeases.has(e)?"running":"pending"};return i.json({record:r})}const n=await this.state.storage.get(`${f}${e}`);return i.json(n===void 0?{}:{record:{...n,state:"failed"}})}async handleHistory(t){let e=t.searchParams.get("cursor")??void 0;const s=[];for(let a=0;a<dt;a+=1){const r=[...(await this.state.storage.list({limit:v+1,prefix:C,...e===void 0?{}:{startAfter:e}})).entries()],d=r.slice(0,v),o=await Promise.all(d.map(async([l,h])=>this.readTerminal(h,l)));if(s.push(...o.filter(l=>l!==void 0)),e=d.at(-1)?.[0],r.length<=v)return i.json({records:s,truncated:!1});if(s.length>0)break}return i.json({cursor:e,records:s,truncated:!0})}async readTerminal(t,e){const s=await this.state.storage.get(`${p}${t}`)??await this.readParked(t);if(!(s===void 0||s.completedAt<=Date.now()-q)&&!(e!==void 0&&i.terminalIndexKey(s.completedAt,t)!==e))return s}async readParked(t){const e=await this.state.storage.get(`${f}${t}`);return e?.completedAt===void 0?void 0:{...e,completedAt:e.completedAt,state:"failed"}}async correctCanceledToSucceeded(t){const e=await this.state.storage.get(`${p}${t.id}`);e?.state!=="canceled"||await this.state.storage.get(`${u}${t.id}`)!==void 0||JSON.stringify(e)===JSON.stringify({...t,completedAt:e.completedAt,state:e.state})&&await Promise.all([this.state.storage.delete(i.terminalIndexKey(e.completedAt,t.id)),this.writeTerminal(t,"succeeded")])}async writeTerminal(t,e,s=Date.now()){const a={...t,completedAt:s,state:e};await this.state.storage.put({[`${p}${t.id}`]:a,[i.terminalIndexKey(s,t.id)]:t.id})}async sweepTerminal(){for(let t=0;t<ut;t+=1)if(await this.sweepTerminalBatch()<Y)return}async sweepTerminalBatch(){const t=i.terminalIndexKey(Date.now()-q+1,"~"),e=await this.state.storage.list({limit:Y,prefix:C,startAfter:t});if(e.size===0)return 0;const s=await Promise.all([...e.entries()].map(async([a,n])=>{const r=await this.state.storage.get(`${p}${n}`);return r!==void 0&&i.terminalIndexKey(r.completedAt,n)===a?[a,`${p}${n}`]:[a]}));return await this.state.storage.delete(s.flat()),e.size}async pauseState(){return await this.state.storage.get(k)??{paused:!1}}async isPaused(){return(await this.pauseState()).paused}async handleRegisterInstance(t){const e=i.cronName(await t.json().catch(()=>{}));return e===void 0?i.error(400,"INVALID_INPUT","name is required"):(await this.state.storage.put(`${S}${m(e)}`,e),i.json(await this.pauseState()))}async handleListInstances(){const t=[];return await this.forEachPage(S,e=>{t.push(e)}),i.json({instances:t})}async handleSetPause(t){const e=await t.json().catch(()=>{});if(typeof e?.paused!="boolean")return i.error(400,"INVALID_INPUT","paused (boolean) is required");if(!e.paused){const n=await this.earliestPendingTime();return n!==void 0&&Number.isFinite(n)&&await this.state.storage.setAlarm(n),await this.state.storage.delete(k),i.json({paused:!1})}const a={paused:!0,pausedAt:(await this.pauseState()).pausedAt??Date.now(),...typeof e.reason=="string"&&e.reason.length>0?{reason:e.reason.slice(0,500)}:{}};return await this.state.storage.put(k,a),await this.state.storage.deleteAlarm(),i.json(a)}async handleCronBegin(t){const e=i.cronName(await t.json().catch(()=>{}));if(e===void 0)return i.error(400,"INVALID_INPUT","name is required");const s=`${$}${m(e)}`,a=Date.now();if((await this.pauseState()).paused)return await this.recordCronSkip(e,"paused",a).catch(()=>{}),i.json({acquired:!1,reason:"paused"});const r=await this.state.storage.get(s);if(r!==void 0&&i.leaseIsLive(r,a))return await this.state.storage.put(s,{...r,skipped:r.skipped+1}).catch(()=>{}),await this.recordCronSkip(e,"in-flight",a).catch(()=>{}),i.json({acquired:!1,reason:"in-flight",runningSince:r.startedAt});r!==void 0&&await this.recordLostCronRun(r,a);const d=L(void 0);return await this.state.storage.put(s,{name:e,runId:d,skipped:0,startedAt:a}),i.json({acquired:!0,runId:d})}async handleCronRenew(t){const e=await t.json().catch(()=>{}),s=i.cronName(e);if(s===void 0||typeof e?.runId!="string")return i.error(400,"INVALID_INPUT","name and runId are required");const a=`${$}${m(s)}`,n=await this.state.storage.get(a),r=Date.now();return n?.runId!==e.runId||!i.leaseIsLive(n,r)?i.json({renewed:!1}):(await this.state.storage.put(a,{...n,renewedAt:r}),i.json({renewed:!0}))}async handleCronEnd(t){const e=await t.json().catch(()=>{}),s=i.cronName(e);if(s===void 0||typeof e?.runId!="string"||e.status!=="succeeded"&&e.status!=="failed")return i.error(400,"INVALID_INPUT","name, runId and status (succeeded | failed) are required");const a=`${$}${m(s)}`,n=await this.state.storage.get(a);if(n?.runId!==e.runId)return i.json({recorded:!1});const r=Date.now();return await this.recordCronRun({durationMs:r-n.startedAt,endedAt:r,...e.status==="failed"&&typeof e.error=="string"?{error:e.error.slice(0,2e3)}:{},name:s,runId:n.runId,skipped:n.skipped,startedAt:n.startedAt,status:e.status}),await this.state.storage.delete(a),i.json({recorded:!0})}async handleCronRuns(){const t=[],e=[],s=[],a=Date.now();await this.forEachPage($,(n,r)=>{i.leaseIsLive(n,a)?t.push(n):e.push([r,n])});for(const[n,r]of e)await this.recordLostCronRun(r,a),await this.state.storage.delete(n);return await this.forEachPage(E,n=>{s.push(n)}),i.json({inFlight:t,runs:s})}async recordLostCronRun(t,e){[...(await this.state.storage.list({limit:F+y,prefix:`${E}${m(t.name)}:`})).keys()].some(a=>a.endsWith(`:${t.runId}`))||await this.recordCronRun({durationMs:e-t.startedAt,endedAt:e,error:"the run never reported completion; its lease expired",name:t.name,runId:t.runId,skipped:t.skipped,startedAt:t.startedAt,status:"failed"})}async recordCronSkip(t,e,s){const a=`${E}${m(t)}:`,n=await this.state.storage.list({limit:1,prefix:a}),[r]=n.entries();if(r?.[1].status==="skipped"&&r[1].reason===e){const[d,o]=r,l={...o,endedAt:s,skipped:o.skipped+1};await this.recordCronRun(l,i.cronRunKey(l)===d?void 0:d);return}await this.recordCronRun({durationMs:0,endedAt:s,name:t,reason:e,runId:L(void 0),skipped:1,startedAt:s,status:"skipped"})}async recordCronRun(t,e){const s=`${E}${m(t.name)}:`,a=this.state.storage.put(i.cronRunKey(t),t);await Promise.all([a,e===void 0?void 0:this.state.storage.delete(e)]);const r=[...(await this.state.storage.list({limit:F+y+1,prefix:s})).keys()].filter(d=>d!==e).slice(F);for(let d=0;d<r.length;d+=y)await this.state.storage.delete(r.slice(d,d+y))}async removeRecord(t){const e=[...i.pendingKeys(t),i.indexKey(t.scheduledFor,t.id),`${I}${t.id}`],s=this.activeLeases.get(t.id);s!==void 0&&(e.push(s),this.cancelledLeases.add(t.id)),await this.state.storage.delete(e)}async armAlarmIfEarlier(t){const e=await this.state.storage.getAlarm();(e===null||t<e)&&await this.state.storage.setAlarm(t)}async reindexOrphanedRecords(){if(this.recovery!==void 0)return this.recovery;const t=this.recoverStore();return this.recovery=t,t.catch(()=>{this.recovery=void 0}),t}async recoverStore(){if(await this.state.storage.get(N)===void 0&&(await this.reindexWalk(),await this.state.storage.put(N,!0)),await this.state.storage.getAlarm()===null){const t=await this.earliestPendingTime();t!==void 0&&Number.isFinite(t)&&await this.state.storage.setAlarm(t)}this.recovered=!0}async restoreOutcome(t){const e=await this.state.storage.get(`${u}${t.id}`);if(e!==void 0&&e.enqueuedAt!==t.enqueuedAt){await this.state.storage.delete(`${P}${t.id}`);return}await this.state.storage.get(`${p}${t.id}`)===void 0&&await this.writeTerminal(t,"succeeded",t.completedAt),await this.state.storage.delete([`${P}${t.id}`,...i.pendingKeys(t),`${I}${t.id}`])}async reindexWalk(){const t=new Set,e=new Set;await this.forEachPage("t:",o=>{t.add(o)}),await this.forEachPage(A,(o,l)=>{e.add(l)});const s=new Map;await this.forEachPage(P,o=>{s.set(o.id,o)});const a=new Set,n=[],r=[];await this.forEachPage(u,o=>{!t.has(o.id)&&M(o.scheduledFor)&&n.push(o),a.add(o.id);const l=i.poolQueueKey(o);l!==void 0&&!e.has(l)&&r.push([l,o.id])});const d=[...e].filter(o=>!a.has(o.slice(o.indexOf(":",A.length)+1)));for(let o=0;o<d.length;o+=y)await this.state.storage.delete(d.slice(o,o+y));for(let o=0;o<r.length;o+=y)await this.state.storage.put(Object.fromEntries(r.slice(o,o+y)));for(const o of s.values())await this.restoreOutcome(o);for(const o of n)await this.state.storage.put(i.indexKey(o.scheduledFor,o.id),o.id),await this.armAlarmIfEarlier(o.scheduledFor)}async earliestPendingTime(){const e=(await this.state.storage.list({limit:1,prefix:"t:"})).entries().next();if(e.done)return;const[s]=e.value;return Number.parseInt(s.slice(2,s.indexOf(":",2)),10)}async rescheduleAlarm(){if(await this.isPaused()){await this.state.storage.deleteAlarm();return}const t=await this.earliestPendingTime();if(t===void 0){await this.state.storage.deleteAlarm();return}Number.isFinite(t)&&await this.state.storage.setAlarm(t)}}export{V as DISPATCH_LEASE_MS,ht as MAX_CONCURRENT_DISPATCHES,ct as MAX_RETRY_ATTEMPTS,j as MAX_SCHEDULED_FOR_MS,H as RETRY_BASE_DELAY_MS,i as SchedulerDO,q as TERMINAL_RETENTION_MS};
|
package/dist/packem_shared/{assertScheduleDelay-CH6-R3I0.mjs → assertScheduleDelay-DjJk5EFP.mjs}
RENAMED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{LunoraError as r}from"@lunora/errors";import{MAX_SCHEDULED_FOR_MS as i}from"./MAX_RETRY_ATTEMPTS-
|
|
1
|
+
import{LunoraError as r}from"@lunora/errors";import{MAX_SCHEDULED_FOR_MS as i}from"./MAX_RETRY_ATTEMPTS-DWQhFLsT.mjs";const s=(t,e,n="delayMs")=>{if(!Number.isFinite(t)||t<0)throw new r("INVALID_INPUT",`${e}: \`${n}\` must be a non-negative finite number`);if(Date.now()+t>i)throw new r("INVALID_INPUT",`${e}: \`${n}\` is later than the latest schedulable instant (${String(i)} ms)`)};export{s as default};
|
package/dist/packem_shared/{assertScheduleInstant-BRQWdkcF.mjs → assertScheduleInstant-DmCFfey6.mjs}
RENAMED
|
@@ -1 +1 @@
|
|
|
1
|
-
import r from"./assertScheduleDelay-
|
|
1
|
+
import r from"./assertScheduleDelay-DjJk5EFP.mjs";const l=(t,s,a)=>{const e=t-s;r(Number.isFinite(e)&&e<0?0:e,a,"date")};export{l as default};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{LunoraError as w}from"@lunora/errors";import{encodeArgsOrThrow as g,decodeWire as A}from"@lunora/wire";import{a as p,c as d,g as f}from"./do-client-DnJqrsmj.mjs";import{isWorkflowReference as h}from"./isWorkflowReference-CT3tdefh.mjs";import N from"./assertScheduleDelay-
|
|
1
|
+
import{LunoraError as w}from"@lunora/errors";import{encodeArgsOrThrow as g,decodeWire as A}from"@lunora/wire";import{a as p,c as d,g as f}from"./do-client-DnJqrsmj.mjs";import{isWorkflowReference as h}from"./isWorkflowReference-CT3tdefh.mjs";import N from"./assertScheduleDelay-DjJk5EFP.mjs";import R from"./assertScheduleInstant-DmCFfey6.mjs";const v=async e=>{const a=[];let o;for(;;){const c=await e(o);if(a.push(...Array.isArray(c.records)?c.records:[]),c.truncated!==!0||typeof c.cursor!="string"||c.cursor.length===0)return a;if(c.cursor===o)throw new Error("collectPages: the list did not advance its cursor — refusing to page forever");o=c.cursor}},x=e=>typeof e=="string"?e:(h(e)?e.className:e.__lunoraRef)??"<unknown>",m=e=>({...e,args:A(e.args)}),K=e=>{p(e);const a=async(s,r,n,t={})=>{const u=Math.ceil(s instanceof Date?s.getTime():s);R(u,Date.now(),"ctx.scheduler.runAt");const i={args:g("ctx.scheduler.runAt",x(r),n),id:t.id,instanceName:e.instanceName??"default",maxConcurrency:t.pool===void 0?void 0:t.maxConcurrency,pool:t.pool,retry:t.retry,scheduledFor:u,shardKey:t.shardKey};if(h(r)){if(typeof r.className!="string"||r.className.length===0)throw new w("INTERNAL","@lunora/scheduler: workflow/agent schedule target is missing its `className` — pass the generated `workflows.<name>` / `agents.<name>` reference");return(await d(e,"/schedule",{...i,workflow:r.className})).id}const y=typeof r=="string"?r:r.__lunoraRef;return(await d(e,"/schedule",{...i,functionPath:y})).id},o=async(s,r,n,t={})=>(N(s,"ctx.scheduler.runAfter"),a(Date.now()+s,r,n,t)),c=async s=>d(e,"/cancel",{id:s}),l=async s=>(await v(async n=>f(e,n===void 0?s:`${s}?cursor=${encodeURIComponent(n)}`))).map(n=>m(n));return{cancel:c,dead:async()=>l("/dead"),deadRetry:async s=>{const{retried:r}=await d(e,"/dead/retry",{id:s});return r===!0},get:async s=>{const r=await f(e,`/get?id=${encodeURIComponent(s)}`);return r.record===void 0?null:m(r.record)},list:async()=>l("/list"),runAfter:o,runAt:a}};export{K as default};
|
package/dist/packem_shared/{createSchedulerHost-Dicjj_iQ.mjs → createSchedulerHost-4IT2KkFo.mjs}
RENAMED
|
@@ -1 +1 @@
|
|
|
1
|
-
import u from"./createScheduler-
|
|
1
|
+
import u from"./createScheduler-Cn8jThvJ.mjs";const l=a=>a?.at!==void 0?typeof a.at=="number"?a.at:a.at.getTime():Date.now()+(a?.delayMs??0),m=a=>{const r=u({instanceName:a.instanceName,jurisdiction:a.jurisdiction,namespace:a.namespace}),c=e=>({attempts:e.attempts??0,functionPath:e.functionPath??e.workflow??"",id:e.id,scheduledFor:e.scheduledFor});return{cancel:async e=>{const{cancelled:t}=await r.cancel(e);return t},deadLetter:{list:async()=>(await r.dead()).map(t=>c(t)),requeue:async e=>r.deadRetry(e)},get:async e=>{const t=await r.get(e);return t===null?null:{...c(t),state:t.state,...t.completedAt===void 0?{}:{completedAt:t.completedAt}}},list:async()=>(await r.list()).map(t=>c(t)),schedule:async(e,t,n)=>{const d=r.runAt,s=l(n);return{id:await d(s,e,t,{retry:n?.retry,shardKey:n?.shardKey}),scheduledFor:s}}}};export{m as createSchedulerHost};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{LunoraError as l}from"@lunora/errors";import{encodeArgsOrThrow as o}from"@lunora/wire";import{a as s,g as m,c as u}from"./do-client-DnJqrsmj.mjs";import y from"./assertScheduleDelay-
|
|
1
|
+
import{LunoraError as l}from"@lunora/errors";import{encodeArgsOrThrow as o}from"@lunora/wire";import{a as s,g as m,c as u}from"./do-client-DnJqrsmj.mjs";import y from"./assertScheduleDelay-DjJk5EFP.mjs";const N=e=>{if(s(e),!Number.isInteger(e.maxConcurrency)||e.maxConcurrency<=0)throw new l("INTERNAL","@lunora/scheduler: `maxConcurrency` must be a positive integer");const a=typeof e.name=="string"&&e.name.length>0?e.name:"default";return{cancel:async r=>u(e,"/cancel",{id:r}),enqueue:async(r,t,n={})=>{const c=n.delayMs??0;return y(c,"workpool.enqueue"),u(e,"/schedule",{args:o("workpool.enqueue",r.__lunoraRef,t),functionPath:r.__lunoraRef,instanceName:e.instanceName??"default",maxConcurrency:e.maxConcurrency,pool:a,retry:n.retry,scheduledFor:Math.ceil(Date.now()+c),shardKey:n.shardKey})},name:a,status:async()=>m(e,`/pool?name=${encodeURIComponent(a)}`)}};export{N as default};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lunora/scheduler",
|
|
3
|
-
"version": "1.0.0-alpha.
|
|
3
|
+
"version": "1.0.0-alpha.165",
|
|
4
4
|
"description": "Scheduling for Lunora: runAfter / runAt and Cron Triggers via SchedulerDO",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cloudflare",
|
|
@@ -51,7 +51,7 @@
|
|
|
51
51
|
},
|
|
52
52
|
"dependencies": {
|
|
53
53
|
"@lunora/errors": ">=1.0.0-alpha.51 <2.0.0-0",
|
|
54
|
-
"@lunora/platform": "1.0.0-alpha.
|
|
54
|
+
"@lunora/platform": "1.0.0-alpha.78",
|
|
55
55
|
"@lunora/wire": "1.0.0-alpha.6",
|
|
56
56
|
"@visulima/cron-parser": "2.0.0"
|
|
57
57
|
},
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import{t as q,r as b}from"./resolve-schedule-id-B2ghxgFm.mjs";const X="DISPATCH_IN_PROGRESS",V="x-lunora-dispatch-declined",z=(c,t)=>{if(c.size<t)return;const e=c.keys().next().value;e!==void 0&&c.delete(e)},B=(c,t,e,s)=>{const a=c.get(t);if(a!==void 0)return a;z(c,s);const n=e().catch(r=>{throw c.get(t)===n&&c.delete(t),r});return c.set(t,n),n},Y=new TextEncoder,J=64,Q=new Map,Z=async c=>B(Q,c,async()=>crypto.subtle.importKey("raw",Y.encode(c),{hash:"SHA-256",name:"HMAC"},!1,["sign","verify"]),J),D=new TextEncoder,O=Array.from({length:32},(c,t)=>t);new RegExp(`[${O.map(c=>String.fromCodePoint(c)).join("")}]`,"u");const tt=async(c,t)=>{const e=await Z(c),s=await crypto.subtle.sign("HMAC",e,D.encode(t));return q(new Uint8Array(s))},M=(c,t=200,e)=>{const s=new Headers({"content-type":"application/json"});for(const[a,n]of Object.entries({}))s.set(a,n);return Response.json(c,{headers:s,status:t})},et="lunora-ping",st="lunora-pong",g="id:",I="retry:",f="dead:",$="pool:",A="pq:",p="done:",C="dt:",P="repair:",U="inst:",N="cl:",R="cr:",L=50,y=128,K=c=>typeof c=="string"&&c.length>0?c:void 0,m=c=>Array.from({length:c.length},(t,e)=>c.charCodeAt(e).toString(16).padStart(4,"0")).join(""),at=c=>{let t="";for(let e=0;e<c.length;e+=4)t+=String.fromCharCode(Number.parseInt(c.slice(e,e+4),16));return t},S=c=>`${A}${m(c)}:`,T="meta:clean",F="meta:paused",v=100,it=10,nt=5,G=3e4,rt=1e3,ot=6,H=9e5,W=6048e5,_=64,dt=4,j=999999999999999,ct=15,x=c=>String(c).padStart(ct,"0"),k=c=>Number.isInteger(c)&&c>0&&c<=j;class i{static indexKey(t,e){return`t:${x(t)}:${e}`}static terminalIndexKey(t,e){return`${C}${x(j-t)}:${e}`}static cronRunKey(t){return`${R}${m(t.name)}:${x(j-t.endedAt)}:${t.runId}`}static leaseIsLive(t,e){return(t.renewedAt??t.startedAt)>e-H}static cronName(t){const e=t?.name;return typeof e=="string"&&e.length>0&&e.length<=256?e:void 0}static poolQueueKey(t){return t.pool===void 0?void 0:`${S(t.pool)}${t.id}`}static pendingRows(t){const e=i.poolQueueKey(t);return{[`${g}${t.id}`]:t,...e===void 0?{}:{[e]:t.id}}}static pendingKeys(t){return Object.keys(i.pendingRows(t))}static json(t,e=200){return M(t,e)}static error(t,e,s){return i.json({error:{code:e,message:s}},t)}static resolveRetry(t){const e=t.retry,s=typeof e?.maxAttempts=="number"&&Number.isInteger(e.maxAttempts)&&e.maxAttempts>0?e.maxAttempts:nt,a=typeof e?.baseMs=="number"&&Number.isFinite(e.baseMs)&&e.baseMs>=0?e.baseMs:G,n=e?.backoff==="linear"?"linear":"exponential",r=typeof e?.maxMs=="number"&&Number.isFinite(e.maxMs)&&e.maxMs>=0?e.maxMs:void 0;return{backoff:n,baseMs:a,maxAttempts:s,maxMs:r}}static normalizeConcurrency(t,e){return typeof t=="number"&&Number.isInteger(t)&&t>0?t:e}static normalizeRetry(t){if(typeof t!="object"||t===null)return;const e=t,s={};return typeof e.maxAttempts=="number"&&Number.isInteger(e.maxAttempts)&&e.maxAttempts>0&&(s.maxAttempts=e.maxAttempts),typeof e.baseMs=="number"&&Number.isFinite(e.baseMs)&&e.baseMs>=0&&(s.baseMs=e.baseMs),(e.backoff==="exponential"||e.backoff==="linear")&&(s.backoff=e.backoff),typeof e.maxMs=="number"&&Number.isFinite(e.maxMs)&&e.maxMs>=0&&(s.maxMs=e.maxMs),Object.keys(s).length===0?void 0:s}static releaseSlot(t,e){if(t.inFlightIds===void 0)return{...t,inFlight:Math.max(0,t.inFlight-1)};const s=t.inFlightIds.filter(a=>a!==e);return{...t,inFlight:s.length,inFlightIds:s}}static releaseFirstSlot(t){if(t.inFlightIds===void 0)return{...t,inFlight:Math.max(0,t.inFlight-1)};const e=t.inFlightIds.slice(0,Math.max(0,t.inFlightIds.length-1));return{...t,inFlight:e.length,inFlightIds:e}}static resolveScheduleTarget(t){const e=typeof t?.functionPath=="string"&&t.functionPath.length>0?t.functionPath:void 0,s=typeof t?.workflow=="string"&&t.workflow.length>0?t.workflow:void 0;if(!(e===void 0&&s===void 0))return{functionPath:e,workflow:s}}state;env;recovery;recovered=!1;dirty=!1;poolLock=Promise.resolve();activeLeases=new Map;cancelledLeases=new Set;constructor(t,e){this.state=t,this.env=e,this.armWebSocketKeepalive()}async fetch(t){await this.reindexOrphanedRecords();const e=new URL(t.url);if(e.pathname==="/ws"&&t.headers.get("Upgrade")==="websocket")return this.handleWebSocketUpgrade();switch(`${t.method} ${e.pathname}`){case"GET /cron/runs":return this.handleCronRuns();case"GET /dead":return this.handleDeadList(e);case"GET /get":return this.handleGet(e);case"GET /history":return this.handleHistory(e);case"GET /instances":return this.handleListInstances();case"GET /list":return this.handleList(e);case"GET /pause":return i.json(await this.pauseState());case"GET /pool":return this.handlePoolStatus(e);case"GET /status":return this.handleStatus();case"POST /cancel":return this.handleCancel(t);case"POST /complete":return this.handleComplete(t);case"POST /cron/begin":return this.handleCronBegin(t);case"POST /cron/end":return this.handleCronEnd(t);case"POST /cron/renew":return this.handleCronRenew(t);case"POST /dead/cancel":return this.handleDeadCancel(t);case"POST /dead/retry":return this.handleDeadRetry(t);case"POST /instances/register":return this.handleRegisterInstance(t);case"POST /pause":return this.handleSetPause(t);case"POST /schedule":return this.handleSchedule(t)}return M({error:{code:"NOT_FOUND"}},404)}async alarm(){if(await this.reindexOrphanedRecords(),(await this.pauseState()).paused){await this.state.storage.deleteAlarm();return}const e=Date.now(),s=[],a=new Set,n=await this.state.storage.list({end:`t:${x(e)}:~`,limit:100,prefix:"t:"}),r=[];for(const[l,h]of n.entries()){const w=Number.parseInt(l.slice(2,l.indexOf(":",2)),10);Number.isFinite(w)&&w<=e&&r.push({indexKey:l,recordId:h})}const d=await Promise.all(r.map(l=>this.state.storage.get(`${g}${l.recordId}`)));for(const[l,{indexKey:h,recordId:w}]of r.entries()){const u=d[l];u&&a.has(w)?await this.state.storage.delete(h):u?(a.add(w),s.push({claimKey:h,record:u})):await this.state.storage.delete(h)}let o=!1;try{s.length>0&&(await this.state.storage.delete(T),o=!0),await this.drainDue(s)}finally{await this.rescheduleAlarm(),o&&this.recovered&&!this.dirty&&await this.state.storage.put(T,!0)}await this.sweepTerminal().catch(()=>{}),s.length>0&&await this.broadcastChange()}async dispatch(t){const e=typeof this.env.LUNORA_ORIGIN_URL=="string"&&this.env.LUNORA_ORIGIN_URL.length>0?this.env.LUNORA_ORIGIN_URL:void 0;if(!e)return!1;const s=JSON.stringify({args:t.args,functionPath:t.functionPath,id:t.id,instanceName:t.instanceName,pool:t.pool,scheduledFor:t.scheduledFor,shardKey:t.shardKey,workflow:t.workflow});try{const a={"content-type":"application/json"},n=await this.signDispatch(s);n!==void 0?a["x-lunora-scheduler-signature"]=n:typeof this.env.LUNORA_ADMIN_TOKEN=="string"&&this.env.LUNORA_ADMIN_TOKEN.length>0&&(a.authorization=`Bearer ${this.env.LUNORA_ADMIN_TOKEN}`);const r=await fetch(`${e}/_lunora/scheduler/dispatch`,{body:s,headers:a,method:"POST"});return r.status===409&&r.headers.get(V)==="1"&&await r.json().then(o=>o?.error?.code===X,()=>!1)?"in-progress":r.ok}catch{return!1}}armWebSocketKeepalive(){const t=this.state.setWebSocketAutoResponse;typeof t!="function"||typeof WebSocketRequestResponsePair>"u"||t.call(this.state,new WebSocketRequestResponsePair(et,st))}async drainDue(t){const e=[...t],s=[],a=Math.min(ot,e.length);for(let n=0;n<a;n+=1)s.push((async()=>{for(let r=e.shift();r!==void 0;r=e.shift())await this.drainRecordGuarded(r.record,r.claimKey)})());await Promise.all(s)}async withPoolLock(t){const e=this.poolLock.then(t,t);return this.poolLock=e.then(()=>{},()=>{}),e}async drainRecordGuarded(t,e){const s=i.indexKey(Date.now()+H,t.id);try{const a=await this.state.storage.get(`${g}${t.id}`);if(a===void 0){await this.state.storage.delete(e);return}if(JSON.stringify(a)!==JSON.stringify(t)||await this.isPaused())return;await this.state.storage.delete(e),await this.state.storage.put(s,t.id),this.activeLeases.set(t.id,s),await this.drainRecord(t)}catch{try{if(this.cancelledLeases.has(t.id)){await this.state.storage.delete(s);return}if(await this.state.storage.get(`${f}${t.id}`)!==void 0){await this.state.storage.delete(s),await this.state.storage.delete([`${I}${t.id}`,...i.pendingKeys(t)]);return}await this.state.storage.put(e,t.id),await this.state.storage.delete(s)}catch{this.dirty=!0}return}finally{this.activeLeases.delete(t.id),this.cancelledLeases.delete(t.id)}try{await this.state.storage.delete(s)}catch{}}async drainRecord(t){if(!await this.reservePoolSlot(t))return!1;const s=await this.dispatch(t),a=s===!0,n=t.pool;if(!a&&n!==void 0&&await this.withPoolLock(async()=>{const r=await this.loadPool(n);await this.savePool(n,i.releaseSlot(r,t.id))}),this.cancelledLeases.has(t.id))return a&&await this.correctCanceledToSucceeded(t).catch(()=>{}),a;if(a){const r=Date.now();try{await this.writeTerminal(t,"succeeded",r)}catch{this.dirty=!0,await this.state.storage.put(`${P}${t.id}`,{...t,completedAt:r,state:"succeeded"}).catch(()=>{})}try{await this.state.storage.delete([...i.pendingKeys(t),`${I}${t.id}`])}catch{this.dirty=!0}return!0}return await this.recordRetry(t,s==="in-progress"),!1}async reservePoolSlot(t){const e=t.pool;if(e===void 0)return!0;const s=await this.withPoolLock(async()=>{const a=await this.loadPool(e);if(a.inFlight>=a.maxConcurrency)return!1;const n=a.inFlightIds??[];return n.includes(t.id)||n.push(t.id),a.inFlightIds=n,a.inFlight=n.length,await this.savePool(e,a),!0});return s||await this.requeuePooled(t),s}async handleWebSocketUpgrade(){if(this.state.acceptWebSocket===void 0)return i.error(501,"WS_UNSUPPORTED","WebSocket subscriptions are not supported in this runtime");const t=new WebSocketPair,e=t[0],s=t[1];this.state.acceptWebSocket(s);const a=await this.listPage(g,v);return s.send(JSON.stringify({records:a.records,truncated:a.truncated,type:"jobs"})),new Response(null,{status:101,webSocket:e})}async broadcastChange(){const t=this.state.getWebSockets?.();if(t===void 0||t.length===0)return;const{records:e,truncated:s}=await this.listPage(g,v),a=JSON.stringify({records:e,truncated:s,type:"jobs"});for(const n of t)try{n.send(a)}catch{}}async listPage(t,e,s){const a=await this.state.storage.list({limit:e+1,prefix:t,...s===void 0?{}:{startAfter:s}}),n=[...a.keys()],r=[...a.values()],d=r.length>e;return d?{cursor:n[e-1],records:r.slice(0,e),truncated:d}:{records:r,truncated:d}}async forEachPage(t,e,s=v){let a;for(;;){const n=await this.state.storage.list(a===void 0?{limit:s,prefix:t}:{limit:s,prefix:t,startAfter:a});if(n.size===0)break;for(const[d,o]of n.entries())e(o,d);if(a=[...n.keys()].at(-1),n.size<s)break}}async signDispatch(t){const e=typeof this.env.LUNORA_SCHEDULER_SECRET=="string"?this.env.LUNORA_SCHEDULER_SECRET:void 0;if(!(!e||e.length===0))return tt(e,t)}async recordRetry(t,e=!1){if(this.cancelledLeases.has(t.id))return;const s=(t.attempts??0)+(e?0:1),a=(t.attempts??0)+1,{backoff:n,baseMs:r,maxAttempts:d,maxMs:o}=i.resolveRetry(t),l=n==="linear"?r*a:r*2**(a-1),h=o===void 0?l:Math.min(l,o),w=e?Math.max(h,G):h,u=Math.round(Date.now()+w);if(s>d){await this.parkDead(t,s,`after ${String(s)} attempts`);return}if(!k(u)){await this.parkDead(t,s,`at attempt ${String(s)}: the retry backoff exceeded the largest schedulable time`);return}const E={...t,attempts:s,scheduledFor:u};await this.state.storage.put({...i.pendingRows(E),[`${I}${t.id}`]:E,[i.indexKey(u,t.id)]:t.id})}async parkDead(t,e,s){const a=Date.now();await this.state.storage.put({[`${f}${t.id}`]:{...t,attempts:e,completedAt:a,error:`dead-lettered ${s}`},[i.terminalIndexKey(a,t.id)]:t.id}),await this.state.storage.delete([`${I}${t.id}`,...i.pendingKeys(t)]),console.warn(`@lunora/scheduler: job "${t.id}" (${t.functionPath??t.workflow??"unknown"}) parked in dead-letter ${s}`)}async loadPool(t,e){const s=await this.state.storage.get(`${$}${t}`);return s!==void 0?Array.isArray(s.inFlightIds)?{inFlight:s.inFlightIds.length,inFlightIds:[...s.inFlightIds],maxConcurrency:s.maxConcurrency}:{inFlight:Math.max(0,s.inFlight),maxConcurrency:s.maxConcurrency}:{inFlight:0,inFlightIds:[],maxConcurrency:i.normalizeConcurrency(e,1)}}async savePool(t,e){await this.state.storage.put(`${$}${t}`,e)}async requeuePooled(t){if(this.cancelledLeases.has(t.id))return;const e=Date.now()+rt,s={...t,scheduledFor:e};await this.state.storage.put({...i.pendingRows(s),[i.indexKey(e,t.id)]:t.id})}async handleComplete(t){const e=await t.json().catch(()=>{}),s=typeof e?.pool=="string"&&e.pool.length>0?e.pool:void 0,a=typeof e?.id=="string"&&e.id.length>0?e.id:void 0;if(s===void 0)return i.error(400,"INVALID_INPUT","pool is required");const n=await this.withPoolLock(async()=>{const r=await this.loadPool(s),d=a===void 0?i.releaseFirstSlot(r):i.releaseSlot(r,a);return await this.savePool(s,d),d});return await this.armAlarmIfEarlier(Date.now()),i.json({inFlight:n.inFlight})}async handlePoolStatus(t){const e=t.searchParams.get("name");if(e===null||e.length===0)return i.error(400,"INVALID_INPUT","name is required");const s=await this.loadPool(e);let a=0;return await this.forEachPage(S(e),()=>{a+=1}),i.json({inFlight:s.inFlight,maxConcurrency:s.maxConcurrency,queued:a})}async handleStatus(){const t=new Map;await this.forEachPage(A,(r,d)=>{const o=at(d.slice(A.length,d.indexOf(":",A.length)));t.set(o,(t.get(o)??0)+1)});const e=[];let s=0,a=0;await this.forEachPage($,(r,d)=>{const o=d.slice($.length),l=Math.max(0,r.inFlight),h=t.get(o)??0;e.push({inFlight:l,maxConcurrency:r.maxConcurrency,name:o,queued:h}),s+=h,a+=l});const n={backlog:s,inFlight:a,pools:e};return i.json(n)}async persistPoolCap(t,e){const s=await this.loadPool(t,e);await this.savePool(t,{inFlight:s.inFlight,...s.inFlightIds===void 0?{}:{inFlightIds:s.inFlightIds},maxConcurrency:i.normalizeConcurrency(e,s.maxConcurrency)})}async idConflict(t){if(await this.state.storage.get(`${g}${t}`)!==void 0)return i.error(409,"DUPLICATE_SCHEDULE_ID",`a job with id "${t}" is already scheduled — cancel it first, or schedule under a different id`);if(await this.state.storage.get(`${f}${t}`)!==void 0)return i.error(409,"DUPLICATE_SCHEDULE_ID",`id "${t}" is held by a dead-letter record — retry or cancel it (POST /dead/retry, POST /dead/cancel) first, or schedule under a different id`)}async resolveId(t){let e;try{e=b(t)}catch(s){return i.error(400,"INVALID_SCHEDULE_ID",s instanceof Error?s.message:"invalid `id`")}return t===void 0?e:await this.idConflict(e)??e}async handleSchedule(t){const e=await t.json().catch(()=>{}),s=i.resolveScheduleTarget(e);if(!e||s===void 0)return i.error(400,"INVALID_INPUT","functionPath or workflow is required");const{functionPath:a,workflow:n}=s;if(typeof e.scheduledFor!="number"||!k(e.scheduledFor))return i.error(400,"INVALID_INPUT","scheduledFor must be a positive integer epoch-millisecond number no greater than 999999999999999");if(typeof this.env.LUNORA_ORIGIN_URL!="string"||this.env.LUNORA_ORIGIN_URL.length===0)return i.error(500,"ORIGIN_NOT_CONFIGURED","LUNORA_ORIGIN_URL env binding must be set on the SchedulerDO");const r=K(e.pool),d=K(e.instanceName),o=i.normalizeRetry(e.retry),l=await this.resolveId(e.id);if(l instanceof Response)return l;const h=l;r!==void 0&&await this.persistPoolCap(r,e.maxConcurrency);const w=e.id===void 0?void 0:this.state.storage.delete([`${p}${h}`,`${P}${h}`]),u={args:e.args??{},enqueuedAt:Date.now(),id:h,...a===void 0?{}:{functionPath:a},...d===void 0?{}:{instanceName:d},...r===void 0?{}:{pool:r},...o===void 0?{}:{retry:o},scheduledFor:e.scheduledFor,shardKey:e.shardKey,...n===void 0?{}:{workflow:n}},E=this.state.storage.put({...i.pendingRows(u),[i.indexKey(u.scheduledFor,h)]:h});return await Promise.all([w,E]),await this.armAlarmIfEarlier(u.scheduledFor),await this.broadcastChange(),i.json({id:h,scheduledFor:u.scheduledFor})}async handleCancel(t){const e=await t.json().catch(()=>{});if(!e?.id)return i.error(400,"INVALID_INPUT","id is required");const s=await this.state.storage.get(`${g}${e.id}`);return s?await this.state.storage.get(`${p}${e.id}`)!==void 0?(await this.removeRecord(s),await this.rescheduleAlarm(),i.json({cancelled:!1})):(await this.removeRecord(s),await this.writeTerminal(s,"canceled").catch(()=>{}),await this.sweepTerminal().catch(()=>{}),await this.rescheduleAlarm(),await this.broadcastChange(),i.json({cancelled:!0})):i.json({cancelled:!1})}async handleList(t){const{cursor:e,records:s,truncated:a}=await this.listPage(g,v,t.searchParams.get("cursor")??void 0);return i.json({cursor:e,records:s,truncated:a})}async handleDeadList(t){const{cursor:e,records:s,truncated:a}=await this.listPage(f,v,t.searchParams.get("cursor")??void 0);return i.json({cursor:e,records:s,truncated:a})}async handleDeadRetry(t){const e=await t.json().catch(()=>{});if(typeof e?.id!="string"||e.id.length===0)return i.error(400,"INVALID_INPUT","id is required");const s=await this.state.storage.get(`${f}${e.id}`);if(s===void 0)return i.json({retried:!1});const a=Date.now(),n={...s,attempts:0,scheduledFor:a};delete n.error,delete n.completedAt,await this.state.storage.delete(`${p}${s.id}`);const r=this.state.storage.put({...i.pendingRows(n),[i.indexKey(a,s.id)]:s.id}),d=this.state.storage.delete(s.completedAt===void 0?[`${f}${s.id}`]:[`${f}${s.id}`,i.terminalIndexKey(s.completedAt,s.id)]);return await Promise.all([r,d]),await this.armAlarmIfEarlier(a),await this.broadcastChange(),i.json({id:s.id,retried:!0,scheduledFor:a})}async handleDeadCancel(t){const e=await t.json().catch(()=>{});if(typeof e?.id!="string"||e.id.length===0)return i.error(400,"INVALID_INPUT","id is required");const s=await this.state.storage.get(`${f}${e.id}`);return s===void 0?i.json({removed:!1}):(await this.writeTerminal(s,"failed",s.completedAt),await this.state.storage.delete(`${f}${e.id}`),await this.sweepTerminal().catch(()=>{}),i.json({removed:!0}))}async handleGet(t){const e=t.searchParams.get("id");if(e===null||e.length===0)return i.error(400,"INVALID_INPUT","id is required");const s=await this.readTerminal(e);if(s!==void 0)return i.json({record:s});const a=await this.state.storage.get(`${g}${e}`);if(a!==void 0){const r={...a,state:this.activeLeases.has(e)?"running":"pending"};return i.json({record:r})}const n=await this.state.storage.get(`${f}${e}`);return i.json(n===void 0?{}:{record:{...n,state:"failed"}})}async handleHistory(t){let e=t.searchParams.get("cursor")??void 0;const s=[];for(let a=0;a<it;a+=1){const r=[...(await this.state.storage.list({limit:v+1,prefix:C,...e===void 0?{}:{startAfter:e}})).entries()],d=r.slice(0,v),o=await Promise.all(d.map(async([l,h])=>this.readTerminal(h,l)));if(s.push(...o.filter(l=>l!==void 0)),e=d.at(-1)?.[0],r.length<=v)return i.json({records:s,truncated:!1});if(s.length>0)break}return i.json({cursor:e,records:s,truncated:!0})}async readTerminal(t,e){const s=await this.state.storage.get(`${p}${t}`)??await this.readParked(t);if(!(s===void 0||s.completedAt<=Date.now()-W)&&!(e!==void 0&&i.terminalIndexKey(s.completedAt,t)!==e))return s}async readParked(t){const e=await this.state.storage.get(`${f}${t}`);return e?.completedAt===void 0?void 0:{...e,completedAt:e.completedAt,state:"failed"}}async correctCanceledToSucceeded(t){const e=await this.state.storage.get(`${p}${t.id}`);e?.state!=="canceled"||await this.state.storage.get(`${g}${t.id}`)!==void 0||JSON.stringify(e)===JSON.stringify({...t,completedAt:e.completedAt,state:e.state})&&await Promise.all([this.state.storage.delete(i.terminalIndexKey(e.completedAt,t.id)),this.writeTerminal(t,"succeeded")])}async writeTerminal(t,e,s=Date.now()){const a={...t,completedAt:s,state:e};await this.state.storage.put({[`${p}${t.id}`]:a,[i.terminalIndexKey(s,t.id)]:t.id})}async sweepTerminal(){for(let t=0;t<dt;t+=1)if(await this.sweepTerminalBatch()<_)return}async sweepTerminalBatch(){const t=i.terminalIndexKey(Date.now()-W+1,"~"),e=await this.state.storage.list({limit:_,prefix:C,startAfter:t});if(e.size===0)return 0;const s=await Promise.all([...e.entries()].map(async([a,n])=>{const r=await this.state.storage.get(`${p}${n}`);return r!==void 0&&i.terminalIndexKey(r.completedAt,n)===a?[a,`${p}${n}`]:[a]}));return await this.state.storage.delete(s.flat()),e.size}async pauseState(){return await this.state.storage.get(F)??{paused:!1}}async isPaused(){return(await this.pauseState()).paused}async handleRegisterInstance(t){const e=i.cronName(await t.json().catch(()=>{}));return e===void 0?i.error(400,"INVALID_INPUT","name is required"):(await this.state.storage.put(`${U}${m(e)}`,e),i.json(await this.pauseState()))}async handleListInstances(){const t=[];return await this.forEachPage(U,e=>{t.push(e)}),i.json({instances:t})}async handleSetPause(t){const e=await t.json().catch(()=>{});if(typeof e?.paused!="boolean")return i.error(400,"INVALID_INPUT","paused (boolean) is required");if(!e.paused){const n=await this.earliestPendingTime();return n!==void 0&&Number.isFinite(n)&&await this.state.storage.setAlarm(n),await this.state.storage.delete(F),i.json({paused:!1})}const a={paused:!0,pausedAt:(await this.pauseState()).pausedAt??Date.now(),...typeof e.reason=="string"&&e.reason.length>0?{reason:e.reason.slice(0,500)}:{}};return await this.state.storage.put(F,a),await this.state.storage.deleteAlarm(),i.json(a)}async handleCronBegin(t){const e=i.cronName(await t.json().catch(()=>{}));if(e===void 0)return i.error(400,"INVALID_INPUT","name is required");const s=`${N}${m(e)}`,a=Date.now();if((await this.pauseState()).paused)return await this.recordCronSkip(e,"paused",a).catch(()=>{}),i.json({acquired:!1,reason:"paused"});const r=await this.state.storage.get(s);if(r!==void 0&&i.leaseIsLive(r,a))return await this.state.storage.put(s,{...r,skipped:r.skipped+1}).catch(()=>{}),await this.recordCronSkip(e,"in-flight",a).catch(()=>{}),i.json({acquired:!1,reason:"in-flight",runningSince:r.startedAt});r!==void 0&&await this.recordLostCronRun(r,a);const d=b(void 0);return await this.state.storage.put(s,{name:e,runId:d,skipped:0,startedAt:a}),i.json({acquired:!0,runId:d})}async handleCronRenew(t){const e=await t.json().catch(()=>{}),s=i.cronName(e);if(s===void 0||typeof e?.runId!="string")return i.error(400,"INVALID_INPUT","name and runId are required");const a=`${N}${m(s)}`,n=await this.state.storage.get(a),r=Date.now();return n?.runId!==e.runId||!i.leaseIsLive(n,r)?i.json({renewed:!1}):(await this.state.storage.put(a,{...n,renewedAt:r}),i.json({renewed:!0}))}async handleCronEnd(t){const e=await t.json().catch(()=>{}),s=i.cronName(e);if(s===void 0||typeof e?.runId!="string"||e.status!=="succeeded"&&e.status!=="failed")return i.error(400,"INVALID_INPUT","name, runId and status (succeeded | failed) are required");const a=`${N}${m(s)}`,n=await this.state.storage.get(a);if(n?.runId!==e.runId)return i.json({recorded:!1});const r=Date.now();return await this.recordCronRun({durationMs:r-n.startedAt,endedAt:r,...e.status==="failed"&&typeof e.error=="string"?{error:e.error.slice(0,2e3)}:{},name:s,runId:n.runId,skipped:n.skipped,startedAt:n.startedAt,status:e.status}),await this.state.storage.delete(a),i.json({recorded:!0})}async handleCronRuns(){const t=[],e=[],s=[],a=Date.now();await this.forEachPage(N,(n,r)=>{i.leaseIsLive(n,a)?t.push(n):e.push([r,n])});for(const[n,r]of e)await this.recordLostCronRun(r,a),await this.state.storage.delete(n);return await this.forEachPage(R,n=>{s.push(n)}),i.json({inFlight:t,runs:s})}async recordLostCronRun(t,e){[...(await this.state.storage.list({limit:L+y,prefix:`${R}${m(t.name)}:`})).keys()].some(a=>a.endsWith(`:${t.runId}`))||await this.recordCronRun({durationMs:e-t.startedAt,endedAt:e,error:"the run never reported completion; its lease expired",name:t.name,runId:t.runId,skipped:t.skipped,startedAt:t.startedAt,status:"failed"})}async recordCronSkip(t,e,s){const a=`${R}${m(t)}:`,n=await this.state.storage.list({limit:1,prefix:a}),[r]=n.entries();if(r?.[1].status==="skipped"&&r[1].reason===e){const[d,o]=r,l={...o,endedAt:s,skipped:o.skipped+1};await this.recordCronRun(l,i.cronRunKey(l)===d?void 0:d);return}await this.recordCronRun({durationMs:0,endedAt:s,name:t,reason:e,runId:b(void 0),skipped:1,startedAt:s,status:"skipped"})}async recordCronRun(t,e){const s=`${R}${m(t.name)}:`,a=this.state.storage.put(i.cronRunKey(t),t);await Promise.all([a,e===void 0?void 0:this.state.storage.delete(e)]);const r=[...(await this.state.storage.list({limit:L+y+1,prefix:s})).keys()].filter(d=>d!==e).slice(L);for(let d=0;d<r.length;d+=y)await this.state.storage.delete(r.slice(d,d+y))}async removeRecord(t){const e=[...i.pendingKeys(t),i.indexKey(t.scheduledFor,t.id),`${I}${t.id}`],s=this.activeLeases.get(t.id);s!==void 0&&(e.push(s),this.cancelledLeases.add(t.id)),await this.state.storage.delete(e)}async armAlarmIfEarlier(t){const e=await this.state.storage.getAlarm();(e===null||t<e)&&await this.state.storage.setAlarm(t)}async reindexOrphanedRecords(){if(this.recovery!==void 0)return this.recovery;const t=this.recoverStore();return this.recovery=t,t.catch(()=>{this.recovery=void 0}),t}async recoverStore(){if(await this.state.storage.get(T)===void 0&&(await this.reindexWalk(),await this.state.storage.put(T,!0)),await this.state.storage.getAlarm()===null){const t=await this.earliestPendingTime();t!==void 0&&Number.isFinite(t)&&await this.state.storage.setAlarm(t)}this.recovered=!0}async restoreOutcome(t){const e=await this.state.storage.get(`${g}${t.id}`);if(e!==void 0&&e.enqueuedAt!==t.enqueuedAt){await this.state.storage.delete(`${P}${t.id}`);return}await this.state.storage.get(`${p}${t.id}`)===void 0&&await this.writeTerminal(t,"succeeded",t.completedAt),await this.state.storage.delete([`${P}${t.id}`,...i.pendingKeys(t),`${I}${t.id}`])}async reindexWalk(){const t=new Set,e=new Set;await this.forEachPage("t:",o=>{t.add(o)}),await this.forEachPage(A,(o,l)=>{e.add(l)});const s=new Map;await this.forEachPage(P,o=>{s.set(o.id,o)});const a=new Set,n=[],r=[];await this.forEachPage(g,o=>{!t.has(o.id)&&k(o.scheduledFor)&&n.push(o),a.add(o.id);const l=i.poolQueueKey(o);l!==void 0&&!e.has(l)&&r.push([l,o.id])});const d=[...e].filter(o=>!a.has(o.slice(o.indexOf(":",A.length)+1)));for(let o=0;o<d.length;o+=y)await this.state.storage.delete(d.slice(o,o+y));for(let o=0;o<r.length;o+=y)await this.state.storage.put(Object.fromEntries(r.slice(o,o+y)));for(const o of s.values())await this.restoreOutcome(o);for(const o of n)await this.state.storage.put(i.indexKey(o.scheduledFor,o.id),o.id),await this.armAlarmIfEarlier(o.scheduledFor)}async earliestPendingTime(){const e=(await this.state.storage.list({limit:1,prefix:"t:"})).entries().next();if(e.done)return;const[s]=e.value;return Number.parseInt(s.slice(2,s.indexOf(":",2)),10)}async rescheduleAlarm(){if(await this.isPaused()){await this.state.storage.deleteAlarm();return}const t=await this.earliestPendingTime();if(t===void 0){await this.state.storage.deleteAlarm();return}Number.isFinite(t)&&await this.state.storage.setAlarm(t)}}export{H as DISPATCH_LEASE_MS,ot as MAX_CONCURRENT_DISPATCHES,nt as MAX_RETRY_ATTEMPTS,j as MAX_SCHEDULED_FOR_MS,G as RETRY_BASE_DELAY_MS,i as SchedulerDO,W as TERMINAL_RETENTION_MS};
|