@yellowpanther/shared 1.2.0 → 1.2.1
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/package.json +2 -1
- package/src/index.js +1 -1
- package/src/queue/imageAlbumPublishQueue.js +164 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@yellowpanther/shared",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.1",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
@@ -25,6 +25,7 @@
|
|
|
25
25
|
"./queue/articlePublishQueue": "./src/queue/articlePublishQueue.js",
|
|
26
26
|
"./queue/videoPublishQueue": "./src/queue/videoPublishQueue.js",
|
|
27
27
|
"./queue/imagePublishQueue": "./src/queue/imagePublishQueue.js",
|
|
28
|
+
"./queue/imageAlbumPublishQueue": "./src/queue/imageAlbumPublishQueue.js",
|
|
28
29
|
"./queue/pagePublishQueue": "./src/queue/pagePublishQueue.js",
|
|
29
30
|
"./queue/productPublishQueue": "./src/queue/productPublishQueue.js",
|
|
30
31
|
"./queue/quizPublishQueue": "./src/queue/quizPublishQueue.js",
|
package/src/index.js
CHANGED
|
@@ -18,5 +18,5 @@ module.exports = {
|
|
|
18
18
|
addProductPublishJob: require('./queue/productPublishQueue').addProductPublishJob,
|
|
19
19
|
addQuizPublishJob: require('./queue/quizPublishQueue').addQuizPublishJob,
|
|
20
20
|
addPredictionPublishJob: require('./queue/predictionPublishQueue').addPredictionPublishJob,
|
|
21
|
-
|
|
21
|
+
addImageAlbumPublishJob: require('./queue/imageAlbumPublishQueue').addImageAlbumPublishJob,
|
|
22
22
|
};
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
// src/queue/imageAlbumPublishQueue.js
|
|
2
|
+
// One-off *publish* scheduler for image album items (BullMQ).
|
|
3
|
+
// Strong upsert: purge any prior job for the same album before adding a new delayed job.
|
|
4
|
+
|
|
5
|
+
const { Queue } = require('bullmq');
|
|
6
|
+
const redisClient = require('../redis/redisClient');
|
|
7
|
+
// const logger = require('../logger'); // uncomment if you have a shared logger
|
|
8
|
+
|
|
9
|
+
const DEBUG = String(process.env.DEBUG_LOGGER || '').trim() === '1';
|
|
10
|
+
const DRIFT_MS = Math.max(0, Number(process.env.SCHEDULE_DRIFT_MS || 250));
|
|
11
|
+
|
|
12
|
+
const imageAlbumPublishQueue = new Queue('imageAlbumPublishQueue', {
|
|
13
|
+
connection: redisClient,
|
|
14
|
+
defaultJobOptions: {
|
|
15
|
+
attempts: 5,
|
|
16
|
+
backoff: { type: 'exponential', delay: 2000 },
|
|
17
|
+
removeOnComplete: 500,
|
|
18
|
+
removeOnFail: 500,
|
|
19
|
+
},
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
// ── ID helpers ────────────────────────────────────────────────────────────────
|
|
23
|
+
|
|
24
|
+
function stableJobId(album_id) {
|
|
25
|
+
return `imageAlbum:publish:${album_id}`;
|
|
26
|
+
}
|
|
27
|
+
function versionedJobId(album_id, runAtIso) {
|
|
28
|
+
return `imageAlbum:publish:${album_id}:${runAtIso}`;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// ── Time helpers ──────────────────────────────────────────────────────────────
|
|
32
|
+
|
|
33
|
+
/** Parse anything into a UTC Date. Strings without timezone are treated as UTC. */
|
|
34
|
+
function normalizeToUtcDate(input) {
|
|
35
|
+
if (input instanceof Date) return new Date(input.getTime());
|
|
36
|
+
if (typeof input === 'number') return new Date(input); // epoch ms
|
|
37
|
+
|
|
38
|
+
if (typeof input === 'string') {
|
|
39
|
+
// Has timezone (Z or ±hh:mm)
|
|
40
|
+
if (/[zZ]|[+\-]\d{2}:\d{2}$/.test(input)) {
|
|
41
|
+
const d = new Date(input);
|
|
42
|
+
if (Number.isNaN(d.getTime())) throw new Error(`Invalid ISO datetime: ${input}`);
|
|
43
|
+
return d;
|
|
44
|
+
}
|
|
45
|
+
// No timezone => treat as UTC
|
|
46
|
+
const s = input.trim().replace(' ', 'T');
|
|
47
|
+
const d = new Date(`${s}Z`);
|
|
48
|
+
if (Number.isNaN(d.getTime())) throw new Error(`Invalid datetime (no tz): ${input}`);
|
|
49
|
+
return d;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
throw new Error(`Unsupported runAtUtc type: ${typeof input}`);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// ── Purge helpers ────────────────────────────────────────────────────────────
|
|
56
|
+
|
|
57
|
+
async function findPendingAlbumJobs(album_id) {
|
|
58
|
+
const states = ['delayed', 'waiting', 'waiting-children', 'active'];
|
|
59
|
+
const jobs = await imageAlbumPublishQueue.getJobs(states);
|
|
60
|
+
return jobs.filter(
|
|
61
|
+
(j) => j?.name === 'imageAlbum:publish' && j?.data?.album_id === String(album_id)
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
async function purgeExistingForAlbum(album_id) {
|
|
66
|
+
let removed = 0;
|
|
67
|
+
|
|
68
|
+
// Remove any pending/waiting/active jobs for this album
|
|
69
|
+
const pendings = await findPendingAlbumJobs(album_id);
|
|
70
|
+
for (const j of pendings) {
|
|
71
|
+
try {
|
|
72
|
+
await j.remove();
|
|
73
|
+
removed++;
|
|
74
|
+
} catch (e) {
|
|
75
|
+
if (DEBUG) console.warn(`[imageAlbumPublishQueue] failed to remove pending job ${j.id}: ${e.message}`);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// Also remove a leftover stable job if present
|
|
80
|
+
const last = await imageAlbumPublishQueue.getJob(stableJobId(album_id));
|
|
81
|
+
if (last) {
|
|
82
|
+
try {
|
|
83
|
+
await last.remove();
|
|
84
|
+
removed++;
|
|
85
|
+
} catch (e) {
|
|
86
|
+
if (DEBUG) console.warn(`[imageAlbumPublishQueue] failed to remove stable job ${last.id}: ${e.message}`);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if (DEBUG) {
|
|
91
|
+
console.info('[imageAlbumPublishQueue] purgeExistingForAlbum', {
|
|
92
|
+
album_id: String(album_id),
|
|
93
|
+
removed,
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
return removed;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// ── Public API ───────────────────────────────────────────────────────────────
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Upsert a one-off publish job for an image album.
|
|
103
|
+
* - Purges any previous jobs for this album
|
|
104
|
+
* - Uses a versioned jobId by default (or stable if useStableId=true)
|
|
105
|
+
*
|
|
106
|
+
* @param {Object} params
|
|
107
|
+
* @param {string|number} params.album_id
|
|
108
|
+
* @param {string|number|Date} params.runAtUtc - ISO string, epoch ms, or Date (UTC)
|
|
109
|
+
* @param {object} [params.extra]
|
|
110
|
+
* @param {boolean} [params.useStableId=false] - if true, uses stable jobId
|
|
111
|
+
*/
|
|
112
|
+
async function addImageAlbumPublishJob({ album_id, runAtUtc, extra = {}, useStableId = false }) {
|
|
113
|
+
if (!album_id || !runAtUtc) {
|
|
114
|
+
throw new Error('addImageAlbumPublishJob: album_id and runAtUtc are required');
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const runAt = normalizeToUtcDate(runAtUtc);
|
|
118
|
+
const runAtIso = runAt.toISOString();
|
|
119
|
+
|
|
120
|
+
// Drift guard to avoid immediate fire due to ms skew
|
|
121
|
+
const now = Date.now();
|
|
122
|
+
let delayMs = runAt.getTime() - now;
|
|
123
|
+
if (delayMs < DRIFT_MS) delayMs = Math.max(0, DRIFT_MS);
|
|
124
|
+
|
|
125
|
+
// Purge any prior jobs for this album (across states)
|
|
126
|
+
await purgeExistingForAlbum(album_id);
|
|
127
|
+
|
|
128
|
+
// Choose jobId strategy
|
|
129
|
+
const jobId = useStableId ? stableJobId(album_id) : versionedJobId(album_id, runAtIso);
|
|
130
|
+
|
|
131
|
+
const job = await imageAlbumPublishQueue.add(
|
|
132
|
+
'imageAlbum:publish',
|
|
133
|
+
{ album_id: String(album_id), runAtUtc: runAtIso, extra },
|
|
134
|
+
{ jobId, delay: delayMs }
|
|
135
|
+
);
|
|
136
|
+
|
|
137
|
+
if (DEBUG) {
|
|
138
|
+
console.info('[imageAlbumPublishQueue] upsert', {
|
|
139
|
+
album_id: String(album_id),
|
|
140
|
+
jobId,
|
|
141
|
+
runAtIso,
|
|
142
|
+
delayMs,
|
|
143
|
+
nowIso: new Date(now).toISOString(),
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
return job;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** Cancel any scheduled publish for a given image album (if present). */
|
|
151
|
+
async function cancelImageAlbumPublishJob(album_id) {
|
|
152
|
+
const removed = await purgeExistingForAlbum(album_id);
|
|
153
|
+
return removed > 0;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
module.exports = {
|
|
157
|
+
imageAlbumPublishQueue,
|
|
158
|
+
addImageAlbumPublishJob,
|
|
159
|
+
cancelImageAlbumPublishJob,
|
|
160
|
+
// helpers for tooling/tests
|
|
161
|
+
stableJobId,
|
|
162
|
+
versionedJobId,
|
|
163
|
+
normalizeToUtcDate,
|
|
164
|
+
};
|