@meridianjs/job-queue-redis 0.1.0
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.d.mts +41 -0
- package/dist/index.d.ts +41 -0
- package/dist/index.js +108 -0
- package/dist/index.mjs +73 -0
- package/package.json +42 -0
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { IScheduler, ScheduledJobConfig, ModuleDefinition } from '@meridianjs/types';
|
|
2
|
+
|
|
3
|
+
interface JobQueueOptions {
|
|
4
|
+
url: string;
|
|
5
|
+
/** Prefix for all queue names — defaults to "meridian:job" */
|
|
6
|
+
prefix?: string;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* BullMQ-backed scheduler for cron and interval jobs.
|
|
10
|
+
*
|
|
11
|
+
* Each registered job gets its own named queue (`meridian:job:<name>`).
|
|
12
|
+
* BullMQ handles persistence, retries, and scheduling across restarts.
|
|
13
|
+
*
|
|
14
|
+
* Supports:
|
|
15
|
+
* schedule: "0 2 * * *" — cron expression (runs daily at 2 AM)
|
|
16
|
+
* schedule: { interval: 5000 } — millisecond interval (runs every 5 s)
|
|
17
|
+
*
|
|
18
|
+
* For local development without Redis, jobs are unavailable.
|
|
19
|
+
* Use the scheduler module only when Redis is configured.
|
|
20
|
+
*/
|
|
21
|
+
declare class RedisScheduler implements IScheduler {
|
|
22
|
+
private readonly connection;
|
|
23
|
+
private readonly prefix;
|
|
24
|
+
private readonly queues;
|
|
25
|
+
private readonly workers;
|
|
26
|
+
constructor(options: JobQueueOptions);
|
|
27
|
+
register(config: ScheduledJobConfig, fn: () => Promise<void>): Promise<void>;
|
|
28
|
+
close(): Promise<void>;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Meridian module definition for the Redis job scheduler.
|
|
32
|
+
*
|
|
33
|
+
* Register in meridian.config.ts:
|
|
34
|
+
* @example
|
|
35
|
+
* modules: [
|
|
36
|
+
* { resolve: "@meridianjs/job-queue-redis", options: { url: process.env.REDIS_URL } }
|
|
37
|
+
* ]
|
|
38
|
+
*/
|
|
39
|
+
declare const JobQueueRedisModule: ModuleDefinition;
|
|
40
|
+
|
|
41
|
+
export { type JobQueueOptions, RedisScheduler, JobQueueRedisModule as default };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { IScheduler, ScheduledJobConfig, ModuleDefinition } from '@meridianjs/types';
|
|
2
|
+
|
|
3
|
+
interface JobQueueOptions {
|
|
4
|
+
url: string;
|
|
5
|
+
/** Prefix for all queue names — defaults to "meridian:job" */
|
|
6
|
+
prefix?: string;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* BullMQ-backed scheduler for cron and interval jobs.
|
|
10
|
+
*
|
|
11
|
+
* Each registered job gets its own named queue (`meridian:job:<name>`).
|
|
12
|
+
* BullMQ handles persistence, retries, and scheduling across restarts.
|
|
13
|
+
*
|
|
14
|
+
* Supports:
|
|
15
|
+
* schedule: "0 2 * * *" — cron expression (runs daily at 2 AM)
|
|
16
|
+
* schedule: { interval: 5000 } — millisecond interval (runs every 5 s)
|
|
17
|
+
*
|
|
18
|
+
* For local development without Redis, jobs are unavailable.
|
|
19
|
+
* Use the scheduler module only when Redis is configured.
|
|
20
|
+
*/
|
|
21
|
+
declare class RedisScheduler implements IScheduler {
|
|
22
|
+
private readonly connection;
|
|
23
|
+
private readonly prefix;
|
|
24
|
+
private readonly queues;
|
|
25
|
+
private readonly workers;
|
|
26
|
+
constructor(options: JobQueueOptions);
|
|
27
|
+
register(config: ScheduledJobConfig, fn: () => Promise<void>): Promise<void>;
|
|
28
|
+
close(): Promise<void>;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Meridian module definition for the Redis job scheduler.
|
|
32
|
+
*
|
|
33
|
+
* Register in meridian.config.ts:
|
|
34
|
+
* @example
|
|
35
|
+
* modules: [
|
|
36
|
+
* { resolve: "@meridianjs/job-queue-redis", options: { url: process.env.REDIS_URL } }
|
|
37
|
+
* ]
|
|
38
|
+
*/
|
|
39
|
+
declare const JobQueueRedisModule: ModuleDefinition;
|
|
40
|
+
|
|
41
|
+
export { type JobQueueOptions, RedisScheduler, JobQueueRedisModule as default };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
|
+
RedisScheduler: () => RedisScheduler,
|
|
34
|
+
default: () => index_default
|
|
35
|
+
});
|
|
36
|
+
module.exports = __toCommonJS(index_exports);
|
|
37
|
+
var import_bullmq = require("bullmq");
|
|
38
|
+
var import_ioredis = __toESM(require("ioredis"));
|
|
39
|
+
var RedisScheduler = class {
|
|
40
|
+
connection;
|
|
41
|
+
prefix;
|
|
42
|
+
queues = /* @__PURE__ */ new Map();
|
|
43
|
+
workers = /* @__PURE__ */ new Map();
|
|
44
|
+
constructor(options) {
|
|
45
|
+
this.prefix = options.prefix ?? "meridian:job";
|
|
46
|
+
this.connection = new import_ioredis.default(options.url, {
|
|
47
|
+
maxRetriesPerRequest: null
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
async register(config, fn) {
|
|
51
|
+
const queueName = `${this.prefix}:${config.name}`;
|
|
52
|
+
const queue = new import_bullmq.Queue(queueName, {
|
|
53
|
+
connection: this.connection
|
|
54
|
+
});
|
|
55
|
+
const repeatOpts = typeof config.schedule === "string" ? { pattern: config.schedule } : { every: config.schedule.interval };
|
|
56
|
+
const existingRepeatable = await queue.getRepeatableJobs();
|
|
57
|
+
for (const job of existingRepeatable) {
|
|
58
|
+
await queue.removeRepeatableByKey(job.key);
|
|
59
|
+
}
|
|
60
|
+
await queue.add(
|
|
61
|
+
config.name,
|
|
62
|
+
{},
|
|
63
|
+
{
|
|
64
|
+
repeat: {
|
|
65
|
+
...repeatOpts,
|
|
66
|
+
...config.numberOfExecutions ? { limit: config.numberOfExecutions } : {}
|
|
67
|
+
},
|
|
68
|
+
removeOnComplete: { count: 100 },
|
|
69
|
+
removeOnFail: { count: 50 }
|
|
70
|
+
}
|
|
71
|
+
);
|
|
72
|
+
const worker = new import_bullmq.Worker(
|
|
73
|
+
queueName,
|
|
74
|
+
async () => {
|
|
75
|
+
await fn();
|
|
76
|
+
},
|
|
77
|
+
{
|
|
78
|
+
connection: this.connection,
|
|
79
|
+
concurrency: 1
|
|
80
|
+
// jobs run sequentially per name
|
|
81
|
+
}
|
|
82
|
+
);
|
|
83
|
+
worker.on("completed", (job) => {
|
|
84
|
+
console.info(`[Scheduler] Job "${config.name}" completed (id: ${job.id})`);
|
|
85
|
+
});
|
|
86
|
+
worker.on("failed", (job, err) => {
|
|
87
|
+
console.error(`[Scheduler] Job "${config.name}" failed:`, err.message);
|
|
88
|
+
});
|
|
89
|
+
this.queues.set(config.name, queue);
|
|
90
|
+
this.workers.set(config.name, worker);
|
|
91
|
+
}
|
|
92
|
+
async close() {
|
|
93
|
+
await Promise.all([
|
|
94
|
+
...[...this.workers.values()].map((w) => w.close()),
|
|
95
|
+
...[...this.queues.values()].map((q) => q.close())
|
|
96
|
+
]);
|
|
97
|
+
await this.connection.quit();
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
var JobQueueRedisModule = {
|
|
101
|
+
key: "scheduler",
|
|
102
|
+
service: RedisScheduler
|
|
103
|
+
};
|
|
104
|
+
var index_default = JobQueueRedisModule;
|
|
105
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
106
|
+
0 && (module.exports = {
|
|
107
|
+
RedisScheduler
|
|
108
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import { Queue, Worker } from "bullmq";
|
|
3
|
+
import IORedis from "ioredis";
|
|
4
|
+
var RedisScheduler = class {
|
|
5
|
+
connection;
|
|
6
|
+
prefix;
|
|
7
|
+
queues = /* @__PURE__ */ new Map();
|
|
8
|
+
workers = /* @__PURE__ */ new Map();
|
|
9
|
+
constructor(options) {
|
|
10
|
+
this.prefix = options.prefix ?? "meridian:job";
|
|
11
|
+
this.connection = new IORedis(options.url, {
|
|
12
|
+
maxRetriesPerRequest: null
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
async register(config, fn) {
|
|
16
|
+
const queueName = `${this.prefix}:${config.name}`;
|
|
17
|
+
const queue = new Queue(queueName, {
|
|
18
|
+
connection: this.connection
|
|
19
|
+
});
|
|
20
|
+
const repeatOpts = typeof config.schedule === "string" ? { pattern: config.schedule } : { every: config.schedule.interval };
|
|
21
|
+
const existingRepeatable = await queue.getRepeatableJobs();
|
|
22
|
+
for (const job of existingRepeatable) {
|
|
23
|
+
await queue.removeRepeatableByKey(job.key);
|
|
24
|
+
}
|
|
25
|
+
await queue.add(
|
|
26
|
+
config.name,
|
|
27
|
+
{},
|
|
28
|
+
{
|
|
29
|
+
repeat: {
|
|
30
|
+
...repeatOpts,
|
|
31
|
+
...config.numberOfExecutions ? { limit: config.numberOfExecutions } : {}
|
|
32
|
+
},
|
|
33
|
+
removeOnComplete: { count: 100 },
|
|
34
|
+
removeOnFail: { count: 50 }
|
|
35
|
+
}
|
|
36
|
+
);
|
|
37
|
+
const worker = new Worker(
|
|
38
|
+
queueName,
|
|
39
|
+
async () => {
|
|
40
|
+
await fn();
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
connection: this.connection,
|
|
44
|
+
concurrency: 1
|
|
45
|
+
// jobs run sequentially per name
|
|
46
|
+
}
|
|
47
|
+
);
|
|
48
|
+
worker.on("completed", (job) => {
|
|
49
|
+
console.info(`[Scheduler] Job "${config.name}" completed (id: ${job.id})`);
|
|
50
|
+
});
|
|
51
|
+
worker.on("failed", (job, err) => {
|
|
52
|
+
console.error(`[Scheduler] Job "${config.name}" failed:`, err.message);
|
|
53
|
+
});
|
|
54
|
+
this.queues.set(config.name, queue);
|
|
55
|
+
this.workers.set(config.name, worker);
|
|
56
|
+
}
|
|
57
|
+
async close() {
|
|
58
|
+
await Promise.all([
|
|
59
|
+
...[...this.workers.values()].map((w) => w.close()),
|
|
60
|
+
...[...this.queues.values()].map((q) => q.close())
|
|
61
|
+
]);
|
|
62
|
+
await this.connection.quit();
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
var JobQueueRedisModule = {
|
|
66
|
+
key: "scheduler",
|
|
67
|
+
service: RedisScheduler
|
|
68
|
+
};
|
|
69
|
+
var index_default = JobQueueRedisModule;
|
|
70
|
+
export {
|
|
71
|
+
RedisScheduler,
|
|
72
|
+
index_default as default
|
|
73
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@meridianjs/job-queue-redis",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Meridian job queue — BullMQ-backed IScheduler for cron and interval jobs",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"module": "./dist/index.mjs",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"import": {
|
|
11
|
+
"types": "./dist/index.d.mts",
|
|
12
|
+
"default": "./dist/index.mjs"
|
|
13
|
+
},
|
|
14
|
+
"require": {
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"default": "./dist/index.js"
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
"scripts": {
|
|
21
|
+
"build": "tsup src/index.ts --format esm,cjs --dts --clean",
|
|
22
|
+
"dev": "tsup src/index.ts --format esm,cjs --dts --watch",
|
|
23
|
+
"typecheck": "tsc --noEmit",
|
|
24
|
+
"clean": "rm -rf dist",
|
|
25
|
+
"prepublishOnly": "npm run build"
|
|
26
|
+
},
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"@meridianjs/types": "^0.1.0",
|
|
29
|
+
"bullmq": "^5.0.0",
|
|
30
|
+
"ioredis": "^5.3.2"
|
|
31
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"tsup": "^8.3.5",
|
|
34
|
+
"typescript": "*"
|
|
35
|
+
},
|
|
36
|
+
"files": [
|
|
37
|
+
"dist"
|
|
38
|
+
],
|
|
39
|
+
"publishConfig": {
|
|
40
|
+
"access": "public"
|
|
41
|
+
}
|
|
42
|
+
}
|