@constructive-io/knative-job-worker 0.8.0 → 0.9.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/README.md CHANGED
@@ -1,3 +1,61 @@
1
1
  # knative-job-worker
2
2
 
3
3
  Knative-compatible job worker that uses the existing Constructive PostgreSQL job queue and job utilities, invoking HTTP functions via `KNATIVE_SERVICE_URL` (or `INTERNAL_GATEWAY_URL` as a fallback) while preserving the same headers and payload shape as the OpenFaaS worker.
4
+
5
+ ---
6
+
7
+ ## Education and Tutorials
8
+
9
+ 1. 🚀 [Quickstart: Getting Up and Running](https://constructive.io/learn/quickstart)
10
+ Get started with modular databases in minutes. Install prerequisites and deploy your first module.
11
+
12
+ 2. 📦 [Modular PostgreSQL Development with Database Packages](https://constructive.io/learn/modular-postgres)
13
+ Learn to organize PostgreSQL projects with pgpm workspaces and reusable database modules.
14
+
15
+ 3. ✏️ [Authoring Database Changes](https://constructive.io/learn/authoring-database-changes)
16
+ Master the workflow for adding, organizing, and managing database changes with pgpm.
17
+
18
+ 4. 🧪 [End-to-End PostgreSQL Testing with TypeScript](https://constructive.io/learn/e2e-postgres-testing)
19
+ Master end-to-end PostgreSQL testing with ephemeral databases, RLS testing, and CI/CD automation.
20
+
21
+ 5. ⚡ [Supabase Testing](https://constructive.io/learn/supabase)
22
+ Use TypeScript-first tools to test Supabase projects with realistic RLS, policies, and auth contexts.
23
+
24
+ 6. 💧 [Drizzle ORM Testing](https://constructive.io/learn/drizzle-testing)
25
+ Run full-stack tests with Drizzle ORM, including database setup, teardown, and RLS enforcement.
26
+
27
+ 7. 🔧 [Troubleshooting](https://constructive.io/learn/troubleshooting)
28
+ Common issues and solutions for pgpm, PostgreSQL, and testing.
29
+
30
+ ## Related Constructive Tooling
31
+
32
+ ### 📦 Package Management
33
+
34
+ * [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.
35
+
36
+ ### 🧪 Testing
37
+
38
+ * [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.
39
+ * [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.
40
+ * [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.
41
+ * [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.
42
+ * [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.
43
+
44
+ ### 🧠 Parsing & AST
45
+
46
+ * [pgsql-parser](https://www.npmjs.com/package/pgsql-parser): **🔄 SQL conversion engine** that interprets and converts PostgreSQL syntax.
47
+ * [libpg-query-node](https://www.npmjs.com/package/libpg-query): **🌉 Node.js bindings** for `libpg_query`, converting SQL into parse trees.
48
+ * [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.
49
+ * [@pgsql/enums](https://www.npmjs.com/package/@pgsql/enums): **🏷️ TypeScript enums** for PostgreSQL AST for safe and ergonomic parsing logic.
50
+ * [@pgsql/types](https://www.npmjs.com/package/@pgsql/types): **📝 Type definitions** for PostgreSQL AST nodes in TypeScript.
51
+ * [@pgsql/utils](https://www.npmjs.com/package/@pgsql/utils): **🛠️ AST utilities** for constructing and transforming PostgreSQL syntax trees.
52
+
53
+ ## Credits
54
+
55
+ **🛠 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).**
56
+
57
+ ## Disclaimer
58
+
59
+ AS DESCRIBED IN THE LICENSES, THE SOFTWARE IS PROVIDED "AS IS", AT YOUR OWN RISK, AND WITHOUT WARRANTIES OF ANY KIND.
60
+
61
+ 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,208 @@
1
+ import poolManager from '@constructive-io/job-pg';
2
+ import * as jobs from '@constructive-io/job-utils';
3
+ import { Logger } from '@pgpmjs/logger';
4
+ import { request as req } from './req';
5
+ const log = new Logger('jobs:worker');
6
+ export default class Worker {
7
+ idleDelay;
8
+ supportedTaskNames;
9
+ workerId;
10
+ doNextTimer;
11
+ pgPool;
12
+ _initialized;
13
+ listenClient;
14
+ listenRelease;
15
+ stopped;
16
+ constructor({ tasks, idleDelay = 15000, pgPool = poolManager.getPool(), workerId = 'worker-0' }) {
17
+ /*
18
+ * idleDelay: This is how long to wait between polling for jobs.
19
+ *
20
+ * Note: this does NOT need to be short, because we use LISTEN/NOTIFY to be
21
+ * notified when new jobs are added - this is just used in the case where
22
+ * LISTEN/NOTIFY fails for whatever reason.
23
+ */
24
+ this.idleDelay = idleDelay;
25
+ this.supportedTaskNames = tasks;
26
+ this.workerId = workerId;
27
+ this.doNextTimer = undefined;
28
+ this.pgPool = pgPool;
29
+ poolManager.onClose(async () => {
30
+ await jobs.releaseJobs(pgPool, { workerId: this.workerId });
31
+ });
32
+ }
33
+ async initialize(client) {
34
+ if (this._initialized === true)
35
+ return;
36
+ // release any jobs not finished from before if fatal error prevented cleanup
37
+ await jobs.releaseJobs(client, { workerId: this.workerId });
38
+ this._initialized = true;
39
+ await this.doNext(client);
40
+ }
41
+ async handleFatalError(client, { err, fatalError, jobId }) {
42
+ const when = err ? `after failure '${err.message}'` : 'after success';
43
+ log.error(`Failed to release job '${jobId}' ${when}; committing seppuku`);
44
+ await poolManager.close();
45
+ log.error(String(fatalError));
46
+ process.exit(1);
47
+ }
48
+ async handleError(client, { err, job, duration }) {
49
+ log.error(`Failed task ${job.id} (${job.task_identifier}) with error ${err.message} (${duration}ms)`);
50
+ if (err.stack) {
51
+ log.debug(err.stack);
52
+ }
53
+ await jobs.failJob(client, {
54
+ workerId: this.workerId,
55
+ jobId: job.id,
56
+ message: err.message
57
+ });
58
+ }
59
+ async handleSuccess(client, { job, duration }) {
60
+ log.info(`Async task ${job.id} (${job.task_identifier}) to be processed`);
61
+ }
62
+ async doWork(job) {
63
+ const { payload, task_identifier } = job;
64
+ log.debug('starting work on job', {
65
+ id: job.id,
66
+ task: task_identifier,
67
+ databaseId: job.database_id
68
+ });
69
+ if (!jobs.getJobSupportAny() &&
70
+ !this.supportedTaskNames.includes(task_identifier)) {
71
+ throw new Error('Unsupported task');
72
+ }
73
+ await req(task_identifier, {
74
+ body: payload,
75
+ databaseId: job.database_id,
76
+ workerId: this.workerId,
77
+ jobId: job.id
78
+ });
79
+ }
80
+ async doNext(client) {
81
+ if (this.stopped)
82
+ return;
83
+ if (!this._initialized) {
84
+ return await this.initialize(client);
85
+ }
86
+ log.debug('checking for jobs...');
87
+ if (this.doNextTimer) {
88
+ clearTimeout(this.doNextTimer);
89
+ this.doNextTimer = undefined;
90
+ }
91
+ try {
92
+ const job = (await jobs.getJob(client, {
93
+ workerId: this.workerId,
94
+ supportedTaskNames: jobs.getJobSupportAny()
95
+ ? null
96
+ : this.supportedTaskNames
97
+ }));
98
+ if (!job || !job.id) {
99
+ if (!this.stopped) {
100
+ this.doNextTimer = setTimeout(() => this.doNext(client), this.idleDelay);
101
+ }
102
+ return;
103
+ }
104
+ const start = process.hrtime();
105
+ let err = null;
106
+ try {
107
+ await this.doWork(job);
108
+ }
109
+ catch (error) {
110
+ err = error;
111
+ }
112
+ const durationRaw = process.hrtime(start);
113
+ const duration = ((durationRaw[0] * 1e9 + durationRaw[1]) / 1e6).toFixed(2);
114
+ const jobId = job.id;
115
+ try {
116
+ if (err) {
117
+ await this.handleError(client, { err, job, duration });
118
+ }
119
+ else {
120
+ await this.handleSuccess(client, { job, duration });
121
+ }
122
+ }
123
+ catch (fatalError) {
124
+ await this.handleFatalError(client, { err, fatalError, jobId });
125
+ }
126
+ if (!this.stopped) {
127
+ return this.doNext(client);
128
+ }
129
+ return;
130
+ }
131
+ catch (err) {
132
+ if (!this.stopped) {
133
+ this.doNextTimer = setTimeout(() => this.doNext(client), this.idleDelay);
134
+ }
135
+ }
136
+ }
137
+ listen() {
138
+ if (this.stopped)
139
+ return;
140
+ const listenForChanges = (err, client, release) => {
141
+ if (err) {
142
+ log.error('Error connecting with notify listener', err);
143
+ if (err instanceof Error && err.stack) {
144
+ log.debug(err.stack);
145
+ }
146
+ // Try again in 5 seconds
147
+ // should this really be done in the node process?
148
+ if (!this.stopped) {
149
+ setTimeout(this.listen, 5000);
150
+ }
151
+ return;
152
+ }
153
+ if (this.stopped) {
154
+ release();
155
+ return;
156
+ }
157
+ this.listenClient = client;
158
+ this.listenRelease = release;
159
+ client.on('notification', () => {
160
+ if (this.doNextTimer) {
161
+ // Must be idle, do something!
162
+ this.doNext(client);
163
+ }
164
+ });
165
+ client.query('LISTEN "jobs:insert"');
166
+ client.on('error', (e) => {
167
+ if (this.stopped) {
168
+ release();
169
+ return;
170
+ }
171
+ log.error('Error with database notify listener', e);
172
+ if (e instanceof Error && e.stack) {
173
+ log.debug(e.stack);
174
+ }
175
+ release();
176
+ if (!this.stopped) {
177
+ this.listen();
178
+ }
179
+ });
180
+ log.info(`${this.workerId} connected and looking for jobs...`);
181
+ this.doNext(client);
182
+ };
183
+ this.pgPool.connect(listenForChanges);
184
+ }
185
+ async stop() {
186
+ this.stopped = true;
187
+ if (this.doNextTimer) {
188
+ clearTimeout(this.doNextTimer);
189
+ this.doNextTimer = undefined;
190
+ }
191
+ const client = this.listenClient;
192
+ const release = this.listenRelease;
193
+ this.listenClient = undefined;
194
+ this.listenRelease = undefined;
195
+ if (client && release) {
196
+ client.removeAllListeners('notification');
197
+ client.removeAllListeners('error');
198
+ try {
199
+ await client.query('UNLISTEN "jobs:insert"');
200
+ }
201
+ catch {
202
+ // Ignore listener cleanup errors during shutdown.
203
+ }
204
+ release();
205
+ }
206
+ }
207
+ }
208
+ export { Worker };
package/esm/req.js ADDED
@@ -0,0 +1,55 @@
1
+ import requestLib from 'request';
2
+ import { getCallbackBaseUrl, getJobGatewayConfig, getJobGatewayDevMap, getNodeEnvironment } from '@constructive-io/job-utils';
3
+ import { Logger } from '@pgpmjs/logger';
4
+ const log = new Logger('jobs:req');
5
+ // callback URL for job completion
6
+ const completeUrl = getCallbackBaseUrl();
7
+ // Development override map (e.g. point a function name at localhost)
8
+ const nodeEnv = getNodeEnvironment();
9
+ const DEV_MAP = nodeEnv !== 'production' ? getJobGatewayDevMap() : null;
10
+ const getFunctionUrl = (fn) => {
11
+ if (DEV_MAP && DEV_MAP[fn]) {
12
+ return DEV_MAP[fn] || completeUrl;
13
+ }
14
+ const { gatewayUrl } = getJobGatewayConfig();
15
+ const base = gatewayUrl.replace(/\/$/, '');
16
+ return `${base}/${fn}`;
17
+ };
18
+ const request = (fn, { body, databaseId, workerId, jobId }) => {
19
+ const url = getFunctionUrl(fn);
20
+ log.info(`dispatching job`, {
21
+ fn,
22
+ url,
23
+ callbackUrl: completeUrl,
24
+ workerId,
25
+ jobId,
26
+ databaseId
27
+ });
28
+ return new Promise((resolve, reject) => {
29
+ requestLib.post({
30
+ headers: {
31
+ 'Content-Type': 'application/json',
32
+ // these are used by job-worker/job-fn
33
+ 'X-Worker-Id': workerId,
34
+ 'X-Job-Id': jobId,
35
+ 'X-Database-Id': databaseId,
36
+ // async HTTP completion callback
37
+ 'X-Callback-Url': completeUrl
38
+ },
39
+ url,
40
+ json: true,
41
+ body
42
+ }, function (error) {
43
+ if (error) {
44
+ log.error(`request error for job[${jobId}] fn[${fn}]`, error);
45
+ if (error instanceof Error && error.stack) {
46
+ log.debug(error.stack);
47
+ }
48
+ return reject(error);
49
+ }
50
+ log.debug(`request success for job[${jobId}] fn[${fn}]`);
51
+ return resolve(true);
52
+ });
53
+ });
54
+ };
55
+ export { request };
@@ -1,18 +1,11 @@
1
1
  #!/usr/bin/env node
2
-
3
2
  import Worker from './index';
4
3
  import poolManager from '@constructive-io/job-pg';
5
- import {
6
- getWorkerHostname,
7
- getJobSupported
8
- } from '@constructive-io/job-utils';
9
-
4
+ import { getWorkerHostname, getJobSupported } from '@constructive-io/job-utils';
10
5
  const pgPool = poolManager.getPool();
11
-
12
6
  const worker = new Worker({
13
- pgPool,
14
- workerId: getWorkerHostname(),
15
- tasks: getJobSupported()
7
+ pgPool,
8
+ workerId: getWorkerHostname(),
9
+ tasks: getJobSupported()
16
10
  });
17
-
18
11
  worker.listen();
package/package.json CHANGED
@@ -1,21 +1,24 @@
1
1
  {
2
2
  "name": "@constructive-io/knative-job-worker",
3
- "version": "0.8.0",
3
+ "version": "0.9.0",
4
4
  "description": "knative job worker",
5
5
  "author": "Constructive <developers@constructive.io>",
6
6
  "homepage": "https://github.com/constructive-io/jobs/tree/master/packages/knative-job-worker#readme",
7
7
  "license": "SEE LICENSE IN LICENSE",
8
- "main": "dist/index.js",
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
  "bin": {
14
- "faas-jobs": "src/run.ts",
15
- "knative-jobs": "src/run.ts"
16
+ "faas-jobs": "run.js",
17
+ "knative-jobs": "run.js"
16
18
  },
17
19
  "publishConfig": {
18
- "access": "public"
20
+ "access": "public",
21
+ "directory": "dist"
19
22
  },
20
23
  "repository": {
21
24
  "type": "git",
@@ -25,21 +28,27 @@
25
28
  "test": "jest --passWithNoTests",
26
29
  "test:watch": "jest --watch",
27
30
  "test:debug": "node --inspect node_modules/.bin/jest --runInBand",
28
- "build": "tsc -p tsconfig.json",
29
- "build:watch": "tsc -p tsconfig.json -w"
31
+ "clean": "makage clean",
32
+ "prepack": "npm run build",
33
+ "build": "makage build",
34
+ "build:dev": "makage build --dev"
30
35
  },
31
36
  "bugs": {
32
37
  "url": "https://github.com/constructive-io/jobs/issues"
33
38
  },
34
39
  "dependencies": {
35
- "@constructive-io/job-pg": "^0.4.0",
36
- "@constructive-io/job-utils": "^0.6.0",
37
- "@pgpmjs/logger": "^1.4.0",
38
- "pg": "8.16.3",
40
+ "@constructive-io/job-pg": "^0.5.0",
41
+ "@constructive-io/job-utils": "^0.7.0",
42
+ "@pgpmjs/logger": "^1.5.0",
43
+ "pg": "8.17.1",
39
44
  "request": "2.88.2"
40
45
  },
41
46
  "devDependencies": {
42
- "pgsql-test": "^2.25.0"
47
+ "@pgpm/database-jobs": "^0.16.0",
48
+ "@pgpm/verify": "^0.16.0",
49
+ "@pgpmjs/core": "^4.17.0",
50
+ "makage": "^0.1.10",
51
+ "pgsql-test": "^2.26.0"
43
52
  },
44
- "gitHead": "481b3a50b4eec2da6b376c4cd1868065e1e28edb"
53
+ "gitHead": "ec2b5f644c479626305ef85a05a6e7105c2c58cd"
45
54
  }
package/CHANGELOG.md DELETED
@@ -1,236 +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.8.0](https://github.com/constructive-io/jobs/compare/@constructive-io/knative-job-worker@0.7.16...@constructive-io/knative-job-worker@0.8.0) (2026-01-18)
7
-
8
- **Note:** Version bump only for package @constructive-io/knative-job-worker
9
-
10
- ## [0.7.16](https://github.com/constructive-io/jobs/compare/@constructive-io/knative-job-worker@0.7.15...@constructive-io/knative-job-worker@0.7.16) (2026-01-18)
11
-
12
- **Note:** Version bump only for package @constructive-io/knative-job-worker
13
-
14
- ## [0.7.15](https://github.com/constructive-io/jobs/compare/@constructive-io/knative-job-worker@0.7.14...@constructive-io/knative-job-worker@0.7.15) (2026-01-14)
15
-
16
- **Note:** Version bump only for package @constructive-io/knative-job-worker
17
-
18
- ## [0.7.14](https://github.com/constructive-io/jobs/compare/@constructive-io/knative-job-worker@0.7.13...@constructive-io/knative-job-worker@0.7.14) (2026-01-14)
19
-
20
- **Note:** Version bump only for package @constructive-io/knative-job-worker
21
-
22
- ## [0.7.13](https://github.com/constructive-io/jobs/compare/@constructive-io/knative-job-worker@0.7.12...@constructive-io/knative-job-worker@0.7.13) (2026-01-10)
23
-
24
- **Note:** Version bump only for package @constructive-io/knative-job-worker
25
-
26
- ## [0.7.12](https://github.com/constructive-io/jobs/compare/@constructive-io/knative-job-worker@0.7.11...@constructive-io/knative-job-worker@0.7.12) (2026-01-09)
27
-
28
- **Note:** Version bump only for package @constructive-io/knative-job-worker
29
-
30
- ## [0.7.11](https://github.com/constructive-io/jobs/compare/@constructive-io/knative-job-worker@0.7.10...@constructive-io/knative-job-worker@0.7.11) (2026-01-08)
31
-
32
- **Note:** Version bump only for package @constructive-io/knative-job-worker
33
-
34
- ## [0.7.10](https://github.com/constructive-io/jobs/compare/@constructive-io/knative-job-worker@0.7.9...@constructive-io/knative-job-worker@0.7.10) (2026-01-08)
35
-
36
- **Note:** Version bump only for package @constructive-io/knative-job-worker
37
-
38
- ## [0.7.9](https://github.com/constructive-io/jobs/compare/@constructive-io/knative-job-worker@0.7.8...@constructive-io/knative-job-worker@0.7.9) (2026-01-08)
39
-
40
- **Note:** Version bump only for package @constructive-io/knative-job-worker
41
-
42
- ## [0.7.8](https://github.com/constructive-io/jobs/compare/@constructive-io/knative-job-worker@0.7.7...@constructive-io/knative-job-worker@0.7.8) (2026-01-08)
43
-
44
- **Note:** Version bump only for package @constructive-io/knative-job-worker
45
-
46
- ## [0.7.7](https://github.com/constructive-io/jobs/compare/@constructive-io/knative-job-worker@0.7.6...@constructive-io/knative-job-worker@0.7.7) (2026-01-08)
47
-
48
- **Note:** Version bump only for package @constructive-io/knative-job-worker
49
-
50
- ## [0.7.6](https://github.com/constructive-io/jobs/compare/@constructive-io/knative-job-worker@0.7.5...@constructive-io/knative-job-worker@0.7.6) (2026-01-08)
51
-
52
- **Note:** Version bump only for package @constructive-io/knative-job-worker
53
-
54
- ## [0.7.5](https://github.com/constructive-io/jobs/compare/@constructive-io/knative-job-worker@0.7.4...@constructive-io/knative-job-worker@0.7.5) (2026-01-08)
55
-
56
- **Note:** Version bump only for package @constructive-io/knative-job-worker
57
-
58
- ## [0.7.4](https://github.com/constructive-io/jobs/compare/@constructive-io/knative-job-worker@0.7.3...@constructive-io/knative-job-worker@0.7.4) (2026-01-08)
59
-
60
- **Note:** Version bump only for package @constructive-io/knative-job-worker
61
-
62
- ## [0.7.3](https://github.com/constructive-io/jobs/compare/@constructive-io/knative-job-worker@0.7.2...@constructive-io/knative-job-worker@0.7.3) (2026-01-07)
63
-
64
- **Note:** Version bump only for package @constructive-io/knative-job-worker
65
-
66
- ## [0.7.2](https://github.com/constructive-io/jobs/compare/@constructive-io/knative-job-worker@0.7.1...@constructive-io/knative-job-worker@0.7.2) (2026-01-07)
67
-
68
- **Note:** Version bump only for package @constructive-io/knative-job-worker
69
-
70
- ## [0.7.1](https://github.com/constructive-io/jobs/compare/@constructive-io/knative-job-worker@0.7.0...@constructive-io/knative-job-worker@0.7.1) (2026-01-06)
71
-
72
- **Note:** Version bump only for package @constructive-io/knative-job-worker
73
-
74
- # [0.7.0](https://github.com/constructive-io/jobs/compare/@constructive-io/knative-job-worker@0.6.0...@constructive-io/knative-job-worker@0.7.0) (2026-01-05)
75
-
76
- **Note:** Version bump only for package @constructive-io/knative-job-worker
77
-
78
- # [0.6.0](https://github.com/constructive-io/jobs/compare/@constructive-io/knative-job-worker@0.5.46...@constructive-io/knative-job-worker@0.6.0) (2026-01-05)
79
-
80
- **Note:** Version bump only for package @constructive-io/knative-job-worker
81
-
82
- ## [0.5.46](https://github.com/constructive-io/jobs/compare/@constructive-io/knative-job-worker@0.5.45...@constructive-io/knative-job-worker@0.5.46) (2026-01-05)
83
-
84
- **Note:** Version bump only for package @constructive-io/knative-job-worker
85
-
86
- ## [0.5.45](https://github.com/constructive-io/jobs/compare/@constructive-io/knative-job-worker@0.5.44...@constructive-io/knative-job-worker@0.5.45) (2026-01-03)
87
-
88
- **Note:** Version bump only for package @constructive-io/knative-job-worker
89
-
90
- ## [0.5.44](https://github.com/constructive-io/jobs/compare/@constructive-io/knative-job-worker@0.5.43...@constructive-io/knative-job-worker@0.5.44) (2026-01-02)
91
-
92
- **Note:** Version bump only for package @constructive-io/knative-job-worker
93
-
94
- ## [0.5.43](https://github.com/constructive-io/jobs/compare/@constructive-io/knative-job-worker@0.5.42...@constructive-io/knative-job-worker@0.5.43) (2026-01-02)
95
-
96
- **Note:** Version bump only for package @constructive-io/knative-job-worker
97
-
98
- ## [0.5.42](https://github.com/constructive-io/jobs/compare/@constructive-io/knative-job-worker@0.5.41...@constructive-io/knative-job-worker@0.5.42) (2025-12-31)
99
-
100
- **Note:** Version bump only for package @constructive-io/knative-job-worker
101
-
102
- ## [0.5.41](https://github.com/constructive-io/jobs/compare/@constructive-io/knative-job-worker@0.5.40...@constructive-io/knative-job-worker@0.5.41) (2025-12-31)
103
-
104
- **Note:** Version bump only for package @constructive-io/knative-job-worker
105
-
106
- ## [0.5.40](https://github.com/constructive-io/jobs/compare/@constructive-io/knative-job-worker@0.5.39...@constructive-io/knative-job-worker@0.5.40) (2025-12-31)
107
-
108
- **Note:** Version bump only for package @constructive-io/knative-job-worker
109
-
110
- ## [0.5.39](https://github.com/constructive-io/jobs/compare/@constructive-io/knative-job-worker@0.5.38...@constructive-io/knative-job-worker@0.5.39) (2025-12-31)
111
-
112
- **Note:** Version bump only for package @constructive-io/knative-job-worker
113
-
114
- ## [0.5.38](https://github.com/constructive-io/jobs/compare/@constructive-io/knative-job-worker@0.5.37...@constructive-io/knative-job-worker@0.5.38) (2025-12-31)
115
-
116
- **Note:** Version bump only for package @constructive-io/knative-job-worker
117
-
118
- ## [0.5.37](https://github.com/constructive-io/jobs/compare/@constructive-io/knative-job-worker@0.5.36...@constructive-io/knative-job-worker@0.5.37) (2025-12-31)
119
-
120
- **Note:** Version bump only for package @constructive-io/knative-job-worker
121
-
122
- ## [0.5.36](https://github.com/constructive-io/jobs/compare/@constructive-io/knative-job-worker@0.5.35...@constructive-io/knative-job-worker@0.5.36) (2025-12-31)
123
-
124
- **Note:** Version bump only for package @constructive-io/knative-job-worker
125
-
126
- ## [0.5.35](https://github.com/constructive-io/jobs/compare/@constructive-io/knative-job-worker@0.5.34...@constructive-io/knative-job-worker@0.5.35) (2025-12-27)
127
-
128
- **Note:** Version bump only for package @constructive-io/knative-job-worker
129
-
130
- ## [0.5.34](https://github.com/constructive-io/jobs/compare/@constructive-io/knative-job-worker@0.5.33...@constructive-io/knative-job-worker@0.5.34) (2025-12-27)
131
-
132
- **Note:** Version bump only for package @constructive-io/knative-job-worker
133
-
134
- ## [0.5.33](https://github.com/constructive-io/jobs/compare/@constructive-io/knative-job-worker@0.5.32...@constructive-io/knative-job-worker@0.5.33) (2025-12-27)
135
-
136
- **Note:** Version bump only for package @constructive-io/knative-job-worker
137
-
138
- ## [0.5.32](https://github.com/constructive-io/jobs/compare/@constructive-io/knative-job-worker@0.5.31...@constructive-io/knative-job-worker@0.5.32) (2025-12-27)
139
-
140
- **Note:** Version bump only for package @constructive-io/knative-job-worker
141
-
142
- ## [0.5.31](https://github.com/constructive-io/jobs/compare/@constructive-io/knative-job-worker@0.5.30...@constructive-io/knative-job-worker@0.5.31) (2025-12-27)
143
-
144
- **Note:** Version bump only for package @constructive-io/knative-job-worker
145
-
146
- ## [0.5.30](https://github.com/constructive-io/jobs/compare/@constructive-io/knative-job-worker@0.5.29...@constructive-io/knative-job-worker@0.5.30) (2025-12-27)
147
-
148
- **Note:** Version bump only for package @constructive-io/knative-job-worker
149
-
150
- ## [0.5.29](https://github.com/constructive-io/jobs/compare/@constructive-io/knative-job-worker@0.5.28...@constructive-io/knative-job-worker@0.5.29) (2025-12-26)
151
-
152
- **Note:** Version bump only for package @constructive-io/knative-job-worker
153
-
154
- ## [0.5.28](https://github.com/constructive-io/jobs/compare/@constructive-io/knative-job-worker@0.5.27...@constructive-io/knative-job-worker@0.5.28) (2025-12-26)
155
-
156
- **Note:** Version bump only for package @constructive-io/knative-job-worker
157
-
158
- ## [0.5.27](https://github.com/constructive-io/jobs/compare/@constructive-io/knative-job-worker@0.5.26...@constructive-io/knative-job-worker@0.5.27) (2025-12-26)
159
-
160
- **Note:** Version bump only for package @constructive-io/knative-job-worker
161
-
162
- ## [0.5.26](https://github.com/constructive-io/jobs/compare/@constructive-io/knative-job-worker@0.5.25...@constructive-io/knative-job-worker@0.5.26) (2025-12-26)
163
-
164
- **Note:** Version bump only for package @constructive-io/knative-job-worker
165
-
166
- ## [0.5.25](https://github.com/constructive-io/jobs/compare/@constructive-io/knative-job-worker@0.5.24...@constructive-io/knative-job-worker@0.5.25) (2025-12-26)
167
-
168
- **Note:** Version bump only for package @constructive-io/knative-job-worker
169
-
170
- ## [0.5.24](https://github.com/constructive-io/jobs/compare/@constructive-io/knative-job-worker@0.5.23...@constructive-io/knative-job-worker@0.5.24) (2025-12-25)
171
-
172
- **Note:** Version bump only for package @constructive-io/knative-job-worker
173
-
174
- ## [0.5.23](https://github.com/constructive-io/jobs/compare/@constructive-io/knative-job-worker@0.5.22...@constructive-io/knative-job-worker@0.5.23) (2025-12-25)
175
-
176
- **Note:** Version bump only for package @constructive-io/knative-job-worker
177
-
178
- ## [0.5.22](https://github.com/constructive-io/jobs/compare/@constructive-io/knative-job-worker@0.5.21...@constructive-io/knative-job-worker@0.5.22) (2025-12-25)
179
-
180
- **Note:** Version bump only for package @constructive-io/knative-job-worker
181
-
182
- ## [0.5.21](https://github.com/constructive-io/jobs/compare/@constructive-io/knative-job-worker@0.5.20...@constructive-io/knative-job-worker@0.5.21) (2025-12-25)
183
-
184
- **Note:** Version bump only for package @constructive-io/knative-job-worker
185
-
186
- ## [0.5.20](https://github.com/constructive-io/jobs/compare/@constructive-io/knative-job-worker@0.5.19...@constructive-io/knative-job-worker@0.5.20) (2025-12-24)
187
-
188
- **Note:** Version bump only for package @constructive-io/knative-job-worker
189
-
190
- ## [0.5.19](https://github.com/constructive-io/jobs/compare/@constructive-io/knative-job-worker@0.5.18...@constructive-io/knative-job-worker@0.5.19) (2025-12-24)
191
-
192
- **Note:** Version bump only for package @constructive-io/knative-job-worker
193
-
194
- ## [0.5.18](https://github.com/constructive-io/jobs/compare/@constructive-io/knative-job-worker@0.5.17...@constructive-io/knative-job-worker@0.5.18) (2025-12-24)
195
-
196
- **Note:** Version bump only for package @constructive-io/knative-job-worker
197
-
198
- ## [0.5.17](https://github.com/constructive-io/jobs/compare/@constructive-io/knative-job-worker@0.5.16...@constructive-io/knative-job-worker@0.5.17) (2025-12-24)
199
-
200
- **Note:** Version bump only for package @constructive-io/knative-job-worker
201
-
202
- ## [0.5.16](https://github.com/constructive-io/jobs/compare/@constructive-io/knative-job-worker@0.5.15...@constructive-io/knative-job-worker@0.5.16) (2025-12-23)
203
-
204
- **Note:** Version bump only for package @constructive-io/knative-job-worker
205
-
206
- ## [0.5.15](https://github.com/constructive-io/jobs/compare/@constructive-io/knative-job-worker@0.5.14...@constructive-io/knative-job-worker@0.5.15) (2025-12-22)
207
-
208
- **Note:** Version bump only for package @constructive-io/knative-job-worker
209
-
210
- ## [0.5.14](https://github.com/constructive-io/jobs/compare/@constructive-io/knative-job-worker@0.5.13...@constructive-io/knative-job-worker@0.5.14) (2025-12-22)
211
-
212
- **Note:** Version bump only for package @constructive-io/knative-job-worker
213
-
214
- ## [0.5.13](https://github.com/constructive-io/jobs/compare/@constructive-io/knative-job-worker@0.5.12...@constructive-io/knative-job-worker@0.5.13) (2025-12-21)
215
-
216
- **Note:** Version bump only for package @constructive-io/knative-job-worker
217
-
218
- ## [0.5.12](https://github.com/constructive-io/jobs/compare/@constructive-io/knative-job-worker@0.5.11...@constructive-io/knative-job-worker@0.5.12) (2025-12-21)
219
-
220
- **Note:** Version bump only for package @constructive-io/knative-job-worker
221
-
222
- ## [0.5.11](https://github.com/constructive-io/jobs/compare/@constructive-io/knative-job-worker@0.5.10...@constructive-io/knative-job-worker@0.5.11) (2025-12-21)
223
-
224
- **Note:** Version bump only for package @constructive-io/knative-job-worker
225
-
226
- ## [0.5.10](https://github.com/constructive-io/jobs/compare/@constructive-io/knative-job-worker@0.5.9...@constructive-io/knative-job-worker@0.5.10) (2025-12-19)
227
-
228
- **Note:** Version bump only for package @constructive-io/knative-job-worker
229
-
230
- ## 0.5.9 (2025-12-18)
231
-
232
- **Note:** Version bump only for package @constructive-io/knative-job-worker
233
-
234
- ## [0.5.8](https://github.com/constructive-io/jobs/compare/@constructive-io/knative-job-worker@0.5.7...@constructive-io/knative-job-worker@0.5.8) (2025-12-17)
235
-
236
- **Note:** Version bump only for package @constructive-io/knative-job-worker
@@ -1,130 +0,0 @@
1
- const postMock = jest.fn();
2
-
3
- jest.mock('request', () => ({
4
- __esModule: true,
5
- default: { post: postMock },
6
- post: postMock
7
- }));
8
-
9
- describe('knative request wrapper', () => {
10
- beforeEach(() => {
11
- jest.resetModules();
12
- postMock.mockReset();
13
-
14
- process.env.PGUSER = 'postgres';
15
- process.env.PGHOST = 'localhost';
16
- process.env.PGPASSWORD = 'password';
17
- process.env.PGPORT = '5432';
18
- process.env.PGDATABASE = 'jobs';
19
- process.env.JOBS_SCHEMA = 'app_jobs';
20
- process.env.INTERNAL_JOBS_CALLBACK_URL =
21
- 'http://callback.internal/jobs-complete';
22
- process.env.NODE_ENV = 'test';
23
- delete process.env.INTERNAL_GATEWAY_URL;
24
- delete process.env.KNATIVE_SERVICE_URL;
25
- delete process.env.INTERNAL_GATEWAY_DEVELOPMENT_MAP;
26
- });
27
-
28
- it('uses KNATIVE_SERVICE_URL as base and preserves headers and body', async () => {
29
- process.env.KNATIVE_SERVICE_URL = 'http://knative.internal';
30
-
31
- postMock.mockImplementation(
32
- (options: any, callback: (err: any) => void) => callback(null)
33
- );
34
-
35
- const { request } = await import('../src/req');
36
-
37
- await request('example-fn', {
38
- body: { value: 1 },
39
- databaseId: 'db-123',
40
- workerId: 'worker-1',
41
- jobId: 42
42
- });
43
-
44
- expect(postMock).toHaveBeenCalledTimes(1);
45
- const [options] = postMock.mock.calls[0];
46
-
47
- expect(options.url).toBe('http://knative.internal/example-fn');
48
- expect(options.headers['Content-Type']).toBe('application/json');
49
- expect(options.headers['X-Worker-Id']).toBe('worker-1');
50
- expect(options.headers['X-Job-Id']).toBe(42);
51
- expect(options.headers['X-Database-Id']).toBe('db-123');
52
- expect(options.headers['X-Callback-Url']).toBe(
53
- 'http://callback.internal/jobs-complete'
54
- );
55
- expect(options.body).toEqual({ value: 1 });
56
- });
57
-
58
- it('falls back to INTERNAL_GATEWAY_URL when KNATIVE_SERVICE_URL is not set', async () => {
59
- process.env.INTERNAL_GATEWAY_URL =
60
- 'http://gateway.internal/async-function';
61
-
62
- postMock.mockImplementation(
63
- (options: any, callback: (err: any) => void) => callback(null)
64
- );
65
-
66
- const { request } = await import('../src/req');
67
-
68
- await request('example-fn', {
69
- body: { value: 2 },
70
- databaseId: 'db-456',
71
- workerId: 'worker-2',
72
- jobId: 43
73
- });
74
-
75
- const [options] = postMock.mock.calls[0];
76
- expect(options.url).toBe(
77
- 'http://gateway.internal/async-function/example-fn'
78
- );
79
- });
80
-
81
- it('uses development map override when provided', async () => {
82
- process.env.KNATIVE_SERVICE_URL = 'http://knative.internal';
83
- process.env.INTERNAL_GATEWAY_DEVELOPMENT_MAP = JSON.stringify({
84
- 'example-fn': 'http://localhost:3000/dev-fn'
85
- });
86
- process.env.NODE_ENV = 'development';
87
-
88
- postMock.mockImplementation(
89
- (options: any, callback: (err: any) => void) => callback(null)
90
- );
91
-
92
- const { request } = await import('../src/req');
93
-
94
- await request('example-fn', {
95
- body: {},
96
- databaseId: 'db-789',
97
- workerId: 'worker-3',
98
- jobId: 44
99
- });
100
-
101
- const [options] = postMock.mock.calls[0];
102
- expect(options.url).toBe('http://localhost:3000/dev-fn');
103
- });
104
-
105
- it('rejects when HTTP request errors', async () => {
106
- process.env.KNATIVE_SERVICE_URL = 'http://knative.internal';
107
-
108
- postMock.mockImplementation(
109
- (options: any, callback: (err: any) => void) =>
110
- callback(new Error('network failure'))
111
- );
112
-
113
- const { request } = await import('../src/req');
114
-
115
- await expect(
116
- request('example-fn', {
117
- body: {},
118
- databaseId: 'db-000',
119
- workerId: 'worker-4',
120
- jobId: 45
121
- })
122
- ).rejects.toThrow('network failure');
123
- });
124
-
125
- it('throws on startup when no base URL env vars are set', async () => {
126
- await expect(import('../src/env')).rejects.toThrow(
127
- /KNATIVE_SERVICE_URL \(or INTERNAL_GATEWAY_URL as fallback\) is required/
128
- );
129
- });
130
- });
@@ -1,117 +0,0 @@
1
- process.env.KNATIVE_SERVICE_URL =
2
- process.env.KNATIVE_SERVICE_URL || 'knative.internal';
3
- process.env.INTERNAL_JOBS_CALLBACK_URL =
4
- process.env.INTERNAL_JOBS_CALLBACK_URL ||
5
- 'http://callback.internal/jobs-complete';
6
-
7
- const postMock = jest.fn();
8
-
9
- jest.mock('request', () => ({
10
- __esModule: true,
11
- default: { post: postMock },
12
- post: postMock
13
- }));
14
-
15
- jest.mock('@constructive-io/job-pg', () => ({
16
- __esModule: true,
17
- default: {
18
- getPool: jest.fn(),
19
- onClose: jest.fn(),
20
- close: jest.fn()
21
- }
22
- }));
23
-
24
- import path from 'path';
25
- import { getConnections, seed } from 'pgsql-test';
26
- import type { PgTestClient } from 'pgsql-test/test-client';
27
- import * as jobUtils from '@constructive-io/job-utils';
28
- import Worker from '../src';
29
-
30
- let db: PgTestClient;
31
- let teardown: () => Promise<void>;
32
-
33
- beforeAll(async () => {
34
- const modulePath = path.resolve(
35
- __dirname,
36
- '../../../extensions/@pgpm/database-jobs'
37
- );
38
- ({ db, teardown } = await getConnections({}, [seed.loadPgpm(modulePath)]));
39
- db.setContext({ role: 'administrator' });
40
- });
41
-
42
- afterAll(async () => {
43
- await teardown();
44
- });
45
-
46
- beforeEach(async () => {
47
- await db.beforeEach();
48
- postMock.mockReset();
49
- });
50
-
51
- afterEach(async () => {
52
- await db.afterEach();
53
- });
54
-
55
- describe('knative worker integration with job queue', () => {
56
- const databaseId = '5b720132-17d5-424d-9bcb-ee7b17c13d43';
57
-
58
- it('pulls a job from the queue and posts to Knative service', async () => {
59
- const insertedJob = await db.one(
60
- 'SELECT * FROM app_jobs.add_job($1::uuid, $2::text, $3::json);',
61
- [databaseId, 'example-fn', { hello: 'world' }]
62
- );
63
-
64
- const worker = new Worker({
65
- tasks: ['example-fn'],
66
- pgPool: {} as any,
67
- workerId: 'worker-integration-1'
68
- });
69
-
70
- const job = await jobUtils.getJob(db as any, {
71
- workerId: 'worker-integration-1',
72
- supportedTaskNames: null
73
- });
74
-
75
- expect(job).toBeTruthy();
76
- expect(job.id).toBe(insertedJob.id);
77
-
78
- postMock.mockImplementation(
79
- (options: any, callback: (err: any) => void) => callback(null)
80
- );
81
-
82
- await (worker as any).doWork(job);
83
-
84
- expect(postMock).toHaveBeenCalledTimes(1);
85
- const [options] = postMock.mock.calls[0];
86
- expect(options.url).toBe('http://example-fn.knative.internal');
87
- expect(options.headers['X-Job-Id']).toBe(job.id);
88
- expect(options.headers['X-Database-Id']).toBe(databaseId);
89
- });
90
-
91
- it('propagates errors from failed Knative calls while keeping job queue interactions intact', async () => {
92
- await db.one(
93
- 'SELECT * FROM app_jobs.add_job($1::uuid, $2::text, $3::json);',
94
- [databaseId, 'example-fn', { hello: 'world' }]
95
- );
96
-
97
- const worker = new Worker({
98
- tasks: ['example-fn'],
99
- pgPool: {} as any,
100
- workerId: 'worker-integration-2'
101
- });
102
-
103
- const job = await jobUtils.getJob(db as any, {
104
- workerId: 'worker-integration-2',
105
- supportedTaskNames: null
106
- });
107
-
108
- postMock.mockImplementation(
109
- (options: any, callback: (err: any) => void) =>
110
- callback(new Error('knative failure'))
111
- );
112
-
113
- await expect((worker as any).doWork(job)).rejects.toThrow(
114
- 'knative failure'
115
- );
116
- });
117
- });
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,262 +0,0 @@
1
- import poolManager from '@constructive-io/job-pg';
2
- import * as jobs from '@constructive-io/job-utils';
3
- import type { PgClientLike } from '@constructive-io/job-utils';
4
- import type { Pool, PoolClient } from 'pg';
5
- import { Logger } from '@pgpmjs/logger';
6
- import { request as req } from './req';
7
-
8
- export interface JobRow {
9
- id: number | string;
10
- task_identifier: string;
11
- payload?: unknown;
12
- database_id?: string;
13
- }
14
-
15
- const log = new Logger('jobs:worker');
16
-
17
- export default class Worker {
18
- idleDelay: number;
19
- supportedTaskNames: string[];
20
- workerId: string;
21
- doNextTimer?: NodeJS.Timeout;
22
- pgPool: Pool;
23
- _initialized?: boolean;
24
- listenClient?: PoolClient;
25
- listenRelease?: () => void;
26
- stopped?: boolean;
27
-
28
- constructor({
29
- tasks,
30
- idleDelay = 15000,
31
- pgPool = poolManager.getPool(),
32
- workerId = 'worker-0'
33
- }: {
34
- tasks: string[];
35
- idleDelay?: number;
36
- pgPool?: Pool;
37
- workerId?: string;
38
- }) {
39
- /*
40
- * idleDelay: This is how long to wait between polling for jobs.
41
- *
42
- * Note: this does NOT need to be short, because we use LISTEN/NOTIFY to be
43
- * notified when new jobs are added - this is just used in the case where
44
- * LISTEN/NOTIFY fails for whatever reason.
45
- */
46
-
47
- this.idleDelay = idleDelay;
48
- this.supportedTaskNames = tasks;
49
- this.workerId = workerId;
50
- this.doNextTimer = undefined;
51
- this.pgPool = pgPool;
52
- poolManager.onClose(async () => {
53
- await jobs.releaseJobs(pgPool, { workerId: this.workerId });
54
- });
55
- }
56
- async initialize(client: PgClientLike) {
57
- if (this._initialized === true) return;
58
-
59
- // release any jobs not finished from before if fatal error prevented cleanup
60
- await jobs.releaseJobs(client, { workerId: this.workerId });
61
-
62
- this._initialized = true;
63
- await this.doNext(client);
64
- }
65
- async handleFatalError(
66
- client: PgClientLike,
67
- {
68
- err,
69
- fatalError,
70
- jobId
71
- }: { err?: Error; fatalError: unknown; jobId: JobRow['id'] }
72
- ) {
73
- const when = err ? `after failure '${err.message}'` : 'after success';
74
- log.error(`Failed to release job '${jobId}' ${when}; committing seppuku`);
75
- await poolManager.close();
76
- log.error(String(fatalError));
77
- process.exit(1);
78
- }
79
- async handleError(
80
- client: PgClientLike,
81
- { err, job, duration }: { err: Error; job: JobRow; duration: string }
82
- ) {
83
- log.error(
84
- `Failed task ${job.id} (${job.task_identifier}) with error ${err.message} (${duration}ms)`
85
- );
86
- if (err.stack) {
87
- log.debug(err.stack);
88
- }
89
- await jobs.failJob(client, {
90
- workerId: this.workerId,
91
- jobId: job.id,
92
- message: err.message
93
- });
94
- }
95
- async handleSuccess(
96
- client: PgClientLike,
97
- { job, duration }: { job: JobRow; duration: string }
98
- ) {
99
- log.info(
100
- `Async task ${job.id} (${job.task_identifier}) to be processed`
101
- );
102
- }
103
- async doWork(job: JobRow) {
104
- const { payload, task_identifier } = job;
105
- log.debug('starting work on job', {
106
- id: job.id,
107
- task: task_identifier,
108
- databaseId: job.database_id
109
- });
110
- if (
111
- !jobs.getJobSupportAny() &&
112
- !this.supportedTaskNames.includes(task_identifier)
113
- ) {
114
- throw new Error('Unsupported task');
115
- }
116
- await req(task_identifier, {
117
- body: payload,
118
- databaseId: job.database_id,
119
- workerId: this.workerId,
120
- jobId: job.id
121
- });
122
- }
123
- async doNext(client: PgClientLike): Promise<void> {
124
- if (this.stopped) return;
125
- if (!this._initialized) {
126
- return await this.initialize(client);
127
- }
128
-
129
- log.debug('checking for jobs...');
130
- if (this.doNextTimer) {
131
- clearTimeout(this.doNextTimer);
132
- this.doNextTimer = undefined;
133
- }
134
- try {
135
- const job = (await jobs.getJob<JobRow>(client, {
136
- workerId: this.workerId,
137
- supportedTaskNames: jobs.getJobSupportAny()
138
- ? null
139
- : this.supportedTaskNames
140
- })) as JobRow | undefined;
141
-
142
- if (!job || !job.id) {
143
- if (!this.stopped) {
144
- this.doNextTimer = setTimeout(
145
- () => this.doNext(client),
146
- this.idleDelay
147
- );
148
- }
149
- return;
150
- }
151
- const start = process.hrtime();
152
-
153
- let err: Error | null = null;
154
- try {
155
- await this.doWork(job);
156
- } catch (error) {
157
- err = error as Error;
158
- }
159
- const durationRaw = process.hrtime(start);
160
- const duration = ((durationRaw[0] * 1e9 + durationRaw[1]) / 1e6).toFixed(
161
- 2
162
- );
163
- const jobId = job.id;
164
- try {
165
- if (err) {
166
- await this.handleError(client, { err, job, duration });
167
- } else {
168
- await this.handleSuccess(client, { job, duration });
169
- }
170
- } catch (fatalError: unknown) {
171
- await this.handleFatalError(client, { err, fatalError, jobId });
172
- }
173
- if (!this.stopped) {
174
- return this.doNext(client);
175
- }
176
- return;
177
- } catch (err: unknown) {
178
- if (!this.stopped) {
179
- this.doNextTimer = setTimeout(
180
- () => this.doNext(client),
181
- this.idleDelay
182
- );
183
- }
184
- }
185
- }
186
- listen() {
187
- if (this.stopped) return;
188
- const listenForChanges = (
189
- err: Error | null,
190
- client: PoolClient,
191
- release: () => void
192
- ) => {
193
- if (err) {
194
- log.error('Error connecting with notify listener', err);
195
- if (err instanceof Error && err.stack) {
196
- log.debug(err.stack);
197
- }
198
- // Try again in 5 seconds
199
- // should this really be done in the node process?
200
- if (!this.stopped) {
201
- setTimeout(this.listen, 5000);
202
- }
203
- return;
204
- }
205
- if (this.stopped) {
206
- release();
207
- return;
208
- }
209
- this.listenClient = client;
210
- this.listenRelease = release;
211
- client.on('notification', () => {
212
- if (this.doNextTimer) {
213
- // Must be idle, do something!
214
- this.doNext(client);
215
- }
216
- });
217
- client.query('LISTEN "jobs:insert"');
218
- client.on('error', (e: unknown) => {
219
- if (this.stopped) {
220
- release();
221
- return;
222
- }
223
- log.error('Error with database notify listener', e);
224
- if (e instanceof Error && e.stack) {
225
- log.debug(e.stack);
226
- }
227
- release();
228
- if (!this.stopped) {
229
- this.listen();
230
- }
231
- });
232
- log.info(`${this.workerId} connected and looking for jobs...`);
233
- this.doNext(client);
234
- };
235
- this.pgPool.connect(listenForChanges);
236
- }
237
-
238
- async stop(): Promise<void> {
239
- this.stopped = true;
240
- if (this.doNextTimer) {
241
- clearTimeout(this.doNextTimer);
242
- this.doNextTimer = undefined;
243
- }
244
- const client = this.listenClient;
245
- const release = this.listenRelease;
246
- this.listenClient = undefined;
247
- this.listenRelease = undefined;
248
-
249
- if (client && release) {
250
- client.removeAllListeners('notification');
251
- client.removeAllListeners('error');
252
- try {
253
- await client.query('UNLISTEN "jobs:insert"');
254
- } catch {
255
- // Ignore listener cleanup errors during shutdown.
256
- }
257
- release();
258
- }
259
- }
260
- }
261
-
262
- export { Worker };
package/src/req.ts DELETED
@@ -1,82 +0,0 @@
1
- import requestLib from 'request';
2
- import {
3
- getCallbackBaseUrl,
4
- getJobGatewayConfig,
5
- getJobGatewayDevMap,
6
- getNodeEnvironment
7
- } from '@constructive-io/job-utils';
8
- import { Logger } from '@pgpmjs/logger';
9
-
10
- const log = new Logger('jobs:req');
11
-
12
- // callback URL for job completion
13
- const completeUrl = getCallbackBaseUrl();
14
-
15
- // Development override map (e.g. point a function name at localhost)
16
- const nodeEnv = getNodeEnvironment();
17
- const DEV_MAP = nodeEnv !== 'production' ? getJobGatewayDevMap() : null;
18
-
19
- const getFunctionUrl = (fn: string): string => {
20
- if (DEV_MAP && DEV_MAP[fn]) {
21
- return DEV_MAP[fn] || completeUrl;
22
- }
23
-
24
- const { gatewayUrl } = getJobGatewayConfig();
25
- const base = gatewayUrl.replace(/\/$/, '');
26
- return `${base}/${fn}`;
27
- };
28
-
29
- interface RequestOptions {
30
- body: unknown;
31
- databaseId: string;
32
- workerId: string;
33
- jobId: string | number;
34
- }
35
-
36
- const request = (
37
- fn: string,
38
- { body, databaseId, workerId, jobId }: RequestOptions
39
- ) => {
40
- const url = getFunctionUrl(fn);
41
- log.info(`dispatching job`, {
42
- fn,
43
- url,
44
- callbackUrl: completeUrl,
45
- workerId,
46
- jobId,
47
- databaseId
48
- });
49
- return new Promise<boolean>((resolve, reject) => {
50
- requestLib.post(
51
- {
52
- headers: {
53
- 'Content-Type': 'application/json',
54
-
55
- // these are used by job-worker/job-fn
56
- 'X-Worker-Id': workerId,
57
- 'X-Job-Id': jobId,
58
- 'X-Database-Id': databaseId,
59
-
60
- // async HTTP completion callback
61
- 'X-Callback-Url': completeUrl
62
- },
63
- url,
64
- json: true,
65
- body
66
- },
67
- function (error: unknown) {
68
- if (error) {
69
- log.error(`request error for job[${jobId}] fn[${fn}]`, error);
70
- if (error instanceof Error && error.stack) {
71
- log.debug(error.stack);
72
- }
73
- return reject(error);
74
- }
75
- log.debug(`request success for job[${jobId}] fn[${fn}]`);
76
- return resolve(true);
77
- }
78
- );
79
- });
80
- };
81
-
82
- export { request };
package/tsconfig.esm.json DELETED
@@ -1,9 +0,0 @@
1
- {
2
- "extends": "./tsconfig.json",
3
- "compilerOptions": {
4
- "outDir": "dist/esm",
5
- "module": "es2022",
6
- "rootDir": "src/",
7
- "declaration": false
8
- }
9
- }
package/tsconfig.json DELETED
@@ -1,9 +0,0 @@
1
- {
2
- "extends": "../../tsconfig.json",
3
- "compilerOptions": {
4
- "outDir": "dist",
5
- "rootDir": "src/"
6
- },
7
- "include": ["src/**/*.ts", "../../types/**/*.d.ts"],
8
- "exclude": ["dist", "node_modules", "**/*.spec.*", "**/*.test.*"]
9
- }
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes