@lunora/scheduler 1.0.0-alpha.39 → 1.0.0-alpha.40

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.mjs CHANGED
@@ -1 +1 @@
1
- import{default as o}from"./packem_shared/createScheduler-DrXa1iXV.mjs";import{default as a}from"./packem_shared/createWorkpool-d3THj5gc.mjs";import{createCronTrigger as c}from"./packem_shared/createCronTrigger-DSuDZtqj.mjs";import{CRON_SCHEDULE_KINDS as f,compileCronSchedule as l,cronJobs as n}from"./packem_shared/CRON_SCHEDULE_KINDS-xyFk800s.mjs";import{createQueueConsumer as u,createQueueWorkpool as x,httpDispatcher as d}from"./packem_shared/createQueueConsumer-DqxvbEwB.mjs";import{MAX_RETRY_ATTEMPTS as S,RETRY_BASE_DELAY_MS as E,SchedulerDO as C}from"./packem_shared/MAX_RETRY_ATTEMPTS-DU_w9jpi.mjs";import{createSchedulerHost as h}from"./packem_shared/createSchedulerHost-C4HUMyFs.mjs";import{isWorkflowReference as T}from"./packem_shared/isWorkflowReference-CT3tdefh.mjs";import{assertValidCronExpression as A,isValidCronExpression as g,warnIfSecondsLeading as k}from"./packem_shared/assertValidCronExpression-DnBtukpq.mjs";export{f as CRON_SCHEDULE_KINDS,S as MAX_RETRY_ATTEMPTS,E as RETRY_BASE_DELAY_MS,C as SchedulerDO,A as assertValidCronExpression,l as compileCronSchedule,c as createCronTrigger,u as createQueueConsumer,x as createQueueWorkpool,o as createScheduler,h as createSchedulerHost,a as createWorkpool,n as cronJobs,d as httpDispatcher,g as isValidCronExpression,T as isWorkflowReference,k as warnIfSecondsLeading};
1
+ import{default as o}from"./packem_shared/createScheduler-DrXa1iXV.mjs";import{default as a}from"./packem_shared/createWorkpool-d3THj5gc.mjs";import{createCronTrigger as c}from"./packem_shared/createCronTrigger-DSuDZtqj.mjs";import{CRON_SCHEDULE_KINDS as f,compileCronSchedule as l,cronJobs as n}from"./packem_shared/CRON_SCHEDULE_KINDS-xyFk800s.mjs";import{createQueueConsumer as u,createQueueWorkpool as x,httpDispatcher as d}from"./packem_shared/createQueueConsumer-zagVuKf0.mjs";import{MAX_RETRY_ATTEMPTS as S,RETRY_BASE_DELAY_MS as E,SchedulerDO as C}from"./packem_shared/MAX_RETRY_ATTEMPTS-D24I8zY0.mjs";import{createSchedulerHost as h}from"./packem_shared/createSchedulerHost-C4HUMyFs.mjs";import{isWorkflowReference as T}from"./packem_shared/isWorkflowReference-CT3tdefh.mjs";import{assertValidCronExpression as A,isValidCronExpression as g,warnIfSecondsLeading as k}from"./packem_shared/assertValidCronExpression-DnBtukpq.mjs";export{f as CRON_SCHEDULE_KINDS,S as MAX_RETRY_ATTEMPTS,E as RETRY_BASE_DELAY_MS,C as SchedulerDO,A as assertValidCronExpression,l as compileCronSchedule,c as createCronTrigger,u as createQueueConsumer,x as createQueueWorkpool,o as createScheduler,h as createSchedulerHost,a as createWorkpool,n as cronJobs,d as httpDispatcher,g as isValidCronExpression,T as isWorkflowReference,k as warnIfSecondsLeading};
@@ -0,0 +1 @@
1
+ const h="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_",R=c=>{let e="",t=0;const s=c.length-2;for(;t<s;t+=3){const n=c[t]<<16|c[t+1]<<8|c[t+2];e+=h.charAt(n>>18&63)+h.charAt(n>>12&63)+h.charAt(n>>6&63)+h.charAt(n&63)}const a=c.length-t;if(a===1){const n=c[t]<<16;e+=h.charAt(n>>18&63)+h.charAt(n>>12&63)}else if(a===2){const n=c[t]<<16|c[t+1]<<8;e+=h.charAt(n>>18&63)+h.charAt(n>>12&63)+h.charAt(n>>6&63)}return e},y=(c,e=200,t)=>Response.json(c,{headers:{"content-type":"application/json",...t},status:e}),A="lunora-ping",E="lunora-pong";const m="retry:",f="dead:",p="pool:";const F=5,P=3e4;const w=c=>String(c).padStart(15,"0"),I=()=>R(crypto.getRandomValues(new Uint8Array(12)));class i{static indexKey(e,t){return`t:${w(e)}:${t}`}static json(e,t=200){return y(e,t)}static error(e,t,s){return i.json({error:{code:t,message:s}},e)}static resolveRetry(e){const t=e.retry,s=typeof t?.maxAttempts=="number"&&Number.isInteger(t.maxAttempts)&&t.maxAttempts>0?t.maxAttempts:5,a=typeof t?.baseMs=="number"&&Number.isFinite(t.baseMs)&&t.baseMs>=0?t.baseMs:3e4,n=t?.backoff==="linear"?"linear":"exponential",o=typeof t?.maxMs=="number"&&Number.isFinite(t.maxMs)&&t.maxMs>=0?t.maxMs:void 0;return{backoff:n,baseMs:a,maxAttempts:s,maxMs:o}}static normalizeConcurrency(e,t){return typeof e=="number"&&Number.isInteger(e)&&e>0?e:t}static normalizeRetry(e){if(typeof e!="object"||e===null)return;const t=e,s={};return typeof t.maxAttempts=="number"&&Number.isInteger(t.maxAttempts)&&t.maxAttempts>0&&(s.maxAttempts=t.maxAttempts),typeof t.baseMs=="number"&&Number.isFinite(t.baseMs)&&t.baseMs>=0&&(s.baseMs=t.baseMs),(t.backoff==="exponential"||t.backoff==="linear")&&(s.backoff=t.backoff),typeof t.maxMs=="number"&&Number.isFinite(t.maxMs)&&t.maxMs>=0&&(s.maxMs=t.maxMs),Object.keys(s).length===0?void 0:s}static releaseSlot(e,t){if(e.inFlightIds===void 0)return{...e,inFlight:Math.max(0,e.inFlight-1)};const s=e.inFlightIds.filter(a=>a!==t);return{...e,inFlight:s.length,inFlightIds:s}}static releaseFirstSlot(e){if(e.inFlightIds===void 0)return{...e,inFlight:Math.max(0,e.inFlight-1)};const t=e.inFlightIds.slice(0,Math.max(0,e.inFlightIds.length-1));return{...e,inFlight:t.length,inFlightIds:t}}static resolveScheduleTarget(e){const t=typeof e?.functionPath=="string"&&e.functionPath.length>0?e.functionPath:void 0,s=typeof e?.workflow=="string"&&e.workflow.length>0?e.workflow:void 0;if(!(t===void 0&&s===void 0))return{functionPath:t,workflow:s}}state;env;constructor(e,t){this.state=e,this.env=t,this.armWebSocketKeepalive()}async fetch(e){const t=new URL(e.url);if(t.pathname==="/ws"&&e.headers.get("Upgrade")==="websocket")return this.handleWebSocketUpgrade();switch(`${e.method} ${t.pathname}`){case"GET /dead":return this.handleDeadList();case"GET /get":return this.handleGet(t);case"GET /list":return this.handleList();case"GET /pool":return this.handlePoolStatus(t);case"GET /status":return this.handleStatus();case"POST /cancel":return this.handleCancel(e);case"POST /complete":return this.handleComplete(e);case"POST /dead/cancel":return this.handleDeadCancel(e);case"POST /dead/retry":return this.handleDeadRetry(e);case"POST /schedule":return this.handleSchedule(e)}return y({error:{code:"NOT_FOUND"}},404)}async alarm(){const e=Date.now(),t=[],s=await this.state.storage.list({end:`t:${w(e)}:~`,limit:100,prefix:"t:"});for(const[a,n]of s.entries()){const o=Number.parseInt(a.slice(2,a.indexOf(":",2)),10);if(Number.isFinite(o)&&o<=e){const r=await this.state.storage.get(`id:${n}`);r?t.push(r):await this.state.storage.delete(a)}}try{for(const a of t)await this.drainRecordGuarded(a)}finally{await this.rescheduleAlarm()}t.length>0&&await this.broadcastChange()}async dispatch(e){const t=typeof this.env.LUNORA_ORIGIN_URL=="string"&&this.env.LUNORA_ORIGIN_URL.length>0?this.env.LUNORA_ORIGIN_URL:void 0;if(!t)return!1;const s=JSON.stringify({args:e.args,functionPath:e.functionPath,id:e.id,instanceName:e.instanceName,pool:e.pool,scheduledFor:e.scheduledFor,shardKey:e.shardKey,workflow:e.workflow});try{const a={"content-type":"application/json"},n=await this.signDispatch(s);return 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}`),(await fetch(`${t}/_lunora/scheduler/dispatch`,{body:s,headers:a,method:"POST"})).ok}catch{return!1}}armWebSocketKeepalive(){const e=this.state.setWebSocketAutoResponse;typeof e!="function"||typeof WebSocketRequestResponsePair>"u"||e.call(this.state,new WebSocketRequestResponsePair(A,E))}async drainRecordGuarded(e){try{await this.state.storage.delete(i.indexKey(e.scheduledFor,e.id)),await this.drainRecord(e)}catch{try{await this.state.storage.put(i.indexKey(e.scheduledFor,e.id),e.id)}catch{}}}async drainRecord(e){if(!await this.reservePoolSlot(e))return!1;const s=await this.dispatch(e);if(!s&&e.pool!==void 0){const a=await this.loadPool(e.pool),n=i.releaseSlot(a,e.id);await this.savePool(e.pool,n)}if(s){try{await this.state.storage.delete([`id:${e.id}`,`${m}${e.id}`])}catch{}return!0}return await this.recordRetry(e),!1}async reservePoolSlot(e){if(e.pool===void 0)return!0;const t=await this.loadPool(e.pool);if(t.inFlight>=t.maxConcurrency)return await this.requeuePooled(e),!1;const s=t.inFlightIds??[];return s.includes(e.id)||s.push(e.id),t.inFlightIds=s,t.inFlight=s.length,await this.savePool(e.pool,t),!0}async handleWebSocketUpgrade(){if(this.state.acceptWebSocket===void 0)return i.error(501,"WS_UNSUPPORTED","WebSocket subscriptions are not supported in this runtime");const e=new WebSocketPair,t=e[0],s=e[1];this.state.acceptWebSocket(s);const a=await this.listRecords();return s.send(JSON.stringify({records:a.records,truncated:a.truncated,type:"jobs"})),new Response(null,{status:101,webSocket:t})}async broadcastChange(){const e=this.state.getWebSockets?.();if(e===void 0||e.length===0)return;const{records:t,truncated:s}=await this.listRecords(),a=JSON.stringify({records:t,truncated:s,type:"jobs"});for(const n of e)try{n.send(a)}catch{}}async listRecords(e=100){const s=[...(await this.state.storage.list({limit:e+1,prefix:"id:"})).values()],a=s.length>e;return{records:a?s.slice(0,e):s,truncated:a}}async countHeaders(e,t=100){let s;for(;;){const a=await this.state.storage.list(s===void 0?{limit:t,prefix:"id:"}:{limit:t,prefix:"id:",startAfter:s});if(a.size===0)break;for(const o of a.values())e(o);if(s=[...a.keys()].at(-1),a.size<t)break}}async signDispatch(e){const t=typeof this.env.LUNORA_SCHEDULER_SECRET=="string"?this.env.LUNORA_SCHEDULER_SECRET:void 0;if(!t||t.length===0)return;const s=new TextEncoder,a=await crypto.subtle.importKey("raw",s.encode(t),{hash:"SHA-256",name:"HMAC"},!1,["sign"]),n=await crypto.subtle.sign("HMAC",a,s.encode(e));return R(new Uint8Array(n))}async recordRetry(e){const t=(e.attempts??0)+1,{backoff:s,baseMs:a,maxAttempts:n,maxMs:o}=i.resolveRetry(e);if(t>n){await this.state.storage.put(`${f}${e.id}`,{...e,attempts:t}),await this.state.storage.delete([`${m}${e.id}`,`id:${e.id}`]),console.warn(`@lunora/scheduler: job "${e.id}" (${e.functionPath??e.workflow??"unknown"}) parked in dead-letter after ${String(t)} attempts`);return}const r=s==="linear"?a*t:a*2**(t-1),u=o===void 0?r:Math.min(r,o),l=Date.now()+u,d={...e,attempts:t,scheduledFor:l};await this.state.storage.put(`${m}${e.id}`,d),await this.state.storage.put(`id:${e.id}`,d),await this.state.storage.put(i.indexKey(l,e.id),e.id)}async loadPool(e,t){const s=await this.state.storage.get(`${p}${e}`);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(t,1)}}async savePool(e,t){await this.state.storage.put(`${p}${e}`,t)}async requeuePooled(e){const t=Date.now()+1e3,s={...e,scheduledFor:t};await this.state.storage.put(`id:${e.id}`,s),await this.state.storage.put(i.indexKey(t,e.id),e.id)}async handleComplete(e){const t=await e.json().catch(()=>{}),s=typeof t?.pool=="string"&&t.pool.length>0?t.pool:void 0,a=typeof t?.id=="string"&&t.id.length>0?t.id:void 0;if(s===void 0)return i.error(400,"INVALID_INPUT","pool is required");const n=await this.loadPool(s),o=a===void 0?i.releaseFirstSlot(n):i.releaseSlot(n,a);return await this.savePool(s,o),await this.armAlarmIfEarlier(Date.now()),i.json({inFlight:o.inFlight})}async handlePoolStatus(e){const t=e.searchParams.get("name");if(t===null||t.length===0)return i.error(400,"INVALID_INPUT","name is required");const s=await this.loadPool(t);let a=0;return await this.countHeaders(n=>{n.pool===t&&(a+=1)}),i.json({inFlight:s.inFlight,maxConcurrency:s.maxConcurrency,queued:a})}async handleStatus(){const e=await this.state.storage.list({prefix:p}),t=new Map;await this.countHeaders(r=>{r.pool!==void 0&&t.set(r.pool,(t.get(r.pool)??0)+1)});const s=[];let a=0,n=0;for(const[r,u]of e.entries()){const l=r.slice(p.length),d=Math.max(0,u.inFlight),g=t.get(l)??0;s.push({inFlight:d,maxConcurrency:u.maxConcurrency,name:l,queued:g}),a+=g,n+=d}const o={backlog:a,inFlight:n,pools:s};return i.json(o)}async handleSchedule(e){const t=await e.json().catch(()=>{}),s=i.resolveScheduleTarget(t);if(!t||s===void 0)return i.error(400,"INVALID_INPUT","functionPath or workflow is required");const{functionPath:a,workflow:n}=s;if(typeof t.scheduledFor!="number"||!Number.isInteger(t.scheduledFor)||t.scheduledFor<=0||t.scheduledFor>999999999999999)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 o=typeof t.pool=="string"&&t.pool.length>0?t.pool:void 0,r=typeof t.instanceName=="string"&&t.instanceName.length>0?t.instanceName:void 0,u=i.normalizeRetry(t.retry),l=I(),d={args:t.args??{},enqueuedAt:Date.now(),id:l,...a===void 0?{}:{functionPath:a},...r===void 0?{}:{instanceName:r},...o===void 0?{}:{pool:o},...u===void 0?{}:{retry:u},scheduledFor:t.scheduledFor,shardKey:t.shardKey,...n===void 0?{}:{workflow:n}};if(o!==void 0){const g=await this.loadPool(o,t.maxConcurrency);await this.savePool(o,{inFlight:g.inFlight,...g.inFlightIds===void 0?{}:{inFlightIds:g.inFlightIds},maxConcurrency:i.normalizeConcurrency(t.maxConcurrency,g.maxConcurrency)})}return await this.state.storage.put(`id:${l}`,d),await this.state.storage.put(i.indexKey(d.scheduledFor,l),l),await this.armAlarmIfEarlier(d.scheduledFor),await this.broadcastChange(),i.json({id:l,scheduledFor:d.scheduledFor})}async handleCancel(e){const t=await e.json().catch(()=>{});if(!t?.id)return i.error(400,"INVALID_INPUT","id is required");const s=await this.state.storage.get(`id:${t.id}`);return s?(await this.removeRecord(s),await this.rescheduleAlarm(),await this.broadcastChange(),i.json({cancelled:!0})):i.json({cancelled:!1})}async handleList(){const{records:e,truncated:t}=await this.listRecords();return i.json({records:e,truncated:t})}async handleDeadList(){const e=await this.state.storage.list({prefix:f});return i.json({records:[...e.values()]})}async handleDeadRetry(e){const t=await e.json().catch(()=>{});if(typeof t?.id!="string"||t.id.length===0)return i.error(400,"INVALID_INPUT","id is required");const s=await this.state.storage.get(`${f}${t.id}`);if(s===void 0)return i.json({retried:!1});const a=Date.now(),n={...s,attempts:0,scheduledFor:a};return await this.state.storage.put(`id:${s.id}`,n),await this.state.storage.put(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(e){const t=await e.json().catch(()=>{});if(typeof t?.id!="string"||t.id.length===0)return i.error(400,"INVALID_INPUT","id is required");const s=await this.state.storage.delete(`${f}${t.id}`);return i.json({removed:!!s})}async handleGet(e){const t=e.searchParams.get("id");if(t===null||t.length===0)return i.error(400,"INVALID_INPUT","id is required");const s=await this.state.storage.get(`id:${t}`);return i.json(s===void 0?{}:{record:s})}async removeRecord(e){await this.state.storage.delete([`id:${e.id}`,i.indexKey(e.scheduledFor,e.id),`${m}${e.id}`])}async armAlarmIfEarlier(e){const t=await this.state.storage.getAlarm();(t===null||e<t)&&await this.state.storage.setAlarm(e)}async rescheduleAlarm(){const t=(await this.state.storage.list({limit:1,prefix:"t:"})).entries().next();if(t.done){await this.state.storage.deleteAlarm();return}const[s]=t.value,a=Number.parseInt(s.slice(2,s.indexOf(":",2)),10);Number.isFinite(a)&&await this.state.storage.setAlarm(a)}}export{F as MAX_RETRY_ATTEMPTS,P as RETRY_BASE_DELAY_MS,i as SchedulerDO};
@@ -0,0 +1 @@
1
+ import{LunoraError as i}from"@lunora/errors";const O=(t,e,n)=>{if(e===void 0)return{dispose:()=>{},signal:t};const o=new AbortController,a=setTimeout(()=>{o.abort(n())},e);return{dispose:()=>{clearTimeout(a)},signal:o.signal}},c="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_",_=t=>{let e="",n=0;const o=t.length-2;for(;n<o;n+=3){const r=t[n]<<16|t[n+1]<<8|t[n+2];e+=c.charAt(r>>18&63)+c.charAt(r>>12&63)+c.charAt(r>>6&63)+c.charAt(r&63)}const a=t.length-n;if(a===1){const r=t[n]<<16;e+=c.charAt(r>>18&63)+c.charAt(r>>12&63)}else if(a===2){const r=t[n]<<16|t[n+1]<<8;e+=c.charAt(r>>18&63)+c.charAt(r>>12&63)+c.charAt(r>>6&63)}return e};new TextDecoder;const I=new TextEncoder,N="=",b=t=>{for(let e=0;e<t.length;e+=1)if(t.charCodeAt(e)>255)return!1;return!0},E=t=>_(I.encode(JSON.stringify(t))),L=t=>!t.startsWith(N)&&b(t)?t:`${N}${_(I.encode(t))}`,$="/_lunora/scheduler/dispatch",S=3e4,v=t=>{let e=t.length;for(;e>0&&t[e-1]==="/";)e-=1;return t.slice(0,e)},U=Symbol("lunoraDispatchFailure"),D=Symbol("lunoraDispatchMessageId"),w=(t,e)=>(Object.defineProperty(t,U,{value:!0}),e!==void 0&&Object.defineProperty(t,D,{value:e}),t),K=(t,e,n,o)=>{try{const a=JSON.parse(n)?.error;if(typeof a=="object"&&a!==null&&typeof a.code=="string"){const{code:r,data:s,message:u}=a;return w(new i(r,typeof u=="string"?u:void 0,{data:s,status:e}),o)}}catch{}return w(new i("INTERNAL",`${t}: function dispatch failed (${String(e)}): ${n}`,{status:e}),o)},M=(t,e,n)=>new i("INTERNAL",`${t}: function dispatch to "${e}" timed out after ${String(n)}ms`,{status:503}),x=t=>{const{label:e}=t,n=globalThis.fetch,o=t.fetchImpl??(typeof n=="function"?n.bind(globalThis):void 0);return async(a,r,s={})=>{if(typeof o!="function")throw new TypeError(`${e}: no fetch implementation available — pass fetchImpl or run on a platform with global fetch`);const u=t.env.LUNORA_ORIGIN_URL;if(typeof u!="string"||u.length===0)throw new i("INTERNAL",`${e}: \`LUNORA_ORIGIN_URL\` must be set on the Worker env so a handler can call back into Lunora functions`);const f=t.env.LUNORA_ADMIN_TOKEN;if(typeof f!="string"||f.length===0)throw new i("INTERNAL",`${e}: \`LUNORA_ADMIN_TOKEN\` must be set on the Worker env to authenticate function dispatch`);const R=`${v(u)}${$}`,y={authorization:`Bearer ${f}`,"content-type":"application/json"};t.identity?.userId!==void 0&&(y["x-lunora-userid"]=L(t.identity.userId)),t.identity?.claims!==void 0&&(y["x-lunora-identity"]=E(t.identity.claims));const g=s.timeoutMs??S,m=O(void 0,g,()=>new DOMException(`dispatch timed out after ${String(g)}ms`,"TimeoutError")),p=d=>{throw d instanceof Error&&d.name==="TimeoutError"?M(e,a.__lunoraRef,g):d};let l;try{try{l=await o(R,{body:JSON.stringify({args:r??{},functionPath:a.__lunoraRef,id:s.dedupId,shardKey:s.shardKey}),headers:y,method:"POST",signal:m.signal})}catch(h){return p(h)}if(!l.ok){let h;try{h=await l.text()}catch(T){return p(T)}throw K(e,l.status,h,s.messageId)}let d;try{d=await l.text()}catch(h){return p(h)}if(d.length===0)return;try{return JSON.parse(d)}catch{throw new i("INTERNAL",`${e}: function dispatch returned a non-JSON body (${String(l.status)}): ${d}`,{status:l.status})}}finally{m.dispose()}}},A=100,P=3e5,k=t=>{if(!t.queue)throw new i("INTERNAL","@lunora/scheduler: `queue` (a Cloudflare Queue binding) is required");return{enqueue:async(o,a,r={})=>{const s={args:a,functionPath:o.__lunoraRef,shardKey:r.shardKey},u=r.delaySeconds===void 0?void 0:{delaySeconds:r.delaySeconds};await t.queue.send(s,u)},enqueueBatch:async(o,a)=>{if(o.length>A)throw new i("VALIDATION_ERROR",`@lunora/scheduler: enqueueBatch exceeds ${String(A)} (got ${String(o.length)}) — split across calls`);const r=o.map(s=>({body:{args:s.args,functionPath:s.ref.__lunoraRef,shardKey:s.shardKey}}));await t.queue.sendBatch(r,a)}}},q=t=>typeof t=="object"&&t!==null&&typeof t.functionPath=="string",B=t=>async e=>{await Promise.all(e.messages.map(async n=>{try{if(!q(n.body))throw new i("INTERNAL","@lunora/scheduler: queue message body is not a QueueJob (missing functionPath)");await t.dispatch(n.body,n.id),n.ack()}catch{n.retry()}}))},C=t=>{const e=x({env:{LUNORA_ADMIN_TOKEN:t.adminToken,LUNORA_ORIGIN_URL:t.originUrl},fetchImpl:t.fetchImpl,label:"@lunora/scheduler"}),n=t.timeoutMs??P;return async(o,a)=>{await e({__lunoraRef:o.functionPath},o.args,{messageId:a,shardKey:o.shardKey,timeoutMs:n})}};export{B as createQueueConsumer,k as createQueueWorkpool,C as httpDispatcher};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lunora/scheduler",
3
- "version": "1.0.0-alpha.39",
3
+ "version": "1.0.0-alpha.40",
4
4
  "description": "Scheduling for Lunora: runAfter / runAt and Cron Triggers via SchedulerDO",
5
5
  "keywords": [
6
6
  "cloudflare",
@@ -1 +0,0 @@
1
- const m=(u,t=200,e)=>Response.json(u,{headers:{"content-type":"application/json",...e},status:t}),R="lunora-ping",I="lunora-pong";const f="retry:",g="dead:",p="pool:";const A=5,F=3e4;const y=u=>String(u).padStart(15,"0"),w=u=>{let t="";for(const e of u)t+=String.fromCodePoint(e);return btoa(t).replaceAll("+","-").replaceAll("/","_").replaceAll("=","")},E=()=>w(crypto.getRandomValues(new Uint8Array(12)));class n{static indexKey(t,e){return`t:${y(t)}:${e}`}static json(t,e=200){return m(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:5,a=typeof e?.baseMs=="number"&&Number.isFinite(e.baseMs)&&e.baseMs>=0?e.baseMs:3e4,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;constructor(t,e){this.state=t,this.env=e,this.armWebSocketKeepalive()}async fetch(t){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();case"GET /get":return this.handleGet(e);case"GET /list":return this.handleList();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 m({error:{code:"NOT_FOUND"}},404)}async alarm(){const t=Date.now(),e=[],s=await this.state.storage.list({end:`t:${y(t)}:~`,limit:100,prefix:"t:"});for(const[a,i]of s.entries()){const o=Number.parseInt(a.slice(2,a.indexOf(":",2)),10);if(Number.isFinite(o)&&o<=t){const r=await this.state.storage.get(`id:${i}`);r?e.push(r):await this.state.storage.delete(a)}}try{for(const a of e)await this.drainRecordGuarded(a)}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);return 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}`),(await fetch(`${e}/_lunora/scheduler/dispatch`,{body:s,headers:a,method:"POST"})).ok}catch{return!1}}armWebSocketKeepalive(){const t=this.state.setWebSocketAutoResponse;typeof t!="function"||typeof WebSocketRequestResponsePair>"u"||t.call(this.state,new WebSocketRequestResponsePair(R,I))}async drainRecordGuarded(t){try{await this.state.storage.delete(n.indexKey(t.scheduledFor,t.id)),await this.drainRecord(t)}catch{try{await this.state.storage.put(n.indexKey(t.scheduledFor,t.id),t.id)}catch{}}}async drainRecord(t){if(!await this.reservePoolSlot(t))return!1;const s=await this.dispatch(t);if(!s&&t.pool!==void 0){const a=await this.loadPool(t.pool),i=n.releaseSlot(a,t.id);await this.savePool(t.pool,i)}if(s){try{await this.state.storage.delete([`id:${t.id}`,`${f}${t.id}`])}catch{}return!0}return await this.recordRetry(t),!1}async reservePoolSlot(t){if(t.pool===void 0)return!0;const e=await this.loadPool(t.pool);if(e.inFlight>=e.maxConcurrency)return await this.requeuePooled(t),!1;const s=e.inFlightIds??[];return s.includes(t.id)||s.push(t.id),e.inFlightIds=s,e.inFlight=s.length,await this.savePool(t.pool,e),!0}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.listRecords();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.listRecords(),a=JSON.stringify({records:e,truncated:s,type:"jobs"});for(const i of t)try{i.send(a)}catch{}}async listRecords(t=100){const s=[...(await this.state.storage.list({limit:t+1,prefix:"id:"})).values()],a=s.length>t;return{records:a?s.slice(0,t):s,truncated:a}}async countHeaders(t,e=100){let s;for(;;){const a=await this.state.storage.list(s===void 0?{limit:e,prefix:"id:"}:{limit:e,prefix:"id:",startAfter:s});if(a.size===0)break;for(const o of a.values())t(o);if(s=[...a.keys()].at(-1),a.size<e)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 w(new Uint8Array(i))}async recordRetry(t){const e=(t.attempts??0)+1,{backoff:s,baseMs:a,maxAttempts:i,maxMs:o}=n.resolveRetry(t);if(e>i){await this.state.storage.put(`${g}${t.id}`,{...t,attempts:e}),await this.state.storage.delete([`${f}${t.id}`,`id:${t.id}`]),console.warn(`@lunora/scheduler: job "${t.id}" (${t.functionPath??t.workflow??"unknown"}) parked in dead-letter after ${String(e)} attempts`);return}const r=s==="linear"?a*e:a*2**(e-1),d=o===void 0?r:Math.min(r,o),c=Date.now()+d,l={...t,attempts:e,scheduledFor:c};await this.state.storage.put(`${f}${t.id}`,l),await this.state.storage.put(`id:${t.id}`,l),await this.state.storage.put(n.indexKey(c,t.id),t.id)}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:n.normalizeConcurrency(e,1)}}async savePool(t,e){await this.state.storage.put(`${p}${t}`,e)}async requeuePooled(t){const e=Date.now()+1e3,s={...t,scheduledFor:e};await this.state.storage.put(`id:${t.id}`,s),await this.state.storage.put(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.loadPool(s),o=a===void 0?n.releaseFirstSlot(i):n.releaseSlot(i,a);return await this.savePool(s,o),await this.armAlarmIfEarlier(Date.now()),n.json({inFlight:o.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.countHeaders(i=>{i.pool===e&&(a+=1)}),n.json({inFlight:s.inFlight,maxConcurrency:s.maxConcurrency,queued:a})}async handleStatus(){const t=await this.state.storage.list({prefix:p}),e=new Map;await this.countHeaders(r=>{r.pool!==void 0&&e.set(r.pool,(e.get(r.pool)??0)+1)});const s=[];let a=0,i=0;for(const[r,d]of t.entries()){const c=r.slice(p.length),l=Math.max(0,d.inFlight),h=e.get(c)??0;s.push({inFlight:l,maxConcurrency:d.maxConcurrency,name:c,queued:h}),a+=h,i+=l}const o={backlog:a,inFlight:i,pools:s};return n.json(o)}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"||!Number.isInteger(e.scheduledFor)||e.scheduledFor<=0||e.scheduledFor>999999999999999)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),c=E(),l={args:e.args??{},enqueuedAt:Date.now(),id:c,...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}};if(o!==void 0){const h=await this.loadPool(o,e.maxConcurrency);await this.savePool(o,{inFlight:h.inFlight,...h.inFlightIds===void 0?{}:{inFlightIds:h.inFlightIds},maxConcurrency:n.normalizeConcurrency(e.maxConcurrency,h.maxConcurrency)})}return await this.state.storage.put(`id:${c}`,l),await this.state.storage.put(n.indexKey(l.scheduledFor,c),c),await this.armAlarmIfEarlier(l.scheduledFor),await this.broadcastChange(),n.json({id:c,scheduledFor:l.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(`id:${e.id}`);return s?(await this.removeRecord(s),await this.rescheduleAlarm(),await this.broadcastChange(),n.json({cancelled:!0})):n.json({cancelled:!1})}async handleList(){const{records:t,truncated:e}=await this.listRecords();return n.json({records:t,truncated:e})}async handleDeadList(){const t=await this.state.storage.list({prefix:g});return n.json({records:[...t.values()]})}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(`id:${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(`id:${e}`);return n.json(s===void 0?{}:{record:s})}async removeRecord(t){await this.state.storage.delete([`id:${t.id}`,n.indexKey(t.scheduledFor,t.id),`${f}${t.id}`])}async armAlarmIfEarlier(t){const e=await this.state.storage.getAlarm();(e===null||t<e)&&await this.state.storage.setAlarm(t)}async rescheduleAlarm(){const e=(await this.state.storage.list({limit:1,prefix:"t:"})).entries().next();if(e.done){await this.state.storage.deleteAlarm();return}const[s]=e.value,a=Number.parseInt(s.slice(2,s.indexOf(":",2)),10);Number.isFinite(a)&&await this.state.storage.setAlarm(a)}}export{A as MAX_RETRY_ATTEMPTS,F as RETRY_BASE_DELAY_MS,n as SchedulerDO};
@@ -1 +0,0 @@
1
- import{LunoraError as c}from"@lunora/errors";const A=(e,t,n)=>{if(t===void 0)return{dispose:()=>{},signal:e};const r=new AbortController,o=setTimeout(()=>{r.abort(n())},t);return{dispose:()=>{clearTimeout(o)},signal:r.signal}},b=e=>{let t="";for(let n=0;n<e.length;n+=32768)t+=String.fromCharCode(...e.subarray(n,n+32768));return btoa(t)},_=e=>b(e).replaceAll("+","-").replaceAll("/","_").replace(/=+$/,"");new TextDecoder;const I=new TextEncoder,m="=",O=e=>{for(let t=0;t<e.length;t+=1)if(e.charCodeAt(t)>255)return!1;return!0},E=e=>_(I.encode(JSON.stringify(e))),$=e=>!e.startsWith(m)&&O(e)?e:`${m}${_(I.encode(e))}`,L="/_lunora/scheduler/dispatch",S=3e4,v=e=>{let t=e.length;for(;t>0&&e[t-1]==="/";)t-=1;return e.slice(0,t)},U=Symbol("lunoraDispatchFailure"),D=Symbol("lunoraDispatchMessageId"),N=(e,t)=>(Object.defineProperty(e,U,{value:!0}),t!==void 0&&Object.defineProperty(e,D,{value:t}),e),K=(e,t,n,r)=>{try{const o=JSON.parse(n)?.error;if(typeof o=="object"&&o!==null&&typeof o.code=="string"){const{code:s,data:a,message:i}=o;return N(new c(s,typeof i=="string"?i:void 0,{data:a,status:t}),r)}}catch{}return N(new c("INTERNAL",`${e}: function dispatch failed (${String(t)}): ${n}`,{status:t}),r)},M=(e,t,n)=>new c("INTERNAL",`${e}: function dispatch to "${t}" timed out after ${String(n)}ms`,{status:503}),P=e=>{const{label:t}=e,n=globalThis.fetch,r=e.fetchImpl??(typeof n=="function"?n.bind(globalThis):void 0);return async(o,s,a={})=>{if(typeof r!="function")throw new TypeError(`${t}: no fetch implementation available — pass fetchImpl or run on a platform with global fetch`);const i=e.env.LUNORA_ORIGIN_URL;if(typeof i!="string"||i.length===0)throw new c("INTERNAL",`${t}: \`LUNORA_ORIGIN_URL\` must be set on the Worker env so a handler can call back into Lunora functions`);const h=e.env.LUNORA_ADMIN_TOKEN;if(typeof h!="string"||h.length===0)throw new c("INTERNAL",`${t}: \`LUNORA_ADMIN_TOKEN\` must be set on the Worker env to authenticate function dispatch`);const R=`${v(i)}${L}`,f={authorization:`Bearer ${h}`,"content-type":"application/json"};e.identity?.userId!==void 0&&(f["x-lunora-userid"]=$(e.identity.userId)),e.identity?.claims!==void 0&&(f["x-lunora-identity"]=E(e.identity.claims));const y=a.timeoutMs??S,g=A(void 0,y,()=>new DOMException(`dispatch timed out after ${String(y)}ms`,"TimeoutError")),p=u=>{throw u instanceof Error&&u.name==="TimeoutError"?M(t,o.__lunoraRef,y):u};let l;try{try{l=await r(R,{body:JSON.stringify({args:s??{},functionPath:o.__lunoraRef,id:a.dedupId,shardKey:a.shardKey}),headers:f,method:"POST",signal:g.signal})}catch(d){return p(d)}if(!l.ok){let d;try{d=await l.text()}catch(T){return p(T)}throw K(t,l.status,d,a.messageId)}let u;try{u=await l.text()}catch(d){return p(d)}if(u.length===0)return;try{return JSON.parse(u)}catch{throw new c("INTERNAL",`${t}: function dispatch returned a non-JSON body (${String(l.status)}): ${u}`,{status:l.status})}}finally{g.dispose()}}},w=100,x=3e5,k=e=>{if(!e.queue)throw new c("INTERNAL","@lunora/scheduler: `queue` (a Cloudflare Queue binding) is required");return{enqueue:async(r,o,s={})=>{const a={args:o,functionPath:r.__lunoraRef,shardKey:s.shardKey},i=s.delaySeconds===void 0?void 0:{delaySeconds:s.delaySeconds};await e.queue.send(a,i)},enqueueBatch:async(r,o)=>{if(r.length>w)throw new c("VALIDATION_ERROR",`@lunora/scheduler: enqueueBatch exceeds ${String(w)} (got ${String(r.length)}) — split across calls`);const s=r.map(a=>({body:{args:a.args,functionPath:a.ref.__lunoraRef,shardKey:a.shardKey}}));await e.queue.sendBatch(s,o)}}},q=e=>typeof e=="object"&&e!==null&&typeof e.functionPath=="string",B=e=>async t=>{await Promise.all(t.messages.map(async n=>{try{if(!q(n.body))throw new c("INTERNAL","@lunora/scheduler: queue message body is not a QueueJob (missing functionPath)");await e.dispatch(n.body,n.id),n.ack()}catch{n.retry()}}))},C=e=>{const t=P({env:{LUNORA_ADMIN_TOKEN:e.adminToken,LUNORA_ORIGIN_URL:e.originUrl},fetchImpl:e.fetchImpl,label:"@lunora/scheduler"}),n=e.timeoutMs??x;return async(r,o)=>{await t({__lunoraRef:r.functionPath},r.args,{messageId:o,shardKey:r.shardKey,timeoutMs:n})}};export{B as createQueueConsumer,k as createQueueWorkpool,C as httpDispatcher};