@redmix/jobs 0.0.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/LICENSE +21 -0
- package/README.md +3 -0
- package/dist/adapters/BaseAdapter/BaseAdapter.d.ts +64 -0
- package/dist/adapters/BaseAdapter/BaseAdapter.d.ts.map +1 -0
- package/dist/adapters/BaseAdapter/BaseAdapter.js +36 -0
- package/dist/adapters/PrismaAdapter/PrismaAdapter.d.ts +68 -0
- package/dist/adapters/PrismaAdapter/PrismaAdapter.d.ts.map +1 -0
- package/dist/adapters/PrismaAdapter/PrismaAdapter.js +189 -0
- package/dist/adapters/PrismaAdapter/errors.d.ts +8 -0
- package/dist/adapters/PrismaAdapter/errors.d.ts.map +1 -0
- package/dist/adapters/PrismaAdapter/errors.js +33 -0
- package/dist/bins/rw-jobs-worker.d.ts +13 -0
- package/dist/bins/rw-jobs-worker.d.ts.map +1 -0
- package/dist/bins/rw-jobs-worker.js +126 -0
- package/dist/bins/rw-jobs.d.ts +20 -0
- package/dist/bins/rw-jobs.d.ts.map +1 -0
- package/dist/bins/rw-jobs.js +248 -0
- package/dist/consts.d.ts +26 -0
- package/dist/consts.d.ts.map +1 -0
- package/dist/consts.js +81 -0
- package/dist/core/Executor.d.ts +29 -0
- package/dist/core/Executor.d.ts.map +1 -0
- package/dist/core/Executor.js +95 -0
- package/dist/core/JobManager.d.ts +20 -0
- package/dist/core/JobManager.d.ts.map +1 -0
- package/dist/core/JobManager.js +78 -0
- package/dist/core/Scheduler.d.ts +27 -0
- package/dist/core/Scheduler.d.ts.map +1 -0
- package/dist/core/Scheduler.js +91 -0
- package/dist/core/Worker.d.ts +49 -0
- package/dist/core/Worker.d.ts.map +1 -0
- package/dist/core/Worker.js +143 -0
- package/dist/errors.d.ts +104 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +156 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +43 -0
- package/dist/loaders.d.ts +13 -0
- package/dist/loaders.d.ts.map +1 -0
- package/dist/loaders.js +71 -0
- package/dist/setupEnv.d.ts +2 -0
- package/dist/setupEnv.d.ts.map +1 -0
- package/dist/setupEnv.js +34 -0
- package/dist/types.d.ts +175 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +16 -0
- package/dist/util.d.ts +2 -0
- package/dist/util.d.ts.map +1 -0
- package/dist/util.js +31 -0
- package/package.json +58 -0
package/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2025 Redmix
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
import type { BaseJob, BasicLogger, PossibleBaseJob } from '../../types.js';
|
2
|
+
export interface SchedulePayload {
|
3
|
+
name: string;
|
4
|
+
path: string;
|
5
|
+
args: unknown[];
|
6
|
+
runAt: Date;
|
7
|
+
queue: string;
|
8
|
+
priority: number;
|
9
|
+
}
|
10
|
+
export interface FindArgs {
|
11
|
+
processName: string;
|
12
|
+
maxRuntime: number;
|
13
|
+
queues: string[];
|
14
|
+
}
|
15
|
+
export interface BaseAdapterOptions {
|
16
|
+
logger?: BasicLogger;
|
17
|
+
}
|
18
|
+
export interface SuccessOptions<TJob extends BaseJob = BaseJob> {
|
19
|
+
job: TJob;
|
20
|
+
deleteJob?: boolean;
|
21
|
+
}
|
22
|
+
export interface ErrorOptions<TJob extends BaseJob = BaseJob> {
|
23
|
+
job: TJob;
|
24
|
+
error: Error;
|
25
|
+
}
|
26
|
+
export interface FailureOptions<TJob extends BaseJob = BaseJob> {
|
27
|
+
job: TJob;
|
28
|
+
deleteJob?: boolean;
|
29
|
+
}
|
30
|
+
/**
|
31
|
+
* Base class for all job adapters. Provides a common interface for scheduling
|
32
|
+
* jobs. At a minimum, you must implement the `schedule` method in your adapter.
|
33
|
+
*
|
34
|
+
* Any object passed to the constructor is saved in `this.options` and should
|
35
|
+
* be used to configure your custom adapter. If `options.logger` is included
|
36
|
+
* you can access it via `this.logger`
|
37
|
+
*/
|
38
|
+
export declare abstract class BaseAdapter<TOptions extends BaseAdapterOptions = BaseAdapterOptions, TScheduleReturn = void | Promise<void>> {
|
39
|
+
options: TOptions;
|
40
|
+
logger: NonNullable<TOptions['logger']>;
|
41
|
+
constructor(options: TOptions);
|
42
|
+
abstract schedule(payload: SchedulePayload): TScheduleReturn;
|
43
|
+
/**
|
44
|
+
* Find a single job that's eligible to run with the given args
|
45
|
+
*/
|
46
|
+
abstract find(args: FindArgs): PossibleBaseJob | Promise<PossibleBaseJob>;
|
47
|
+
/**
|
48
|
+
* Called when a job has successfully completed
|
49
|
+
*/
|
50
|
+
abstract success(options: SuccessOptions): void | Promise<void>;
|
51
|
+
/**
|
52
|
+
* Called when an attempt to run a job produced an error
|
53
|
+
*/
|
54
|
+
abstract error(options: ErrorOptions): void | Promise<void>;
|
55
|
+
/**
|
56
|
+
* Called when a job has errored more than maxAttempts and will not be retried
|
57
|
+
*/
|
58
|
+
abstract failure(options: FailureOptions): void | Promise<void>;
|
59
|
+
/**
|
60
|
+
* Clear all jobs from storage
|
61
|
+
*/
|
62
|
+
abstract clear(): void | Promise<void>;
|
63
|
+
}
|
64
|
+
//# sourceMappingURL=BaseAdapter.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"BaseAdapter.d.ts","sourceRoot":"","sources":["../../../src/adapters/BaseAdapter/BaseAdapter.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,OAAO,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAA;AAG3E,MAAM,WAAW,eAAe;IAC9B,IAAI,EAAE,MAAM,CAAA;IACZ,IAAI,EAAE,MAAM,CAAA;IACZ,IAAI,EAAE,OAAO,EAAE,CAAA;IACf,KAAK,EAAE,IAAI,CAAA;IACX,KAAK,EAAE,MAAM,CAAA;IACb,QAAQ,EAAE,MAAM,CAAA;CACjB;AAED,MAAM,WAAW,QAAQ;IACvB,WAAW,EAAE,MAAM,CAAA;IACnB,UAAU,EAAE,MAAM,CAAA;IAClB,MAAM,EAAE,MAAM,EAAE,CAAA;CACjB;AAED,MAAM,WAAW,kBAAkB;IACjC,MAAM,CAAC,EAAE,WAAW,CAAA;CACrB;AAED,MAAM,WAAW,cAAc,CAAC,IAAI,SAAS,OAAO,GAAG,OAAO;IAC5D,GAAG,EAAE,IAAI,CAAA;IACT,SAAS,CAAC,EAAE,OAAO,CAAA;CACpB;AAED,MAAM,WAAW,YAAY,CAAC,IAAI,SAAS,OAAO,GAAG,OAAO;IAC1D,GAAG,EAAE,IAAI,CAAA;IACT,KAAK,EAAE,KAAK,CAAA;CACb;AAED,MAAM,WAAW,cAAc,CAAC,IAAI,SAAS,OAAO,GAAG,OAAO;IAC5D,GAAG,EAAE,IAAI,CAAA;IACT,SAAS,CAAC,EAAE,OAAO,CAAA;CACpB;AAED;;;;;;;GAOG;AACH,8BAAsB,WAAW,CAC/B,QAAQ,SAAS,kBAAkB,GAAG,kBAAkB,EACxD,eAAe,GAAG,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;IAEtC,OAAO,EAAE,QAAQ,CAAA;IACjB,MAAM,EAAE,WAAW,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC,CAAA;gBAE3B,OAAO,EAAE,QAAQ;IAU7B,QAAQ,CAAC,QAAQ,CAAC,OAAO,EAAE,eAAe,GAAG,eAAe;IAE5D;;OAEG;IACH,QAAQ,CAAC,IAAI,CAAC,IAAI,EAAE,QAAQ,GAAG,eAAe,GAAG,OAAO,CAAC,eAAe,CAAC;IAEzE;;OAEG;IACH,QAAQ,CAAC,OAAO,CAAC,OAAO,EAAE,cAAc,GAAG,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;IAE/D;;OAEG;IACH,QAAQ,CAAC,KAAK,CAAC,OAAO,EAAE,YAAY,GAAG,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;IAE3D;;OAEG;IACH,QAAQ,CAAC,OAAO,CAAC,OAAO,EAAE,cAAc,GAAG,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;IAE/D;;OAEG;IACH,QAAQ,CAAC,KAAK,IAAI,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;CACvC"}
|
@@ -0,0 +1,36 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __defProp = Object.defineProperty;
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
6
|
+
var __export = (target, all) => {
|
7
|
+
for (var name in all)
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
9
|
+
};
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
12
|
+
for (let key of __getOwnPropNames(from))
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
15
|
+
}
|
16
|
+
return to;
|
17
|
+
};
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
19
|
+
var BaseAdapter_exports = {};
|
20
|
+
__export(BaseAdapter_exports, {
|
21
|
+
BaseAdapter: () => BaseAdapter
|
22
|
+
});
|
23
|
+
module.exports = __toCommonJS(BaseAdapter_exports);
|
24
|
+
var import_consts = require("../../consts.js");
|
25
|
+
class BaseAdapter {
|
26
|
+
options;
|
27
|
+
logger;
|
28
|
+
constructor(options) {
|
29
|
+
this.options = options;
|
30
|
+
this.logger = options?.logger ?? import_consts.DEFAULT_LOGGER;
|
31
|
+
}
|
32
|
+
}
|
33
|
+
// Annotate the CommonJS export names for ESM import in node:
|
34
|
+
0 && (module.exports = {
|
35
|
+
BaseAdapter
|
36
|
+
});
|
@@ -0,0 +1,68 @@
|
|
1
|
+
import type { PrismaClient } from '@prisma/client';
|
2
|
+
import type { BaseJob } from '../../types.js';
|
3
|
+
import type { BaseAdapterOptions, SchedulePayload, FindArgs, SuccessOptions, ErrorOptions, FailureOptions } from '../BaseAdapter/BaseAdapter.js';
|
4
|
+
import { BaseAdapter } from '../BaseAdapter/BaseAdapter.js';
|
5
|
+
export interface PrismaJob extends BaseJob {
|
6
|
+
id: number;
|
7
|
+
handler: string;
|
8
|
+
runAt: Date;
|
9
|
+
lockedAt: Date;
|
10
|
+
lockedBy: string;
|
11
|
+
lastError: string | null;
|
12
|
+
failedAt: Date | null;
|
13
|
+
createdAt: Date;
|
14
|
+
updatedAt: Date;
|
15
|
+
}
|
16
|
+
export interface PrismaAdapterOptions extends BaseAdapterOptions {
|
17
|
+
/**
|
18
|
+
* An instance of PrismaClient which will be used to talk to the database
|
19
|
+
*/
|
20
|
+
db: PrismaClient;
|
21
|
+
/**
|
22
|
+
* The name of the model in the Prisma schema that represents the job table.
|
23
|
+
* @default 'BackgroundJob'
|
24
|
+
*/
|
25
|
+
model?: string;
|
26
|
+
}
|
27
|
+
/**
|
28
|
+
* Implements a job adapter using Prisma ORM.
|
29
|
+
*
|
30
|
+
* Assumes a table exists with the following schema (the table name can be customized):
|
31
|
+
* ```prisma
|
32
|
+
* model BackgroundJob {
|
33
|
+
* id Int \@id \@default(autoincrement())
|
34
|
+
* attempts Int \@default(0)
|
35
|
+
* handler String
|
36
|
+
* queue String
|
37
|
+
* priority Int
|
38
|
+
* runAt DateTime
|
39
|
+
* lockedAt DateTime?
|
40
|
+
* lockedBy String?
|
41
|
+
* lastError String?
|
42
|
+
* failedAt DateTime?
|
43
|
+
* createdAt DateTime \@default(now())
|
44
|
+
* updatedAt DateTime \@updatedAt
|
45
|
+
* }
|
46
|
+
* ```
|
47
|
+
*/
|
48
|
+
export declare class PrismaAdapter extends BaseAdapter<PrismaAdapterOptions> {
|
49
|
+
db: PrismaClient;
|
50
|
+
model: string;
|
51
|
+
accessor: PrismaClient[keyof PrismaClient];
|
52
|
+
provider: string;
|
53
|
+
constructor(options: PrismaAdapterOptions);
|
54
|
+
/**
|
55
|
+
* Finds the next job to run, locking it so that no other process can pick it
|
56
|
+
* The act of locking a job is dependant on the DB server, so we'll run some
|
57
|
+
* raw SQL to do it in each case—Prisma doesn't provide enough flexibility
|
58
|
+
* in their generated code to do this in a DB-agnostic way.
|
59
|
+
*/
|
60
|
+
find({ processName, maxRuntime, queues, }: FindArgs): Promise<PrismaJob | undefined>;
|
61
|
+
success({ job, deleteJob }: SuccessOptions<PrismaJob>): Promise<void>;
|
62
|
+
error({ job, error }: ErrorOptions<PrismaJob>): Promise<void>;
|
63
|
+
failure({ job, deleteJob }: FailureOptions<PrismaJob>): Promise<void>;
|
64
|
+
schedule({ name, path, args, runAt, queue, priority, }: SchedulePayload): Promise<void>;
|
65
|
+
clear(): Promise<void>;
|
66
|
+
backoffMilliseconds(attempts: number): number;
|
67
|
+
}
|
68
|
+
//# sourceMappingURL=PrismaAdapter.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"PrismaAdapter.d.ts","sourceRoot":"","sources":["../../../src/adapters/PrismaAdapter/PrismaAdapter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAA;AAIlD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAA;AAC7C,OAAO,KAAK,EACV,kBAAkB,EAClB,eAAe,EACf,QAAQ,EACR,cAAc,EACd,YAAY,EACZ,cAAc,EACf,MAAM,+BAA+B,CAAA;AACtC,OAAO,EAAE,WAAW,EAAE,MAAM,+BAA+B,CAAA;AAI3D,MAAM,WAAW,SAAU,SAAQ,OAAO;IACxC,EAAE,EAAE,MAAM,CAAA;IACV,OAAO,EAAE,MAAM,CAAA;IACf,KAAK,EAAE,IAAI,CAAA;IACX,QAAQ,EAAE,IAAI,CAAA;IACd,QAAQ,EAAE,MAAM,CAAA;IAChB,SAAS,EAAE,MAAM,GAAG,IAAI,CAAA;IACxB,QAAQ,EAAE,IAAI,GAAG,IAAI,CAAA;IACrB,SAAS,EAAE,IAAI,CAAA;IACf,SAAS,EAAE,IAAI,CAAA;CAChB;AAED,MAAM,WAAW,oBAAqB,SAAQ,kBAAkB;IAC9D;;OAEG;IACH,EAAE,EAAE,YAAY,CAAA;IAEhB;;;OAGG;IACH,KAAK,CAAC,EAAE,MAAM,CAAA;CACf;AAUD;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,qBAAa,aAAc,SAAQ,WAAW,CAAC,oBAAoB,CAAC;IAClE,EAAE,EAAE,YAAY,CAAA;IAChB,KAAK,EAAE,MAAM,CAAA;IACb,QAAQ,EAAE,YAAY,CAAC,MAAM,YAAY,CAAC,CAAA;IAC1C,QAAQ,EAAE,MAAM,CAAA;gBAEJ,OAAO,EAAE,oBAAoB;IAqBzC;;;;;OAKG;IACY,IAAI,CAAC,EAClB,WAAW,EACX,UAAU,EACV,MAAM,GACP,EAAE,QAAQ,GAAG,OAAO,CAAC,SAAS,GAAG,SAAS,CAAC;IA4F7B,OAAO,CAAC,EAAE,GAAG,EAAE,SAAS,EAAE,EAAE,cAAc,CAAC,SAAS,CAAC;IAkBrD,KAAK,CAAC,EAAE,GAAG,EAAE,KAAK,EAAE,EAAE,YAAY,CAAC,SAAS,CAAC;IAmB7C,OAAO,CAAC,EAAE,GAAG,EAAE,SAAS,EAAE,EAAE,cAAc,CAAC,SAAS,CAAC;IAYrD,QAAQ,CAAC,EACtB,IAAI,EACJ,IAAI,EACJ,IAAI,EACJ,KAAK,EACL,KAAK,EACL,QAAQ,GACT,EAAE,eAAe;IAWH,KAAK;IAIpB,mBAAmB,CAAC,QAAQ,EAAE,MAAM;CAGrC"}
|
@@ -0,0 +1,189 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __defProp = Object.defineProperty;
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
6
|
+
var __export = (target, all) => {
|
7
|
+
for (var name in all)
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
9
|
+
};
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
12
|
+
for (let key of __getOwnPropNames(from))
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
15
|
+
}
|
16
|
+
return to;
|
17
|
+
};
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
19
|
+
var PrismaAdapter_exports = {};
|
20
|
+
__export(PrismaAdapter_exports, {
|
21
|
+
PrismaAdapter: () => PrismaAdapter
|
22
|
+
});
|
23
|
+
module.exports = __toCommonJS(PrismaAdapter_exports);
|
24
|
+
var import_change_case = require("change-case");
|
25
|
+
var import_consts = require("../../consts.js");
|
26
|
+
var import_BaseAdapter = require("../BaseAdapter/BaseAdapter.js");
|
27
|
+
var import_errors = require("./errors.js");
|
28
|
+
class PrismaAdapter extends import_BaseAdapter.BaseAdapter {
|
29
|
+
db;
|
30
|
+
model;
|
31
|
+
accessor;
|
32
|
+
provider;
|
33
|
+
constructor(options) {
|
34
|
+
super(options);
|
35
|
+
this.db = options.db;
|
36
|
+
this.model = options.model || import_consts.DEFAULT_MODEL_NAME;
|
37
|
+
this.accessor = this.db[(0, import_change_case.camelCase)(this.model)];
|
38
|
+
this.provider = options.db._activeProvider;
|
39
|
+
if (!this.accessor) {
|
40
|
+
throw new import_errors.ModelNameError(this.model);
|
41
|
+
}
|
42
|
+
}
|
43
|
+
/**
|
44
|
+
* Finds the next job to run, locking it so that no other process can pick it
|
45
|
+
* The act of locking a job is dependant on the DB server, so we'll run some
|
46
|
+
* raw SQL to do it in each case—Prisma doesn't provide enough flexibility
|
47
|
+
* in their generated code to do this in a DB-agnostic way.
|
48
|
+
*/
|
49
|
+
async find({
|
50
|
+
processName,
|
51
|
+
maxRuntime,
|
52
|
+
queues
|
53
|
+
}) {
|
54
|
+
const maxRuntimeExpire = new Date(
|
55
|
+
(/* @__PURE__ */ new Date()).getTime() + (maxRuntime || import_consts.DEFAULT_MAX_RUNTIME * 1e3)
|
56
|
+
);
|
57
|
+
const where = {
|
58
|
+
AND: [
|
59
|
+
{
|
60
|
+
OR: [
|
61
|
+
{
|
62
|
+
AND: [
|
63
|
+
{ runAt: { lte: /* @__PURE__ */ new Date() } },
|
64
|
+
{
|
65
|
+
OR: [
|
66
|
+
{ lockedAt: null },
|
67
|
+
{
|
68
|
+
lockedAt: {
|
69
|
+
lt: maxRuntimeExpire
|
70
|
+
}
|
71
|
+
}
|
72
|
+
]
|
73
|
+
}
|
74
|
+
]
|
75
|
+
},
|
76
|
+
{ lockedBy: processName }
|
77
|
+
]
|
78
|
+
},
|
79
|
+
{ failedAt: null }
|
80
|
+
]
|
81
|
+
};
|
82
|
+
const whereWithQueue = where;
|
83
|
+
if (queues.length > 1 || queues[0] !== "*") {
|
84
|
+
Object.assign(whereWithQueue, {
|
85
|
+
AND: [...where.AND, { queue: { in: queues } }]
|
86
|
+
});
|
87
|
+
}
|
88
|
+
const job = await this.accessor.findFirst({
|
89
|
+
select: { id: true, attempts: true },
|
90
|
+
where: whereWithQueue,
|
91
|
+
orderBy: [{ priority: "asc" }, { runAt: "asc" }],
|
92
|
+
take: 1
|
93
|
+
});
|
94
|
+
if (job) {
|
95
|
+
const whereWithQueueAndId = Object.assign(whereWithQueue, {
|
96
|
+
AND: [...whereWithQueue.AND, { id: job.id }]
|
97
|
+
});
|
98
|
+
const { count } = await this.accessor.updateMany({
|
99
|
+
where: whereWithQueueAndId,
|
100
|
+
data: {
|
101
|
+
lockedAt: /* @__PURE__ */ new Date(),
|
102
|
+
lockedBy: processName,
|
103
|
+
attempts: job.attempts + 1
|
104
|
+
}
|
105
|
+
});
|
106
|
+
if (count) {
|
107
|
+
const data = await this.accessor.findFirst({ where: { id: job.id } });
|
108
|
+
const { name, path, args } = JSON.parse(data.handler);
|
109
|
+
return { ...data, name, path, args };
|
110
|
+
}
|
111
|
+
}
|
112
|
+
return void 0;
|
113
|
+
}
|
114
|
+
// Prisma queries are lazily evaluated and only sent to the db when they are
|
115
|
+
// awaited, so do the await here to ensure they actually run (if the user
|
116
|
+
// doesn't await the Promise then the queries will never be executed!)
|
117
|
+
async success({ job, deleteJob }) {
|
118
|
+
this.logger.debug(`[RedwoodJob] Job ${job.id} success`);
|
119
|
+
if (deleteJob) {
|
120
|
+
await this.accessor.delete({ where: { id: job.id } });
|
121
|
+
} else {
|
122
|
+
await this.accessor.update({
|
123
|
+
where: { id: job.id },
|
124
|
+
data: {
|
125
|
+
lockedAt: null,
|
126
|
+
lockedBy: null,
|
127
|
+
lastError: null,
|
128
|
+
runAt: null
|
129
|
+
}
|
130
|
+
});
|
131
|
+
}
|
132
|
+
}
|
133
|
+
async error({ job, error }) {
|
134
|
+
this.logger.debug(`[RedwoodJob] Job ${job.id} failure`);
|
135
|
+
const data = {
|
136
|
+
lockedAt: null,
|
137
|
+
lockedBy: null,
|
138
|
+
lastError: `${error.message}
|
139
|
+
|
140
|
+
${error.stack}`,
|
141
|
+
runAt: new Date(
|
142
|
+
(/* @__PURE__ */ new Date()).getTime() + this.backoffMilliseconds(job.attempts)
|
143
|
+
)
|
144
|
+
};
|
145
|
+
await this.accessor.update({
|
146
|
+
where: { id: job.id },
|
147
|
+
data
|
148
|
+
});
|
149
|
+
}
|
150
|
+
// Job has had too many attempts, it has now permanently failed.
|
151
|
+
async failure({ job, deleteJob }) {
|
152
|
+
if (deleteJob) {
|
153
|
+
await this.accessor.delete({ where: { id: job.id } });
|
154
|
+
} else {
|
155
|
+
await this.accessor.update({
|
156
|
+
where: { id: job.id },
|
157
|
+
data: { failedAt: /* @__PURE__ */ new Date() }
|
158
|
+
});
|
159
|
+
}
|
160
|
+
}
|
161
|
+
// Schedules a job by creating a new record in the background job table
|
162
|
+
async schedule({
|
163
|
+
name,
|
164
|
+
path,
|
165
|
+
args,
|
166
|
+
runAt,
|
167
|
+
queue,
|
168
|
+
priority
|
169
|
+
}) {
|
170
|
+
await this.accessor.create({
|
171
|
+
data: {
|
172
|
+
handler: JSON.stringify({ name, path, args }),
|
173
|
+
runAt,
|
174
|
+
queue,
|
175
|
+
priority
|
176
|
+
}
|
177
|
+
});
|
178
|
+
}
|
179
|
+
async clear() {
|
180
|
+
await this.accessor.deleteMany();
|
181
|
+
}
|
182
|
+
backoffMilliseconds(attempts) {
|
183
|
+
return 1e3 * attempts ** 4;
|
184
|
+
}
|
185
|
+
}
|
186
|
+
// Annotate the CommonJS export names for ESM import in node:
|
187
|
+
0 && (module.exports = {
|
188
|
+
PrismaAdapter
|
189
|
+
});
|
@@ -0,0 +1,8 @@
|
|
1
|
+
import { RedwoodJobError } from '../../errors.js';
|
2
|
+
/**
|
3
|
+
* Thrown when a given model name isn't actually available in the PrismaClient
|
4
|
+
*/
|
5
|
+
export declare class ModelNameError extends RedwoodJobError {
|
6
|
+
constructor(name: string);
|
7
|
+
}
|
8
|
+
//# sourceMappingURL=errors.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../../src/adapters/PrismaAdapter/errors.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAA;AAEjD;;GAEG;AACH,qBAAa,cAAe,SAAQ,eAAe;gBACrC,IAAI,EAAE,MAAM;CAGzB"}
|
@@ -0,0 +1,33 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __defProp = Object.defineProperty;
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
6
|
+
var __export = (target, all) => {
|
7
|
+
for (var name in all)
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
9
|
+
};
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
12
|
+
for (let key of __getOwnPropNames(from))
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
15
|
+
}
|
16
|
+
return to;
|
17
|
+
};
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
19
|
+
var errors_exports = {};
|
20
|
+
__export(errors_exports, {
|
21
|
+
ModelNameError: () => ModelNameError
|
22
|
+
});
|
23
|
+
module.exports = __toCommonJS(errors_exports);
|
24
|
+
var import_errors = require("../../errors.js");
|
25
|
+
class ModelNameError extends import_errors.RedwoodJobError {
|
26
|
+
constructor(name) {
|
27
|
+
super(`Model \`${name}\` not found in PrismaClient`);
|
28
|
+
}
|
29
|
+
}
|
30
|
+
// Annotate the CommonJS export names for ESM import in node:
|
31
|
+
0 && (module.exports = {
|
32
|
+
ModelNameError
|
33
|
+
});
|
@@ -0,0 +1,13 @@
|
|
1
|
+
#!/usr/bin/env node
|
2
|
+
import type { Worker } from '../core/Worker.js';
|
3
|
+
export declare const processName: ({ id, queues, }: {
|
4
|
+
id: number;
|
5
|
+
queues: string | string[];
|
6
|
+
}) => string;
|
7
|
+
export declare const getWorker: ({ index, id, clear, workoff, }: {
|
8
|
+
index: number;
|
9
|
+
id: number;
|
10
|
+
clear: boolean;
|
11
|
+
workoff: boolean;
|
12
|
+
}) => Promise<Worker>;
|
13
|
+
//# sourceMappingURL=rw-jobs-worker.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"rw-jobs-worker.d.ts","sourceRoot":"","sources":["../../src/bins/rw-jobs-worker.ts"],"names":[],"mappings":";AAWA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAA;AAqC/C,eAAO,MAAM,WAAW,oBAGrB;IACD,EAAE,EAAE,MAAM,CAAA;IACV,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,CAAA;CAC1B,WAEA,CAAA;AAwBD,eAAO,MAAM,SAAS,mCAKnB;IACD,KAAK,EAAE,MAAM,CAAA;IACb,EAAE,EAAE,MAAM,CAAA;IACV,KAAK,EAAE,OAAO,CAAA;IACd,OAAO,EAAE,OAAO,CAAA;CACjB,oBAqBA,CAAA"}
|
@@ -0,0 +1,126 @@
|
|
1
|
+
#!/usr/bin/env node
|
2
|
+
"use strict";
|
3
|
+
var __create = Object.create;
|
4
|
+
var __defProp = Object.defineProperty;
|
5
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
7
|
+
var __getProtoOf = Object.getPrototypeOf;
|
8
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
9
|
+
var __export = (target, all) => {
|
10
|
+
for (var name in all)
|
11
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
12
|
+
};
|
13
|
+
var __copyProps = (to, from, except, desc) => {
|
14
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
15
|
+
for (let key of __getOwnPropNames(from))
|
16
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
17
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
18
|
+
}
|
19
|
+
return to;
|
20
|
+
};
|
21
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
22
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
23
|
+
// file that has been converted to a CommonJS file using a Babel-
|
24
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
25
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
26
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
27
|
+
mod
|
28
|
+
));
|
29
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
30
|
+
var rw_jobs_worker_exports = {};
|
31
|
+
__export(rw_jobs_worker_exports, {
|
32
|
+
getWorker: () => getWorker,
|
33
|
+
processName: () => processName
|
34
|
+
});
|
35
|
+
module.exports = __toCommonJS(rw_jobs_worker_exports);
|
36
|
+
var import_node_process = __toESM(require("node:process"));
|
37
|
+
var import_helpers = require("yargs/helpers");
|
38
|
+
var import_yargs = __toESM(require("yargs/yargs"));
|
39
|
+
var import_consts = require("../consts.js");
|
40
|
+
var import_errors = require("../errors.js");
|
41
|
+
var import_loaders = require("../loaders.js");
|
42
|
+
var import_setupEnv = require("../setupEnv.js");
|
43
|
+
(0, import_setupEnv.setupEnv)();
|
44
|
+
const parseArgs = (argv) => {
|
45
|
+
return (0, import_yargs.default)((0, import_helpers.hideBin)(argv)).usage(
|
46
|
+
"Starts a single RedwoodJob worker to process background jobs\n\nUsage: $0 [options]"
|
47
|
+
).option("index", {
|
48
|
+
type: "number",
|
49
|
+
required: true,
|
50
|
+
description: "The index of the `workers` array from the exported `jobs` config to use to configure this worker"
|
51
|
+
}).option("id", {
|
52
|
+
type: "number",
|
53
|
+
required: true,
|
54
|
+
description: "The worker count id to identify this worker. ie: if you had `count: 2` in your worker config, you would have two workers with ids 0 and 1"
|
55
|
+
}).option("workoff", {
|
56
|
+
type: "boolean",
|
57
|
+
default: false,
|
58
|
+
description: "Work off all jobs in the queue(s) and exit"
|
59
|
+
}).option("clear", {
|
60
|
+
type: "boolean",
|
61
|
+
default: false,
|
62
|
+
description: "Remove all jobs in all queues and exit"
|
63
|
+
}).help().argv;
|
64
|
+
};
|
65
|
+
const processName = ({
|
66
|
+
id,
|
67
|
+
queues
|
68
|
+
}) => {
|
69
|
+
return `${import_consts.PROCESS_TITLE_PREFIX}.${[queues].flat().join("-")}.${id}`;
|
70
|
+
};
|
71
|
+
const setupSignals = ({ worker }) => {
|
72
|
+
import_node_process.default.on("SIGINT", () => {
|
73
|
+
worker.logger.warn(
|
74
|
+
`[${import_node_process.default.title}] SIGINT received at ${(/* @__PURE__ */ new Date()).toISOString()}, finishing work...`
|
75
|
+
);
|
76
|
+
worker.forever = false;
|
77
|
+
});
|
78
|
+
import_node_process.default.on("SIGTERM", () => {
|
79
|
+
worker.logger.warn(
|
80
|
+
`[${import_node_process.default.title}] SIGTERM received at ${(/* @__PURE__ */ new Date()).toISOString()}, exiting now!`
|
81
|
+
);
|
82
|
+
import_node_process.default.exit(0);
|
83
|
+
});
|
84
|
+
};
|
85
|
+
const getWorker = async ({
|
86
|
+
index,
|
87
|
+
id,
|
88
|
+
clear,
|
89
|
+
workoff
|
90
|
+
}) => {
|
91
|
+
let manager;
|
92
|
+
try {
|
93
|
+
manager = await (0, import_loaders.loadJobsManager)();
|
94
|
+
} catch (e) {
|
95
|
+
console.error(e);
|
96
|
+
import_node_process.default.exit(1);
|
97
|
+
}
|
98
|
+
const workerConfig = manager.workers[index];
|
99
|
+
if (!workerConfig) {
|
100
|
+
throw new import_errors.WorkerConfigIndexNotFoundError(index);
|
101
|
+
}
|
102
|
+
return manager.createWorker({
|
103
|
+
index,
|
104
|
+
clear,
|
105
|
+
workoff,
|
106
|
+
processName: processName({ id, queues: workerConfig.queue })
|
107
|
+
});
|
108
|
+
};
|
109
|
+
const main = async () => {
|
110
|
+
const { index, id, clear, workoff } = await parseArgs(import_node_process.default.argv);
|
111
|
+
const worker = await getWorker({ index, id, clear, workoff });
|
112
|
+
import_node_process.default.title = processName({ id, queues: worker.queues });
|
113
|
+
worker.run().then(() => {
|
114
|
+
worker.logger.info(`[${import_node_process.default.title}] Worker finished, shutting down.`);
|
115
|
+
import_node_process.default.exit(0);
|
116
|
+
});
|
117
|
+
setupSignals({ worker });
|
118
|
+
};
|
119
|
+
if (import_node_process.default.env.NODE_ENV !== "test") {
|
120
|
+
main();
|
121
|
+
}
|
122
|
+
// Annotate the CommonJS export names for ESM import in node:
|
123
|
+
0 && (module.exports = {
|
124
|
+
getWorker,
|
125
|
+
processName
|
126
|
+
});
|
@@ -0,0 +1,20 @@
|
|
1
|
+
#!/usr/bin/env node
|
2
|
+
import type { ChildProcess } from 'node:child_process';
|
3
|
+
import type { BasicLogger } from '../types.js';
|
4
|
+
export type NumWorkersConfig = [number, number][];
|
5
|
+
export declare const buildNumWorkers: (config: any) => NumWorkersConfig;
|
6
|
+
export declare const startWorkers: ({ numWorkers, detach, workoff, logger, }: {
|
7
|
+
numWorkers: NumWorkersConfig;
|
8
|
+
detach?: boolean;
|
9
|
+
workoff?: boolean;
|
10
|
+
logger: BasicLogger;
|
11
|
+
}) => ChildProcess[];
|
12
|
+
export declare const stopWorkers: ({ numWorkers, signal, logger, }: {
|
13
|
+
numWorkers: NumWorkersConfig;
|
14
|
+
signal: string;
|
15
|
+
logger: BasicLogger;
|
16
|
+
}) => Promise<void>;
|
17
|
+
export declare const clearQueue: ({ logger }: {
|
18
|
+
logger: BasicLogger;
|
19
|
+
}) => void;
|
20
|
+
//# sourceMappingURL=rw-jobs.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"rw-jobs.d.ts","sourceRoot":"","sources":["../../src/bins/rw-jobs.ts"],"names":[],"mappings":";AAKA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAA;AAatD,OAAO,KAAK,EAEV,WAAW,EAGZ,MAAM,aAAa,CAAA;AAEpB,MAAM,MAAM,gBAAgB,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,EAAE,CAAA;AAsEjD,eAAO,MAAM,eAAe,WAAY,GAAG,qBAU1C,CAAA;AAED,eAAO,MAAM,YAAY,6CAKtB;IACD,UAAU,EAAE,gBAAgB,CAAA;IAC5B,MAAM,CAAC,EAAE,OAAO,CAAA;IAChB,OAAO,CAAC,EAAE,OAAO,CAAA;IACjB,MAAM,EAAE,WAAW,CAAA;CACpB,mBA2BA,CAAA;AAGD,eAAO,MAAM,WAAW,oCAIrB;IACD,UAAU,EAAE,gBAAgB,CAAA;IAC5B,MAAM,EAAE,MAAM,CAAA;IACd,MAAM,EAAE,WAAW,CAAA;CACpB,kBAqBA,CAAA;AAED,eAAO,MAAM,UAAU,eAAgB;IAAE,MAAM,EAAE,WAAW,CAAA;CAAE,SAG7D,CAAA"}
|