@constructive-io/job-scheduler 0.4.0 → 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/package.json +16 -8
- package/CHANGELOG.md +0 -80
- package/jest.config.js +0 -18
- package/src/index.ts +0 -284
- package/tsconfig.esm.json +0 -9
- package/tsconfig.json +0 -9
- /package/{dist/index.d.ts → index.d.ts} +0 -0
- /package/{dist/index.js → index.js} +0 -0
- /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();
|
package/package.json
CHANGED
|
@@ -1,17 +1,20 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@constructive-io/job-scheduler",
|
|
3
|
-
"version": "0.4.
|
|
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.4.
|
|
32
|
-
"@constructive-io/job-utils": "^0.6.
|
|
39
|
+
"@constructive-io/job-pg": "^0.4.1",
|
|
40
|
+
"@constructive-io/job-utils": "^0.6.1",
|
|
33
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,80 +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.4.0](https://github.com/constructive-io/jobs/compare/@constructive-io/job-scheduler@0.3.23...@constructive-io/job-scheduler@0.4.0) (2026-01-18)
|
|
7
|
-
|
|
8
|
-
**Note:** Version bump only for package @constructive-io/job-scheduler
|
|
9
|
-
|
|
10
|
-
## [0.3.23](https://github.com/constructive-io/jobs/compare/@constructive-io/job-scheduler@0.3.22...@constructive-io/job-scheduler@0.3.23) (2026-01-18)
|
|
11
|
-
|
|
12
|
-
**Note:** Version bump only for package @constructive-io/job-scheduler
|
|
13
|
-
|
|
14
|
-
## [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)
|
|
15
|
-
|
|
16
|
-
**Note:** Version bump only for package @constructive-io/job-scheduler
|
|
17
|
-
|
|
18
|
-
## [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)
|
|
19
|
-
|
|
20
|
-
**Note:** Version bump only for package @constructive-io/job-scheduler
|
|
21
|
-
|
|
22
|
-
## [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)
|
|
23
|
-
|
|
24
|
-
**Note:** Version bump only for package @constructive-io/job-scheduler
|
|
25
|
-
|
|
26
|
-
## [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)
|
|
27
|
-
|
|
28
|
-
**Note:** Version bump only for package @constructive-io/job-scheduler
|
|
29
|
-
|
|
30
|
-
## [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)
|
|
31
|
-
|
|
32
|
-
**Note:** Version bump only for package @constructive-io/job-scheduler
|
|
33
|
-
|
|
34
|
-
## [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)
|
|
35
|
-
|
|
36
|
-
**Note:** Version bump only for package @constructive-io/job-scheduler
|
|
37
|
-
|
|
38
|
-
## [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)
|
|
39
|
-
|
|
40
|
-
**Note:** Version bump only for package @constructive-io/job-scheduler
|
|
41
|
-
|
|
42
|
-
## [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)
|
|
43
|
-
|
|
44
|
-
**Note:** Version bump only for package @constructive-io/job-scheduler
|
|
45
|
-
|
|
46
|
-
## [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)
|
|
47
|
-
|
|
48
|
-
**Note:** Version bump only for package @constructive-io/job-scheduler
|
|
49
|
-
|
|
50
|
-
## [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)
|
|
51
|
-
|
|
52
|
-
**Note:** Version bump only for package @constructive-io/job-scheduler
|
|
53
|
-
|
|
54
|
-
## [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)
|
|
55
|
-
|
|
56
|
-
**Note:** Version bump only for package @constructive-io/job-scheduler
|
|
57
|
-
|
|
58
|
-
## [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)
|
|
59
|
-
|
|
60
|
-
**Note:** Version bump only for package @constructive-io/job-scheduler
|
|
61
|
-
|
|
62
|
-
## [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)
|
|
63
|
-
|
|
64
|
-
**Note:** Version bump only for package @constructive-io/job-scheduler
|
|
65
|
-
|
|
66
|
-
## [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)
|
|
67
|
-
|
|
68
|
-
**Note:** Version bump only for package @constructive-io/job-scheduler
|
|
69
|
-
|
|
70
|
-
## [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)
|
|
71
|
-
|
|
72
|
-
**Note:** Version bump only for package @constructive-io/job-scheduler
|
|
73
|
-
|
|
74
|
-
## 0.3.7 (2025-12-18)
|
|
75
|
-
|
|
76
|
-
**Note:** Version bump only for package @constructive-io/job-scheduler
|
|
77
|
-
|
|
78
|
-
## [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)
|
|
79
|
-
|
|
80
|
-
**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,284 +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
|
-
listenClient?: PoolClient;
|
|
29
|
-
listenRelease?: () => void;
|
|
30
|
-
stopped?: boolean;
|
|
31
|
-
|
|
32
|
-
constructor({
|
|
33
|
-
tasks,
|
|
34
|
-
idleDelay = 15000,
|
|
35
|
-
pgPool = poolManager.getPool(),
|
|
36
|
-
workerId = 'scheduler-0'
|
|
37
|
-
}: {
|
|
38
|
-
tasks: string[];
|
|
39
|
-
idleDelay?: number;
|
|
40
|
-
pgPool?: Pool;
|
|
41
|
-
workerId?: string;
|
|
42
|
-
}) {
|
|
43
|
-
/*
|
|
44
|
-
* idleDelay: This is how long to wait between polling for jobs.
|
|
45
|
-
*
|
|
46
|
-
* Note: this does NOT need to be short, because we use LISTEN/NOTIFY to be
|
|
47
|
-
* notified when new jobs are added - this is just used in the case where
|
|
48
|
-
* LISTEN/NOTIFY fails for whatever reason.
|
|
49
|
-
*/
|
|
50
|
-
this.idleDelay = idleDelay;
|
|
51
|
-
this.supportedTaskNames = tasks;
|
|
52
|
-
this.workerId = workerId;
|
|
53
|
-
this.doNextTimer = undefined;
|
|
54
|
-
this.pgPool = pgPool;
|
|
55
|
-
this.jobs = {};
|
|
56
|
-
poolManager.onClose(async () => {
|
|
57
|
-
await jobs.releaseScheduledJobs(pgPool, {
|
|
58
|
-
workerId: this.workerId,
|
|
59
|
-
// When ids is omitted the DB function releases all scheduled jobs
|
|
60
|
-
ids: undefined as unknown as Array<number | string>
|
|
61
|
-
});
|
|
62
|
-
});
|
|
63
|
-
}
|
|
64
|
-
async initialize(client: PgClientLike) {
|
|
65
|
-
if (this._initialized === true) return;
|
|
66
|
-
await jobs.releaseScheduledJobs(client, {
|
|
67
|
-
workerId: this.workerId,
|
|
68
|
-
// When ids is omitted the DB function releases all scheduled jobs
|
|
69
|
-
ids: undefined as unknown as Array<number | string>
|
|
70
|
-
});
|
|
71
|
-
this._initialized = true;
|
|
72
|
-
await this.doNext(client);
|
|
73
|
-
}
|
|
74
|
-
async handleFatalError(
|
|
75
|
-
client: PgClientLike,
|
|
76
|
-
{
|
|
77
|
-
err,
|
|
78
|
-
fatalError,
|
|
79
|
-
jobId
|
|
80
|
-
}: { err?: Error; fatalError: unknown; jobId: ScheduledJobRow['id'] }
|
|
81
|
-
) {
|
|
82
|
-
const when = err ? `after failure '${err.message}'` : 'after success';
|
|
83
|
-
log.error(`Failed to release job '${jobId}' ${when}; committing seppuku`);
|
|
84
|
-
log.error(String(fatalError));
|
|
85
|
-
await poolManager.close();
|
|
86
|
-
process.exit(1);
|
|
87
|
-
}
|
|
88
|
-
async handleError(
|
|
89
|
-
client: PgClientLike,
|
|
90
|
-
{
|
|
91
|
-
err,
|
|
92
|
-
job,
|
|
93
|
-
duration
|
|
94
|
-
}: { err: Error; job: ScheduledJobRow; duration: string }
|
|
95
|
-
) {
|
|
96
|
-
log.error(
|
|
97
|
-
`Failed to initialize scheduler for ${job.id} (${job.task_identifier}) with error ${err.message} (${duration}ms)`
|
|
98
|
-
);
|
|
99
|
-
const j = this.jobs[job.id];
|
|
100
|
-
if (j) j.cancel();
|
|
101
|
-
await jobs.releaseScheduledJobs(client, {
|
|
102
|
-
workerId: this.workerId,
|
|
103
|
-
ids: [job.id]
|
|
104
|
-
});
|
|
105
|
-
}
|
|
106
|
-
async handleSuccess(
|
|
107
|
-
client: PgClientLike,
|
|
108
|
-
{ job, duration }: { job: ScheduledJobRow; duration: string }
|
|
109
|
-
) {
|
|
110
|
-
log.info(
|
|
111
|
-
`initialized ${job.id} (${job.task_identifier}) with success (${duration}ms)`
|
|
112
|
-
);
|
|
113
|
-
}
|
|
114
|
-
async scheduleJob(client: PgClientLike, job: ScheduledJobRow) {
|
|
115
|
-
const { id, task_identifier, schedule_info } = job;
|
|
116
|
-
const j = schedule.scheduleJob(schedule_info as never, async () => {
|
|
117
|
-
const newjob = (await jobs.runScheduledJob(client, {
|
|
118
|
-
jobId: id
|
|
119
|
-
})) as ScheduledJobRow | null;
|
|
120
|
-
|
|
121
|
-
if (newjob) {
|
|
122
|
-
if (newjob.id) {
|
|
123
|
-
log.info(`spinning up job[${newjob.task_identifier}]`);
|
|
124
|
-
} else {
|
|
125
|
-
// this means the scheduled_job has been deleted from db, so cancel it
|
|
126
|
-
log.info(
|
|
127
|
-
`attempted job[${job.task_identifier}] but it's probably non existent, unscheduling...`
|
|
128
|
-
);
|
|
129
|
-
const scheduledJob = this.jobs[job.id];
|
|
130
|
-
if (scheduledJob) scheduledJob.cancel();
|
|
131
|
-
}
|
|
132
|
-
} else {
|
|
133
|
-
log.info(
|
|
134
|
-
`job already scheduled but not yet run or complete: [${job.task_identifier}]`
|
|
135
|
-
);
|
|
136
|
-
}
|
|
137
|
-
});
|
|
138
|
-
this.jobs[id] = j as SchedulerJobHandle;
|
|
139
|
-
}
|
|
140
|
-
async doNext(client: PgClientLike): Promise<void> {
|
|
141
|
-
if (this.stopped) return;
|
|
142
|
-
if (!this._initialized) {
|
|
143
|
-
return await this.initialize(client);
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
if (this.doNextTimer) {
|
|
147
|
-
clearTimeout(this.doNextTimer);
|
|
148
|
-
this.doNextTimer = undefined;
|
|
149
|
-
}
|
|
150
|
-
try {
|
|
151
|
-
const job = await jobs.getScheduledJob<ScheduledJobRow>(client, {
|
|
152
|
-
workerId: this.workerId,
|
|
153
|
-
supportedTaskNames: jobs.getJobSupportAny()
|
|
154
|
-
? null
|
|
155
|
-
: this.supportedTaskNames
|
|
156
|
-
});
|
|
157
|
-
if (!job || !job.id) {
|
|
158
|
-
if (!this.stopped) {
|
|
159
|
-
this.doNextTimer = setTimeout(
|
|
160
|
-
() => this.doNext(client),
|
|
161
|
-
this.idleDelay
|
|
162
|
-
);
|
|
163
|
-
}
|
|
164
|
-
return;
|
|
165
|
-
}
|
|
166
|
-
const start = process.hrtime();
|
|
167
|
-
|
|
168
|
-
let err: Error | null = null;
|
|
169
|
-
try {
|
|
170
|
-
await this.scheduleJob(client, job);
|
|
171
|
-
} catch (error) {
|
|
172
|
-
err = error as Error;
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
const durationRaw = process.hrtime(start);
|
|
176
|
-
const duration = ((durationRaw[0] * 1e9 + durationRaw[1]) / 1e6).toFixed(
|
|
177
|
-
2
|
|
178
|
-
);
|
|
179
|
-
const jobId = job.id;
|
|
180
|
-
try {
|
|
181
|
-
if (err) {
|
|
182
|
-
await this.handleError(client, { err, job, duration });
|
|
183
|
-
} else {
|
|
184
|
-
await this.handleSuccess(client, { job, duration });
|
|
185
|
-
}
|
|
186
|
-
} catch (fatalError: unknown) {
|
|
187
|
-
await this.handleFatalError(client, { err, fatalError, jobId });
|
|
188
|
-
}
|
|
189
|
-
if (!this.stopped) {
|
|
190
|
-
return this.doNext(client);
|
|
191
|
-
}
|
|
192
|
-
return;
|
|
193
|
-
} catch (err: unknown) {
|
|
194
|
-
if (!this.stopped) {
|
|
195
|
-
this.doNextTimer = setTimeout(
|
|
196
|
-
() => this.doNext(client),
|
|
197
|
-
this.idleDelay
|
|
198
|
-
);
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
listen() {
|
|
203
|
-
if (this.stopped) return;
|
|
204
|
-
const listenForChanges = (
|
|
205
|
-
err: Error | null,
|
|
206
|
-
client: PoolClient,
|
|
207
|
-
release: () => void
|
|
208
|
-
) => {
|
|
209
|
-
if (err) {
|
|
210
|
-
log.error('Error connecting with notify listener', err);
|
|
211
|
-
if (err instanceof Error && err.stack) {
|
|
212
|
-
log.debug(err.stack);
|
|
213
|
-
}
|
|
214
|
-
// Try again in 5 seconds
|
|
215
|
-
// should this really be done in the node process?
|
|
216
|
-
if (!this.stopped) {
|
|
217
|
-
setTimeout(this.listen, 5000);
|
|
218
|
-
}
|
|
219
|
-
return;
|
|
220
|
-
}
|
|
221
|
-
if (this.stopped) {
|
|
222
|
-
release();
|
|
223
|
-
return;
|
|
224
|
-
}
|
|
225
|
-
this.listenClient = client;
|
|
226
|
-
this.listenRelease = release;
|
|
227
|
-
client.on('notification', () => {
|
|
228
|
-
log.info('a NEW scheduled JOB!');
|
|
229
|
-
if (this.doNextTimer) {
|
|
230
|
-
// Must be idle, do something!
|
|
231
|
-
this.doNext(client);
|
|
232
|
-
}
|
|
233
|
-
});
|
|
234
|
-
client.query('LISTEN "scheduled_jobs:insert"');
|
|
235
|
-
client.on('error', (e: unknown) => {
|
|
236
|
-
if (this.stopped) {
|
|
237
|
-
release();
|
|
238
|
-
return;
|
|
239
|
-
}
|
|
240
|
-
log.error('Error with database notify listener', e);
|
|
241
|
-
if (e instanceof Error && e.stack) {
|
|
242
|
-
log.debug(e.stack);
|
|
243
|
-
}
|
|
244
|
-
release();
|
|
245
|
-
if (!this.stopped) {
|
|
246
|
-
this.listen();
|
|
247
|
-
}
|
|
248
|
-
});
|
|
249
|
-
log.info(
|
|
250
|
-
`${this.workerId} connected and looking for scheduled jobs...`
|
|
251
|
-
);
|
|
252
|
-
this.doNext(client);
|
|
253
|
-
};
|
|
254
|
-
this.pgPool.connect(listenForChanges);
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
async stop(): Promise<void> {
|
|
258
|
-
this.stopped = true;
|
|
259
|
-
if (this.doNextTimer) {
|
|
260
|
-
clearTimeout(this.doNextTimer);
|
|
261
|
-
this.doNextTimer = undefined;
|
|
262
|
-
}
|
|
263
|
-
Object.values(this.jobs).forEach((job) => job.cancel());
|
|
264
|
-
this.jobs = {};
|
|
265
|
-
|
|
266
|
-
const client = this.listenClient;
|
|
267
|
-
const release = this.listenRelease;
|
|
268
|
-
this.listenClient = undefined;
|
|
269
|
-
this.listenRelease = undefined;
|
|
270
|
-
|
|
271
|
-
if (client && release) {
|
|
272
|
-
client.removeAllListeners('notification');
|
|
273
|
-
client.removeAllListeners('error');
|
|
274
|
-
try {
|
|
275
|
-
await client.query('UNLISTEN "scheduled_jobs:insert"');
|
|
276
|
-
} catch {
|
|
277
|
-
// Ignore listener cleanup errors during shutdown.
|
|
278
|
-
}
|
|
279
|
-
release();
|
|
280
|
-
}
|
|
281
|
-
}
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
export { Scheduler };
|
package/tsconfig.esm.json
DELETED
package/tsconfig.json
DELETED
|
File without changes
|
|
File without changes
|
|
File without changes
|
/package/{dist/run.js → run.js}
RENAMED
|
File without changes
|