@detiq/crawler 1.1.7 → 1.1.9
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/accessibility-auditor.js +74 -54
- package/dist/ai-login.js +249 -227
- package/dist/auth-token-rotator.js +67 -57
- package/dist/auth.js +336 -299
- package/dist/browser-extension.js +142 -104
- package/dist/captcha-solver.js +125 -98
- package/dist/color-contrast.js +89 -77
- package/dist/content-chunker.js +126 -126
- package/dist/content-dedup.js +71 -54
- package/dist/cookie-consent.js +83 -68
- package/dist/cookie-jar.js +60 -53
- package/dist/crawler.js +5345 -4486
- package/dist/custom-js-injector.js +61 -45
- package/dist/dark-mode-capture.js +81 -67
- package/dist/dead-link-detector.js +55 -47
- package/dist/element-hash.js +12 -8
- package/dist/email-otp.js +195 -154
- package/dist/engine-router.js +98 -87
- package/dist/export/bulk-export.js +55 -70
- package/dist/export/vector-export.js +105 -77
- package/dist/extractors/advanced.d.ts +10 -0
- package/dist/extractors/advanced.js +266 -249
- package/dist/extractors/artifact-envelope.js +58 -59
- package/dist/failure-analysis-agent.js +33 -37
- package/dist/feature-flag-detector.js +66 -51
- package/dist/form-detector.js +75 -55
- package/dist/frontier.js +101 -76
- package/dist/gap-pass.js +89 -68
- package/dist/graphql-detector.js +72 -62
- package/dist/http-engine.js +330 -282
- package/dist/incremental-diff.js +87 -54
- package/dist/index.d.ts +2 -0
- package/dist/index.js +28 -63
- package/dist/index.js.map +2 -2
- package/dist/input-fuzzer.js +97 -93
- package/dist/interactive-probe.d.ts +36 -0
- package/dist/interactive-probe.js +827 -715
- package/dist/job-queue.js +2169 -2151
- package/dist/js-coverage.js +56 -52
- package/dist/js-router-detector.js +117 -86
- package/dist/language-detector.js +63 -51
- package/dist/link-classifier.js +50 -32
- package/dist/live-view.js +108 -91
- package/dist/locator-extractor.d.ts +37 -0
- package/dist/locator-extractor.js +199 -0
- package/dist/locator-extractor.js.map +7 -0
- package/dist/logger.js +7 -9
- package/dist/markdown-extract.js +61 -48
- package/dist/native-crawler.js +946 -780
- package/dist/network-observer.js +156 -135
- package/dist/network-throttle.js +40 -29
- package/dist/openapi-generator.js +92 -77
- package/dist/pdf-extractor.js +90 -72
- package/dist/performance-metrics.js +102 -80
- package/dist/phash.js +144 -125
- package/dist/pii-scrubber.js +68 -65
- package/dist/playwright-recorder.js +58 -70
- package/dist/policies/crawl-safety.js +34 -31
- package/dist/profiles/domain-profile.js +125 -130
- package/dist/proxy.js +172 -146
- package/dist/rate-limiter.js +65 -58
- package/dist/redirect-tracker.js +64 -60
- package/dist/redis-frontier.js +136 -116
- package/dist/redis-queue.js +144 -157
- package/dist/response-cache.js +140 -127
- package/dist/responsive-detector.js +46 -42
- package/dist/retry-policy.js +50 -46
- package/dist/robots-txt.js +83 -69
- package/dist/screen-reader.js +70 -45
- package/dist/screenshot-masker.js +77 -59
- package/dist/seo-checker.js +107 -88
- package/dist/session-capture.js +212 -192
- package/dist/session-export.js +133 -107
- package/dist/sitemap-writer.js +41 -38
- package/dist/sitemap.js +202 -141
- package/dist/skyvern.js +148 -123
- package/dist/sqs-queue.js +42 -42
- package/dist/sso-token.js +90 -74
- package/dist/stealth/human-behavior.js +173 -156
- package/dist/telemetry.js +82 -68
- package/dist/test-executor.js +335 -309
- package/dist/tls-fingerprint.js +88 -77
- package/dist/url-pattern-completer.js +90 -71
- package/dist/visual-diff-verdict.js +38 -23
- package/dist/webhook-notifier.js +69 -71
- package/dist/websocket-detector.js +68 -56
- package/dist/worker.js +38 -29
- package/package.json +1 -1
- package/src/index.ts +2 -0
- package/src/locator-extractor.ts +226 -0
- package/types/index.d.ts +2 -0
- package/types/locator-extractor.d.ts +37 -0
package/dist/redis-frontier.js
CHANGED
|
@@ -1,126 +1,146 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
}
|
|
9
|
-
jobId
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
this.
|
|
27
|
-
this.
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
this.useRedis = false;
|
|
31
|
-
this.redis = null;
|
|
1
|
+
/**
|
|
2
|
+
* Redis-backed distributed crawl frontier.
|
|
3
|
+
*
|
|
4
|
+
* Replaces the in-memory CrawlFrontier for horizontally-scaled crawler deployments.
|
|
5
|
+
* Multiple crawler workers on different containers can share the same URL queue for
|
|
6
|
+
* a given job — enabling 10x crawl throughput on large enterprise apps.
|
|
7
|
+
*
|
|
8
|
+
* Redis key namespace: zeta:frontier:{jobId}
|
|
9
|
+
* LIST zeta:frontier:{jobId}:queue — BFS queue (LPUSH/RPOP)
|
|
10
|
+
* SET zeta:frontier:{jobId}:visited — deduplication set
|
|
11
|
+
* EXPIRE — 24h TTL on all keys
|
|
12
|
+
*
|
|
13
|
+
* Graceful fallback: if REDIS_URL is not set, RedisCrawlFrontier silently falls
|
|
14
|
+
* back to in-memory arrays so local development works without Redis.
|
|
15
|
+
*/
|
|
16
|
+
const FRONTIER_TTL = 86400; // 24h in seconds
|
|
17
|
+
export class RedisCrawlFrontier {
|
|
18
|
+
jobId;
|
|
19
|
+
redis;
|
|
20
|
+
queueKey;
|
|
21
|
+
visitedKey;
|
|
22
|
+
inMemoryQueue = [];
|
|
23
|
+
inMemoryVisited = new Set();
|
|
24
|
+
useRedis;
|
|
25
|
+
constructor(jobId) {
|
|
26
|
+
this.jobId = jobId;
|
|
27
|
+
this.queueKey = `zeta:frontier:${jobId}:queue`;
|
|
28
|
+
this.visitedKey = `zeta:frontier:${jobId}:visited`;
|
|
29
|
+
this.useRedis = !!process.env.REDIS_URL;
|
|
32
30
|
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
31
|
+
async init() {
|
|
32
|
+
if (!this.useRedis)
|
|
33
|
+
return;
|
|
34
|
+
try {
|
|
35
|
+
const Redis = await import('ioredis').then(m => m.default ?? m);
|
|
36
|
+
this.redis = new Redis(process.env.REDIS_URL, {
|
|
37
|
+
maxRetriesPerRequest: 2,
|
|
38
|
+
enableReadyCheck: false,
|
|
39
|
+
lazyConnect: true,
|
|
40
|
+
});
|
|
41
|
+
await this.redis.connect().catch(() => {
|
|
42
|
+
this.useRedis = false;
|
|
43
|
+
this.redis = null;
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
catch {
|
|
47
|
+
this.useRedis = false;
|
|
48
|
+
this.redis = null;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
/** Add a URL to the frontier. No-op if already visited. Returns true if added. */
|
|
52
|
+
async push(url) {
|
|
53
|
+
if (this.useRedis && this.redis) {
|
|
54
|
+
try {
|
|
55
|
+
const isNew = await this.redis.sadd(this.visitedKey, url);
|
|
56
|
+
if (!isNew)
|
|
57
|
+
return false;
|
|
58
|
+
await this.redis.lpush(this.queueKey, url);
|
|
59
|
+
await this.redis.expire(this.queueKey, FRONTIER_TTL);
|
|
60
|
+
await this.redis.expire(this.visitedKey, FRONTIER_TTL);
|
|
61
|
+
return true;
|
|
62
|
+
}
|
|
63
|
+
catch {
|
|
64
|
+
// Fall through to in-memory on Redis error
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
if (this.inMemoryVisited.has(url))
|
|
68
|
+
return false;
|
|
69
|
+
this.inMemoryVisited.add(url);
|
|
70
|
+
this.inMemoryQueue.push(url);
|
|
43
71
|
return true;
|
|
44
|
-
} catch {
|
|
45
|
-
}
|
|
46
72
|
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
73
|
+
/** Pop the next URL (FIFO/BFS). Returns null if queue empty. */
|
|
74
|
+
async shift() {
|
|
75
|
+
if (this.useRedis && this.redis) {
|
|
76
|
+
try {
|
|
77
|
+
const url = await this.redis.rpop(this.queueKey);
|
|
78
|
+
return url ?? null;
|
|
79
|
+
}
|
|
80
|
+
catch {
|
|
81
|
+
// Fall through
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return this.inMemoryQueue.shift() ?? null;
|
|
85
|
+
}
|
|
86
|
+
/** Check if a URL has already been visited/queued. */
|
|
87
|
+
async hasVisited(url) {
|
|
88
|
+
if (this.useRedis && this.redis) {
|
|
89
|
+
try {
|
|
90
|
+
return (await this.redis.sismember(this.visitedKey, url)) === 1;
|
|
91
|
+
}
|
|
92
|
+
catch { /* fall through */ }
|
|
93
|
+
}
|
|
94
|
+
return this.inMemoryVisited.has(url);
|
|
95
|
+
}
|
|
96
|
+
/** Current queue length. */
|
|
97
|
+
async length() {
|
|
98
|
+
if (this.useRedis && this.redis) {
|
|
99
|
+
try {
|
|
100
|
+
return await this.redis.llen(this.queueKey);
|
|
101
|
+
}
|
|
102
|
+
catch { /* fall through */ }
|
|
103
|
+
}
|
|
104
|
+
return this.inMemoryQueue.length;
|
|
60
105
|
}
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
106
|
+
/** Total visited count. */
|
|
107
|
+
async visitedCount() {
|
|
108
|
+
if (this.useRedis && this.redis) {
|
|
109
|
+
try {
|
|
110
|
+
return await this.redis.scard(this.visitedKey);
|
|
111
|
+
}
|
|
112
|
+
catch { /* fall through */ }
|
|
113
|
+
}
|
|
114
|
+
return this.inMemoryVisited.size;
|
|
70
115
|
}
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
if (this.useRedis && this.redis) {
|
|
76
|
-
try {
|
|
77
|
-
return await this.redis.llen(this.queueKey);
|
|
78
|
-
} catch {
|
|
79
|
-
}
|
|
116
|
+
/** Seed initial URLs (used at job start). */
|
|
117
|
+
async seed(urls) {
|
|
118
|
+
for (const url of urls)
|
|
119
|
+
await this.push(url);
|
|
80
120
|
}
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
121
|
+
/** Clean up all Redis keys for this frontier. Call at job completion. */
|
|
122
|
+
async cleanup() {
|
|
123
|
+
if (this.useRedis && this.redis) {
|
|
124
|
+
try {
|
|
125
|
+
await this.redis.del(this.queueKey, this.visitedKey);
|
|
126
|
+
}
|
|
127
|
+
catch { /* ignore */ }
|
|
128
|
+
try {
|
|
129
|
+
await this.redis.quit();
|
|
130
|
+
}
|
|
131
|
+
catch { /* ignore */ }
|
|
132
|
+
}
|
|
90
133
|
}
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
async cleanup() {
|
|
99
|
-
if (this.useRedis && this.redis) {
|
|
100
|
-
try {
|
|
101
|
-
await this.redis.del(this.queueKey, this.visitedKey);
|
|
102
|
-
} catch {
|
|
103
|
-
}
|
|
104
|
-
try {
|
|
105
|
-
await this.redis.quit();
|
|
106
|
-
} catch {
|
|
107
|
-
}
|
|
134
|
+
/** Stats for monitoring. */
|
|
135
|
+
async stats() {
|
|
136
|
+
return {
|
|
137
|
+
backend: this.useRedis && this.redis ? 'redis' : 'memory',
|
|
138
|
+
queued: await this.length(),
|
|
139
|
+
visited: await this.visitedCount(),
|
|
140
|
+
};
|
|
108
141
|
}
|
|
109
|
-
}
|
|
110
|
-
/** Stats for monitoring. */
|
|
111
|
-
async stats() {
|
|
112
|
-
return {
|
|
113
|
-
backend: this.useRedis && this.redis ? "redis" : "memory",
|
|
114
|
-
queued: await this.length(),
|
|
115
|
-
visited: await this.visitedCount()
|
|
116
|
-
};
|
|
117
|
-
}
|
|
118
142
|
}
|
|
119
|
-
|
|
120
|
-
|
|
143
|
+
/** Check if Redis is configured for distributed frontier. */
|
|
144
|
+
export function isRedisFrontierEnabled() {
|
|
145
|
+
return !!process.env.REDIS_URL;
|
|
121
146
|
}
|
|
122
|
-
export {
|
|
123
|
-
RedisCrawlFrontier,
|
|
124
|
-
isRedisFrontierEnabled
|
|
125
|
-
};
|
|
126
|
-
//# sourceMappingURL=redis-frontier.js.map
|
package/dist/redis-queue.js
CHANGED
|
@@ -1,165 +1,152 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Redis Streams distributed job queue.
|
|
3
|
+
*
|
|
4
|
+
* Provides horizontal crawler scaling via Redis Streams consumer groups.
|
|
5
|
+
* Multiple crawler nodes can pull from the same stream — each job is
|
|
6
|
+
* delivered to exactly one consumer (at-least-once via ACK/PENDING).
|
|
7
|
+
*
|
|
8
|
+
* Used when REDIS_URL is set AND JOB_QUEUE_BACKEND=redis.
|
|
9
|
+
* Falls back to the Postgres-backed queue when Redis is unavailable.
|
|
10
|
+
*
|
|
11
|
+
* Stream key: zeta:crawl:stream
|
|
12
|
+
* Group name: zeta:crawl:workers
|
|
13
|
+
* Consumer id: zeta:worker:<hostname>:<pid>
|
|
14
|
+
*/
|
|
15
|
+
import { hostname } from 'node:os';
|
|
16
|
+
const STREAM_KEY = 'zeta:crawl:stream';
|
|
17
|
+
const GROUP_NAME = 'zeta:crawl:workers';
|
|
18
|
+
const MAX_LEN = 10_000; // approx cap on stream length
|
|
5
19
|
function consumerId() {
|
|
6
|
-
|
|
20
|
+
return `zeta:worker:${hostname()}:${process.pid}`;
|
|
7
21
|
}
|
|
8
22
|
let ioredis;
|
|
9
23
|
async function getIoredis() {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
}
|
|
13
|
-
return ioredis;
|
|
14
|
-
}
|
|
15
|
-
async function createRedisQueue() {
|
|
16
|
-
const redisUrl = process.env.REDIS_URL;
|
|
17
|
-
if (!redisUrl) throw new Error("REDIS_URL env var required for redis queue backend");
|
|
18
|
-
const Redis = await getIoredis();
|
|
19
|
-
const redis = new Redis(redisUrl, {
|
|
20
|
-
maxRetriesPerRequest: 3,
|
|
21
|
-
enableReadyCheck: true,
|
|
22
|
-
lazyConnect: false
|
|
23
|
-
});
|
|
24
|
-
await redis.xgroup("CREATE", STREAM_KEY, GROUP_NAME, "$", "MKSTREAM").catch((e) => {
|
|
25
|
-
if (!e.message?.includes("BUSYGROUP")) throw e;
|
|
26
|
-
});
|
|
27
|
-
const consumer = consumerId();
|
|
28
|
-
return {
|
|
29
|
-
async enqueue(jobId, tenantId, projectId, payload = {}) {
|
|
30
|
-
const entryId = await redis.xadd(
|
|
31
|
-
STREAM_KEY,
|
|
32
|
-
"MAXLEN",
|
|
33
|
-
"~",
|
|
34
|
-
MAX_LEN,
|
|
35
|
-
"*",
|
|
36
|
-
"jobId",
|
|
37
|
-
jobId,
|
|
38
|
-
"tenantId",
|
|
39
|
-
tenantId,
|
|
40
|
-
"projectId",
|
|
41
|
-
projectId,
|
|
42
|
-
"payload",
|
|
43
|
-
JSON.stringify(payload),
|
|
44
|
-
"enqueuedAt",
|
|
45
|
-
(/* @__PURE__ */ new Date()).toISOString()
|
|
46
|
-
);
|
|
47
|
-
return entryId;
|
|
48
|
-
},
|
|
49
|
-
async dequeue(timeoutMs = 5e3) {
|
|
50
|
-
const pending = await redis.xreadgroup(
|
|
51
|
-
"GROUP",
|
|
52
|
-
GROUP_NAME,
|
|
53
|
-
consumer,
|
|
54
|
-
"COUNT",
|
|
55
|
-
"1",
|
|
56
|
-
"STREAMS",
|
|
57
|
-
STREAM_KEY,
|
|
58
|
-
"0"
|
|
59
|
-
) ?? [];
|
|
60
|
-
let entries = pending?.[0]?.[1] ?? [];
|
|
61
|
-
if (!entries.length) {
|
|
62
|
-
const fresh = await redis.xreadgroup(
|
|
63
|
-
"GROUP",
|
|
64
|
-
GROUP_NAME,
|
|
65
|
-
consumer,
|
|
66
|
-
"COUNT",
|
|
67
|
-
"1",
|
|
68
|
-
"BLOCK",
|
|
69
|
-
timeoutMs,
|
|
70
|
-
"STREAMS",
|
|
71
|
-
STREAM_KEY,
|
|
72
|
-
">"
|
|
73
|
-
) ?? [];
|
|
74
|
-
entries = fresh?.[0]?.[1] ?? [];
|
|
75
|
-
}
|
|
76
|
-
if (!entries.length) return null;
|
|
77
|
-
const [entryId, fields] = entries[0];
|
|
78
|
-
const obj = {};
|
|
79
|
-
for (let i = 0; i < fields.length; i += 2) {
|
|
80
|
-
obj[fields[i]] = fields[i + 1];
|
|
81
|
-
}
|
|
82
|
-
return {
|
|
83
|
-
id: entryId,
|
|
84
|
-
jobId: obj.jobId,
|
|
85
|
-
tenantId: obj.tenantId,
|
|
86
|
-
projectId: obj.projectId,
|
|
87
|
-
payload: JSON.parse(obj.payload ?? "{}"),
|
|
88
|
-
enqueuedAt: obj.enqueuedAt
|
|
89
|
-
};
|
|
90
|
-
},
|
|
91
|
-
async ack(streamEntryId) {
|
|
92
|
-
await redis.xack(STREAM_KEY, GROUP_NAME, streamEntryId);
|
|
93
|
-
},
|
|
94
|
-
async nack(streamEntryId) {
|
|
95
|
-
await redis.xack(STREAM_KEY, GROUP_NAME, streamEntryId);
|
|
96
|
-
},
|
|
97
|
-
async reclaimStale(maxIdleMs = 6e4) {
|
|
98
|
-
const result = await redis.xautoclaim(
|
|
99
|
-
STREAM_KEY,
|
|
100
|
-
GROUP_NAME,
|
|
101
|
-
consumer,
|
|
102
|
-
maxIdleMs,
|
|
103
|
-
"0-0",
|
|
104
|
-
"COUNT",
|
|
105
|
-
"10"
|
|
106
|
-
);
|
|
107
|
-
const entries = Array.isArray(result) ? result[1] ?? [] : [];
|
|
108
|
-
return entries.map(([entryId, fields]) => {
|
|
109
|
-
const obj = {};
|
|
110
|
-
for (let i = 0; i < fields.length; i += 2) obj[fields[i]] = fields[i + 1];
|
|
111
|
-
return {
|
|
112
|
-
id: entryId,
|
|
113
|
-
jobId: obj.jobId,
|
|
114
|
-
tenantId: obj.tenantId,
|
|
115
|
-
projectId: obj.projectId,
|
|
116
|
-
payload: JSON.parse(obj.payload ?? "{}"),
|
|
117
|
-
enqueuedAt: obj.enqueuedAt
|
|
118
|
-
};
|
|
119
|
-
});
|
|
120
|
-
},
|
|
121
|
-
async stats() {
|
|
122
|
-
const info = await redis.xinfo("GROUPS", STREAM_KEY);
|
|
123
|
-
const group = info.find((g) => {
|
|
124
|
-
const arr = Array.isArray(g) ? g : [];
|
|
125
|
-
for (let i = 0; i < arr.length; i += 2) {
|
|
126
|
-
if (arr[i] === "name" && arr[i + 1] === GROUP_NAME) return true;
|
|
127
|
-
}
|
|
128
|
-
return false;
|
|
129
|
-
});
|
|
130
|
-
if (!group) return { pending: 0, delivered: 0, groupLag: 0 };
|
|
131
|
-
const flat = Array.isArray(group) ? group : [];
|
|
132
|
-
const get = (key) => {
|
|
133
|
-
for (let i = 0; i < flat.length - 1; i += 2) {
|
|
134
|
-
if (flat[i] === key) return Number(flat[i + 1]) || 0;
|
|
135
|
-
}
|
|
136
|
-
return 0;
|
|
137
|
-
};
|
|
138
|
-
return {
|
|
139
|
-
pending: get("pel-count"),
|
|
140
|
-
delivered: get("delivered-count"),
|
|
141
|
-
groupLag: get("lag")
|
|
142
|
-
};
|
|
143
|
-
},
|
|
144
|
-
async close() {
|
|
145
|
-
await redis.quit().catch(() => {
|
|
146
|
-
});
|
|
24
|
+
if (!ioredis) {
|
|
25
|
+
ioredis = await import('ioredis').then(m => m.default ?? m);
|
|
147
26
|
}
|
|
148
|
-
|
|
27
|
+
return ioredis;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Create a Redis Streams job queue client.
|
|
31
|
+
* Throws if REDIS_URL is not set.
|
|
32
|
+
*/
|
|
33
|
+
export async function createRedisQueue() {
|
|
34
|
+
const redisUrl = process.env.REDIS_URL;
|
|
35
|
+
if (!redisUrl)
|
|
36
|
+
throw new Error('REDIS_URL env var required for redis queue backend');
|
|
37
|
+
const Redis = await getIoredis();
|
|
38
|
+
const redis = new Redis(redisUrl, {
|
|
39
|
+
maxRetriesPerRequest: 3,
|
|
40
|
+
enableReadyCheck: true,
|
|
41
|
+
lazyConnect: false,
|
|
42
|
+
});
|
|
43
|
+
// Create stream + consumer group if they don't exist
|
|
44
|
+
await redis.xgroup('CREATE', STREAM_KEY, GROUP_NAME, '$', 'MKSTREAM').catch((e) => {
|
|
45
|
+
if (!e.message?.includes('BUSYGROUP'))
|
|
46
|
+
throw e;
|
|
47
|
+
});
|
|
48
|
+
const consumer = consumerId();
|
|
49
|
+
return {
|
|
50
|
+
async enqueue(jobId, tenantId, projectId, payload = {}) {
|
|
51
|
+
const entryId = await redis.xadd(STREAM_KEY, 'MAXLEN', '~', MAX_LEN, '*', 'jobId', jobId, 'tenantId', tenantId, 'projectId', projectId, 'payload', JSON.stringify(payload), 'enqueuedAt', new Date().toISOString());
|
|
52
|
+
return entryId;
|
|
53
|
+
},
|
|
54
|
+
async dequeue(timeoutMs = 5_000) {
|
|
55
|
+
// First drain pending (unacknowledged) messages for this consumer
|
|
56
|
+
const pending = await redis.xreadgroup('GROUP', GROUP_NAME, consumer, 'COUNT', '1', 'STREAMS', STREAM_KEY, '0') ?? [];
|
|
57
|
+
let entries = pending?.[0]?.[1] ?? [];
|
|
58
|
+
if (!entries.length) {
|
|
59
|
+
// Block-read for new messages
|
|
60
|
+
const fresh = await redis.xreadgroup('GROUP', GROUP_NAME, consumer, 'COUNT', '1', 'BLOCK', timeoutMs, 'STREAMS', STREAM_KEY, '>') ?? [];
|
|
61
|
+
entries = fresh?.[0]?.[1] ?? [];
|
|
62
|
+
}
|
|
63
|
+
if (!entries.length)
|
|
64
|
+
return null;
|
|
65
|
+
const [entryId, fields] = entries[0];
|
|
66
|
+
const obj = {};
|
|
67
|
+
for (let i = 0; i < fields.length; i += 2) {
|
|
68
|
+
obj[fields[i]] = fields[i + 1];
|
|
69
|
+
}
|
|
70
|
+
return {
|
|
71
|
+
id: entryId,
|
|
72
|
+
jobId: obj.jobId,
|
|
73
|
+
tenantId: obj.tenantId,
|
|
74
|
+
projectId: obj.projectId,
|
|
75
|
+
payload: JSON.parse(obj.payload ?? '{}'),
|
|
76
|
+
enqueuedAt: obj.enqueuedAt,
|
|
77
|
+
};
|
|
78
|
+
},
|
|
79
|
+
async ack(streamEntryId) {
|
|
80
|
+
await redis.xack(STREAM_KEY, GROUP_NAME, streamEntryId);
|
|
81
|
+
},
|
|
82
|
+
async nack(streamEntryId) {
|
|
83
|
+
// Move back to '>'-readable by deleting and re-adding (simplest approach)
|
|
84
|
+
// In production, prefer a dead-letter stream instead.
|
|
85
|
+
await redis.xack(STREAM_KEY, GROUP_NAME, streamEntryId);
|
|
86
|
+
},
|
|
87
|
+
async reclaimStale(maxIdleMs = 60_000) {
|
|
88
|
+
const result = await redis.xautoclaim(STREAM_KEY, GROUP_NAME, consumer, maxIdleMs, '0-0', 'COUNT', '10');
|
|
89
|
+
const entries = Array.isArray(result) ? (result[1] ?? []) : [];
|
|
90
|
+
return entries.map(([entryId, fields]) => {
|
|
91
|
+
const obj = {};
|
|
92
|
+
for (let i = 0; i < fields.length; i += 2)
|
|
93
|
+
obj[fields[i]] = fields[i + 1];
|
|
94
|
+
return {
|
|
95
|
+
id: entryId,
|
|
96
|
+
jobId: obj.jobId,
|
|
97
|
+
tenantId: obj.tenantId,
|
|
98
|
+
projectId: obj.projectId,
|
|
99
|
+
payload: JSON.parse(obj.payload ?? '{}'),
|
|
100
|
+
enqueuedAt: obj.enqueuedAt,
|
|
101
|
+
};
|
|
102
|
+
});
|
|
103
|
+
},
|
|
104
|
+
async stats() {
|
|
105
|
+
const info = await redis.xinfo('GROUPS', STREAM_KEY);
|
|
106
|
+
// xinfo groups returns array of arrays: [name, ..., pel-count, ..., lag, ...]
|
|
107
|
+
const group = info.find((g) => {
|
|
108
|
+
const arr = Array.isArray(g) ? g : [];
|
|
109
|
+
for (let i = 0; i < arr.length; i += 2) {
|
|
110
|
+
if (arr[i] === 'name' && arr[i + 1] === GROUP_NAME)
|
|
111
|
+
return true;
|
|
112
|
+
}
|
|
113
|
+
return false;
|
|
114
|
+
});
|
|
115
|
+
if (!group)
|
|
116
|
+
return { pending: 0, delivered: 0, groupLag: 0 };
|
|
117
|
+
const flat = Array.isArray(group) ? group : [];
|
|
118
|
+
const get = (key) => {
|
|
119
|
+
for (let i = 0; i < flat.length - 1; i += 2) {
|
|
120
|
+
if (flat[i] === key)
|
|
121
|
+
return Number(flat[i + 1]) || 0;
|
|
122
|
+
}
|
|
123
|
+
return 0;
|
|
124
|
+
};
|
|
125
|
+
return {
|
|
126
|
+
pending: get('pel-count'),
|
|
127
|
+
delivered: get('delivered-count'),
|
|
128
|
+
groupLag: get('lag'),
|
|
129
|
+
};
|
|
130
|
+
},
|
|
131
|
+
async close() {
|
|
132
|
+
await redis.quit().catch(() => { });
|
|
133
|
+
},
|
|
134
|
+
};
|
|
149
135
|
}
|
|
150
|
-
|
|
151
|
-
|
|
136
|
+
/**
|
|
137
|
+
* Check if Redis queue backend is configured.
|
|
138
|
+
*/
|
|
139
|
+
export function isRedisQueueEnabled() {
|
|
140
|
+
return !!(process.env.REDIS_URL && process.env.JOB_QUEUE_BACKEND === 'redis');
|
|
152
141
|
}
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
142
|
+
/**
|
|
143
|
+
* Convenience: create queue only if enabled, else return null.
|
|
144
|
+
*/
|
|
145
|
+
export async function maybeCreateRedisQueue() {
|
|
146
|
+
if (!isRedisQueueEnabled())
|
|
147
|
+
return null;
|
|
148
|
+
return createRedisQueue().catch((e) => {
|
|
149
|
+
console.error('[redis-queue] Failed to connect:', e.message);
|
|
150
|
+
return null;
|
|
151
|
+
});
|
|
159
152
|
}
|
|
160
|
-
export {
|
|
161
|
-
createRedisQueue,
|
|
162
|
-
isRedisQueueEnabled,
|
|
163
|
-
maybeCreateRedisQueue
|
|
164
|
-
};
|
|
165
|
-
//# sourceMappingURL=redis-queue.js.map
|