lonnymq 0.0.14 → 0.0.16
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.cjs +48 -43
- package/dist/index.d.ts +1 -2
- package/dist/index.js +46 -41
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
var{defineProperty:h,getOwnPropertyNames:W,getOwnPropertyDescriptor:Y}=Object,X=Object.prototype.hasOwnProperty;var U=new WeakMap,J=(e)=>{var t=U.get(e),n;if(t)return t;if(t=h({},"__esModule",{value:!0}),e&&typeof e==="object"||typeof e==="function")W(e).map((E)=>!X.call(t,E)&&h(t,E,{get:()=>e[E],enumerable:!(n=Y(e,E))||n.enumerable}));return U.set(e,t),t};var K=(e,t)=>{for(var n in t)h(e,n,{get:t[n],enumerable:!0,configurable:!0,set:(E)=>t[n]=()=>E})};var
|
|
1
|
+
var{defineProperty:h,getOwnPropertyNames:W,getOwnPropertyDescriptor:Y}=Object,X=Object.prototype.hasOwnProperty;var U=new WeakMap,J=(e)=>{var t=U.get(e),n;if(t)return t;if(t=h({},"__esModule",{value:!0}),e&&typeof e==="object"||typeof e==="function")W(e).map((E)=>!X.call(t,E)&&h(t,E,{get:()=>e[E],enumerable:!(n=Y(e,E))||n.enumerable}));return U.set(e,t),t};var K=(e,t)=>{for(var n in t)h(e,n,{get:t[n],enumerable:!0,configurable:!0,set:(E)=>t[n]=()=>E})};var Te={};K(Te,{queueEventDecode:()=>x,Queue:()=>v,MessageHeartbeatCommand:()=>S,MessageDequeueCommand:()=>i,MessageDeleteCommand:()=>u,MessageDeferCommand:()=>o,MessageCreateCommand:()=>c,ChannelPolicySetCommand:()=>l,ChannelPolicyClearCommand:()=>m});module.exports=J(Te);var r=(e)=>({nodeType:"VALUE",value:e}),s=(e)=>({nodeType:"REF",value:e}),j=(e)=>({nodeType:"RAW",value:e}),Z=(e)=>{return`'${e.replace(/'/g,"''")}'`},ee=(e)=>{if(e===null)return"NULL";else if(typeof e==="string")return Z(e);else if(typeof e==="number")return e.toString();else if(typeof e==="boolean")return e?"TRUE":"FALSE";else if(e instanceof Date)return`'${e.toISOString()}'`;else if(typeof e==="bigint")return e.toString();else throw new Error(`Unsupported value type: ${typeof e}`)},te=(e)=>{return`"${e.replace(/"/g,'""')}"`},se=(e)=>{if(e.nodeType==="VALUE")return ee(e.value);else if(e.nodeType==="REF")return te(e.value);else if(e.nodeType==="RAW")return e.value;else throw new Error("Unsupported SQL node type")};var a=(e,...t)=>{let n=[];for(let E=0;E<e.length;E+=1)if(n.push(e[E]),E<t.length)n.push(se(t[E]));return j(n.join(""))};class m{schema;channelName;createdAt;constructor(e){this.schema=e.schema,this.channelName=e.channelName,this.createdAt=new Date}sortKeyGet(){return JSON.stringify([this.channelName,null,this.createdAt.toISOString()])}async execute(e){await e.query(a`
|
|
2
2
|
SELECT 1 FROM ${s(this.schema)}."channel_policy_clear"(
|
|
3
3
|
$1
|
|
4
4
|
)
|
|
5
|
-
`.value,[this.channelName])}}class
|
|
5
|
+
`.value,[this.channelName])}}class l{schema;channelName;maxSize;maxConcurrency;releaseIntervalMs;createdAt;constructor(e){this.schema=e.schema,this.channelName=e.channelName;let t=e.maxConcurrency??null;this.maxConcurrency=t!==null?Math.max(0,t):null;let n=e.maxSize??null;this.maxSize=n!==null?Math.max(0,n):null;let E=e.releaseIntervalMs??null;this.releaseIntervalMs=E!==null?Math.max(0,E):null,this.createdAt=new Date}sortKeyGet(){return JSON.stringify([this.channelName,null,this.createdAt.toISOString()])}async execute(e){await e.query(a`
|
|
6
6
|
SELECT 1 FROM ${s(this.schema)}."channel_policy_set"(
|
|
7
7
|
$1,
|
|
8
8
|
$2::INTEGER,
|
|
@@ -42,9 +42,9 @@ var{defineProperty:h,getOwnPropertyNames:W,getOwnPropertyDescriptor:Y}=Object,X=
|
|
|
42
42
|
$1,
|
|
43
43
|
$2::BIGINT
|
|
44
44
|
)
|
|
45
|
-
`.value,[this.id,this.numAttempts]).then((n)=>n.rows[0]);if(t.result_code===0)return{resultType:"MESSAGE_NOT_FOUND"};else if(t.result_code===1)return{resultType:"MESSAGE_STATE_INVALID"};else if(t.result_code===2)return{resultType:"
|
|
45
|
+
`.value,[this.id,this.numAttempts]).then((n)=>n.rows[0]);if(t.result_code===0)return{resultType:"MESSAGE_NOT_FOUND"};else if(t.result_code===1)return{resultType:"MESSAGE_STATE_INVALID"};else if(t.result_code===2)return{resultType:"MESSAGE_HEARTBEATED"};else throw new Error("Unexpected result")}}var F=(e)=>{let t=e.split(`
|
|
46
46
|
`),n=Number.MAX_SAFE_INTEGER;for(let E of t){if(E.trim().length===0)continue;let T=E.search(/\S/);n=Math.min(n,T)}return t.map((E)=>E.slice(n)).join(`
|
|
47
|
-
`).trim()};var g=require("path"),__filename="/home/runner/work/lonnymq/lonnymq/src/core/path.ts",
|
|
47
|
+
`).trim()};var g=require("path"),__filename="/home/runner/work/lonnymq/lonnymq/src/core/path.ts",le=g.dirname(g.dirname(__filename)),ce=new RegExp(`^${le}/`),_=(e)=>{return e.replace(ce,"")};var __filename="/home/runner/work/lonnymq/lonnymq/src/migration/00-table-channel-policy.ts",q={name:_(__filename),sql:(e)=>{return[a`
|
|
48
48
|
CREATE TABLE ${s(e.schema)}."channel_policy" (
|
|
49
49
|
"id" UUID NOT NULL DEFAULT GEN_RANDOM_UUID(),
|
|
50
50
|
"name" TEXT NOT NULL,
|
|
@@ -130,7 +130,6 @@ var{defineProperty:h,getOwnPropertyNames:W,getOwnPropertyDescriptor:Y}=Object,X=
|
|
|
130
130
|
) AS $$
|
|
131
131
|
DECLARE
|
|
132
132
|
v_now TIMESTAMP;
|
|
133
|
-
v_active_next_at TIMESTAMP;
|
|
134
133
|
v_channel_policy RECORD;
|
|
135
134
|
v_channel_state RECORD;
|
|
136
135
|
v_message RECORD;
|
|
@@ -165,7 +164,7 @@ var{defineProperty:h,getOwnPropertyNames:W,getOwnPropertyDescriptor:Y}=Object,X=
|
|
|
165
164
|
v_now,
|
|
166
165
|
v_now
|
|
167
166
|
) ON CONFLICT ("name")
|
|
168
|
-
DO UPDATE SET "
|
|
167
|
+
DO UPDATE SET "name" = EXCLUDED."name"
|
|
169
168
|
RETURNING
|
|
170
169
|
"id",
|
|
171
170
|
"current_size",
|
|
@@ -259,7 +258,43 @@ var{defineProperty:h,getOwnPropertyNames:W,getOwnPropertyDescriptor:Y}=Object,X=
|
|
|
259
258
|
RETURN;
|
|
260
259
|
END;
|
|
261
260
|
$$ LANGUAGE plpgsql;
|
|
262
|
-
`]}};var __filename="/home/runner/work/lonnymq/lonnymq/src/migration/04-function-message-dequeue.ts",
|
|
261
|
+
`]}};var __filename="/home/runner/work/lonnymq/lonnymq/src/migration/04-function-message-dequeue.ts",oe=(e)=>a`
|
|
262
|
+
SELECT
|
|
263
|
+
"message"."id",
|
|
264
|
+
"message"."name",
|
|
265
|
+
"message"."state",
|
|
266
|
+
"message"."content",
|
|
267
|
+
"message"."channel_name",
|
|
268
|
+
"message"."lock_ms",
|
|
269
|
+
"message"."unlock_at",
|
|
270
|
+
"message"."num_attempts"
|
|
271
|
+
FROM ${s(e.schema)}."message"
|
|
272
|
+
WHERE "is_locked"
|
|
273
|
+
AND "unlock_at" <= ${e.now}
|
|
274
|
+
ORDER BY "unlock_at" ASC
|
|
275
|
+
`,ie=(e)=>a`
|
|
276
|
+
SELECT
|
|
277
|
+
"channel_state"."id",
|
|
278
|
+
"channel_state"."name",
|
|
279
|
+
"channel_state"."release_interval_ms",
|
|
280
|
+
"channel_state"."message_id",
|
|
281
|
+
"channel_state"."active_next_at",
|
|
282
|
+
"channel_state"."active_prev_at",
|
|
283
|
+
"channel_state"."current_concurrency"
|
|
284
|
+
FROM ${s(e.schema)}."channel_state"
|
|
285
|
+
WHERE "message_id" IS NOT NULL
|
|
286
|
+
AND ("max_concurrency" IS NULL OR "current_concurrency" < "max_concurrency")
|
|
287
|
+
ORDER BY "active_next_at" ASC
|
|
288
|
+
`,ue=(e)=>a`
|
|
289
|
+
SELECT
|
|
290
|
+
"message"."id",
|
|
291
|
+
"message"."dequeue_at",
|
|
292
|
+
"message"."seq_no"
|
|
293
|
+
FROM ${s(e.schema)}."message"
|
|
294
|
+
WHERE NOT "is_locked"
|
|
295
|
+
AND "channel_name" = ${e.channelName}
|
|
296
|
+
ORDER BY "dequeue_at" ASC, "seq_no" ASC
|
|
297
|
+
`,P={name:_(__filename),sql:(e)=>{let t=oe({now:a`v_now`,schema:e.schema}),n=ue({channelName:a`v_channel_state."name"`,schema:e.schema}),E=ie({schema:e.schema});return[a`
|
|
263
298
|
CREATE FUNCTION ${s(e.schema)}."message_dequeue" ()
|
|
264
299
|
RETURNS TABLE (
|
|
265
300
|
result_code INTEGER,
|
|
@@ -276,19 +311,7 @@ var{defineProperty:h,getOwnPropertyNames:W,getOwnPropertyDescriptor:Y}=Object,X=
|
|
|
276
311
|
BEGIN
|
|
277
312
|
v_now := NOW();
|
|
278
313
|
|
|
279
|
-
|
|
280
|
-
"message"."id",
|
|
281
|
-
"message"."name",
|
|
282
|
-
"message"."state",
|
|
283
|
-
"message"."content",
|
|
284
|
-
"message"."channel_name",
|
|
285
|
-
"message"."lock_ms",
|
|
286
|
-
"message"."unlock_at",
|
|
287
|
-
"message"."num_attempts"
|
|
288
|
-
FROM ${s(e.schema)}."message"
|
|
289
|
-
WHERE "is_locked"
|
|
290
|
-
AND "unlock_at" <= v_now
|
|
291
|
-
ORDER BY "unlock_at" ASC
|
|
314
|
+
${t}
|
|
292
315
|
FOR UPDATE
|
|
293
316
|
SKIP LOCKED
|
|
294
317
|
LIMIT 1
|
|
@@ -314,18 +337,7 @@ var{defineProperty:h,getOwnPropertyNames:W,getOwnPropertyDescriptor:Y}=Object,X=
|
|
|
314
337
|
RETURN;
|
|
315
338
|
END IF;
|
|
316
339
|
|
|
317
|
-
|
|
318
|
-
"channel_state"."id",
|
|
319
|
-
"channel_state"."name",
|
|
320
|
-
"channel_state"."release_interval_ms",
|
|
321
|
-
"channel_state"."message_id",
|
|
322
|
-
"channel_state"."active_next_at",
|
|
323
|
-
"channel_state"."active_prev_at",
|
|
324
|
-
"channel_state"."current_concurrency"
|
|
325
|
-
FROM ${s(e.schema)}."channel_state"
|
|
326
|
-
WHERE "message_id" IS NOT NULL
|
|
327
|
-
AND ("max_concurrency" IS NULL OR "current_concurrency" < "max_concurrency")
|
|
328
|
-
ORDER BY "active_next_at" ASC
|
|
340
|
+
${E}
|
|
329
341
|
FOR UPDATE
|
|
330
342
|
SKIP LOCKED
|
|
331
343
|
LIMIT 1
|
|
@@ -369,14 +381,7 @@ var{defineProperty:h,getOwnPropertyNames:W,getOwnPropertyDescriptor:Y}=Object,X=
|
|
|
369
381
|
"unlock_at" = v_now + (v_message_dequeue."lock_ms" * INTERVAL '1 millisecond')
|
|
370
382
|
WHERE "id" = v_message_dequeue."id";
|
|
371
383
|
|
|
372
|
-
|
|
373
|
-
"message"."id",
|
|
374
|
-
"message"."dequeue_at",
|
|
375
|
-
"message"."seq_no"
|
|
376
|
-
FROM ${s(e.schema)}."message"
|
|
377
|
-
WHERE NOT "is_locked"
|
|
378
|
-
AND "channel_name" = v_message_dequeue."channel_name"
|
|
379
|
-
ORDER BY "dequeue_at" ASC, "seq_no" ASC
|
|
384
|
+
${n}
|
|
380
385
|
LIMIT 1
|
|
381
386
|
INTO v_message_next;
|
|
382
387
|
|
|
@@ -491,7 +496,7 @@ var{defineProperty:h,getOwnPropertyNames:W,getOwnPropertyDescriptor:Y}=Object,X=
|
|
|
491
496
|
RETURN;
|
|
492
497
|
END;
|
|
493
498
|
$$ LANGUAGE plpgsql;
|
|
494
|
-
`]}};var __filename="/home/runner/work/lonnymq/lonnymq/src/migration/06-function-message-defer.ts",
|
|
499
|
+
`]}};var __filename="/home/runner/work/lonnymq/lonnymq/src/migration/06-function-message-defer.ts",w={name:_(__filename),sql:(e)=>{return[a`
|
|
495
500
|
CREATE FUNCTION ${s(e.schema)}."message_defer" (
|
|
496
501
|
p_id UUID,
|
|
497
502
|
p_num_attempts BIGINT,
|
|
@@ -587,7 +592,7 @@ var{defineProperty:h,getOwnPropertyNames:W,getOwnPropertyDescriptor:Y}=Object,X=
|
|
|
587
592
|
RETURN;
|
|
588
593
|
END;
|
|
589
594
|
$$ LANGUAGE plpgsql;
|
|
590
|
-
`]}};var __filename="/home/runner/work/lonnymq/lonnymq/src/migration/07-function-message-heartbeat.ts",
|
|
595
|
+
`]}};var __filename="/home/runner/work/lonnymq/lonnymq/src/migration/07-function-message-heartbeat.ts",H={name:_(__filename),sql:(e)=>{return[a`
|
|
591
596
|
CREATE FUNCTION ${s(e.schema)}."message_heartbeat" (
|
|
592
597
|
p_id UUID,
|
|
593
598
|
p_num_attempts BIGINT
|
|
@@ -696,4 +701,4 @@ var{defineProperty:h,getOwnPropertyNames:W,getOwnPropertyDescriptor:Y}=Object,X=
|
|
|
696
701
|
WHERE "name" = p_name;
|
|
697
702
|
END;
|
|
698
703
|
$$ LANGUAGE plpgsql;
|
|
699
|
-
`]}};class A{schema;channelName;registerFn;constructor(e){this.schema=e.schema,this.channelName=e.channelName,this.registerFn=e.registerFn}create(e){let t=new c({schema:this.schema,channelName:this.channelName,name:e.name,content:e.content,lockMs:e.lockMs,delayMs:e.delayMs}),n=new Promise((E)=>{this.registerFn({sortKey:JSON.stringify([t.channelName,t.name,t.createdAt.toISOString()]),execute:(T)=>t.execute(T).then((z)=>E(z))})});return{messageId:t.id,promise:n}}}class D{schema;channelName;registerFn;constructor(e){this.schema=e.schema,this.channelName=e.channelName,this.registerFn=e.registerFn}set(e){let t=new
|
|
704
|
+
`]}};class A{schema;channelName;registerFn;constructor(e){this.schema=e.schema,this.channelName=e.channelName,this.registerFn=e.registerFn}create(e){let t=new c({schema:this.schema,channelName:this.channelName,name:e.name,content:e.content,lockMs:e.lockMs,delayMs:e.delayMs}),n=new Promise((E)=>{this.registerFn({sortKey:JSON.stringify([t.channelName,t.name,t.createdAt.toISOString()]),execute:(T)=>t.execute(T).then((z)=>E(z))})});return{messageId:t.id,promise:n}}}class D{schema;channelName;registerFn;constructor(e){this.schema=e.schema,this.channelName=e.channelName,this.registerFn=e.registerFn}set(e){let t=new l({schema:this.schema,channelName:this.channelName,maxConcurrency:e.maxConcurrency,maxSize:e.maxSize,releaseIntervalMs:e.releaseIntervalMs});this.registerFn({sortKey:JSON.stringify([t.channelName,null,t.createdAt.toISOString()]),execute:async(n)=>{await t.execute(n)}})}clear(){let e=new m({schema:this.schema,channelName:this.channelName});this.registerFn({sortKey:JSON.stringify([e.channelName,null,e.createdAt.toISOString()]),execute:async(t)=>{await e.execute(t)}})}}class p{policy;message;constructor(e){this.message=new A(e),this.policy=new D(e)}}var de=(e,t)=>{return e.sortKey.localeCompare(t.sortKey)};class M{commands;schema;constructor(e){this.commands=[],this.schema=e.schema}channel(e){return new p({schema:this.schema,channelName:e,registerFn:(t)=>{this.commands.push(t)}})}async execute(e){for(let t of this.commands.sort(de))await t.execute(e.databaseClient)}}class y{schema;channelName;constructor(e){this.schema=e.schema,this.channelName=e.channelName}async create(e){return new c({schema:this.schema,channelName:this.channelName,name:e.name,content:e.content,lockMs:e.lockMs,delayMs:e.delayMs}).execute(e.databaseClient)}}class C{schema;channelName;constructor(e){this.schema=e.schema,this.channelName=e.channelName}set(e){return new l({schema:this.schema,channelName:this.channelName,maxConcurrency:e.maxConcurrency,maxSize:e.maxSize,releaseIntervalMs:e.releaseIntervalMs}).execute(e.databaseClient)}clear(e){return new m({schema:this.schema,channelName:this.channelName}).execute(e.databaseClient)}}class I{policy;message;constructor(e){this.message=new y({schema:e.schema,channelName:e.channelName}),this.policy=new C({schema:e.schema,channelName:e.channelName})}}class L{schema;id;isUnlocked;channelName;name;content;state;numAttempts;constructor(e){this.schema=e.schema,this.id=e.id,this.channelName=e.channelName,this.isUnlocked=e.isUnlocked,this.name=e.name,this.content=e.content,this.state=e.state,this.numAttempts=e.numAttempts}async defer(e){return new o({schema:this.schema,id:this.id,numAttempts:this.numAttempts,delayMs:e.delayMs,state:e.state}).execute(e.databaseClient)}async delete(e){return new u({schema:this.schema,numAttempts:this.numAttempts,id:this.id}).execute(e.databaseClient)}async heartbeat(e){return new o({schema:this.schema,id:this.id,numAttempts:this.numAttempts}).execute(e.databaseClient)}}class v{schema;constructor(e){this.schema=e.schema}async dequeue(e){let n=await new i({schema:this.schema}).execute(e.databaseClient);if(n.resultType==="MESSAGE_DEQUEUED")return{resultType:"MESSAGE_DEQUEUED",message:new L({schema:this.schema,id:n.message.id,channelName:n.message.channelName,name:n.message.name,isUnlocked:n.message.isUnlocked,content:n.message.content,state:n.message.state,numAttempts:n.message.numAttempts})};else return n}channel(e){return new I({schema:this.schema,channelName:e})}batch(){return new M({schema:this.schema})}migrations(e={}){return[q,$,b,B,P,Q,w,H,V,k].sort((t,n)=>t.name.localeCompare(n.name)).flatMap((t)=>t.sql({schema:this.schema,eventChannel:e.eventChannel??null})).map((t)=>F(t.value))}}
|
package/dist/index.d.ts
CHANGED
|
@@ -161,7 +161,7 @@ export type MessageHeartbeatCommandResultStateInvalid = {
|
|
|
161
161
|
resultType: "MESSAGE_STATE_INVALID";
|
|
162
162
|
};
|
|
163
163
|
export type MessageHeartbeatCommandResultMessageHeartbeated = {
|
|
164
|
-
resultType: "
|
|
164
|
+
resultType: "MESSAGE_HEARTBEATED";
|
|
165
165
|
};
|
|
166
166
|
export type MessageHeartbeatCommandResult = MessageHeartbeatCommandResultMessageNotFound | MessageHeartbeatCommandResultStateInvalid | MessageHeartbeatCommandResultMessageHeartbeated;
|
|
167
167
|
export declare class MessageHeartbeatCommand {
|
|
@@ -305,7 +305,6 @@ export declare class QueueMessage {
|
|
|
305
305
|
}): Promise<MessageDeleteCommandResult>;
|
|
306
306
|
heartbeat(params: {
|
|
307
307
|
databaseClient: DatabaseClient;
|
|
308
|
-
delayMs?: number;
|
|
309
308
|
}): Promise<MessageDeferCommandResult>;
|
|
310
309
|
}
|
|
311
310
|
export type MessageDequeueResult = {
|
package/dist/index.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
var r=(e)=>({nodeType:"VALUE",value:e}),s=(e)=>({nodeType:"REF",value:e}),
|
|
1
|
+
var r=(e)=>({nodeType:"VALUE",value:e}),s=(e)=>({nodeType:"REF",value:e}),H=(e)=>({nodeType:"RAW",value:e}),k=(e)=>{return`'${e.replace(/'/g,"''")}'`},V=(e)=>{if(e===null)return"NULL";else if(typeof e==="string")return k(e);else if(typeof e==="number")return e.toString();else if(typeof e==="boolean")return e?"TRUE":"FALSE";else if(e instanceof Date)return`'${e.toISOString()}'`;else if(typeof e==="bigint")return e.toString();else throw new Error(`Unsupported value type: ${typeof e}`)},z=(e)=>{return`"${e.replace(/"/g,'""')}"`},W=(e)=>{if(e.nodeType==="VALUE")return V(e.value);else if(e.nodeType==="REF")return z(e.value);else if(e.nodeType==="RAW")return e.value;else throw new Error("Unsupported SQL node type")};var n=(e,...t)=>{let a=[];for(let _=0;_<e.length;_+=1)if(a.push(e[_]),_<t.length)a.push(W(t[_]));return H(a.join(""))};class m{schema;channelName;createdAt;constructor(e){this.schema=e.schema,this.channelName=e.channelName,this.createdAt=new Date}sortKeyGet(){return JSON.stringify([this.channelName,null,this.createdAt.toISOString()])}async execute(e){await e.query(n`
|
|
2
2
|
SELECT 1 FROM ${s(this.schema)}."channel_policy_clear"(
|
|
3
3
|
$1
|
|
4
4
|
)
|
|
5
|
-
`.value,[this.channelName])}}class
|
|
5
|
+
`.value,[this.channelName])}}class l{schema;channelName;maxSize;maxConcurrency;releaseIntervalMs;createdAt;constructor(e){this.schema=e.schema,this.channelName=e.channelName;let t=e.maxConcurrency??null;this.maxConcurrency=t!==null?Math.max(0,t):null;let a=e.maxSize??null;this.maxSize=a!==null?Math.max(0,a):null;let _=e.releaseIntervalMs??null;this.releaseIntervalMs=_!==null?Math.max(0,_):null,this.createdAt=new Date}sortKeyGet(){return JSON.stringify([this.channelName,null,this.createdAt.toISOString()])}async execute(e){await e.query(n`
|
|
6
6
|
SELECT 1 FROM ${s(this.schema)}."channel_policy_set"(
|
|
7
7
|
$1,
|
|
8
8
|
$2::INTEGER,
|
|
@@ -37,12 +37,12 @@ var r=(e)=>({nodeType:"VALUE",value:e}),s=(e)=>({nodeType:"REF",value:e}),w=(e)=
|
|
|
37
37
|
$3::BIGINT,
|
|
38
38
|
$4
|
|
39
39
|
)
|
|
40
|
-
`.value,[this.id,this.numAttempts,this.delayMs,this.state]).then((a)=>a.rows[0]);if(t.result_code===0)return{resultType:"MESSAGE_NOT_FOUND"};else if(t.result_code===1)return{resultType:"STATE_INVALID"};else if(t.result_code===2)return{resultType:"MESSAGE_DEFERRED"};else throw new Error("Unexpected result")}}class
|
|
40
|
+
`.value,[this.id,this.numAttempts,this.delayMs,this.state]).then((a)=>a.rows[0]);if(t.result_code===0)return{resultType:"MESSAGE_NOT_FOUND"};else if(t.result_code===1)return{resultType:"STATE_INVALID"};else if(t.result_code===2)return{resultType:"MESSAGE_DEFERRED"};else throw new Error("Unexpected result")}}class L{schema;id;numAttempts;constructor(e){this.schema=e.schema,this.numAttempts=e.numAttempts,this.id=e.id}async execute(e){let t=await e.query(n`
|
|
41
41
|
SELECT * FROM ${s(this.schema)}."message_heartbeat"(
|
|
42
42
|
$1,
|
|
43
43
|
$2::BIGINT
|
|
44
44
|
)
|
|
45
|
-
`.value,[this.id,this.numAttempts]).then((a)=>a.rows[0]);if(t.result_code===0)return{resultType:"MESSAGE_NOT_FOUND"};else if(t.result_code===1)return{resultType:"MESSAGE_STATE_INVALID"};else if(t.result_code===2)return{resultType:"
|
|
45
|
+
`.value,[this.id,this.numAttempts]).then((a)=>a.rows[0]);if(t.result_code===0)return{resultType:"MESSAGE_NOT_FOUND"};else if(t.result_code===1)return{resultType:"MESSAGE_STATE_INVALID"};else if(t.result_code===2)return{resultType:"MESSAGE_HEARTBEATED"};else throw new Error("Unexpected result")}}var v=(e)=>{let t=e.split(`
|
|
46
46
|
`),a=Number.MAX_SAFE_INTEGER;for(let _ of t){if(_.trim().length===0)continue;let T=_.search(/\S/);a=Math.min(a,T)}return t.map((_)=>_.slice(a)).join(`
|
|
47
47
|
`).trim()};import{dirname as U}from"path";var __filename="/home/runner/work/lonnymq/lonnymq/src/core/path.ts",se=U(U(__filename)),ne=new RegExp(`^${se}/`),E=(e)=>{return e.replace(ne,"")};var __filename="/home/runner/work/lonnymq/lonnymq/src/migration/00-table-channel-policy.ts",O={name:E(__filename),sql:(e)=>{return[n`
|
|
48
48
|
CREATE TABLE ${s(e.schema)}."channel_policy" (
|
|
@@ -130,7 +130,6 @@ var r=(e)=>({nodeType:"VALUE",value:e}),s=(e)=>({nodeType:"REF",value:e}),w=(e)=
|
|
|
130
130
|
) AS $$
|
|
131
131
|
DECLARE
|
|
132
132
|
v_now TIMESTAMP;
|
|
133
|
-
v_active_next_at TIMESTAMP;
|
|
134
133
|
v_channel_policy RECORD;
|
|
135
134
|
v_channel_state RECORD;
|
|
136
135
|
v_message RECORD;
|
|
@@ -165,7 +164,7 @@ var r=(e)=>({nodeType:"VALUE",value:e}),s=(e)=>({nodeType:"REF",value:e}),w=(e)=
|
|
|
165
164
|
v_now,
|
|
166
165
|
v_now
|
|
167
166
|
) ON CONFLICT ("name")
|
|
168
|
-
DO UPDATE SET "
|
|
167
|
+
DO UPDATE SET "name" = EXCLUDED."name"
|
|
169
168
|
RETURNING
|
|
170
169
|
"id",
|
|
171
170
|
"current_size",
|
|
@@ -259,7 +258,43 @@ var r=(e)=>({nodeType:"VALUE",value:e}),s=(e)=>({nodeType:"REF",value:e}),w=(e)=
|
|
|
259
258
|
RETURN;
|
|
260
259
|
END;
|
|
261
260
|
$$ LANGUAGE plpgsql;
|
|
262
|
-
`]}};var __filename="/home/runner/work/lonnymq/lonnymq/src/migration/04-function-message-dequeue.ts",
|
|
261
|
+
`]}};var __filename="/home/runner/work/lonnymq/lonnymq/src/migration/04-function-message-dequeue.ts",ae=(e)=>n`
|
|
262
|
+
SELECT
|
|
263
|
+
"message"."id",
|
|
264
|
+
"message"."name",
|
|
265
|
+
"message"."state",
|
|
266
|
+
"message"."content",
|
|
267
|
+
"message"."channel_name",
|
|
268
|
+
"message"."lock_ms",
|
|
269
|
+
"message"."unlock_at",
|
|
270
|
+
"message"."num_attempts"
|
|
271
|
+
FROM ${s(e.schema)}."message"
|
|
272
|
+
WHERE "is_locked"
|
|
273
|
+
AND "unlock_at" <= ${e.now}
|
|
274
|
+
ORDER BY "unlock_at" ASC
|
|
275
|
+
`,re=(e)=>n`
|
|
276
|
+
SELECT
|
|
277
|
+
"channel_state"."id",
|
|
278
|
+
"channel_state"."name",
|
|
279
|
+
"channel_state"."release_interval_ms",
|
|
280
|
+
"channel_state"."message_id",
|
|
281
|
+
"channel_state"."active_next_at",
|
|
282
|
+
"channel_state"."active_prev_at",
|
|
283
|
+
"channel_state"."current_concurrency"
|
|
284
|
+
FROM ${s(e.schema)}."channel_state"
|
|
285
|
+
WHERE "message_id" IS NOT NULL
|
|
286
|
+
AND ("max_concurrency" IS NULL OR "current_concurrency" < "max_concurrency")
|
|
287
|
+
ORDER BY "active_next_at" ASC
|
|
288
|
+
`,Ee=(e)=>n`
|
|
289
|
+
SELECT
|
|
290
|
+
"message"."id",
|
|
291
|
+
"message"."dequeue_at",
|
|
292
|
+
"message"."seq_no"
|
|
293
|
+
FROM ${s(e.schema)}."message"
|
|
294
|
+
WHERE NOT "is_locked"
|
|
295
|
+
AND "channel_name" = ${e.channelName}
|
|
296
|
+
ORDER BY "dequeue_at" ASC, "seq_no" ASC
|
|
297
|
+
`,F={name:E(__filename),sql:(e)=>{let t=ae({now:n`v_now`,schema:e.schema}),a=Ee({channelName:n`v_channel_state."name"`,schema:e.schema}),_=re({schema:e.schema});return[n`
|
|
263
298
|
CREATE FUNCTION ${s(e.schema)}."message_dequeue" ()
|
|
264
299
|
RETURNS TABLE (
|
|
265
300
|
result_code INTEGER,
|
|
@@ -276,19 +311,7 @@ var r=(e)=>({nodeType:"VALUE",value:e}),s=(e)=>({nodeType:"REF",value:e}),w=(e)=
|
|
|
276
311
|
BEGIN
|
|
277
312
|
v_now := NOW();
|
|
278
313
|
|
|
279
|
-
|
|
280
|
-
"message"."id",
|
|
281
|
-
"message"."name",
|
|
282
|
-
"message"."state",
|
|
283
|
-
"message"."content",
|
|
284
|
-
"message"."channel_name",
|
|
285
|
-
"message"."lock_ms",
|
|
286
|
-
"message"."unlock_at",
|
|
287
|
-
"message"."num_attempts"
|
|
288
|
-
FROM ${s(e.schema)}."message"
|
|
289
|
-
WHERE "is_locked"
|
|
290
|
-
AND "unlock_at" <= v_now
|
|
291
|
-
ORDER BY "unlock_at" ASC
|
|
314
|
+
${t}
|
|
292
315
|
FOR UPDATE
|
|
293
316
|
SKIP LOCKED
|
|
294
317
|
LIMIT 1
|
|
@@ -314,18 +337,7 @@ var r=(e)=>({nodeType:"VALUE",value:e}),s=(e)=>({nodeType:"REF",value:e}),w=(e)=
|
|
|
314
337
|
RETURN;
|
|
315
338
|
END IF;
|
|
316
339
|
|
|
317
|
-
|
|
318
|
-
"channel_state"."id",
|
|
319
|
-
"channel_state"."name",
|
|
320
|
-
"channel_state"."release_interval_ms",
|
|
321
|
-
"channel_state"."message_id",
|
|
322
|
-
"channel_state"."active_next_at",
|
|
323
|
-
"channel_state"."active_prev_at",
|
|
324
|
-
"channel_state"."current_concurrency"
|
|
325
|
-
FROM ${s(e.schema)}."channel_state"
|
|
326
|
-
WHERE "message_id" IS NOT NULL
|
|
327
|
-
AND ("max_concurrency" IS NULL OR "current_concurrency" < "max_concurrency")
|
|
328
|
-
ORDER BY "active_next_at" ASC
|
|
340
|
+
${_}
|
|
329
341
|
FOR UPDATE
|
|
330
342
|
SKIP LOCKED
|
|
331
343
|
LIMIT 1
|
|
@@ -369,14 +381,7 @@ var r=(e)=>({nodeType:"VALUE",value:e}),s=(e)=>({nodeType:"REF",value:e}),w=(e)=
|
|
|
369
381
|
"unlock_at" = v_now + (v_message_dequeue."lock_ms" * INTERVAL '1 millisecond')
|
|
370
382
|
WHERE "id" = v_message_dequeue."id";
|
|
371
383
|
|
|
372
|
-
|
|
373
|
-
"message"."id",
|
|
374
|
-
"message"."dequeue_at",
|
|
375
|
-
"message"."seq_no"
|
|
376
|
-
FROM ${s(e.schema)}."message"
|
|
377
|
-
WHERE NOT "is_locked"
|
|
378
|
-
AND "channel_name" = v_message_dequeue."channel_name"
|
|
379
|
-
ORDER BY "dequeue_at" ASC, "seq_no" ASC
|
|
384
|
+
${a}
|
|
380
385
|
LIMIT 1
|
|
381
386
|
INTO v_message_next;
|
|
382
387
|
|
|
@@ -696,4 +701,4 @@ var r=(e)=>({nodeType:"VALUE",value:e}),s=(e)=>({nodeType:"REF",value:e}),w=(e)=
|
|
|
696
701
|
WHERE "name" = p_name;
|
|
697
702
|
END;
|
|
698
703
|
$$ LANGUAGE plpgsql;
|
|
699
|
-
`]}};class R{schema;channelName;registerFn;constructor(e){this.schema=e.schema,this.channelName=e.channelName,this.registerFn=e.registerFn}create(e){let t=new c({schema:this.schema,channelName:this.channelName,name:e.name,content:e.content,lockMs:e.lockMs,delayMs:e.delayMs}),a=new Promise((_)=>{this.registerFn({sortKey:JSON.stringify([t.channelName,t.name,t.createdAt.toISOString()]),execute:(T)=>t.execute(T).then((
|
|
704
|
+
`]}};class R{schema;channelName;registerFn;constructor(e){this.schema=e.schema,this.channelName=e.channelName,this.registerFn=e.registerFn}create(e){let t=new c({schema:this.schema,channelName:this.channelName,name:e.name,content:e.content,lockMs:e.lockMs,delayMs:e.delayMs}),a=new Promise((_)=>{this.registerFn({sortKey:JSON.stringify([t.channelName,t.name,t.createdAt.toISOString()]),execute:(T)=>t.execute(T).then((w)=>_(w))})});return{messageId:t.id,promise:a}}}class S{schema;channelName;registerFn;constructor(e){this.schema=e.schema,this.channelName=e.channelName,this.registerFn=e.registerFn}set(e){let t=new l({schema:this.schema,channelName:this.channelName,maxConcurrency:e.maxConcurrency,maxSize:e.maxSize,releaseIntervalMs:e.releaseIntervalMs});this.registerFn({sortKey:JSON.stringify([t.channelName,null,t.createdAt.toISOString()]),execute:async(a)=>{await t.execute(a)}})}clear(){let e=new m({schema:this.schema,channelName:this.channelName});this.registerFn({sortKey:JSON.stringify([e.channelName,null,e.createdAt.toISOString()]),execute:async(t)=>{await e.execute(t)}})}}class g{policy;message;constructor(e){this.message=new R(e),this.policy=new S(e)}}var _e=(e,t)=>{return e.sortKey.localeCompare(t.sortKey)};class A{commands;schema;constructor(e){this.commands=[],this.schema=e.schema}channel(e){return new g({schema:this.schema,channelName:e,registerFn:(t)=>{this.commands.push(t)}})}async execute(e){for(let t of this.commands.sort(_e))await t.execute(e.databaseClient)}}class D{schema;channelName;constructor(e){this.schema=e.schema,this.channelName=e.channelName}async create(e){return new c({schema:this.schema,channelName:this.channelName,name:e.name,content:e.content,lockMs:e.lockMs,delayMs:e.delayMs}).execute(e.databaseClient)}}class p{schema;channelName;constructor(e){this.schema=e.schema,this.channelName=e.channelName}set(e){return new l({schema:this.schema,channelName:this.channelName,maxConcurrency:e.maxConcurrency,maxSize:e.maxSize,releaseIntervalMs:e.releaseIntervalMs}).execute(e.databaseClient)}clear(e){return new m({schema:this.schema,channelName:this.channelName}).execute(e.databaseClient)}}class M{policy;message;constructor(e){this.message=new D({schema:e.schema,channelName:e.channelName}),this.policy=new p({schema:e.schema,channelName:e.channelName})}}class y{schema;id;isUnlocked;channelName;name;content;state;numAttempts;constructor(e){this.schema=e.schema,this.id=e.id,this.channelName=e.channelName,this.isUnlocked=e.isUnlocked,this.name=e.name,this.content=e.content,this.state=e.state,this.numAttempts=e.numAttempts}async defer(e){return new o({schema:this.schema,id:this.id,numAttempts:this.numAttempts,delayMs:e.delayMs,state:e.state}).execute(e.databaseClient)}async delete(e){return new d({schema:this.schema,numAttempts:this.numAttempts,id:this.id}).execute(e.databaseClient)}async heartbeat(e){return new o({schema:this.schema,id:this.id,numAttempts:this.numAttempts}).execute(e.databaseClient)}}class Q{schema;constructor(e){this.schema=e.schema}async dequeue(e){let a=await new u({schema:this.schema}).execute(e.databaseClient);if(a.resultType==="MESSAGE_DEQUEUED")return{resultType:"MESSAGE_DEQUEUED",message:new y({schema:this.schema,id:a.message.id,channelName:a.message.channelName,name:a.message.name,isUnlocked:a.message.isUnlocked,content:a.message.content,state:a.message.state,numAttempts:a.message.numAttempts})};else return a}channel(e){return new M({schema:this.schema,channelName:e})}batch(){return new A({schema:this.schema})}migrations(e={}){return[O,f,x,G,F,q,$,b,P,B].sort((t,a)=>t.name.localeCompare(a.name)).flatMap((t)=>t.sql({schema:this.schema,eventChannel:e.eventChannel??null})).map((t)=>v(t.value))}}export{X as queueEventDecode,Q as Queue,L as MessageHeartbeatCommand,u as MessageDequeueCommand,d as MessageDeleteCommand,o as MessageDeferCommand,c as MessageCreateCommand,l as ChannelPolicySetCommand,m as ChannelPolicyClearCommand};
|