@constructive-io/job-scheduler 0.3.22 → 0.4.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/README.md +59 -1
- package/esm/index.js +224 -0
- package/{src/run.ts → esm/run.js} +4 -11
- package/{dist/index.d.ts → index.d.ts} +5 -1
- package/{dist/index.js → index.js} +57 -5
- package/package.json +17 -9
- package/CHANGELOG.md +0 -72
- package/jest.config.js +0 -18
- package/src/index.ts +0 -229
- package/tsconfig.esm.json +0 -9
- package/tsconfig.json +0 -9
- /package/{dist/run.d.ts → run.d.ts} +0 -0
- /package/{dist/run.js → run.js} +0 -0
package/README.md
CHANGED
|
@@ -1 +1,59 @@
|
|
|
1
|
-
# job-scheduler
|
|
1
|
+
# job-scheduler
|
|
2
|
+
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
## Education and Tutorials
|
|
6
|
+
|
|
7
|
+
1. 🚀 [Quickstart: Getting Up and Running](https://constructive.io/learn/quickstart)
|
|
8
|
+
Get started with modular databases in minutes. Install prerequisites and deploy your first module.
|
|
9
|
+
|
|
10
|
+
2. 📦 [Modular PostgreSQL Development with Database Packages](https://constructive.io/learn/modular-postgres)
|
|
11
|
+
Learn to organize PostgreSQL projects with pgpm workspaces and reusable database modules.
|
|
12
|
+
|
|
13
|
+
3. ✏️ [Authoring Database Changes](https://constructive.io/learn/authoring-database-changes)
|
|
14
|
+
Master the workflow for adding, organizing, and managing database changes with pgpm.
|
|
15
|
+
|
|
16
|
+
4. 🧪 [End-to-End PostgreSQL Testing with TypeScript](https://constructive.io/learn/e2e-postgres-testing)
|
|
17
|
+
Master end-to-end PostgreSQL testing with ephemeral databases, RLS testing, and CI/CD automation.
|
|
18
|
+
|
|
19
|
+
5. ⚡ [Supabase Testing](https://constructive.io/learn/supabase)
|
|
20
|
+
Use TypeScript-first tools to test Supabase projects with realistic RLS, policies, and auth contexts.
|
|
21
|
+
|
|
22
|
+
6. 💧 [Drizzle ORM Testing](https://constructive.io/learn/drizzle-testing)
|
|
23
|
+
Run full-stack tests with Drizzle ORM, including database setup, teardown, and RLS enforcement.
|
|
24
|
+
|
|
25
|
+
7. 🔧 [Troubleshooting](https://constructive.io/learn/troubleshooting)
|
|
26
|
+
Common issues and solutions for pgpm, PostgreSQL, and testing.
|
|
27
|
+
|
|
28
|
+
## Related Constructive Tooling
|
|
29
|
+
|
|
30
|
+
### 📦 Package Management
|
|
31
|
+
|
|
32
|
+
* [pgpm](https://github.com/constructive-io/constructive/tree/main/pgpm/pgpm): **🖥️ PostgreSQL Package Manager** for modular Postgres development. Works with database workspaces, scaffolding, migrations, seeding, and installing database packages.
|
|
33
|
+
|
|
34
|
+
### 🧪 Testing
|
|
35
|
+
|
|
36
|
+
* [pgsql-test](https://github.com/constructive-io/constructive/tree/main/postgres/pgsql-test): **📊 Isolated testing environments** with per-test transaction rollbacks—ideal for integration tests, complex migrations, and RLS simulation.
|
|
37
|
+
* [pgsql-seed](https://github.com/constructive-io/constructive/tree/main/postgres/pgsql-seed): **🌱 PostgreSQL seeding utilities** for CSV, JSON, SQL data loading, and pgpm deployment.
|
|
38
|
+
* [supabase-test](https://github.com/constructive-io/constructive/tree/main/postgres/supabase-test): **🧪 Supabase-native test harness** preconfigured for the local Supabase stack—per-test rollbacks, JWT/role context helpers, and CI/GitHub Actions ready.
|
|
39
|
+
* [graphile-test](https://github.com/constructive-io/constructive/tree/main/graphile/graphile-test): **🔐 Authentication mocking** for Graphile-focused test helpers and emulating row-level security contexts.
|
|
40
|
+
* [pg-query-context](https://github.com/constructive-io/constructive/tree/main/postgres/pg-query-context): **🔒 Session context injection** to add session-local context (e.g., `SET LOCAL`) into queries—ideal for setting `role`, `jwt.claims`, and other session settings.
|
|
41
|
+
|
|
42
|
+
### 🧠 Parsing & AST
|
|
43
|
+
|
|
44
|
+
* [pgsql-parser](https://www.npmjs.com/package/pgsql-parser): **🔄 SQL conversion engine** that interprets and converts PostgreSQL syntax.
|
|
45
|
+
* [libpg-query-node](https://www.npmjs.com/package/libpg-query): **🌉 Node.js bindings** for `libpg_query`, converting SQL into parse trees.
|
|
46
|
+
* [pg-proto-parser](https://www.npmjs.com/package/pg-proto-parser): **📦 Protobuf parser** for parsing PostgreSQL Protocol Buffers definitions to generate TypeScript interfaces, utility functions, and JSON mappings for enums.
|
|
47
|
+
* [@pgsql/enums](https://www.npmjs.com/package/@pgsql/enums): **🏷️ TypeScript enums** for PostgreSQL AST for safe and ergonomic parsing logic.
|
|
48
|
+
* [@pgsql/types](https://www.npmjs.com/package/@pgsql/types): **📝 Type definitions** for PostgreSQL AST nodes in TypeScript.
|
|
49
|
+
* [@pgsql/utils](https://www.npmjs.com/package/@pgsql/utils): **🛠️ AST utilities** for constructing and transforming PostgreSQL syntax trees.
|
|
50
|
+
|
|
51
|
+
## Credits
|
|
52
|
+
|
|
53
|
+
**🛠 Built by the [Constructive](https://constructive.io) team — creators of modular Postgres tooling for secure, composable backends. If you like our work, contribute on [GitHub](https://github.com/constructive-io).**
|
|
54
|
+
|
|
55
|
+
## Disclaimer
|
|
56
|
+
|
|
57
|
+
AS DESCRIBED IN THE LICENSES, THE SOFTWARE IS PROVIDED "AS IS", AT YOUR OWN RISK, AND WITHOUT WARRANTIES OF ANY KIND.
|
|
58
|
+
|
|
59
|
+
No developer or entity involved in creating this software will be liable for any claims or damages whatsoever associated with your use, inability to use, or your interaction with other users of the code, including any direct, indirect, incidental, special, exemplary, punitive or consequential damages, or loss of profits, cryptocurrencies, tokens, or anything else of value.
|
package/esm/index.js
ADDED
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
import * as jobs from '@constructive-io/job-utils';
|
|
2
|
+
import schedule from 'node-schedule';
|
|
3
|
+
import poolManager from '@constructive-io/job-pg';
|
|
4
|
+
import { Logger } from '@pgpmjs/logger';
|
|
5
|
+
const log = new Logger('jobs:scheduler');
|
|
6
|
+
export default class Scheduler {
|
|
7
|
+
idleDelay;
|
|
8
|
+
supportedTaskNames;
|
|
9
|
+
workerId;
|
|
10
|
+
doNextTimer;
|
|
11
|
+
pgPool;
|
|
12
|
+
jobs;
|
|
13
|
+
_initialized;
|
|
14
|
+
listenClient;
|
|
15
|
+
listenRelease;
|
|
16
|
+
stopped;
|
|
17
|
+
constructor({ tasks, idleDelay = 15000, pgPool = poolManager.getPool(), workerId = 'scheduler-0' }) {
|
|
18
|
+
/*
|
|
19
|
+
* idleDelay: This is how long to wait between polling for jobs.
|
|
20
|
+
*
|
|
21
|
+
* Note: this does NOT need to be short, because we use LISTEN/NOTIFY to be
|
|
22
|
+
* notified when new jobs are added - this is just used in the case where
|
|
23
|
+
* LISTEN/NOTIFY fails for whatever reason.
|
|
24
|
+
*/
|
|
25
|
+
this.idleDelay = idleDelay;
|
|
26
|
+
this.supportedTaskNames = tasks;
|
|
27
|
+
this.workerId = workerId;
|
|
28
|
+
this.doNextTimer = undefined;
|
|
29
|
+
this.pgPool = pgPool;
|
|
30
|
+
this.jobs = {};
|
|
31
|
+
poolManager.onClose(async () => {
|
|
32
|
+
await jobs.releaseScheduledJobs(pgPool, {
|
|
33
|
+
workerId: this.workerId,
|
|
34
|
+
// When ids is omitted the DB function releases all scheduled jobs
|
|
35
|
+
ids: undefined
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
async initialize(client) {
|
|
40
|
+
if (this._initialized === true)
|
|
41
|
+
return;
|
|
42
|
+
await jobs.releaseScheduledJobs(client, {
|
|
43
|
+
workerId: this.workerId,
|
|
44
|
+
// When ids is omitted the DB function releases all scheduled jobs
|
|
45
|
+
ids: undefined
|
|
46
|
+
});
|
|
47
|
+
this._initialized = true;
|
|
48
|
+
await this.doNext(client);
|
|
49
|
+
}
|
|
50
|
+
async handleFatalError(client, { err, fatalError, jobId }) {
|
|
51
|
+
const when = err ? `after failure '${err.message}'` : 'after success';
|
|
52
|
+
log.error(`Failed to release job '${jobId}' ${when}; committing seppuku`);
|
|
53
|
+
log.error(String(fatalError));
|
|
54
|
+
await poolManager.close();
|
|
55
|
+
process.exit(1);
|
|
56
|
+
}
|
|
57
|
+
async handleError(client, { err, job, duration }) {
|
|
58
|
+
log.error(`Failed to initialize scheduler for ${job.id} (${job.task_identifier}) with error ${err.message} (${duration}ms)`);
|
|
59
|
+
const j = this.jobs[job.id];
|
|
60
|
+
if (j)
|
|
61
|
+
j.cancel();
|
|
62
|
+
await jobs.releaseScheduledJobs(client, {
|
|
63
|
+
workerId: this.workerId,
|
|
64
|
+
ids: [job.id]
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
async handleSuccess(client, { job, duration }) {
|
|
68
|
+
log.info(`initialized ${job.id} (${job.task_identifier}) with success (${duration}ms)`);
|
|
69
|
+
}
|
|
70
|
+
async scheduleJob(client, job) {
|
|
71
|
+
const { id, task_identifier, schedule_info } = job;
|
|
72
|
+
const j = schedule.scheduleJob(schedule_info, async () => {
|
|
73
|
+
const newjob = (await jobs.runScheduledJob(client, {
|
|
74
|
+
jobId: id
|
|
75
|
+
}));
|
|
76
|
+
if (newjob) {
|
|
77
|
+
if (newjob.id) {
|
|
78
|
+
log.info(`spinning up job[${newjob.task_identifier}]`);
|
|
79
|
+
}
|
|
80
|
+
else {
|
|
81
|
+
// this means the scheduled_job has been deleted from db, so cancel it
|
|
82
|
+
log.info(`attempted job[${job.task_identifier}] but it's probably non existent, unscheduling...`);
|
|
83
|
+
const scheduledJob = this.jobs[job.id];
|
|
84
|
+
if (scheduledJob)
|
|
85
|
+
scheduledJob.cancel();
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
log.info(`job already scheduled but not yet run or complete: [${job.task_identifier}]`);
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
this.jobs[id] = j;
|
|
93
|
+
}
|
|
94
|
+
async doNext(client) {
|
|
95
|
+
if (this.stopped)
|
|
96
|
+
return;
|
|
97
|
+
if (!this._initialized) {
|
|
98
|
+
return await this.initialize(client);
|
|
99
|
+
}
|
|
100
|
+
if (this.doNextTimer) {
|
|
101
|
+
clearTimeout(this.doNextTimer);
|
|
102
|
+
this.doNextTimer = undefined;
|
|
103
|
+
}
|
|
104
|
+
try {
|
|
105
|
+
const job = await jobs.getScheduledJob(client, {
|
|
106
|
+
workerId: this.workerId,
|
|
107
|
+
supportedTaskNames: jobs.getJobSupportAny()
|
|
108
|
+
? null
|
|
109
|
+
: this.supportedTaskNames
|
|
110
|
+
});
|
|
111
|
+
if (!job || !job.id) {
|
|
112
|
+
if (!this.stopped) {
|
|
113
|
+
this.doNextTimer = setTimeout(() => this.doNext(client), this.idleDelay);
|
|
114
|
+
}
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
const start = process.hrtime();
|
|
118
|
+
let err = null;
|
|
119
|
+
try {
|
|
120
|
+
await this.scheduleJob(client, job);
|
|
121
|
+
}
|
|
122
|
+
catch (error) {
|
|
123
|
+
err = error;
|
|
124
|
+
}
|
|
125
|
+
const durationRaw = process.hrtime(start);
|
|
126
|
+
const duration = ((durationRaw[0] * 1e9 + durationRaw[1]) / 1e6).toFixed(2);
|
|
127
|
+
const jobId = job.id;
|
|
128
|
+
try {
|
|
129
|
+
if (err) {
|
|
130
|
+
await this.handleError(client, { err, job, duration });
|
|
131
|
+
}
|
|
132
|
+
else {
|
|
133
|
+
await this.handleSuccess(client, { job, duration });
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
catch (fatalError) {
|
|
137
|
+
await this.handleFatalError(client, { err, fatalError, jobId });
|
|
138
|
+
}
|
|
139
|
+
if (!this.stopped) {
|
|
140
|
+
return this.doNext(client);
|
|
141
|
+
}
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
catch (err) {
|
|
145
|
+
if (!this.stopped) {
|
|
146
|
+
this.doNextTimer = setTimeout(() => this.doNext(client), this.idleDelay);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
listen() {
|
|
151
|
+
if (this.stopped)
|
|
152
|
+
return;
|
|
153
|
+
const listenForChanges = (err, client, release) => {
|
|
154
|
+
if (err) {
|
|
155
|
+
log.error('Error connecting with notify listener', err);
|
|
156
|
+
if (err instanceof Error && err.stack) {
|
|
157
|
+
log.debug(err.stack);
|
|
158
|
+
}
|
|
159
|
+
// Try again in 5 seconds
|
|
160
|
+
// should this really be done in the node process?
|
|
161
|
+
if (!this.stopped) {
|
|
162
|
+
setTimeout(this.listen, 5000);
|
|
163
|
+
}
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
if (this.stopped) {
|
|
167
|
+
release();
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
this.listenClient = client;
|
|
171
|
+
this.listenRelease = release;
|
|
172
|
+
client.on('notification', () => {
|
|
173
|
+
log.info('a NEW scheduled JOB!');
|
|
174
|
+
if (this.doNextTimer) {
|
|
175
|
+
// Must be idle, do something!
|
|
176
|
+
this.doNext(client);
|
|
177
|
+
}
|
|
178
|
+
});
|
|
179
|
+
client.query('LISTEN "scheduled_jobs:insert"');
|
|
180
|
+
client.on('error', (e) => {
|
|
181
|
+
if (this.stopped) {
|
|
182
|
+
release();
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
log.error('Error with database notify listener', e);
|
|
186
|
+
if (e instanceof Error && e.stack) {
|
|
187
|
+
log.debug(e.stack);
|
|
188
|
+
}
|
|
189
|
+
release();
|
|
190
|
+
if (!this.stopped) {
|
|
191
|
+
this.listen();
|
|
192
|
+
}
|
|
193
|
+
});
|
|
194
|
+
log.info(`${this.workerId} connected and looking for scheduled jobs...`);
|
|
195
|
+
this.doNext(client);
|
|
196
|
+
};
|
|
197
|
+
this.pgPool.connect(listenForChanges);
|
|
198
|
+
}
|
|
199
|
+
async stop() {
|
|
200
|
+
this.stopped = true;
|
|
201
|
+
if (this.doNextTimer) {
|
|
202
|
+
clearTimeout(this.doNextTimer);
|
|
203
|
+
this.doNextTimer = undefined;
|
|
204
|
+
}
|
|
205
|
+
Object.values(this.jobs).forEach((job) => job.cancel());
|
|
206
|
+
this.jobs = {};
|
|
207
|
+
const client = this.listenClient;
|
|
208
|
+
const release = this.listenRelease;
|
|
209
|
+
this.listenClient = undefined;
|
|
210
|
+
this.listenRelease = undefined;
|
|
211
|
+
if (client && release) {
|
|
212
|
+
client.removeAllListeners('notification');
|
|
213
|
+
client.removeAllListeners('error');
|
|
214
|
+
try {
|
|
215
|
+
await client.query('UNLISTEN "scheduled_jobs:insert"');
|
|
216
|
+
}
|
|
217
|
+
catch {
|
|
218
|
+
// Ignore listener cleanup errors during shutdown.
|
|
219
|
+
}
|
|
220
|
+
release();
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
export { Scheduler };
|
|
@@ -1,18 +1,11 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
|
|
3
2
|
import Scheduler from './index';
|
|
4
3
|
import poolManager from '@constructive-io/job-pg';
|
|
5
|
-
import {
|
|
6
|
-
getSchedulerHostname,
|
|
7
|
-
getJobSupported
|
|
8
|
-
} from '@constructive-io/job-utils';
|
|
9
|
-
|
|
4
|
+
import { getSchedulerHostname, getJobSupported } from '@constructive-io/job-utils';
|
|
10
5
|
const pgPool = poolManager.getPool();
|
|
11
|
-
|
|
12
6
|
const scheduler = new Scheduler({
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
7
|
+
pgPool,
|
|
8
|
+
workerId: getSchedulerHostname(),
|
|
9
|
+
tasks: getJobSupported()
|
|
16
10
|
});
|
|
17
|
-
|
|
18
11
|
scheduler.listen();
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { PgClientLike } from '@constructive-io/job-utils';
|
|
2
|
-
import type { Pool } from 'pg';
|
|
2
|
+
import type { Pool, PoolClient } from 'pg';
|
|
3
3
|
export interface ScheduledJobRow {
|
|
4
4
|
id: number | string;
|
|
5
5
|
task_identifier: string;
|
|
@@ -16,6 +16,9 @@ export default class Scheduler {
|
|
|
16
16
|
pgPool: Pool;
|
|
17
17
|
jobs: Record<ScheduledJobRow['id'], SchedulerJobHandle>;
|
|
18
18
|
_initialized?: boolean;
|
|
19
|
+
listenClient?: PoolClient;
|
|
20
|
+
listenRelease?: () => void;
|
|
21
|
+
stopped?: boolean;
|
|
19
22
|
constructor({ tasks, idleDelay, pgPool, workerId }: {
|
|
20
23
|
tasks: string[];
|
|
21
24
|
idleDelay?: number;
|
|
@@ -40,5 +43,6 @@ export default class Scheduler {
|
|
|
40
43
|
scheduleJob(client: PgClientLike, job: ScheduledJobRow): Promise<void>;
|
|
41
44
|
doNext(client: PgClientLike): Promise<void>;
|
|
42
45
|
listen(): void;
|
|
46
|
+
stop(): Promise<void>;
|
|
43
47
|
}
|
|
44
48
|
export { Scheduler };
|
|
@@ -50,6 +50,9 @@ class Scheduler {
|
|
|
50
50
|
pgPool;
|
|
51
51
|
jobs;
|
|
52
52
|
_initialized;
|
|
53
|
+
listenClient;
|
|
54
|
+
listenRelease;
|
|
55
|
+
stopped;
|
|
53
56
|
constructor({ tasks, idleDelay = 15000, pgPool = job_pg_1.default.getPool(), workerId = 'scheduler-0' }) {
|
|
54
57
|
/*
|
|
55
58
|
* idleDelay: This is how long to wait between polling for jobs.
|
|
@@ -128,6 +131,8 @@ class Scheduler {
|
|
|
128
131
|
this.jobs[id] = j;
|
|
129
132
|
}
|
|
130
133
|
async doNext(client) {
|
|
134
|
+
if (this.stopped)
|
|
135
|
+
return;
|
|
131
136
|
if (!this._initialized) {
|
|
132
137
|
return await this.initialize(client);
|
|
133
138
|
}
|
|
@@ -143,7 +148,9 @@ class Scheduler {
|
|
|
143
148
|
: this.supportedTaskNames
|
|
144
149
|
});
|
|
145
150
|
if (!job || !job.id) {
|
|
146
|
-
|
|
151
|
+
if (!this.stopped) {
|
|
152
|
+
this.doNextTimer = setTimeout(() => this.doNext(client), this.idleDelay);
|
|
153
|
+
}
|
|
147
154
|
return;
|
|
148
155
|
}
|
|
149
156
|
const start = process.hrtime();
|
|
@@ -168,13 +175,20 @@ class Scheduler {
|
|
|
168
175
|
catch (fatalError) {
|
|
169
176
|
await this.handleFatalError(client, { err, fatalError, jobId });
|
|
170
177
|
}
|
|
171
|
-
|
|
178
|
+
if (!this.stopped) {
|
|
179
|
+
return this.doNext(client);
|
|
180
|
+
}
|
|
181
|
+
return;
|
|
172
182
|
}
|
|
173
183
|
catch (err) {
|
|
174
|
-
|
|
184
|
+
if (!this.stopped) {
|
|
185
|
+
this.doNextTimer = setTimeout(() => this.doNext(client), this.idleDelay);
|
|
186
|
+
}
|
|
175
187
|
}
|
|
176
188
|
}
|
|
177
189
|
listen() {
|
|
190
|
+
if (this.stopped)
|
|
191
|
+
return;
|
|
178
192
|
const listenForChanges = (err, client, release) => {
|
|
179
193
|
if (err) {
|
|
180
194
|
log.error('Error connecting with notify listener', err);
|
|
@@ -183,9 +197,17 @@ class Scheduler {
|
|
|
183
197
|
}
|
|
184
198
|
// Try again in 5 seconds
|
|
185
199
|
// should this really be done in the node process?
|
|
186
|
-
|
|
200
|
+
if (!this.stopped) {
|
|
201
|
+
setTimeout(this.listen, 5000);
|
|
202
|
+
}
|
|
203
|
+
return;
|
|
204
|
+
}
|
|
205
|
+
if (this.stopped) {
|
|
206
|
+
release();
|
|
187
207
|
return;
|
|
188
208
|
}
|
|
209
|
+
this.listenClient = client;
|
|
210
|
+
this.listenRelease = release;
|
|
189
211
|
client.on('notification', () => {
|
|
190
212
|
log.info('a NEW scheduled JOB!');
|
|
191
213
|
if (this.doNextTimer) {
|
|
@@ -195,18 +217,48 @@ class Scheduler {
|
|
|
195
217
|
});
|
|
196
218
|
client.query('LISTEN "scheduled_jobs:insert"');
|
|
197
219
|
client.on('error', (e) => {
|
|
220
|
+
if (this.stopped) {
|
|
221
|
+
release();
|
|
222
|
+
return;
|
|
223
|
+
}
|
|
198
224
|
log.error('Error with database notify listener', e);
|
|
199
225
|
if (e instanceof Error && e.stack) {
|
|
200
226
|
log.debug(e.stack);
|
|
201
227
|
}
|
|
202
228
|
release();
|
|
203
|
-
this.
|
|
229
|
+
if (!this.stopped) {
|
|
230
|
+
this.listen();
|
|
231
|
+
}
|
|
204
232
|
});
|
|
205
233
|
log.info(`${this.workerId} connected and looking for scheduled jobs...`);
|
|
206
234
|
this.doNext(client);
|
|
207
235
|
};
|
|
208
236
|
this.pgPool.connect(listenForChanges);
|
|
209
237
|
}
|
|
238
|
+
async stop() {
|
|
239
|
+
this.stopped = true;
|
|
240
|
+
if (this.doNextTimer) {
|
|
241
|
+
clearTimeout(this.doNextTimer);
|
|
242
|
+
this.doNextTimer = undefined;
|
|
243
|
+
}
|
|
244
|
+
Object.values(this.jobs).forEach((job) => job.cancel());
|
|
245
|
+
this.jobs = {};
|
|
246
|
+
const client = this.listenClient;
|
|
247
|
+
const release = this.listenRelease;
|
|
248
|
+
this.listenClient = undefined;
|
|
249
|
+
this.listenRelease = undefined;
|
|
250
|
+
if (client && release) {
|
|
251
|
+
client.removeAllListeners('notification');
|
|
252
|
+
client.removeAllListeners('error');
|
|
253
|
+
try {
|
|
254
|
+
await client.query('UNLISTEN "scheduled_jobs:insert"');
|
|
255
|
+
}
|
|
256
|
+
catch {
|
|
257
|
+
// Ignore listener cleanup errors during shutdown.
|
|
258
|
+
}
|
|
259
|
+
release();
|
|
260
|
+
}
|
|
261
|
+
}
|
|
210
262
|
}
|
|
211
263
|
exports.default = Scheduler;
|
|
212
264
|
exports.Scheduler = Scheduler;
|
package/package.json
CHANGED
|
@@ -1,17 +1,20 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@constructive-io/job-scheduler",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.1",
|
|
4
4
|
"description": "job scheduler",
|
|
5
5
|
"author": "Constructive <developers@constructive.io>",
|
|
6
6
|
"homepage": "https://github.com/constructive-io/jobs/tree/master/packages/job-scheduler#readme",
|
|
7
7
|
"license": "SEE LICENSE IN LICENSE",
|
|
8
|
-
"main": "
|
|
8
|
+
"main": "index.js",
|
|
9
|
+
"module": "esm/index.js",
|
|
10
|
+
"types": "index.d.ts",
|
|
9
11
|
"directories": {
|
|
10
12
|
"lib": "src",
|
|
11
13
|
"test": "__tests__"
|
|
12
14
|
},
|
|
13
15
|
"publishConfig": {
|
|
14
|
-
"access": "public"
|
|
16
|
+
"access": "public",
|
|
17
|
+
"directory": "dist"
|
|
15
18
|
},
|
|
16
19
|
"repository": {
|
|
17
20
|
"type": "git",
|
|
@@ -21,17 +24,22 @@
|
|
|
21
24
|
"test": "jest --passWithNoTests",
|
|
22
25
|
"test:watch": "jest --watch",
|
|
23
26
|
"test:debug": "node --inspect node_modules/.bin/jest --runInBand",
|
|
24
|
-
"
|
|
25
|
-
"
|
|
27
|
+
"clean": "makage clean",
|
|
28
|
+
"prepack": "npm run build",
|
|
29
|
+
"build": "makage build",
|
|
30
|
+
"build:dev": "makage build --dev"
|
|
26
31
|
},
|
|
27
32
|
"bugs": {
|
|
28
33
|
"url": "https://github.com/constructive-io/jobs/issues"
|
|
29
34
|
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"makage": "^0.1.10"
|
|
37
|
+
},
|
|
30
38
|
"dependencies": {
|
|
31
|
-
"@constructive-io/job-pg": "^0.
|
|
32
|
-
"@constructive-io/job-utils": "^0.
|
|
33
|
-
"@pgpmjs/logger": "^1.
|
|
39
|
+
"@constructive-io/job-pg": "^0.4.1",
|
|
40
|
+
"@constructive-io/job-utils": "^0.6.1",
|
|
41
|
+
"@pgpmjs/logger": "^1.4.0",
|
|
34
42
|
"node-schedule": "1.3.2"
|
|
35
43
|
},
|
|
36
|
-
"gitHead": "
|
|
44
|
+
"gitHead": "3ffd5718e86ea5fa9ca6e0930aeb510cf392f343"
|
|
37
45
|
}
|
package/CHANGELOG.md
DELETED
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
# Change Log
|
|
2
|
-
|
|
3
|
-
All notable changes to this project will be documented in this file.
|
|
4
|
-
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
|
-
|
|
6
|
-
## [0.3.22](https://github.com/constructive-io/jobs/compare/@constructive-io/job-scheduler@0.3.21...@constructive-io/job-scheduler@0.3.22) (2026-01-09)
|
|
7
|
-
|
|
8
|
-
**Note:** Version bump only for package @constructive-io/job-scheduler
|
|
9
|
-
|
|
10
|
-
## [0.3.21](https://github.com/constructive-io/jobs/compare/@constructive-io/job-scheduler@0.3.20...@constructive-io/job-scheduler@0.3.21) (2026-01-07)
|
|
11
|
-
|
|
12
|
-
**Note:** Version bump only for package @constructive-io/job-scheduler
|
|
13
|
-
|
|
14
|
-
## [0.3.20](https://github.com/constructive-io/jobs/compare/@constructive-io/job-scheduler@0.3.19...@constructive-io/job-scheduler@0.3.20) (2026-01-02)
|
|
15
|
-
|
|
16
|
-
**Note:** Version bump only for package @constructive-io/job-scheduler
|
|
17
|
-
|
|
18
|
-
## [0.3.19](https://github.com/constructive-io/jobs/compare/@constructive-io/job-scheduler@0.3.18...@constructive-io/job-scheduler@0.3.19) (2026-01-02)
|
|
19
|
-
|
|
20
|
-
**Note:** Version bump only for package @constructive-io/job-scheduler
|
|
21
|
-
|
|
22
|
-
## [0.3.18](https://github.com/constructive-io/jobs/compare/@constructive-io/job-scheduler@0.3.17...@constructive-io/job-scheduler@0.3.18) (2025-12-31)
|
|
23
|
-
|
|
24
|
-
**Note:** Version bump only for package @constructive-io/job-scheduler
|
|
25
|
-
|
|
26
|
-
## [0.3.17](https://github.com/constructive-io/jobs/compare/@constructive-io/job-scheduler@0.3.16...@constructive-io/job-scheduler@0.3.17) (2025-12-31)
|
|
27
|
-
|
|
28
|
-
**Note:** Version bump only for package @constructive-io/job-scheduler
|
|
29
|
-
|
|
30
|
-
## [0.3.16](https://github.com/constructive-io/jobs/compare/@constructive-io/job-scheduler@0.3.15...@constructive-io/job-scheduler@0.3.16) (2025-12-27)
|
|
31
|
-
|
|
32
|
-
**Note:** Version bump only for package @constructive-io/job-scheduler
|
|
33
|
-
|
|
34
|
-
## [0.3.15](https://github.com/constructive-io/jobs/compare/@constructive-io/job-scheduler@0.3.14...@constructive-io/job-scheduler@0.3.15) (2025-12-26)
|
|
35
|
-
|
|
36
|
-
**Note:** Version bump only for package @constructive-io/job-scheduler
|
|
37
|
-
|
|
38
|
-
## [0.3.14](https://github.com/constructive-io/jobs/compare/@constructive-io/job-scheduler@0.3.13...@constructive-io/job-scheduler@0.3.14) (2025-12-26)
|
|
39
|
-
|
|
40
|
-
**Note:** Version bump only for package @constructive-io/job-scheduler
|
|
41
|
-
|
|
42
|
-
## [0.3.13](https://github.com/constructive-io/jobs/compare/@constructive-io/job-scheduler@0.3.12...@constructive-io/job-scheduler@0.3.13) (2025-12-24)
|
|
43
|
-
|
|
44
|
-
**Note:** Version bump only for package @constructive-io/job-scheduler
|
|
45
|
-
|
|
46
|
-
## [0.3.12](https://github.com/constructive-io/jobs/compare/@constructive-io/job-scheduler@0.3.11...@constructive-io/job-scheduler@0.3.12) (2025-12-23)
|
|
47
|
-
|
|
48
|
-
**Note:** Version bump only for package @constructive-io/job-scheduler
|
|
49
|
-
|
|
50
|
-
## [0.3.11](https://github.com/constructive-io/jobs/compare/@constructive-io/job-scheduler@0.3.10...@constructive-io/job-scheduler@0.3.11) (2025-12-22)
|
|
51
|
-
|
|
52
|
-
**Note:** Version bump only for package @constructive-io/job-scheduler
|
|
53
|
-
|
|
54
|
-
## [0.3.10](https://github.com/constructive-io/jobs/compare/@constructive-io/job-scheduler@0.3.9...@constructive-io/job-scheduler@0.3.10) (2025-12-22)
|
|
55
|
-
|
|
56
|
-
**Note:** Version bump only for package @constructive-io/job-scheduler
|
|
57
|
-
|
|
58
|
-
## [0.3.9](https://github.com/constructive-io/jobs/compare/@constructive-io/job-scheduler@0.3.8...@constructive-io/job-scheduler@0.3.9) (2025-12-21)
|
|
59
|
-
|
|
60
|
-
**Note:** Version bump only for package @constructive-io/job-scheduler
|
|
61
|
-
|
|
62
|
-
## [0.3.8](https://github.com/constructive-io/jobs/compare/@constructive-io/job-scheduler@0.3.7...@constructive-io/job-scheduler@0.3.8) (2025-12-19)
|
|
63
|
-
|
|
64
|
-
**Note:** Version bump only for package @constructive-io/job-scheduler
|
|
65
|
-
|
|
66
|
-
## 0.3.7 (2025-12-18)
|
|
67
|
-
|
|
68
|
-
**Note:** Version bump only for package @constructive-io/job-scheduler
|
|
69
|
-
|
|
70
|
-
## [0.3.6](https://github.com/constructive-io/jobs/compare/@constructive-io/job-scheduler@0.3.5...@constructive-io/job-scheduler@0.3.6) (2025-12-17)
|
|
71
|
-
|
|
72
|
-
**Note:** Version bump only for package @constructive-io/job-scheduler
|
package/jest.config.js
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
/** @type {import('ts-jest').JestConfigWithTsJest} */
|
|
2
|
-
module.exports = {
|
|
3
|
-
preset: 'ts-jest',
|
|
4
|
-
testEnvironment: 'node',
|
|
5
|
-
transform: {
|
|
6
|
-
'^.+\\.tsx?$': [
|
|
7
|
-
'ts-jest',
|
|
8
|
-
{
|
|
9
|
-
babelConfig: false,
|
|
10
|
-
tsconfig: 'tsconfig.json',
|
|
11
|
-
},
|
|
12
|
-
],
|
|
13
|
-
},
|
|
14
|
-
transformIgnorePatterns: [`/node_modules/*`],
|
|
15
|
-
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$',
|
|
16
|
-
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
|
|
17
|
-
modulePathIgnorePatterns: ['dist/*'],
|
|
18
|
-
};
|
package/src/index.ts
DELETED
|
@@ -1,229 +0,0 @@
|
|
|
1
|
-
import * as jobs from '@constructive-io/job-utils';
|
|
2
|
-
import type { PgClientLike } from '@constructive-io/job-utils';
|
|
3
|
-
import schedule from 'node-schedule';
|
|
4
|
-
import poolManager from '@constructive-io/job-pg';
|
|
5
|
-
import type { Pool, PoolClient } from 'pg';
|
|
6
|
-
import { Logger } from '@pgpmjs/logger';
|
|
7
|
-
|
|
8
|
-
export interface ScheduledJobRow {
|
|
9
|
-
id: number | string;
|
|
10
|
-
task_identifier: string;
|
|
11
|
-
schedule_info: unknown;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
interface SchedulerJobHandle {
|
|
15
|
-
cancel(): void;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
const log = new Logger('jobs:scheduler');
|
|
19
|
-
|
|
20
|
-
export default class Scheduler {
|
|
21
|
-
idleDelay: number;
|
|
22
|
-
supportedTaskNames: string[];
|
|
23
|
-
workerId: string;
|
|
24
|
-
doNextTimer?: NodeJS.Timeout;
|
|
25
|
-
pgPool: Pool;
|
|
26
|
-
jobs: Record<ScheduledJobRow['id'], SchedulerJobHandle>;
|
|
27
|
-
_initialized?: boolean;
|
|
28
|
-
|
|
29
|
-
constructor({
|
|
30
|
-
tasks,
|
|
31
|
-
idleDelay = 15000,
|
|
32
|
-
pgPool = poolManager.getPool(),
|
|
33
|
-
workerId = 'scheduler-0'
|
|
34
|
-
}: {
|
|
35
|
-
tasks: string[];
|
|
36
|
-
idleDelay?: number;
|
|
37
|
-
pgPool?: Pool;
|
|
38
|
-
workerId?: string;
|
|
39
|
-
}) {
|
|
40
|
-
/*
|
|
41
|
-
* idleDelay: This is how long to wait between polling for jobs.
|
|
42
|
-
*
|
|
43
|
-
* Note: this does NOT need to be short, because we use LISTEN/NOTIFY to be
|
|
44
|
-
* notified when new jobs are added - this is just used in the case where
|
|
45
|
-
* LISTEN/NOTIFY fails for whatever reason.
|
|
46
|
-
*/
|
|
47
|
-
this.idleDelay = idleDelay;
|
|
48
|
-
this.supportedTaskNames = tasks;
|
|
49
|
-
this.workerId = workerId;
|
|
50
|
-
this.doNextTimer = undefined;
|
|
51
|
-
this.pgPool = pgPool;
|
|
52
|
-
this.jobs = {};
|
|
53
|
-
poolManager.onClose(async () => {
|
|
54
|
-
await jobs.releaseScheduledJobs(pgPool, {
|
|
55
|
-
workerId: this.workerId,
|
|
56
|
-
// When ids is omitted the DB function releases all scheduled jobs
|
|
57
|
-
ids: undefined as unknown as Array<number | string>
|
|
58
|
-
});
|
|
59
|
-
});
|
|
60
|
-
}
|
|
61
|
-
async initialize(client: PgClientLike) {
|
|
62
|
-
if (this._initialized === true) return;
|
|
63
|
-
await jobs.releaseScheduledJobs(client, {
|
|
64
|
-
workerId: this.workerId,
|
|
65
|
-
// When ids is omitted the DB function releases all scheduled jobs
|
|
66
|
-
ids: undefined as unknown as Array<number | string>
|
|
67
|
-
});
|
|
68
|
-
this._initialized = true;
|
|
69
|
-
await this.doNext(client);
|
|
70
|
-
}
|
|
71
|
-
async handleFatalError(
|
|
72
|
-
client: PgClientLike,
|
|
73
|
-
{
|
|
74
|
-
err,
|
|
75
|
-
fatalError,
|
|
76
|
-
jobId
|
|
77
|
-
}: { err?: Error; fatalError: unknown; jobId: ScheduledJobRow['id'] }
|
|
78
|
-
) {
|
|
79
|
-
const when = err ? `after failure '${err.message}'` : 'after success';
|
|
80
|
-
log.error(`Failed to release job '${jobId}' ${when}; committing seppuku`);
|
|
81
|
-
log.error(String(fatalError));
|
|
82
|
-
await poolManager.close();
|
|
83
|
-
process.exit(1);
|
|
84
|
-
}
|
|
85
|
-
async handleError(
|
|
86
|
-
client: PgClientLike,
|
|
87
|
-
{
|
|
88
|
-
err,
|
|
89
|
-
job,
|
|
90
|
-
duration
|
|
91
|
-
}: { err: Error; job: ScheduledJobRow; duration: string }
|
|
92
|
-
) {
|
|
93
|
-
log.error(
|
|
94
|
-
`Failed to initialize scheduler for ${job.id} (${job.task_identifier}) with error ${err.message} (${duration}ms)`
|
|
95
|
-
);
|
|
96
|
-
const j = this.jobs[job.id];
|
|
97
|
-
if (j) j.cancel();
|
|
98
|
-
await jobs.releaseScheduledJobs(client, {
|
|
99
|
-
workerId: this.workerId,
|
|
100
|
-
ids: [job.id]
|
|
101
|
-
});
|
|
102
|
-
}
|
|
103
|
-
async handleSuccess(
|
|
104
|
-
client: PgClientLike,
|
|
105
|
-
{ job, duration }: { job: ScheduledJobRow; duration: string }
|
|
106
|
-
) {
|
|
107
|
-
log.info(
|
|
108
|
-
`initialized ${job.id} (${job.task_identifier}) with success (${duration}ms)`
|
|
109
|
-
);
|
|
110
|
-
}
|
|
111
|
-
async scheduleJob(client: PgClientLike, job: ScheduledJobRow) {
|
|
112
|
-
const { id, task_identifier, schedule_info } = job;
|
|
113
|
-
const j = schedule.scheduleJob(schedule_info as never, async () => {
|
|
114
|
-
const newjob = (await jobs.runScheduledJob(client, {
|
|
115
|
-
jobId: id
|
|
116
|
-
})) as ScheduledJobRow | null;
|
|
117
|
-
|
|
118
|
-
if (newjob) {
|
|
119
|
-
if (newjob.id) {
|
|
120
|
-
log.info(`spinning up job[${newjob.task_identifier}]`);
|
|
121
|
-
} else {
|
|
122
|
-
// this means the scheduled_job has been deleted from db, so cancel it
|
|
123
|
-
log.info(
|
|
124
|
-
`attempted job[${job.task_identifier}] but it's probably non existent, unscheduling...`
|
|
125
|
-
);
|
|
126
|
-
const scheduledJob = this.jobs[job.id];
|
|
127
|
-
if (scheduledJob) scheduledJob.cancel();
|
|
128
|
-
}
|
|
129
|
-
} else {
|
|
130
|
-
log.info(
|
|
131
|
-
`job already scheduled but not yet run or complete: [${job.task_identifier}]`
|
|
132
|
-
);
|
|
133
|
-
}
|
|
134
|
-
});
|
|
135
|
-
this.jobs[id] = j as SchedulerJobHandle;
|
|
136
|
-
}
|
|
137
|
-
async doNext(client: PgClientLike): Promise<void> {
|
|
138
|
-
if (!this._initialized) {
|
|
139
|
-
return await this.initialize(client);
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
if (this.doNextTimer) {
|
|
143
|
-
clearTimeout(this.doNextTimer);
|
|
144
|
-
this.doNextTimer = undefined;
|
|
145
|
-
}
|
|
146
|
-
try {
|
|
147
|
-
const job = await jobs.getScheduledJob<ScheduledJobRow>(client, {
|
|
148
|
-
workerId: this.workerId,
|
|
149
|
-
supportedTaskNames: jobs.getJobSupportAny()
|
|
150
|
-
? null
|
|
151
|
-
: this.supportedTaskNames
|
|
152
|
-
});
|
|
153
|
-
if (!job || !job.id) {
|
|
154
|
-
this.doNextTimer = setTimeout(
|
|
155
|
-
() => this.doNext(client),
|
|
156
|
-
this.idleDelay
|
|
157
|
-
);
|
|
158
|
-
return;
|
|
159
|
-
}
|
|
160
|
-
const start = process.hrtime();
|
|
161
|
-
|
|
162
|
-
let err: Error | null = null;
|
|
163
|
-
try {
|
|
164
|
-
await this.scheduleJob(client, job);
|
|
165
|
-
} catch (error) {
|
|
166
|
-
err = error as Error;
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
const durationRaw = process.hrtime(start);
|
|
170
|
-
const duration = ((durationRaw[0] * 1e9 + durationRaw[1]) / 1e6).toFixed(
|
|
171
|
-
2
|
|
172
|
-
);
|
|
173
|
-
const jobId = job.id;
|
|
174
|
-
try {
|
|
175
|
-
if (err) {
|
|
176
|
-
await this.handleError(client, { err, job, duration });
|
|
177
|
-
} else {
|
|
178
|
-
await this.handleSuccess(client, { job, duration });
|
|
179
|
-
}
|
|
180
|
-
} catch (fatalError: unknown) {
|
|
181
|
-
await this.handleFatalError(client, { err, fatalError, jobId });
|
|
182
|
-
}
|
|
183
|
-
return this.doNext(client);
|
|
184
|
-
} catch (err: unknown) {
|
|
185
|
-
this.doNextTimer = setTimeout(() => this.doNext(client), this.idleDelay);
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
listen() {
|
|
189
|
-
const listenForChanges = (
|
|
190
|
-
err: Error | null,
|
|
191
|
-
client: PoolClient,
|
|
192
|
-
release: () => void
|
|
193
|
-
) => {
|
|
194
|
-
if (err) {
|
|
195
|
-
log.error('Error connecting with notify listener', err);
|
|
196
|
-
if (err instanceof Error && err.stack) {
|
|
197
|
-
log.debug(err.stack);
|
|
198
|
-
}
|
|
199
|
-
// Try again in 5 seconds
|
|
200
|
-
// should this really be done in the node process?
|
|
201
|
-
setTimeout(this.listen, 5000);
|
|
202
|
-
return;
|
|
203
|
-
}
|
|
204
|
-
client.on('notification', () => {
|
|
205
|
-
log.info('a NEW scheduled JOB!');
|
|
206
|
-
if (this.doNextTimer) {
|
|
207
|
-
// Must be idle, do something!
|
|
208
|
-
this.doNext(client);
|
|
209
|
-
}
|
|
210
|
-
});
|
|
211
|
-
client.query('LISTEN "scheduled_jobs:insert"');
|
|
212
|
-
client.on('error', (e: unknown) => {
|
|
213
|
-
log.error('Error with database notify listener', e);
|
|
214
|
-
if (e instanceof Error && e.stack) {
|
|
215
|
-
log.debug(e.stack);
|
|
216
|
-
}
|
|
217
|
-
release();
|
|
218
|
-
this.listen();
|
|
219
|
-
});
|
|
220
|
-
log.info(
|
|
221
|
-
`${this.workerId} connected and looking for scheduled jobs...`
|
|
222
|
-
);
|
|
223
|
-
this.doNext(client);
|
|
224
|
-
};
|
|
225
|
-
this.pgPool.connect(listenForChanges);
|
|
226
|
-
}
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
export { Scheduler };
|
package/tsconfig.esm.json
DELETED
package/tsconfig.json
DELETED
|
File without changes
|
/package/{dist/run.js → run.js}
RENAMED
|
File without changes
|