@constructive-io/job-worker 0.3.22 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +8 -0
- package/dist/index.js +11 -10
- package/dist/run.js +4 -2
- package/package.json +4 -3
- package/src/index.ts +11 -10
- package/src/run.ts +5 -2
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,14 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
# [0.4.0](https://github.com/constructive-io/jobs/compare/@constructive-io/job-worker@0.3.23...@constructive-io/job-worker@0.4.0) (2026-01-18)
|
|
7
|
+
|
|
8
|
+
**Note:** Version bump only for package @constructive-io/job-worker
|
|
9
|
+
|
|
10
|
+
## [0.3.23](https://github.com/constructive-io/jobs/compare/@constructive-io/job-worker@0.3.22...@constructive-io/job-worker@0.3.23) (2026-01-18)
|
|
11
|
+
|
|
12
|
+
**Note:** Version bump only for package @constructive-io/job-worker
|
|
13
|
+
|
|
6
14
|
## [0.3.22](https://github.com/constructive-io/jobs/compare/@constructive-io/job-worker@0.3.21...@constructive-io/job-worker@0.3.22) (2026-01-09)
|
|
7
15
|
|
|
8
16
|
**Note:** Version bump only for package @constructive-io/job-worker
|
package/dist/index.js
CHANGED
|
@@ -39,6 +39,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
39
39
|
exports.Worker = void 0;
|
|
40
40
|
const pg_1 = __importDefault(require("pg"));
|
|
41
41
|
const jobs = __importStar(require("@constructive-io/job-utils"));
|
|
42
|
+
const logger_1 = require("@pgpmjs/logger");
|
|
42
43
|
const pgPoolConfig = {
|
|
43
44
|
connectionString: jobs.getJobConnectionString()
|
|
44
45
|
};
|
|
@@ -52,7 +53,7 @@ function once(fn, context) {
|
|
|
52
53
|
return result;
|
|
53
54
|
};
|
|
54
55
|
}
|
|
55
|
-
|
|
56
|
+
const logger = (0, logger_1.createLogger)('job-worker');
|
|
56
57
|
class Worker {
|
|
57
58
|
tasks;
|
|
58
59
|
idleDelay;
|
|
@@ -77,7 +78,7 @@ class Worker {
|
|
|
77
78
|
this.doNextTimer = undefined;
|
|
78
79
|
this.pgPool = pgPool;
|
|
79
80
|
const close = () => {
|
|
80
|
-
|
|
81
|
+
logger.info('closing connection...');
|
|
81
82
|
this.close();
|
|
82
83
|
};
|
|
83
84
|
process.once('SIGTERM', close);
|
|
@@ -91,13 +92,13 @@ class Worker {
|
|
|
91
92
|
}
|
|
92
93
|
handleFatalError({ err, fatalError, jobId }) {
|
|
93
94
|
const when = err ? `after failure '${err.message}'` : 'after success';
|
|
94
|
-
|
|
95
|
-
|
|
95
|
+
logger.error(`Failed to release job '${jobId}' ${when}; committing seppuku`);
|
|
96
|
+
logger.error(fatalError);
|
|
96
97
|
process.exit(1);
|
|
97
98
|
}
|
|
98
99
|
async handleError(client, { err, job, duration }) {
|
|
99
|
-
|
|
100
|
-
|
|
100
|
+
logger.error(`Failed task ${job.id} (${job.task_identifier}) with error ${err.message} (${duration}ms)`, { err, stack: err.stack });
|
|
101
|
+
logger.error(err.stack);
|
|
101
102
|
await jobs.failJob(client, {
|
|
102
103
|
workerId: this.workerId,
|
|
103
104
|
jobId: job.id,
|
|
@@ -105,7 +106,7 @@ class Worker {
|
|
|
105
106
|
});
|
|
106
107
|
}
|
|
107
108
|
async handleSuccess(client, { job, duration }) {
|
|
108
|
-
|
|
109
|
+
logger.info(`Completed task ${job.id} (${job.task_identifier}) with success (${duration}ms)`);
|
|
109
110
|
await jobs.completeJob(client, { workerId: this.workerId, jobId: job.id });
|
|
110
111
|
}
|
|
111
112
|
async doWork(job) {
|
|
@@ -164,7 +165,7 @@ class Worker {
|
|
|
164
165
|
listen() {
|
|
165
166
|
const listenForChanges = (err, client, release) => {
|
|
166
167
|
if (err) {
|
|
167
|
-
|
|
168
|
+
logger.error('Error connecting with notify listener', err);
|
|
168
169
|
// Try again in 5 seconds
|
|
169
170
|
// should this really be done in the node process?
|
|
170
171
|
setTimeout(this.listen, 5000);
|
|
@@ -178,11 +179,11 @@ class Worker {
|
|
|
178
179
|
});
|
|
179
180
|
client.query('LISTEN "jobs:insert"');
|
|
180
181
|
client.on('error', (e) => {
|
|
181
|
-
|
|
182
|
+
logger.error('Error with database notify listener', e);
|
|
182
183
|
release();
|
|
183
184
|
this.listen();
|
|
184
185
|
});
|
|
185
|
-
|
|
186
|
+
logger.info(`${this.workerId} connected and looking for jobs...`);
|
|
186
187
|
this.doNext(client);
|
|
187
188
|
};
|
|
188
189
|
this.pgPool.connect(listenForChanges);
|
package/dist/run.js
CHANGED
|
@@ -4,12 +4,14 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
const index_1 = __importDefault(require("./index"));
|
|
7
|
+
const logger_1 = require("@pgpmjs/logger");
|
|
8
|
+
const logger = (0, logger_1.createLogger)('job-worker-run');
|
|
7
9
|
const worker = new index_1.default({
|
|
8
10
|
tasks: {
|
|
9
11
|
hello: async ({ pgPool, workerId }, job) => {
|
|
10
|
-
|
|
12
|
+
logger.info('hello');
|
|
11
13
|
await pgPool.query('select 1');
|
|
12
|
-
|
|
14
|
+
logger.info(JSON.stringify(job, null, 2));
|
|
13
15
|
}
|
|
14
16
|
}
|
|
15
17
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@constructive-io/job-worker",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "job worker",
|
|
5
5
|
"author": "Constructive <developers@constructive.io>",
|
|
6
6
|
"homepage": "https://github.com/constructive-io/jobs/tree/master/packages/job-worker#readme",
|
|
@@ -28,8 +28,9 @@
|
|
|
28
28
|
"url": "https://github.com/constructive-io/jobs/issues"
|
|
29
29
|
},
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@constructive-io/job-utils": "^0.
|
|
31
|
+
"@constructive-io/job-utils": "^0.6.0",
|
|
32
|
+
"@pgpmjs/logger": "^1.4.0",
|
|
32
33
|
"pg": "8.16.3"
|
|
33
34
|
},
|
|
34
|
-
"gitHead": "
|
|
35
|
+
"gitHead": "481b3a50b4eec2da6b376c4cd1868065e1e28edb"
|
|
35
36
|
}
|
package/src/index.ts
CHANGED
|
@@ -2,6 +2,7 @@ import pg from 'pg';
|
|
|
2
2
|
import type { Pool, PoolClient } from 'pg';
|
|
3
3
|
import * as jobs from '@constructive-io/job-utils';
|
|
4
4
|
import type { PgClientLike } from '@constructive-io/job-utils';
|
|
5
|
+
import { createLogger } from '@pgpmjs/logger';
|
|
5
6
|
|
|
6
7
|
const pgPoolConfig = {
|
|
7
8
|
connectionString: jobs.getJobConnectionString()
|
|
@@ -37,7 +38,7 @@ export type TaskHandler = (
|
|
|
37
38
|
job: JobRow
|
|
38
39
|
) => Promise<void> | void;
|
|
39
40
|
|
|
40
|
-
|
|
41
|
+
const logger = createLogger('job-worker');
|
|
41
42
|
|
|
42
43
|
export default class Worker {
|
|
43
44
|
tasks: Record<string, TaskHandler>;
|
|
@@ -75,7 +76,7 @@ export default class Worker {
|
|
|
75
76
|
this.doNextTimer = undefined;
|
|
76
77
|
this.pgPool = pgPool;
|
|
77
78
|
const close = () => {
|
|
78
|
-
|
|
79
|
+
logger.info('closing connection...');
|
|
79
80
|
this.close();
|
|
80
81
|
};
|
|
81
82
|
process.once('SIGTERM', close);
|
|
@@ -97,21 +98,21 @@ export default class Worker {
|
|
|
97
98
|
jobId: JobRow['id'];
|
|
98
99
|
}) {
|
|
99
100
|
const when = err ? `after failure '${err.message}'` : 'after success';
|
|
100
|
-
|
|
101
|
+
logger.error(
|
|
101
102
|
`Failed to release job '${jobId}' ${when}; committing seppuku`
|
|
102
103
|
);
|
|
103
|
-
|
|
104
|
+
logger.error(fatalError);
|
|
104
105
|
process.exit(1);
|
|
105
106
|
}
|
|
106
107
|
async handleError(
|
|
107
108
|
client: PgClientLike,
|
|
108
109
|
{ err, job, duration }: { err: Error; job: JobRow; duration: string }
|
|
109
110
|
) {
|
|
110
|
-
|
|
111
|
+
logger.error(
|
|
111
112
|
`Failed task ${job.id} (${job.task_identifier}) with error ${err.message} (${duration}ms)`,
|
|
112
113
|
{ err, stack: err.stack }
|
|
113
114
|
);
|
|
114
|
-
|
|
115
|
+
logger.error(err.stack);
|
|
115
116
|
await jobs.failJob(client, {
|
|
116
117
|
workerId: this.workerId,
|
|
117
118
|
jobId: job.id,
|
|
@@ -122,7 +123,7 @@ export default class Worker {
|
|
|
122
123
|
client: PgClientLike,
|
|
123
124
|
{ job, duration }: { job: JobRow; duration: string }
|
|
124
125
|
) {
|
|
125
|
-
|
|
126
|
+
logger.info(
|
|
126
127
|
`Completed task ${job.id} (${job.task_identifier}) with success (${duration}ms)`
|
|
127
128
|
);
|
|
128
129
|
await jobs.completeJob(client, { workerId: this.workerId, jobId: job.id });
|
|
@@ -192,7 +193,7 @@ export default class Worker {
|
|
|
192
193
|
release: () => void
|
|
193
194
|
) => {
|
|
194
195
|
if (err) {
|
|
195
|
-
|
|
196
|
+
logger.error('Error connecting with notify listener', err);
|
|
196
197
|
// Try again in 5 seconds
|
|
197
198
|
// should this really be done in the node process?
|
|
198
199
|
setTimeout(this.listen, 5000);
|
|
@@ -206,11 +207,11 @@ export default class Worker {
|
|
|
206
207
|
});
|
|
207
208
|
client.query('LISTEN "jobs:insert"');
|
|
208
209
|
client.on('error', (e: unknown) => {
|
|
209
|
-
|
|
210
|
+
logger.error('Error with database notify listener', e);
|
|
210
211
|
release();
|
|
211
212
|
this.listen();
|
|
212
213
|
});
|
|
213
|
-
|
|
214
|
+
logger.info(`${this.workerId} connected and looking for jobs...`);
|
|
214
215
|
this.doNext(client);
|
|
215
216
|
};
|
|
216
217
|
this.pgPool.connect(listenForChanges);
|
package/src/run.ts
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
import Worker, { WorkerContext, JobRow } from './index';
|
|
2
|
+
import { createLogger } from '@pgpmjs/logger';
|
|
3
|
+
|
|
4
|
+
const logger = createLogger('job-worker-run');
|
|
2
5
|
|
|
3
6
|
const worker = new Worker({
|
|
4
7
|
tasks: {
|
|
@@ -6,9 +9,9 @@ const worker = new Worker({
|
|
|
6
9
|
{ pgPool, workerId }: WorkerContext,
|
|
7
10
|
job: JobRow
|
|
8
11
|
) => {
|
|
9
|
-
|
|
12
|
+
logger.info('hello');
|
|
10
13
|
await pgPool.query('select 1');
|
|
11
|
-
|
|
14
|
+
logger.info(JSON.stringify(job, null, 2));
|
|
12
15
|
}
|
|
13
16
|
}
|
|
14
17
|
});
|