@lunora/scheduler 1.0.0-alpha.146 → 1.0.0-alpha.147

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 CHANGED
@@ -853,6 +853,16 @@ interface SchedulerStatus {
853
853
  */
854
854
  declare class SchedulerDO {
855
855
  private static indexKey;
856
+ /** The record's `pq:` row key, or `undefined` for a job outside any pool. */
857
+ private static poolQueueKey;
858
+ /**
859
+ * The rows that make `record` a pending job — its header, plus its `pq:` row
860
+ * when pooled — ready to spread into one multi-key `put`, so the two can
861
+ * never disagree.
862
+ */
863
+ private static pendingRows;
864
+ /** The header key plus the `pq:` row key, to spread into the same multi-key `delete`. */
865
+ private static pendingKeys;
856
866
  private static json;
857
867
  private static error;
858
868
  /**
@@ -899,11 +909,20 @@ declare class SchedulerDO {
899
909
  protected readonly state: SchedulerDOState;
900
910
  protected readonly env: SchedulerEnv;
901
911
  /**
902
- * Whether {@link SchedulerDO.reindexOrphanedRecords} has already run in THIS
903
- * instance. Once is enough: an orphan can only be minted by an eviction, and
904
- * an eviction ends the instance that minted it.
912
+ * This instance's one run of {@link SchedulerDO.reindexOrphanedRecords},
913
+ * shared by every caller so none proceeds before it settles. Once is enough:
914
+ * an orphan can only be minted by an eviction, and an eviction ends the
915
+ * instance that minted it. A rejection is surfaced to the first caller only.
905
916
  */
906
- private reindexed;
917
+ private recovery;
918
+ /** Whether {@link SchedulerDO.recovery} completed, so this instance may vouch for the store via {@link CLEAN_KEY}. */
919
+ private recovered;
920
+ /**
921
+ * Set when a swallowed storage failure may have left a header with no `t:`
922
+ * entry in THIS instance (see `drainRecordGuarded`). Sticky: the instance
923
+ * then never writes {@link CLEAN_KEY} back, so its successor runs the walk.
924
+ */
925
+ private dirty;
907
926
  /**
908
927
  * Tail of the serialized `pool:<name>` critical section — see
909
928
  * the pool lock (`withPoolLock`). Per-instance, which is the right scope: the pool rows
@@ -1205,15 +1224,14 @@ declare class SchedulerDO {
1205
1224
  * `GET /status` — the app-level backlog signal that powers the studio's
1206
1225
  * SLO view. Enumerates every durable `pool:<name>` row for its `inFlight`/
1207
1226
  * `maxConcurrency` semaphore, counts the pending (not-yet-dispatched) jobs
1208
- * routed to each pool with the same single-pass scan {@link handlePoolStatus}
1209
- * uses, and rolls those up into app-wide `backlog` (sum of `queued`) and
1210
- * `inFlight` (sum of held slots) totals.
1227
+ * routed to each pool from its `pq:` rows, and rolls those up into app-wide
1228
+ * `backlog` (sum of `queued`) and `inFlight` (sum of held slots) totals.
1211
1229
  *
1212
1230
  * Pools that have rows but no queued jobs still appear (with `queued: 0`) so
1213
1231
  * a saturated-but-idle pool stays visible; a pool that only ever existed as
1214
1232
  * queued jobs without a persisted row is unreachable here (the schedule path
1215
1233
  * always writes a `pool:<name>` row before the job's header), so a single
1216
- * scan over `pool:` plus a cursor loop over `id:` is sufficient.
1234
+ * scan over `pool:` plus a cursor loop over `pq:` is sufficient.
1217
1235
  */
1218
1236
  private handleStatus;
1219
1237
  /**
@@ -1340,10 +1358,27 @@ declare class SchedulerDO {
1340
1358
  * key `409 DISPATCH_IN_PROGRESS`, which is a non-2xx, so the record is
1341
1359
  * re-armed rather than cleared.
1342
1360
  *
1343
- * Two bounded walks (all `t:` values, then all `id:` headers) rather than a
1344
- * per-header `get`, so the cost is one pass over each prefix.
1361
+ * Bounded walks (all `t:` values, `pq:` rows, then all `id:` headers) rather
1362
+ * than a per-header `get`, so the cost is one pass over each prefix.
1363
+ *
1364
+ * **The walk runs only when {@link CLEAN_KEY} is absent.** It is O(every
1365
+ * pending job), and an instance is evicted between alarms spaced wider than the
1366
+ * idle timeout, so it must not run on every new instance. The key is sufficient because
1367
+ * every remaining way to mint an orphan sits inside an alarm drain: the
1368
+ * schedule, dead-retry, retry and requeue writes each put header and index in
1369
+ * ONE call, and cancel deletes them in one. `alarm()` deletes the key before
1370
+ * a non-empty drain and writes it back only after the drain settled with no
1371
+ * swallowed failure ({@link dirty}) in an instance whose own recovery
1372
+ * finished — so an instance that dies mid-drain, or swallowed a failed
1373
+ * index write, leaves the key absent and its successor walks. A store from
1374
+ * an older build has no key and is walked once. The no-clock check at the
1375
+ * end is cheap and runs on every new instance regardless.
1345
1376
  */
1346
1377
  private reindexOrphanedRecords;
1378
+ /** One run of {@link reindexOrphanedRecords}. */
1379
+ private recoverStore;
1380
+ /** The full orphan walk — see {@link reindexOrphanedRecords}. */
1381
+ private reindexWalk;
1347
1382
  /**
1348
1383
  * The time component of the earliest pending `t:` entry, or `undefined` when
1349
1384
  * there is no pending entry at all. Reads exactly one row — the index's
package/dist/index.d.ts CHANGED
@@ -853,6 +853,16 @@ interface SchedulerStatus {
853
853
  */
854
854
  declare class SchedulerDO {
855
855
  private static indexKey;
856
+ /** The record's `pq:` row key, or `undefined` for a job outside any pool. */
857
+ private static poolQueueKey;
858
+ /**
859
+ * The rows that make `record` a pending job — its header, plus its `pq:` row
860
+ * when pooled — ready to spread into one multi-key `put`, so the two can
861
+ * never disagree.
862
+ */
863
+ private static pendingRows;
864
+ /** The header key plus the `pq:` row key, to spread into the same multi-key `delete`. */
865
+ private static pendingKeys;
856
866
  private static json;
857
867
  private static error;
858
868
  /**
@@ -899,11 +909,20 @@ declare class SchedulerDO {
899
909
  protected readonly state: SchedulerDOState;
900
910
  protected readonly env: SchedulerEnv;
901
911
  /**
902
- * Whether {@link SchedulerDO.reindexOrphanedRecords} has already run in THIS
903
- * instance. Once is enough: an orphan can only be minted by an eviction, and
904
- * an eviction ends the instance that minted it.
912
+ * This instance's one run of {@link SchedulerDO.reindexOrphanedRecords},
913
+ * shared by every caller so none proceeds before it settles. Once is enough:
914
+ * an orphan can only be minted by an eviction, and an eviction ends the
915
+ * instance that minted it. A rejection is surfaced to the first caller only.
905
916
  */
906
- private reindexed;
917
+ private recovery;
918
+ /** Whether {@link SchedulerDO.recovery} completed, so this instance may vouch for the store via {@link CLEAN_KEY}. */
919
+ private recovered;
920
+ /**
921
+ * Set when a swallowed storage failure may have left a header with no `t:`
922
+ * entry in THIS instance (see `drainRecordGuarded`). Sticky: the instance
923
+ * then never writes {@link CLEAN_KEY} back, so its successor runs the walk.
924
+ */
925
+ private dirty;
907
926
  /**
908
927
  * Tail of the serialized `pool:<name>` critical section — see
909
928
  * the pool lock (`withPoolLock`). Per-instance, which is the right scope: the pool rows
@@ -1205,15 +1224,14 @@ declare class SchedulerDO {
1205
1224
  * `GET /status` — the app-level backlog signal that powers the studio's
1206
1225
  * SLO view. Enumerates every durable `pool:<name>` row for its `inFlight`/
1207
1226
  * `maxConcurrency` semaphore, counts the pending (not-yet-dispatched) jobs
1208
- * routed to each pool with the same single-pass scan {@link handlePoolStatus}
1209
- * uses, and rolls those up into app-wide `backlog` (sum of `queued`) and
1210
- * `inFlight` (sum of held slots) totals.
1227
+ * routed to each pool from its `pq:` rows, and rolls those up into app-wide
1228
+ * `backlog` (sum of `queued`) and `inFlight` (sum of held slots) totals.
1211
1229
  *
1212
1230
  * Pools that have rows but no queued jobs still appear (with `queued: 0`) so
1213
1231
  * a saturated-but-idle pool stays visible; a pool that only ever existed as
1214
1232
  * queued jobs without a persisted row is unreachable here (the schedule path
1215
1233
  * always writes a `pool:<name>` row before the job's header), so a single
1216
- * scan over `pool:` plus a cursor loop over `id:` is sufficient.
1234
+ * scan over `pool:` plus a cursor loop over `pq:` is sufficient.
1217
1235
  */
1218
1236
  private handleStatus;
1219
1237
  /**
@@ -1340,10 +1358,27 @@ declare class SchedulerDO {
1340
1358
  * key `409 DISPATCH_IN_PROGRESS`, which is a non-2xx, so the record is
1341
1359
  * re-armed rather than cleared.
1342
1360
  *
1343
- * Two bounded walks (all `t:` values, then all `id:` headers) rather than a
1344
- * per-header `get`, so the cost is one pass over each prefix.
1361
+ * Bounded walks (all `t:` values, `pq:` rows, then all `id:` headers) rather
1362
+ * than a per-header `get`, so the cost is one pass over each prefix.
1363
+ *
1364
+ * **The walk runs only when {@link CLEAN_KEY} is absent.** It is O(every
1365
+ * pending job), and an instance is evicted between alarms spaced wider than the
1366
+ * idle timeout, so it must not run on every new instance. The key is sufficient because
1367
+ * every remaining way to mint an orphan sits inside an alarm drain: the
1368
+ * schedule, dead-retry, retry and requeue writes each put header and index in
1369
+ * ONE call, and cancel deletes them in one. `alarm()` deletes the key before
1370
+ * a non-empty drain and writes it back only after the drain settled with no
1371
+ * swallowed failure ({@link dirty}) in an instance whose own recovery
1372
+ * finished — so an instance that dies mid-drain, or swallowed a failed
1373
+ * index write, leaves the key absent and its successor walks. A store from
1374
+ * an older build has no key and is walked once. The no-clock check at the
1375
+ * end is cheap and runs on every new instance regardless.
1345
1376
  */
1346
1377
  private reindexOrphanedRecords;
1378
+ /** One run of {@link reindexOrphanedRecords}. */
1379
+ private recoverStore;
1380
+ /** The full orphan walk — see {@link reindexOrphanedRecords}. */
1381
+ private reindexWalk;
1347
1382
  /**
1348
1383
  * The time component of the earliest pending `t:` entry, or `undefined` when
1349
1384
  * there is no pending entry at all. Reads exactly one row — the index's
package/dist/index.mjs CHANGED
@@ -1 +1 @@
1
- import{default as o}from"./packem_shared/createScheduler-3p6oGhSi.mjs";import{default as a}from"./packem_shared/createWorkpool-Bo9avoNF.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-v0tEYoxJ.mjs";import{createQueueConsumer as m,createQueueWorkpool as n,httpDispatcher as x}from"./packem_shared/createQueueConsumer-B2s3Xod5.mjs";import{r as i}from"./packem_shared/resolve-schedule-id-B2ghxgFm.mjs";import{MAX_RETRY_ATTEMPTS as E,RETRY_BASE_DELAY_MS as C,SchedulerDO as _}from"./packem_shared/MAX_RETRY_ATTEMPTS-C7-WenJ5.mjs";import{createSchedulerHost as R}from"./packem_shared/createSchedulerHost-Cjqd9wDF.mjs";import{isWorkflowReference as A}from"./packem_shared/isWorkflowReference-CT3tdefh.mjs";import{assertValidCronExpression as g,isValidCronExpression as k,warnIfSecondsLeading as L}from"./packem_shared/assertValidCronExpression-fiNfNiWf.mjs";import{default as W}from"./packem_shared/assertScheduleDelay-CVFZanKn.mjs";import{default as w}from"./packem_shared/assertScheduleInstant-DcEKtg0c.mjs";export{p as CRON_SCHEDULE_KINDS,E as MAX_RETRY_ATTEMPTS,C as RETRY_BASE_DELAY_MS,_ as SchedulerDO,W as assertScheduleDelay,w as assertScheduleInstant,g as assertValidCronExpression,c as compileCronSchedule,f as createCronTrigger,m as createQueueConsumer,n as createQueueWorkpool,o as createScheduler,R as createSchedulerHost,a as createWorkpool,d as cronJobs,x as httpDispatcher,k as isValidCronExpression,A as isWorkflowReference,i as resolveScheduleId,L as warnIfSecondsLeading};
1
+ import{default as o}from"./packem_shared/createScheduler-D7F48b45.mjs";import{default as a}from"./packem_shared/createWorkpool-CRgaqKBp.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-v0tEYoxJ.mjs";import{createQueueConsumer as m,createQueueWorkpool as n,httpDispatcher as x}from"./packem_shared/createQueueConsumer-B2s3Xod5.mjs";import{r as i}from"./packem_shared/resolve-schedule-id-B2ghxgFm.mjs";import{MAX_RETRY_ATTEMPTS as E,RETRY_BASE_DELAY_MS as C,SchedulerDO as _}from"./packem_shared/MAX_RETRY_ATTEMPTS-UyUIuyeb.mjs";import{createSchedulerHost as R}from"./packem_shared/createSchedulerHost-DvY88elW.mjs";import{isWorkflowReference as A}from"./packem_shared/isWorkflowReference-CT3tdefh.mjs";import{assertValidCronExpression as g,isValidCronExpression as k,warnIfSecondsLeading as L}from"./packem_shared/assertValidCronExpression-fiNfNiWf.mjs";import{default as W}from"./packem_shared/assertScheduleDelay-XVk4jfO1.mjs";import{default as w}from"./packem_shared/assertScheduleInstant-DewOrnLr.mjs";export{p as CRON_SCHEDULE_KINDS,E as MAX_RETRY_ATTEMPTS,C as RETRY_BASE_DELAY_MS,_ as SchedulerDO,W as assertScheduleDelay,w as assertScheduleInstant,g as assertValidCronExpression,c as compileCronSchedule,f as createCronTrigger,m as createQueueConsumer,n as createQueueWorkpool,o as createScheduler,R as createSchedulerHost,a as createWorkpool,d as cronJobs,x as httpDispatcher,k as isValidCronExpression,A as isWorkflowReference,i as resolveScheduleId,L as warnIfSecondsLeading};
@@ -0,0 +1 @@
1
+ import{t as L,r as k}from"./resolve-schedule-id-B2ghxgFm.mjs";const C="DISPATCH_IN_PROGRESS",T="x-lunora-dispatch-declined",b=(d,t=200,e)=>{const s=new Headers({"content-type":"application/json"});for(const[a,o]of Object.entries({}))s.set(a,o);return Response.json(d,{headers:s,status:t})},$="lunora-ping",M="lunora-pong",g="id:",w="retry:",f="dead:",p="pool:",y="pq:",v=128,E=d=>typeof d=="string"&&d.length>0?d:void 0,U=d=>Array.from({length:d.length},(t,e)=>d.charCodeAt(e).toString(16).padStart(4,"0")).join(""),S=d=>{let t="";for(let e=0;e<d.length;e+=4)t+=String.fromCharCode(Number.parseInt(d.slice(e,e+4),16));return t},N=d=>`${y}${U(d)}:`,I="meta:clean",m=100,K=5,x=3e4,j=1e3,_=6,G=9e5,H=999999999999999,W=15,F=d=>String(d).padStart(W,"0"),R=d=>Number.isInteger(d)&&d>0&&d<=H;class i{static indexKey(t,e){return`t:${F(t)}:${e}`}static poolQueueKey(t){return t.pool===void 0?void 0:`${N(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 b(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:K,a=typeof e?.baseMs=="number"&&Number.isFinite(e.baseMs)&&e.baseMs>=0?e.baseMs:x,o=e?.backoff==="linear"?"linear":"exponential",r=typeof e?.maxMs=="number"&&Number.isFinite(e.maxMs)&&e.maxMs>=0?e.maxMs:void 0;return{backoff:o,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 /dead":return this.handleDeadList(e);case"GET /get":return this.handleGet(e);case"GET /list":return this.handleList(e);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 /dead/cancel":return this.handleDeadCancel(t);case"POST /dead/retry":return this.handleDeadRetry(t);case"POST /schedule":return this.handleSchedule(t)}return b({error:{code:"NOT_FOUND"}},404)}async alarm(){await this.reindexOrphanedRecords();const t=Date.now(),e=[],s=new Set,a=await this.state.storage.list({end:`t:${F(t)}:~`,limit:100,prefix:"t:"}),o=[];for(const[c,h]of a.entries()){const l=Number.parseInt(c.slice(2,c.indexOf(":",2)),10);Number.isFinite(l)&&l<=t&&o.push({indexKey:c,recordId:h})}const r=await Promise.all(o.map(c=>this.state.storage.get(`${g}${c.recordId}`)));for(const[c,{indexKey:h,recordId:l}]of o.entries()){const u=r[c];u&&s.has(l)?await this.state.storage.delete(h):u?(s.add(l),e.push({claimKey:h,record:u})):await this.state.storage.delete(h)}let n=!1;try{e.length>0&&(await this.state.storage.delete(I),n=!0),await this.drainDue(e)}finally{await this.rescheduleAlarm(),n&&this.recovered&&!this.dirty&&await this.state.storage.put(I,!0)}e.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"},o=await this.signDispatch(s);o!==void 0?a["x-lunora-scheduler-signature"]=o: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(T)==="1"&&await r.json().then(c=>c?.error?.code===C,()=>!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($,M))}async drainDue(t){const e=[...t],s=[],a=Math.min(_,e.length);for(let o=0;o<a;o+=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()+G,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))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([`${w}${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,o=t.pool;if(!a&&o!==void 0&&await this.withPoolLock(async()=>{const r=await this.loadPool(o);await this.savePool(o,i.releaseSlot(r,t.id))}),this.cancelledLeases.has(t.id))return a;if(a){try{await this.state.storage.delete([...i.pendingKeys(t),`${w}${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 o=a.inFlightIds??[];return o.includes(t.id)||o.push(t.id),a.inFlightIds=o,a.inFlight=o.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,m);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,m),a=JSON.stringify({records:e,truncated:s,type:"jobs"});for(const o of t)try{o.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}}),o=[...a.keys()],r=[...a.values()],n=r.length>e;return n?{cursor:o[e-1],records:r.slice(0,e),truncated:n}:{records:r,truncated:n}}async forEachPage(t,e,s=m){let a;for(;;){const o=await this.state.storage.list(a===void 0?{limit:s,prefix:t}:{limit:s,prefix:t,startAfter:a});if(o.size===0)break;for(const[n,c]of o.entries())e(c,n);if(a=[...o.keys()].at(-1),o.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;const s=new TextEncoder,a=await crypto.subtle.importKey("raw",s.encode(e),{hash:"SHA-256",name:"HMAC"},!1,["sign"]),o=await crypto.subtle.sign("HMAC",a,s.encode(t));return L(new Uint8Array(o))}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:o,baseMs:r,maxAttempts:n,maxMs:c}=i.resolveRetry(t),h=o==="linear"?r*a:r*2**(a-1),l=c===void 0?h:Math.min(h,c),u=e?Math.max(l,x):l,P=Math.round(Date.now()+u);if(s>n){await this.parkDead(t,s,`after ${String(s)} attempts`);return}if(!R(P)){await this.parkDead(t,s,`at attempt ${String(s)}: the retry backoff exceeded the largest schedulable time`);return}const A={...t,attempts:s,scheduledFor:P};await this.state.storage.put({...i.pendingRows(A),[`${w}${t.id}`]:A,[i.indexKey(P,t.id)]:t.id})}async parkDead(t,e,s){await this.state.storage.put(`${f}${t.id}`,{...t,attempts:e}),await this.state.storage.delete([`${w}${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(`${p}${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(`${p}${t}`,e)}async requeuePooled(t){if(this.cancelledLeases.has(t.id))return;const e=Date.now()+j,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 o=await this.withPoolLock(async()=>{const r=await this.loadPool(s),n=a===void 0?i.releaseFirstSlot(r):i.releaseSlot(r,a);return await this.savePool(s,n),n});return await this.armAlarmIfEarlier(Date.now()),i.json({inFlight:o.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(N(e),()=>{a+=1}),i.json({inFlight:s.inFlight,maxConcurrency:s.maxConcurrency,queued:a})}async handleStatus(){const t=new Map;await this.forEachPage(y,(r,n)=>{const c=S(n.slice(y.length,n.indexOf(":",y.length)));t.set(c,(t.get(c)??0)+1)});const e=[];let s=0,a=0;await this.forEachPage(p,(r,n)=>{const c=n.slice(p.length),h=Math.max(0,r.inFlight),l=t.get(c)??0;e.push({inFlight:h,maxConcurrency:r.maxConcurrency,name:c,queued:l}),s+=l,a+=h});const o={backlog:s,inFlight:a,pools:e};return i.json(o)}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=k(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:o}=s;if(typeof e.scheduledFor!="number"||!R(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(e.pool),n=E(e.instanceName),c=i.normalizeRetry(e.retry),h=await this.resolveId(e.id);if(h instanceof Response)return h;const l=h,u={args:e.args??{},enqueuedAt:Date.now(),id:l,...a===void 0?{}:{functionPath:a},...n===void 0?{}:{instanceName:n},...r===void 0?{}:{pool:r},...c===void 0?{}:{retry:c},scheduledFor:e.scheduledFor,shardKey:e.shardKey,...o===void 0?{}:{workflow:o}};return r!==void 0&&await this.persistPoolCap(r,e.maxConcurrency),await this.state.storage.put({...i.pendingRows(u),[i.indexKey(u.scheduledFor,l)]:l}),await this.armAlarmIfEarlier(u.scheduledFor),await this.broadcastChange(),i.json({id:l,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.removeRecord(s),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,m,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,m,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(),o={...s,attempts:0,scheduledFor:a};return await this.state.storage.put({...i.pendingRows(o),[i.indexKey(a,s.id)]:s.id}),await this.state.storage.delete(`${f}${s.id}`),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.delete(`${f}${e.id}`);return i.json({removed:!!s})}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.state.storage.get(`${g}${e}`);return i.json(s===void 0?{}:{record:s})}async removeRecord(t){const e=[...i.pendingKeys(t),i.indexKey(t.scheduledFor,t.id),`${w}${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(I)===void 0&&(await this.reindexWalk(),await this.state.storage.put(I,!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 reindexWalk(){const t=new Set,e=new Set;await this.forEachPage("t:",n=>{t.add(n)}),await this.forEachPage(y,(n,c)=>{e.add(c)});const s=new Set,a=[],o=[];await this.forEachPage(g,n=>{!t.has(n.id)&&R(n.scheduledFor)&&a.push(n),s.add(n.id);const c=i.poolQueueKey(n);c!==void 0&&!e.has(c)&&o.push([c,n.id])});const r=[...e].filter(n=>!s.has(n.slice(n.indexOf(":",y.length)+1)));for(let n=0;n<r.length;n+=v)await this.state.storage.delete(r.slice(n,n+v));for(let n=0;n<o.length;n+=v)await this.state.storage.put(Object.fromEntries(o.slice(n,n+v)));for(const n of a)await this.state.storage.put(i.indexKey(n.scheduledFor,n.id),n.id),await this.armAlarmIfEarlier(n.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(){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{G as DISPATCH_LEASE_MS,_ as MAX_CONCURRENT_DISPATCHES,K as MAX_RETRY_ATTEMPTS,H as MAX_SCHEDULED_FOR_MS,x as RETRY_BASE_DELAY_MS,i as SchedulerDO};
@@ -1 +1 @@
1
- import{LunoraError as r}from"@lunora/errors";import{MAX_SCHEDULED_FOR_MS as i}from"./MAX_RETRY_ATTEMPTS-C7-WenJ5.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};
1
+ import{LunoraError as r}from"@lunora/errors";import{MAX_SCHEDULED_FOR_MS as i}from"./MAX_RETRY_ATTEMPTS-UyUIuyeb.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};
@@ -1 +1 @@
1
- import r from"./assertScheduleDelay-CVFZanKn.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
+ import r from"./assertScheduleDelay-XVk4jfO1.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-XQayNStI.mjs";import{isWorkflowReference as h}from"./isWorkflowReference-CT3tdefh.mjs";import N from"./assertScheduleDelay-CVFZanKn.mjs";import R from"./assertScheduleInstant-DcEKtg0c.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};
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-XQayNStI.mjs";import{isWorkflowReference as h}from"./isWorkflowReference-CT3tdefh.mjs";import N from"./assertScheduleDelay-XVk4jfO1.mjs";import R from"./assertScheduleInstant-DewOrnLr.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};
@@ -1 +1 @@
1
- import u from"./createScheduler-3p6oGhSi.mjs";const i=t=>t?.at!==void 0?typeof t.at=="number"?t.at:t.at.getTime():Date.now()+(t?.delayMs??0),m=t=>{const r=u({instanceName:t.instanceName,jurisdiction:t.jurisdiction,namespace:t.namespace}),n=e=>({attempts:e.attempts??0,functionPath:e.functionPath??e.workflow??"",id:e.id,scheduledFor:e.scheduledFor});return{cancel:async e=>{const{cancelled:a}=await r.cancel(e);return a},deadLetter:{list:async()=>(await r.dead()).map(a=>n(a)),requeue:async e=>r.deadRetry(e)},list:async()=>(await r.list()).map(a=>n(a)),schedule:async(e,a,c)=>{const d=r.runAt,s=i(c);return{id:await d(s,e,a,{retry:c?.retry,shardKey:c?.shardKey}),scheduledFor:s}}}};export{m as createSchedulerHost};
1
+ import u from"./createScheduler-D7F48b45.mjs";const i=t=>t?.at!==void 0?typeof t.at=="number"?t.at:t.at.getTime():Date.now()+(t?.delayMs??0),m=t=>{const r=u({instanceName:t.instanceName,jurisdiction:t.jurisdiction,namespace:t.namespace}),n=e=>({attempts:e.attempts??0,functionPath:e.functionPath??e.workflow??"",id:e.id,scheduledFor:e.scheduledFor});return{cancel:async e=>{const{cancelled:a}=await r.cancel(e);return a},deadLetter:{list:async()=>(await r.dead()).map(a=>n(a)),requeue:async e=>r.deadRetry(e)},list:async()=>(await r.list()).map(a=>n(a)),schedule:async(e,a,c)=>{const d=r.runAt,s=i(c);return{id:await d(s,e,a,{retry:c?.retry,shardKey:c?.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-XQayNStI.mjs";import y from"./assertScheduleDelay-CVFZanKn.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};
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-XQayNStI.mjs";import y from"./assertScheduleDelay-XVk4jfO1.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.146",
3
+ "version": "1.0.0-alpha.147",
4
4
  "description": "Scheduling for Lunora: runAfter / runAt and Cron Triggers via SchedulerDO",
5
5
  "keywords": [
6
6
  "cloudflare",
@@ -1 +0,0 @@
1
- import{t as N,r as R}from"./resolve-schedule-id-B2ghxgFm.mjs";const x="DISPATCH_IN_PROGRESS",E="x-lunora-dispatch-declined",P=(u,t=200,e)=>{const s=new Headers({"content-type":"application/json"});for(const[a,i]of Object.entries({}))s.set(a,i);return Response.json(u,{headers:s,status:t})},F="lunora-ping",L="lunora-pong",c="id:",m="retry:",g="dead:",y="pool:",w=100,$=5,b=3e4,k=1e3,T=6,C=9e5,M=999999999999999,U=15,A=u=>String(u).padStart(U,"0"),I=u=>Number.isInteger(u)&&u>0&&u<=M;class n{static indexKey(t,e){return`t:${A(t)}:${e}`}static json(t,e=200){return P(t,e)}static error(t,e,s){return n.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:$,a=typeof e?.baseMs=="number"&&Number.isFinite(e.baseMs)&&e.baseMs>=0?e.baseMs:b,i=e?.backoff==="linear"?"linear":"exponential",o=typeof e?.maxMs=="number"&&Number.isFinite(e.maxMs)&&e.maxMs>=0?e.maxMs:void 0;return{backoff:i,baseMs:a,maxAttempts:s,maxMs:o}}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;reindexed=!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 /dead":return this.handleDeadList(e);case"GET /get":return this.handleGet(e);case"GET /list":return this.handleList(e);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 /dead/cancel":return this.handleDeadCancel(t);case"POST /dead/retry":return this.handleDeadRetry(t);case"POST /schedule":return this.handleSchedule(t)}return P({error:{code:"NOT_FOUND"}},404)}async alarm(){await this.reindexOrphanedRecords();const t=Date.now(),e=[],s=new Set,a=await this.state.storage.list({end:`t:${A(t)}:~`,limit:100,prefix:"t:"}),i=[];for(const[r,d]of a.entries()){const l=Number.parseInt(r.slice(2,r.indexOf(":",2)),10);Number.isFinite(l)&&l<=t&&i.push({indexKey:r,recordId:d})}const o=await Promise.all(i.map(r=>this.state.storage.get(`${c}${r.recordId}`)));for(const[r,{indexKey:d,recordId:l}]of i.entries()){const h=o[r];h&&s.has(l)?await this.state.storage.delete(d):h?(s.add(l),e.push({claimKey:d,record:h})):await this.state.storage.delete(d)}try{await this.drainDue(e)}finally{await this.rescheduleAlarm()}e.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"},i=await this.signDispatch(s);i!==void 0?a["x-lunora-scheduler-signature"]=i:typeof this.env.LUNORA_ADMIN_TOKEN=="string"&&this.env.LUNORA_ADMIN_TOKEN.length>0&&(a.authorization=`Bearer ${this.env.LUNORA_ADMIN_TOKEN}`);const o=await fetch(`${e}/_lunora/scheduler/dispatch`,{body:s,headers:a,method:"POST"});return o.status===409&&o.headers.get(E)==="1"&&await o.json().then(d=>d?.error?.code===x,()=>!1)?"in-progress":o.ok}catch{return!1}}armWebSocketKeepalive(){const t=this.state.setWebSocketAutoResponse;typeof t!="function"||typeof WebSocketRequestResponsePair>"u"||t.call(this.state,new WebSocketRequestResponsePair(F,L))}async drainDue(t){const e=[...t],s=[],a=Math.min(T,e.length);for(let i=0;i<a;i+=1)s.push((async()=>{for(let o=e.shift();o!==void 0;o=e.shift())await this.drainRecordGuarded(o.record,o.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=n.indexKey(Date.now()+C,t.id);try{const a=await this.state.storage.get(`${c}${t.id}`);if(a===void 0){await this.state.storage.delete(e);return}if(JSON.stringify(a)!==JSON.stringify(t))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(`${g}${t.id}`)!==void 0){await this.state.storage.delete(s),await this.state.storage.delete([`${m}${t.id}`,`${c}${t.id}`]);return}await this.state.storage.put(e,t.id),await this.state.storage.delete(s)}catch{}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,i=t.pool;if(!a&&i!==void 0&&await this.withPoolLock(async()=>{const o=await this.loadPool(i);await this.savePool(i,n.releaseSlot(o,t.id))}),this.cancelledLeases.has(t.id))return a;if(a){try{await this.state.storage.delete([`${c}${t.id}`,`${m}${t.id}`])}catch{}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 i=a.inFlightIds??[];return i.includes(t.id)||i.push(t.id),a.inFlightIds=i,a.inFlight=i.length,await this.savePool(e,a),!0});return s||await this.requeuePooled(t),s}async handleWebSocketUpgrade(){if(this.state.acceptWebSocket===void 0)return n.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(c,w);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(c,w),a=JSON.stringify({records:e,truncated:s,type:"jobs"});for(const i of t)try{i.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}}),i=[...a.keys()],o=[...a.values()],r=o.length>e;return r?{cursor:i[e-1],records:o.slice(0,e),truncated:r}:{records:o,truncated:r}}async forEachPage(t,e,s=w){let a;for(;;){const i=await this.state.storage.list(a===void 0?{limit:s,prefix:t}:{limit:s,prefix:t,startAfter:a});if(i.size===0)break;for(const[r,d]of i.entries())e(d,r);if(a=[...i.keys()].at(-1),i.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;const s=new TextEncoder,a=await crypto.subtle.importKey("raw",s.encode(e),{hash:"SHA-256",name:"HMAC"},!1,["sign"]),i=await crypto.subtle.sign("HMAC",a,s.encode(t));return N(new Uint8Array(i))}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:i,baseMs:o,maxAttempts:r,maxMs:d}=n.resolveRetry(t),l=i==="linear"?o*a:o*2**(a-1),h=d===void 0?l:Math.min(l,d),f=e?Math.max(h,b):h,p=Math.round(Date.now()+f);if(s>r){await this.parkDead(t,s,`after ${String(s)} attempts`);return}if(!I(p)){await this.parkDead(t,s,`at attempt ${String(s)}: the retry backoff exceeded the largest schedulable time`);return}const v={...t,attempts:s,scheduledFor:p};await this.state.storage.put({[`${c}${t.id}`]:v,[`${m}${t.id}`]:v,[n.indexKey(p,t.id)]:t.id})}async parkDead(t,e,s){await this.state.storage.put(`${g}${t.id}`,{...t,attempts:e}),await this.state.storage.delete([`${m}${t.id}`,`${c}${t.id}`]),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(`${y}${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:n.normalizeConcurrency(e,1)}}async savePool(t,e){await this.state.storage.put(`${y}${t}`,e)}async requeuePooled(t){if(this.cancelledLeases.has(t.id))return;const e=Date.now()+k,s={...t,scheduledFor:e};await this.state.storage.put({[`${c}${t.id}`]:s,[n.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 n.error(400,"INVALID_INPUT","pool is required");const i=await this.withPoolLock(async()=>{const o=await this.loadPool(s),r=a===void 0?n.releaseFirstSlot(o):n.releaseSlot(o,a);return await this.savePool(s,r),r});return await this.armAlarmIfEarlier(Date.now()),n.json({inFlight:i.inFlight})}async handlePoolStatus(t){const e=t.searchParams.get("name");if(e===null||e.length===0)return n.error(400,"INVALID_INPUT","name is required");const s=await this.loadPool(e);let a=0;return await this.forEachPage(c,i=>{i.pool===e&&(a+=1)}),n.json({inFlight:s.inFlight,maxConcurrency:s.maxConcurrency,queued:a})}async handleStatus(){const t=new Map;await this.forEachPage(c,o=>{o.pool!==void 0&&t.set(o.pool,(t.get(o.pool)??0)+1)});const e=[];let s=0,a=0;await this.forEachPage(y,(o,r)=>{const d=r.slice(y.length),l=Math.max(0,o.inFlight),h=t.get(d)??0;e.push({inFlight:l,maxConcurrency:o.maxConcurrency,name:d,queued:h}),s+=h,a+=l});const i={backlog:s,inFlight:a,pools:e};return n.json(i)}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:n.normalizeConcurrency(e,s.maxConcurrency)})}async idConflict(t){if(await this.state.storage.get(`${c}${t}`)!==void 0)return n.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(`${g}${t}`)!==void 0)return n.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=R(t)}catch(s){return n.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=n.resolveScheduleTarget(e);if(!e||s===void 0)return n.error(400,"INVALID_INPUT","functionPath or workflow is required");const{functionPath:a,workflow:i}=s;if(typeof e.scheduledFor!="number"||!I(e.scheduledFor))return n.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 n.error(500,"ORIGIN_NOT_CONFIGURED","LUNORA_ORIGIN_URL env binding must be set on the SchedulerDO");const o=typeof e.pool=="string"&&e.pool.length>0?e.pool:void 0,r=typeof e.instanceName=="string"&&e.instanceName.length>0?e.instanceName:void 0,d=n.normalizeRetry(e.retry),l=await this.resolveId(e.id);if(l instanceof Response)return l;const h=l,f={args:e.args??{},enqueuedAt:Date.now(),id:h,...a===void 0?{}:{functionPath:a},...r===void 0?{}:{instanceName:r},...o===void 0?{}:{pool:o},...d===void 0?{}:{retry:d},scheduledFor:e.scheduledFor,shardKey:e.shardKey,...i===void 0?{}:{workflow:i}};return o!==void 0&&await this.persistPoolCap(o,e.maxConcurrency),await this.state.storage.put(`${c}${h}`,f),await this.state.storage.put(n.indexKey(f.scheduledFor,h),h),await this.armAlarmIfEarlier(f.scheduledFor),await this.broadcastChange(),n.json({id:h,scheduledFor:f.scheduledFor})}async handleCancel(t){const e=await t.json().catch(()=>{});if(!e?.id)return n.error(400,"INVALID_INPUT","id is required");const s=await this.state.storage.get(`${c}${e.id}`);return s?(await this.removeRecord(s),await this.rescheduleAlarm(),await this.broadcastChange(),n.json({cancelled:!0})):n.json({cancelled:!1})}async handleList(t){const{cursor:e,records:s,truncated:a}=await this.listPage(c,w,t.searchParams.get("cursor")??void 0);return n.json({cursor:e,records:s,truncated:a})}async handleDeadList(t){const{cursor:e,records:s,truncated:a}=await this.listPage(g,w,t.searchParams.get("cursor")??void 0);return n.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 n.error(400,"INVALID_INPUT","id is required");const s=await this.state.storage.get(`${g}${e.id}`);if(s===void 0)return n.json({retried:!1});const a=Date.now(),i={...s,attempts:0,scheduledFor:a};return await this.state.storage.put(`${c}${s.id}`,i),await this.state.storage.put(n.indexKey(a,s.id),s.id),await this.state.storage.delete(`${g}${s.id}`),await this.armAlarmIfEarlier(a),await this.broadcastChange(),n.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 n.error(400,"INVALID_INPUT","id is required");const s=await this.state.storage.delete(`${g}${e.id}`);return n.json({removed:!!s})}async handleGet(t){const e=t.searchParams.get("id");if(e===null||e.length===0)return n.error(400,"INVALID_INPUT","id is required");const s=await this.state.storage.get(`${c}${e}`);return n.json(s===void 0?{}:{record:s})}async removeRecord(t){const e=[`${c}${t.id}`,n.indexKey(t.scheduledFor,t.id),`${m}${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.reindexed)return;this.reindexed=!0;const t=new Set;await this.forEachPage("t:",s=>{t.add(s)});const e=[];await this.forEachPage(c,s=>{!t.has(s.id)&&I(s.scheduledFor)&&e.push(s)});for(const s of e)await this.state.storage.put(n.indexKey(s.scheduledFor,s.id),s.id),await this.armAlarmIfEarlier(s.scheduledFor);if(await this.state.storage.getAlarm()===null){const s=await this.earliestPendingTime();s!==void 0&&Number.isFinite(s)&&await this.state.storage.setAlarm(s)}}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(){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{C as DISPATCH_LEASE_MS,T as MAX_CONCURRENT_DISPATCHES,$ as MAX_RETRY_ATTEMPTS,M as MAX_SCHEDULED_FOR_MS,b as RETRY_BASE_DELAY_MS,n as SchedulerDO};