@constructive-io/knative-job-worker 1.1.1 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/esm/index.js CHANGED
@@ -134,53 +134,54 @@ export default class Worker {
134
134
  }
135
135
  }
136
136
  }
137
- listen() {
137
+ async listen() {
138
138
  if (this.stopped)
139
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;
140
+ let client;
141
+ let release;
142
+ try {
143
+ client = await this.pgPool.connect();
144
+ release = () => client.release();
145
+ }
146
+ catch (err) {
147
+ log.error('Error connecting with notify listener', err);
148
+ if (err instanceof Error && err.stack) {
149
+ log.debug(err.stack);
152
150
  }
151
+ if (!this.stopped) {
152
+ setTimeout(() => this.listen(), 5000);
153
+ }
154
+ return;
155
+ }
156
+ if (this.stopped) {
157
+ release();
158
+ return;
159
+ }
160
+ this.listenClient = client;
161
+ this.listenRelease = release;
162
+ client.on('notification', () => {
163
+ if (this.doNextTimer) {
164
+ // Must be idle, do something!
165
+ this.doNext(client);
166
+ }
167
+ });
168
+ client.query('LISTEN "jobs:insert"');
169
+ client.on('error', (e) => {
153
170
  if (this.stopped) {
154
171
  release();
155
172
  return;
156
173
  }
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);
174
+ log.error('Error with database notify listener', e);
175
+ if (e instanceof Error && e.stack) {
176
+ log.debug(e.stack);
177
+ }
178
+ release();
179
+ if (!this.stopped) {
180
+ this.listen();
181
+ }
182
+ });
183
+ log.info(`${this.workerId} connected and looking for jobs...`);
184
+ this.doNext(client);
184
185
  }
185
186
  async stop() {
186
187
  this.stopped = true;
package/esm/req.js CHANGED
@@ -1,4 +1,6 @@
1
- import requestLib from 'request';
1
+ import http from 'node:http';
2
+ import https from 'node:https';
3
+ import { URL } from 'node:url';
2
4
  import { getCallbackBaseUrl, getJobGatewayConfig, getJobGatewayDevMap, getNodeEnvironment } from '@constructive-io/job-utils';
3
5
  import { Logger } from '@pgpmjs/logger';
4
6
  const log = new Logger('jobs:req');
@@ -26,30 +28,47 @@ const request = (fn, { body, databaseId, workerId, jobId }) => {
26
28
  databaseId
27
29
  });
28
30
  return new Promise((resolve, reject) => {
29
- requestLib.post({
31
+ let parsed;
32
+ try {
33
+ parsed = new URL(url);
34
+ }
35
+ catch (e) {
36
+ return reject(e);
37
+ }
38
+ const isHttps = parsed.protocol === 'https:';
39
+ const client = isHttps ? https : http;
40
+ const payload = JSON.stringify(body);
41
+ const req = client.request({
42
+ hostname: parsed.hostname,
43
+ port: parsed.port || (isHttps ? 443 : 80),
44
+ path: parsed.pathname + parsed.search,
45
+ method: 'POST',
30
46
  headers: {
31
47
  'Content-Type': 'application/json',
48
+ 'Content-Length': Buffer.byteLength(payload),
32
49
  // these are used by job-worker/job-fn
33
50
  'X-Worker-Id': workerId,
34
- 'X-Job-Id': jobId,
51
+ 'X-Job-Id': String(jobId),
35
52
  'X-Database-Id': databaseId,
36
53
  // async HTTP completion callback
37
54
  '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
55
  }
50
- log.debug(`request success for job[${jobId}] fn[${fn}]`);
51
- return resolve(true);
56
+ }, (res) => {
57
+ res.on('data', () => { });
58
+ res.on('end', () => {
59
+ log.debug(`request success for job[${jobId}] fn[${fn}]`);
60
+ resolve(true);
61
+ });
52
62
  });
63
+ req.on('error', (error) => {
64
+ log.error(`request error for job[${jobId}] fn[${fn}]`, error);
65
+ if (error.stack) {
66
+ log.debug(error.stack);
67
+ }
68
+ reject(error);
69
+ });
70
+ req.write(payload);
71
+ req.end();
53
72
  });
54
73
  };
55
74
  export { request };
package/index.d.ts CHANGED
@@ -39,7 +39,7 @@ export default class Worker {
39
39
  }): Promise<void>;
40
40
  doWork(job: JobRow): Promise<void>;
41
41
  doNext(client: PgClientLike): Promise<void>;
42
- listen(): void;
42
+ listen(): Promise<void>;
43
43
  stop(): Promise<void>;
44
44
  }
45
45
  export { Worker };
package/index.js CHANGED
@@ -173,53 +173,54 @@ class Worker {
173
173
  }
174
174
  }
175
175
  }
176
- listen() {
176
+ async listen() {
177
177
  if (this.stopped)
178
178
  return;
179
- const listenForChanges = (err, client, release) => {
180
- if (err) {
181
- log.error('Error connecting with notify listener', err);
182
- if (err instanceof Error && err.stack) {
183
- log.debug(err.stack);
184
- }
185
- // Try again in 5 seconds
186
- // should this really be done in the node process?
187
- if (!this.stopped) {
188
- setTimeout(this.listen, 5000);
189
- }
190
- return;
179
+ let client;
180
+ let release;
181
+ try {
182
+ client = await this.pgPool.connect();
183
+ release = () => client.release();
184
+ }
185
+ catch (err) {
186
+ log.error('Error connecting with notify listener', err);
187
+ if (err instanceof Error && err.stack) {
188
+ log.debug(err.stack);
189
+ }
190
+ if (!this.stopped) {
191
+ setTimeout(() => this.listen(), 5000);
192
+ }
193
+ return;
194
+ }
195
+ if (this.stopped) {
196
+ release();
197
+ return;
198
+ }
199
+ this.listenClient = client;
200
+ this.listenRelease = release;
201
+ client.on('notification', () => {
202
+ if (this.doNextTimer) {
203
+ // Must be idle, do something!
204
+ this.doNext(client);
191
205
  }
206
+ });
207
+ client.query('LISTEN "jobs:insert"');
208
+ client.on('error', (e) => {
192
209
  if (this.stopped) {
193
210
  release();
194
211
  return;
195
212
  }
196
- this.listenClient = client;
197
- this.listenRelease = release;
198
- client.on('notification', () => {
199
- if (this.doNextTimer) {
200
- // Must be idle, do something!
201
- this.doNext(client);
202
- }
203
- });
204
- client.query('LISTEN "jobs:insert"');
205
- client.on('error', (e) => {
206
- if (this.stopped) {
207
- release();
208
- return;
209
- }
210
- log.error('Error with database notify listener', e);
211
- if (e instanceof Error && e.stack) {
212
- log.debug(e.stack);
213
- }
214
- release();
215
- if (!this.stopped) {
216
- this.listen();
217
- }
218
- });
219
- log.info(`${this.workerId} connected and looking for jobs...`);
220
- this.doNext(client);
221
- };
222
- this.pgPool.connect(listenForChanges);
213
+ log.error('Error with database notify listener', e);
214
+ if (e instanceof Error && e.stack) {
215
+ log.debug(e.stack);
216
+ }
217
+ release();
218
+ if (!this.stopped) {
219
+ this.listen();
220
+ }
221
+ });
222
+ log.info(`${this.workerId} connected and looking for jobs...`);
223
+ this.doNext(client);
223
224
  }
224
225
  async stop() {
225
226
  this.stopped = true;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@constructive-io/knative-job-worker",
3
- "version": "1.1.1",
3
+ "version": "2.0.1",
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",
@@ -37,18 +37,17 @@
37
37
  "url": "https://github.com/constructive-io/jobs/issues"
38
38
  },
39
39
  "dependencies": {
40
- "@constructive-io/job-pg": "^1.1.0",
41
- "@constructive-io/job-utils": "^1.1.0",
40
+ "@constructive-io/job-pg": "^2.0.0",
41
+ "@constructive-io/job-utils": "^2.0.0",
42
42
  "@pgpmjs/logger": "^2.1.0",
43
- "pg": "8.17.1",
44
- "request": "2.88.2"
43
+ "pg": "8.17.1"
45
44
  },
46
45
  "devDependencies": {
47
46
  "@pgpm/database-jobs": "^0.16.0",
48
47
  "@pgpm/verify": "^0.16.0",
49
- "@pgpmjs/core": "^5.2.1",
48
+ "@pgpmjs/core": "^6.0.0",
50
49
  "makage": "^0.1.12",
51
- "pgsql-test": "^3.1.1"
50
+ "pgsql-test": "^4.0.0"
52
51
  },
53
- "gitHead": "49049ad3ddd762d35625f657cb42fa0862b829a0"
52
+ "gitHead": "c05d6bd3dfa36690aefe21079042666dfae801a1"
54
53
  }
package/req.js CHANGED
@@ -4,7 +4,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.request = void 0;
7
- const request_1 = __importDefault(require("request"));
7
+ const node_http_1 = __importDefault(require("node:http"));
8
+ const node_https_1 = __importDefault(require("node:https"));
9
+ const node_url_1 = require("node:url");
8
10
  const job_utils_1 = require("@constructive-io/job-utils");
9
11
  const logger_1 = require("@pgpmjs/logger");
10
12
  const log = new logger_1.Logger('jobs:req');
@@ -32,30 +34,47 @@ const request = (fn, { body, databaseId, workerId, jobId }) => {
32
34
  databaseId
33
35
  });
34
36
  return new Promise((resolve, reject) => {
35
- request_1.default.post({
37
+ let parsed;
38
+ try {
39
+ parsed = new node_url_1.URL(url);
40
+ }
41
+ catch (e) {
42
+ return reject(e);
43
+ }
44
+ const isHttps = parsed.protocol === 'https:';
45
+ const client = isHttps ? node_https_1.default : node_http_1.default;
46
+ const payload = JSON.stringify(body);
47
+ const req = client.request({
48
+ hostname: parsed.hostname,
49
+ port: parsed.port || (isHttps ? 443 : 80),
50
+ path: parsed.pathname + parsed.search,
51
+ method: 'POST',
36
52
  headers: {
37
53
  'Content-Type': 'application/json',
54
+ 'Content-Length': Buffer.byteLength(payload),
38
55
  // these are used by job-worker/job-fn
39
56
  'X-Worker-Id': workerId,
40
- 'X-Job-Id': jobId,
57
+ 'X-Job-Id': String(jobId),
41
58
  'X-Database-Id': databaseId,
42
59
  // async HTTP completion callback
43
60
  'X-Callback-Url': completeUrl
44
- },
45
- url,
46
- json: true,
47
- body
48
- }, function (error) {
49
- if (error) {
50
- log.error(`request error for job[${jobId}] fn[${fn}]`, error);
51
- if (error instanceof Error && error.stack) {
52
- log.debug(error.stack);
53
- }
54
- return reject(error);
55
61
  }
56
- log.debug(`request success for job[${jobId}] fn[${fn}]`);
57
- return resolve(true);
62
+ }, (res) => {
63
+ res.on('data', () => { });
64
+ res.on('end', () => {
65
+ log.debug(`request success for job[${jobId}] fn[${fn}]`);
66
+ resolve(true);
67
+ });
58
68
  });
69
+ req.on('error', (error) => {
70
+ log.error(`request error for job[${jobId}] fn[${fn}]`, error);
71
+ if (error.stack) {
72
+ log.debug(error.stack);
73
+ }
74
+ reject(error);
75
+ });
76
+ req.write(payload);
77
+ req.end();
59
78
  });
60
79
  };
61
80
  exports.request = request;