@rei-standard/amsg-server 2.6.0-next.13 → 2.6.0-next.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/README.md +109 -1
- package/dist/adapters/d1.d.ts +130 -0
- package/dist/adapters/interface.d.ts +67 -0
- package/dist/adapters/neon.d.ts +20 -58
- package/dist/adapters/pg-shared.d.ts +114 -0
- package/dist/adapters/pg.d.ts +20 -58
- package/dist/adapters/schema.d.ts +1 -1
- package/dist/adapters/schema.sqlite.d.ts +15 -1
- package/dist/{chunk-B2FDV7YK.mjs → chunk-34I2YSWE.mjs} +10 -0
- package/dist/{chunk-5IOYCCFD.cjs → chunk-H4V7OXHE.cjs} +1326 -384
- package/dist/{chunk-TV7N3RTH.mjs → chunk-KVHR3RCU.mjs} +1324 -382
- package/dist/{chunk-V2SDWGUB.cjs → chunk-OUPM4HAV.cjs} +10 -0
- package/dist/chunk-TFFBMYA2.cjs +95 -0
- package/dist/chunk-UYROU4L3.mjs +95 -0
- package/dist/cloudflare/single-user-worker.d.ts +35 -2
- package/dist/cloudflare.cjs +17 -2
- package/dist/cloudflare.d.ts +3 -1
- package/dist/cloudflare.mjs +18 -3
- package/dist/handlers/outbox.d.ts +7 -0
- package/dist/index.cjs +72 -36
- package/dist/index.d.cts +5 -2
- package/dist/index.d.ts +5 -2
- package/dist/index.mjs +53 -17
- package/dist/lib/client-state-store.d.ts +18 -3
- package/dist/lib/errors.d.ts +93 -0
- package/dist/lib/message-processor.d.ts +8 -2
- package/dist/lib/outbox-store.d.ts +33 -0
- package/dist/lib/push-subscription-store.d.ts +10 -2
- package/dist/lib/request.d.ts +36 -0
- package/dist/lib/run-tick.d.ts +38 -0
- package/dist/lib/schema-version.d.ts +50 -0
- package/dist/lib/validation.d.ts +1 -7
- package/dist/{neon-3ASBLLCM.cjs → neon-G3G4TDAY.cjs} +52 -112
- package/dist/{neon-CUIGAB2T.mjs → neon-GBUF4G76.mjs} +44 -104
- package/dist/{pg-IEZTAR43.cjs → pg-C3JZKBUM.cjs} +49 -112
- package/dist/{pg-KMJPL6QV.mjs → pg-MRXP5MHJ.mjs} +41 -104
- package/dist/single-user.d.ts +4 -0
- package/package.json +2 -2
|
@@ -5,8 +5,16 @@
|
|
|
5
5
|
|
|
6
6
|
|
|
7
7
|
|
|
8
|
+
var _chunkTFFBMYA2cjs = require('./chunk-TFFBMYA2.cjs');
|
|
8
9
|
|
|
9
|
-
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
var _chunkOUPM4HAVcjs = require('./chunk-OUPM4HAV.cjs');
|
|
10
18
|
|
|
11
19
|
// src/server/adapters/neon.js
|
|
12
20
|
var _serverless = require('@neondatabase/serverless');
|
|
@@ -25,13 +33,13 @@ var NeonAdapter = class {
|
|
|
25
33
|
}
|
|
26
34
|
async initSchema() {
|
|
27
35
|
const sql = this._getSql();
|
|
28
|
-
await sql.query(
|
|
29
|
-
await sql.query(
|
|
30
|
-
for (const migration of
|
|
36
|
+
await sql.query(_chunkOUPM4HAVcjs.TABLE_SQL);
|
|
37
|
+
await sql.query(_chunkOUPM4HAVcjs.PUSH_SUBSCRIPTION_TABLE_SQL);
|
|
38
|
+
for (const migration of _chunkOUPM4HAVcjs.MIGRATIONS) {
|
|
31
39
|
await sql.query(migration.sql);
|
|
32
40
|
}
|
|
33
41
|
const indexResults = [];
|
|
34
|
-
for (const index of
|
|
42
|
+
for (const index of _chunkOUPM4HAVcjs.INDEXES) {
|
|
35
43
|
try {
|
|
36
44
|
await sql.query(index.sql);
|
|
37
45
|
indexResults.push({
|
|
@@ -57,11 +65,11 @@ var NeonAdapter = class {
|
|
|
57
65
|
`Critical index creation failed (${failedNames}). Please remove duplicate UUID rows and run initSchema again.`
|
|
58
66
|
);
|
|
59
67
|
}
|
|
60
|
-
const tableCheck = await sql.query(
|
|
68
|
+
const tableCheck = await sql.query(_chunkOUPM4HAVcjs.VERIFY_TABLE_SQL);
|
|
61
69
|
if (tableCheck.length === 0) {
|
|
62
70
|
throw new Error("Table creation verification failed");
|
|
63
71
|
}
|
|
64
|
-
const columns = await sql.query(
|
|
72
|
+
const columns = await sql.query(_chunkOUPM4HAVcjs.COLUMNS_SQL);
|
|
65
73
|
return {
|
|
66
74
|
columnsCreated: columns.length,
|
|
67
75
|
indexesCreated: indexResults.filter((r) => r.status === "success").length,
|
|
@@ -91,7 +99,7 @@ var NeonAdapter = class {
|
|
|
91
99
|
async getTaskByUuid(uuid, userId) {
|
|
92
100
|
const sql = this._getSql();
|
|
93
101
|
const rows = await sql.query(
|
|
94
|
-
`SELECT id, user_id, uuid, encrypted_payload, message_type, next_send_at, status, retry_count, created_at, updated_at
|
|
102
|
+
`SELECT id, user_id, uuid, encrypted_payload, message_type, next_send_at, status, retry_count, last_error, created_at, updated_at
|
|
95
103
|
FROM scheduled_messages
|
|
96
104
|
WHERE uuid = $1 AND user_id = $2 AND status = 'pending'
|
|
97
105
|
LIMIT 1`,
|
|
@@ -110,13 +118,28 @@ var NeonAdapter = class {
|
|
|
110
118
|
);
|
|
111
119
|
return rows[0] || null;
|
|
112
120
|
}
|
|
121
|
+
/**
|
|
122
|
+
* 这条 uuid 现在是什么状态——不限用户,也不限状态(上面那个只看 pending
|
|
123
|
+
* 行)。`runTask` 用它把「这条已经跑完了」和「压根没这条」分开回报。
|
|
124
|
+
*
|
|
125
|
+
* @param {string} uuid
|
|
126
|
+
* @returns {Promise<{ status: string }|null>}
|
|
127
|
+
*/
|
|
128
|
+
async getTaskStatusByUuidOnly(uuid) {
|
|
129
|
+
const sql = this._getSql();
|
|
130
|
+
const rows = await sql.query(
|
|
131
|
+
`SELECT status FROM scheduled_messages WHERE uuid = $1 LIMIT 1`,
|
|
132
|
+
[uuid]
|
|
133
|
+
);
|
|
134
|
+
return rows[0] ? { status: rows[0].status } : null;
|
|
135
|
+
}
|
|
113
136
|
async updateTaskById(taskId, updates) {
|
|
114
137
|
const sql = this._getSql();
|
|
115
138
|
const sets = [];
|
|
116
139
|
const values = [];
|
|
117
140
|
let idx = 1;
|
|
118
141
|
for (const [key, value] of Object.entries(updates)) {
|
|
119
|
-
if (!
|
|
142
|
+
if (!_chunkOUPM4HAVcjs.UPDATABLE_COLUMNS.has(key)) {
|
|
120
143
|
throw new Error(`[amsg-server neon] rejected unknown update column: ${key}`);
|
|
121
144
|
}
|
|
122
145
|
sets.push(`${key} = $${idx}`);
|
|
@@ -138,7 +161,7 @@ var NeonAdapter = class {
|
|
|
138
161
|
let idx = 2;
|
|
139
162
|
if (extraFields) {
|
|
140
163
|
for (const [key, value] of Object.entries(extraFields)) {
|
|
141
|
-
if (!
|
|
164
|
+
if (!_chunkOUPM4HAVcjs.UPDATABLE_COLUMNS.has(key)) {
|
|
142
165
|
throw new Error(`[amsg-server neon] rejected unknown update column: ${key}`);
|
|
143
166
|
}
|
|
144
167
|
sets.push(`${key} = $${idx}`);
|
|
@@ -171,7 +194,7 @@ var NeonAdapter = class {
|
|
|
171
194
|
async getPendingTasks(limit = 50) {
|
|
172
195
|
const sql = this._getSql();
|
|
173
196
|
return sql.query(
|
|
174
|
-
`SELECT id, user_id, uuid, encrypted_payload, message_type, next_send_at, status, retry_count
|
|
197
|
+
`SELECT id, user_id, uuid, encrypted_payload, message_type, next_send_at, retry_after, status, retry_count
|
|
175
198
|
FROM scheduled_messages
|
|
176
199
|
WHERE status = 'pending' AND next_send_at <= NOW()
|
|
177
200
|
AND (lease_until IS NULL OR lease_until <= NOW())
|
|
@@ -181,65 +204,16 @@ var NeonAdapter = class {
|
|
|
181
204
|
[limit]
|
|
182
205
|
);
|
|
183
206
|
}
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
* 本次投递期间别的 tick 领不走它。
|
|
187
|
-
*
|
|
188
|
-
* 租约写在自己的列上,next_send_at 全程不动——那一列是用户设的触发时刻,
|
|
189
|
-
* 任务列表要读它、循环任务推进下一次也要拿它当基准。
|
|
190
|
-
*
|
|
191
|
-
* 两个 tick 抢同一行时只有一个改得动,另一个拿不到 RETURNING 行,据此跳
|
|
192
|
-
* 过。WHERE 里的两个条件各管一件事:
|
|
193
|
-
* - lease_until 为空或已过期:没人正在跑这条。领了任务的 tick 中途没了
|
|
194
|
-
* 也不会把行焊死,租约到期后自然可以被接手。
|
|
195
|
-
* - next_send_at 等于读这行时看到的值:读出来之后用户又改了排期的话,
|
|
196
|
-
* 这一跳就不该再按旧时刻发。
|
|
197
|
-
*
|
|
198
|
-
* 不加一个 'sending' 状态来表达「正在跑」:status 上有 CHECK 约束,加值
|
|
199
|
-
* 要改表。
|
|
200
|
-
*
|
|
201
|
-
* 比 next_send_at 时两边都截到毫秒:列是 timestamptz(微秒精度),驱动读
|
|
202
|
-
* 出来是 JS Date(毫秒精度),原值送回去可能因为亚毫秒差对不上。
|
|
203
|
-
*
|
|
204
|
-
* 带 serializeGroup 时多一道分组门:同一分组里已经有别的行拿着未到期的租
|
|
205
|
-
* 约,这条就领不走(同一分组同时只跑一条)。判定和写租约在同一条 UPDATE
|
|
206
|
-
* 里完成,「先查再占」的空档天然不存在。分组门只看租约,不看
|
|
207
|
-
* `retry_after`:等着重试的任务其实闲着,不该把同分组的其他任务一起堵住。
|
|
208
|
-
*
|
|
209
|
-
* @param {number} taskId
|
|
210
|
-
* @param {string|Date} expectedNextSendAt - 读这行时拿到的 next_send_at 原值
|
|
211
|
-
* @param {string|Date} leaseUntil - 租期末尾
|
|
212
|
-
* @param {string|null} [serializeGroup] - 串行分组标识;空表示不参与分组串行
|
|
213
|
-
* @returns {Promise<boolean>} true = 领到了;false = 别人正拿着租约、同分组
|
|
214
|
-
* 有任务正在跑、排期被改过、或行已不是 pending
|
|
215
|
-
*/
|
|
207
|
+
// 领取一条到点的任务。SQL 与并发语义在 pg-shared.js(pg / neon 共用一份,
|
|
208
|
+
// 语义说明也在那里)。
|
|
216
209
|
async claimTask(taskId, expectedNextSendAt, leaseUntil, serializeGroup = null) {
|
|
217
210
|
const sql = this._getSql();
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
setClause = "lease_until = $1, serialize_group = $4, updated_at = NOW()";
|
|
225
|
-
groupGuard = `
|
|
226
|
-
AND NOT EXISTS (
|
|
227
|
-
SELECT 1 FROM scheduled_messages busy
|
|
228
|
-
WHERE busy.serialize_group = $4 AND busy.id <> $2
|
|
229
|
-
AND busy.status = 'pending' AND busy.lease_until > NOW()
|
|
230
|
-
)`;
|
|
231
|
-
}
|
|
232
|
-
const rows = await sql.query(
|
|
233
|
-
`UPDATE scheduled_messages
|
|
234
|
-
SET ${setClause}
|
|
235
|
-
WHERE id = $2 AND status = 'pending'
|
|
236
|
-
AND date_trunc('milliseconds', next_send_at)
|
|
237
|
-
= date_trunc('milliseconds', $3::timestamptz)
|
|
238
|
-
AND (lease_until IS NULL OR lease_until <= NOW())${groupGuard}
|
|
239
|
-
RETURNING id`,
|
|
240
|
-
params
|
|
241
|
-
);
|
|
242
|
-
return rows.length > 0;
|
|
211
|
+
return _chunkTFFBMYA2cjs.claimTask.call(void 0, (text, params) => sql.query(text, params), taskId, expectedNextSendAt, leaseUntil, serializeGroup);
|
|
212
|
+
}
|
|
213
|
+
// 投递期间的租约续期(run-tick 心跳;语义见 pg-shared.js)。
|
|
214
|
+
async renewTaskLease(taskId, leaseUntil) {
|
|
215
|
+
const sql = this._getSql();
|
|
216
|
+
return _chunkTFFBMYA2cjs.renewTaskLease.call(void 0, (text, params) => sql.query(text, params), taskId, leaseUntil);
|
|
243
217
|
}
|
|
244
218
|
async listTasks(userId, opts = {}) {
|
|
245
219
|
const sql = this._getSql();
|
|
@@ -260,7 +234,7 @@ var NeonAdapter = class {
|
|
|
260
234
|
const total = parseInt(countRows[0].count, 10);
|
|
261
235
|
const taskParams = [...params, limit, offset];
|
|
262
236
|
const tasks = await sql.query(
|
|
263
|
-
`SELECT id, user_id, uuid, encrypted_payload, message_type, next_send_at, status, retry_count, created_at, updated_at
|
|
237
|
+
`SELECT id, user_id, uuid, encrypted_payload, message_type, next_send_at, status, retry_count, last_error, created_at, updated_at
|
|
264
238
|
FROM scheduled_messages
|
|
265
239
|
WHERE ${where}
|
|
266
240
|
ORDER BY next_send_at ASC
|
|
@@ -289,57 +263,23 @@ var NeonAdapter = class {
|
|
|
289
263
|
);
|
|
290
264
|
return rows.length > 0 ? rows[0].status : null;
|
|
291
265
|
}
|
|
292
|
-
//
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
*/
|
|
266
|
+
// 状态 + 失败摘要(GET /message 对已失败的行透出原因;语义见 pg-shared.js)。
|
|
267
|
+
async getTaskStatusInfo(uuid, userId) {
|
|
268
|
+
const sql = this._getSql();
|
|
269
|
+
return _chunkTFFBMYA2cjs.getTaskStatusInfo.call(void 0, (text, params) => sql.query(text, params), uuid, userId);
|
|
270
|
+
}
|
|
271
|
+
// ── push_subscriptions(实现见 pg-shared.js,pg / neon 共用一份)─────────
|
|
299
272
|
async getPushSubscription(userId) {
|
|
300
273
|
const sql = this._getSql();
|
|
301
|
-
|
|
302
|
-
"SELECT subscription, updated_at FROM push_subscriptions WHERE user_id = $1 LIMIT 1",
|
|
303
|
-
[userId]
|
|
304
|
-
);
|
|
305
|
-
if (rows.length === 0) return null;
|
|
306
|
-
return { subscription: rows[0].subscription, updated_at: Number(rows[0].updated_at) };
|
|
274
|
+
return _chunkTFFBMYA2cjs.getPushSubscription.call(void 0, (text, params) => sql.query(text, params), userId);
|
|
307
275
|
}
|
|
308
|
-
/**
|
|
309
|
-
* 覆盖写这个用户的订阅。一个用户一行,没有 last-write-wins 之类的比较——
|
|
310
|
-
* 客户端拿到的新订阅永远比旧的有效,旧的那份只会 410。
|
|
311
|
-
*
|
|
312
|
-
* @param {string} userId
|
|
313
|
-
* @param {string} encryptedSubscription
|
|
314
|
-
* @param {number} updatedAt - epoch 毫秒
|
|
315
|
-
* @returns {Promise<boolean>}
|
|
316
|
-
*/
|
|
317
276
|
async upsertPushSubscription(userId, encryptedSubscription, updatedAt) {
|
|
318
277
|
const sql = this._getSql();
|
|
319
|
-
|
|
320
|
-
`INSERT INTO push_subscriptions (user_id, subscription, updated_at)
|
|
321
|
-
VALUES ($1, $2, $3)
|
|
322
|
-
ON CONFLICT (user_id) DO UPDATE SET
|
|
323
|
-
subscription = EXCLUDED.subscription,
|
|
324
|
-
updated_at = EXCLUDED.updated_at
|
|
325
|
-
RETURNING user_id`,
|
|
326
|
-
[userId, encryptedSubscription, updatedAt]
|
|
327
|
-
);
|
|
328
|
-
return rows.length > 0;
|
|
278
|
+
return _chunkTFFBMYA2cjs.upsertPushSubscription.call(void 0, (text, params) => sql.query(text, params), userId, encryptedSubscription, updatedAt);
|
|
329
279
|
}
|
|
330
|
-
/**
|
|
331
|
-
* 删掉这个用户的订阅(设置页「停止接收推送」)。
|
|
332
|
-
*
|
|
333
|
-
* @param {string} userId
|
|
334
|
-
* @returns {Promise<boolean>} true = 确实删掉了一行
|
|
335
|
-
*/
|
|
336
280
|
async deletePushSubscription(userId) {
|
|
337
281
|
const sql = this._getSql();
|
|
338
|
-
|
|
339
|
-
"DELETE FROM push_subscriptions WHERE user_id = $1 RETURNING user_id",
|
|
340
|
-
[userId]
|
|
341
|
-
);
|
|
342
|
-
return rows.length > 0;
|
|
282
|
+
return _chunkTFFBMYA2cjs.deletePushSubscription.call(void 0, (text, params) => sql.query(text, params), userId);
|
|
343
283
|
}
|
|
344
284
|
};
|
|
345
285
|
|
|
@@ -1,3 +1,11 @@
|
|
|
1
|
+
import {
|
|
2
|
+
claimTask,
|
|
3
|
+
deletePushSubscription,
|
|
4
|
+
getPushSubscription,
|
|
5
|
+
getTaskStatusInfo,
|
|
6
|
+
renewTaskLease,
|
|
7
|
+
upsertPushSubscription
|
|
8
|
+
} from "./chunk-UYROU4L3.mjs";
|
|
1
9
|
import {
|
|
2
10
|
COLUMNS_SQL,
|
|
3
11
|
INDEXES,
|
|
@@ -6,7 +14,7 @@ import {
|
|
|
6
14
|
TABLE_SQL,
|
|
7
15
|
UPDATABLE_COLUMNS,
|
|
8
16
|
VERIFY_TABLE_SQL
|
|
9
|
-
} from "./chunk-
|
|
17
|
+
} from "./chunk-34I2YSWE.mjs";
|
|
10
18
|
|
|
11
19
|
// src/server/adapters/neon.js
|
|
12
20
|
import { neon } from "@neondatabase/serverless";
|
|
@@ -91,7 +99,7 @@ var NeonAdapter = class {
|
|
|
91
99
|
async getTaskByUuid(uuid, userId) {
|
|
92
100
|
const sql = this._getSql();
|
|
93
101
|
const rows = await sql.query(
|
|
94
|
-
`SELECT id, user_id, uuid, encrypted_payload, message_type, next_send_at, status, retry_count, created_at, updated_at
|
|
102
|
+
`SELECT id, user_id, uuid, encrypted_payload, message_type, next_send_at, status, retry_count, last_error, created_at, updated_at
|
|
95
103
|
FROM scheduled_messages
|
|
96
104
|
WHERE uuid = $1 AND user_id = $2 AND status = 'pending'
|
|
97
105
|
LIMIT 1`,
|
|
@@ -110,6 +118,21 @@ var NeonAdapter = class {
|
|
|
110
118
|
);
|
|
111
119
|
return rows[0] || null;
|
|
112
120
|
}
|
|
121
|
+
/**
|
|
122
|
+
* 这条 uuid 现在是什么状态——不限用户,也不限状态(上面那个只看 pending
|
|
123
|
+
* 行)。`runTask` 用它把「这条已经跑完了」和「压根没这条」分开回报。
|
|
124
|
+
*
|
|
125
|
+
* @param {string} uuid
|
|
126
|
+
* @returns {Promise<{ status: string }|null>}
|
|
127
|
+
*/
|
|
128
|
+
async getTaskStatusByUuidOnly(uuid) {
|
|
129
|
+
const sql = this._getSql();
|
|
130
|
+
const rows = await sql.query(
|
|
131
|
+
`SELECT status FROM scheduled_messages WHERE uuid = $1 LIMIT 1`,
|
|
132
|
+
[uuid]
|
|
133
|
+
);
|
|
134
|
+
return rows[0] ? { status: rows[0].status } : null;
|
|
135
|
+
}
|
|
113
136
|
async updateTaskById(taskId, updates) {
|
|
114
137
|
const sql = this._getSql();
|
|
115
138
|
const sets = [];
|
|
@@ -171,7 +194,7 @@ var NeonAdapter = class {
|
|
|
171
194
|
async getPendingTasks(limit = 50) {
|
|
172
195
|
const sql = this._getSql();
|
|
173
196
|
return sql.query(
|
|
174
|
-
`SELECT id, user_id, uuid, encrypted_payload, message_type, next_send_at, status, retry_count
|
|
197
|
+
`SELECT id, user_id, uuid, encrypted_payload, message_type, next_send_at, retry_after, status, retry_count
|
|
175
198
|
FROM scheduled_messages
|
|
176
199
|
WHERE status = 'pending' AND next_send_at <= NOW()
|
|
177
200
|
AND (lease_until IS NULL OR lease_until <= NOW())
|
|
@@ -181,65 +204,16 @@ var NeonAdapter = class {
|
|
|
181
204
|
[limit]
|
|
182
205
|
);
|
|
183
206
|
}
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
* 本次投递期间别的 tick 领不走它。
|
|
187
|
-
*
|
|
188
|
-
* 租约写在自己的列上,next_send_at 全程不动——那一列是用户设的触发时刻,
|
|
189
|
-
* 任务列表要读它、循环任务推进下一次也要拿它当基准。
|
|
190
|
-
*
|
|
191
|
-
* 两个 tick 抢同一行时只有一个改得动,另一个拿不到 RETURNING 行,据此跳
|
|
192
|
-
* 过。WHERE 里的两个条件各管一件事:
|
|
193
|
-
* - lease_until 为空或已过期:没人正在跑这条。领了任务的 tick 中途没了
|
|
194
|
-
* 也不会把行焊死,租约到期后自然可以被接手。
|
|
195
|
-
* - next_send_at 等于读这行时看到的值:读出来之后用户又改了排期的话,
|
|
196
|
-
* 这一跳就不该再按旧时刻发。
|
|
197
|
-
*
|
|
198
|
-
* 不加一个 'sending' 状态来表达「正在跑」:status 上有 CHECK 约束,加值
|
|
199
|
-
* 要改表。
|
|
200
|
-
*
|
|
201
|
-
* 比 next_send_at 时两边都截到毫秒:列是 timestamptz(微秒精度),驱动读
|
|
202
|
-
* 出来是 JS Date(毫秒精度),原值送回去可能因为亚毫秒差对不上。
|
|
203
|
-
*
|
|
204
|
-
* 带 serializeGroup 时多一道分组门:同一分组里已经有别的行拿着未到期的租
|
|
205
|
-
* 约,这条就领不走(同一分组同时只跑一条)。判定和写租约在同一条 UPDATE
|
|
206
|
-
* 里完成,「先查再占」的空档天然不存在。分组门只看租约,不看
|
|
207
|
-
* `retry_after`:等着重试的任务其实闲着,不该把同分组的其他任务一起堵住。
|
|
208
|
-
*
|
|
209
|
-
* @param {number} taskId
|
|
210
|
-
* @param {string|Date} expectedNextSendAt - 读这行时拿到的 next_send_at 原值
|
|
211
|
-
* @param {string|Date} leaseUntil - 租期末尾
|
|
212
|
-
* @param {string|null} [serializeGroup] - 串行分组标识;空表示不参与分组串行
|
|
213
|
-
* @returns {Promise<boolean>} true = 领到了;false = 别人正拿着租约、同分组
|
|
214
|
-
* 有任务正在跑、排期被改过、或行已不是 pending
|
|
215
|
-
*/
|
|
207
|
+
// 领取一条到点的任务。SQL 与并发语义在 pg-shared.js(pg / neon 共用一份,
|
|
208
|
+
// 语义说明也在那里)。
|
|
216
209
|
async claimTask(taskId, expectedNextSendAt, leaseUntil, serializeGroup = null) {
|
|
217
210
|
const sql = this._getSql();
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
setClause = "lease_until = $1, serialize_group = $4, updated_at = NOW()";
|
|
225
|
-
groupGuard = `
|
|
226
|
-
AND NOT EXISTS (
|
|
227
|
-
SELECT 1 FROM scheduled_messages busy
|
|
228
|
-
WHERE busy.serialize_group = $4 AND busy.id <> $2
|
|
229
|
-
AND busy.status = 'pending' AND busy.lease_until > NOW()
|
|
230
|
-
)`;
|
|
231
|
-
}
|
|
232
|
-
const rows = await sql.query(
|
|
233
|
-
`UPDATE scheduled_messages
|
|
234
|
-
SET ${setClause}
|
|
235
|
-
WHERE id = $2 AND status = 'pending'
|
|
236
|
-
AND date_trunc('milliseconds', next_send_at)
|
|
237
|
-
= date_trunc('milliseconds', $3::timestamptz)
|
|
238
|
-
AND (lease_until IS NULL OR lease_until <= NOW())${groupGuard}
|
|
239
|
-
RETURNING id`,
|
|
240
|
-
params
|
|
241
|
-
);
|
|
242
|
-
return rows.length > 0;
|
|
211
|
+
return claimTask((text, params) => sql.query(text, params), taskId, expectedNextSendAt, leaseUntil, serializeGroup);
|
|
212
|
+
}
|
|
213
|
+
// 投递期间的租约续期(run-tick 心跳;语义见 pg-shared.js)。
|
|
214
|
+
async renewTaskLease(taskId, leaseUntil) {
|
|
215
|
+
const sql = this._getSql();
|
|
216
|
+
return renewTaskLease((text, params) => sql.query(text, params), taskId, leaseUntil);
|
|
243
217
|
}
|
|
244
218
|
async listTasks(userId, opts = {}) {
|
|
245
219
|
const sql = this._getSql();
|
|
@@ -260,7 +234,7 @@ var NeonAdapter = class {
|
|
|
260
234
|
const total = parseInt(countRows[0].count, 10);
|
|
261
235
|
const taskParams = [...params, limit, offset];
|
|
262
236
|
const tasks = await sql.query(
|
|
263
|
-
`SELECT id, user_id, uuid, encrypted_payload, message_type, next_send_at, status, retry_count, created_at, updated_at
|
|
237
|
+
`SELECT id, user_id, uuid, encrypted_payload, message_type, next_send_at, status, retry_count, last_error, created_at, updated_at
|
|
264
238
|
FROM scheduled_messages
|
|
265
239
|
WHERE ${where}
|
|
266
240
|
ORDER BY next_send_at ASC
|
|
@@ -289,57 +263,23 @@ var NeonAdapter = class {
|
|
|
289
263
|
);
|
|
290
264
|
return rows.length > 0 ? rows[0].status : null;
|
|
291
265
|
}
|
|
292
|
-
//
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
*/
|
|
266
|
+
// 状态 + 失败摘要(GET /message 对已失败的行透出原因;语义见 pg-shared.js)。
|
|
267
|
+
async getTaskStatusInfo(uuid, userId) {
|
|
268
|
+
const sql = this._getSql();
|
|
269
|
+
return getTaskStatusInfo((text, params) => sql.query(text, params), uuid, userId);
|
|
270
|
+
}
|
|
271
|
+
// ── push_subscriptions(实现见 pg-shared.js,pg / neon 共用一份)─────────
|
|
299
272
|
async getPushSubscription(userId) {
|
|
300
273
|
const sql = this._getSql();
|
|
301
|
-
|
|
302
|
-
"SELECT subscription, updated_at FROM push_subscriptions WHERE user_id = $1 LIMIT 1",
|
|
303
|
-
[userId]
|
|
304
|
-
);
|
|
305
|
-
if (rows.length === 0) return null;
|
|
306
|
-
return { subscription: rows[0].subscription, updated_at: Number(rows[0].updated_at) };
|
|
274
|
+
return getPushSubscription((text, params) => sql.query(text, params), userId);
|
|
307
275
|
}
|
|
308
|
-
/**
|
|
309
|
-
* 覆盖写这个用户的订阅。一个用户一行,没有 last-write-wins 之类的比较——
|
|
310
|
-
* 客户端拿到的新订阅永远比旧的有效,旧的那份只会 410。
|
|
311
|
-
*
|
|
312
|
-
* @param {string} userId
|
|
313
|
-
* @param {string} encryptedSubscription
|
|
314
|
-
* @param {number} updatedAt - epoch 毫秒
|
|
315
|
-
* @returns {Promise<boolean>}
|
|
316
|
-
*/
|
|
317
276
|
async upsertPushSubscription(userId, encryptedSubscription, updatedAt) {
|
|
318
277
|
const sql = this._getSql();
|
|
319
|
-
|
|
320
|
-
`INSERT INTO push_subscriptions (user_id, subscription, updated_at)
|
|
321
|
-
VALUES ($1, $2, $3)
|
|
322
|
-
ON CONFLICT (user_id) DO UPDATE SET
|
|
323
|
-
subscription = EXCLUDED.subscription,
|
|
324
|
-
updated_at = EXCLUDED.updated_at
|
|
325
|
-
RETURNING user_id`,
|
|
326
|
-
[userId, encryptedSubscription, updatedAt]
|
|
327
|
-
);
|
|
328
|
-
return rows.length > 0;
|
|
278
|
+
return upsertPushSubscription((text, params) => sql.query(text, params), userId, encryptedSubscription, updatedAt);
|
|
329
279
|
}
|
|
330
|
-
/**
|
|
331
|
-
* 删掉这个用户的订阅(设置页「停止接收推送」)。
|
|
332
|
-
*
|
|
333
|
-
* @param {string} userId
|
|
334
|
-
* @returns {Promise<boolean>} true = 确实删掉了一行
|
|
335
|
-
*/
|
|
336
280
|
async deletePushSubscription(userId) {
|
|
337
281
|
const sql = this._getSql();
|
|
338
|
-
|
|
339
|
-
"DELETE FROM push_subscriptions WHERE user_id = $1 RETURNING user_id",
|
|
340
|
-
[userId]
|
|
341
|
-
);
|
|
342
|
-
return rows.length > 0;
|
|
282
|
+
return deletePushSubscription((text, params) => sql.query(text, params), userId);
|
|
343
283
|
}
|
|
344
284
|
};
|
|
345
285
|
export {
|