@constructive-io/job-scheduler 2.0.0 → 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
@@ -147,54 +147,55 @@ export default class Scheduler {
147
147
  }
148
148
  }
149
149
  }
150
- listen() {
150
+ async listen() {
151
151
  if (this.stopped)
152
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;
153
+ let client;
154
+ let release;
155
+ try {
156
+ client = await this.pgPool.connect();
157
+ release = () => client.release();
158
+ }
159
+ catch (err) {
160
+ log.error('Error connecting with notify listener', err);
161
+ if (err instanceof Error && err.stack) {
162
+ log.debug(err.stack);
163
+ }
164
+ if (!this.stopped) {
165
+ setTimeout(() => this.listen(), 5000);
166
+ }
167
+ return;
168
+ }
169
+ if (this.stopped) {
170
+ release();
171
+ return;
172
+ }
173
+ this.listenClient = client;
174
+ this.listenRelease = release;
175
+ client.on('notification', () => {
176
+ log.info('a NEW scheduled JOB!');
177
+ if (this.doNextTimer) {
178
+ // Must be idle, do something!
179
+ this.doNext(client);
165
180
  }
181
+ });
182
+ client.query('LISTEN "scheduled_jobs:insert"');
183
+ client.on('error', (e) => {
166
184
  if (this.stopped) {
167
185
  release();
168
186
  return;
169
187
  }
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);
188
+ log.error('Error with database notify listener', e);
189
+ if (e instanceof Error && e.stack) {
190
+ log.debug(e.stack);
191
+ }
192
+ release();
193
+ if (!this.stopped) {
194
+ this.listen();
195
+ }
196
+ });
197
+ log.info(`${this.workerId} connected and looking for scheduled jobs...`);
198
+ this.doNext(client);
198
199
  }
199
200
  async stop() {
200
201
  this.stopped = true;
package/index.d.ts CHANGED
@@ -42,7 +42,7 @@ export default class Scheduler {
42
42
  }): Promise<void>;
43
43
  scheduleJob(client: PgClientLike, job: ScheduledJobRow): Promise<void>;
44
44
  doNext(client: PgClientLike): Promise<void>;
45
- listen(): void;
45
+ listen(): Promise<void>;
46
46
  stop(): Promise<void>;
47
47
  }
48
48
  export { Scheduler };
package/index.js CHANGED
@@ -186,54 +186,55 @@ class Scheduler {
186
186
  }
187
187
  }
188
188
  }
189
- listen() {
189
+ async listen() {
190
190
  if (this.stopped)
191
191
  return;
192
- const listenForChanges = (err, client, release) => {
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;
192
+ let client;
193
+ let release;
194
+ try {
195
+ client = await this.pgPool.connect();
196
+ release = () => client.release();
197
+ }
198
+ catch (err) {
199
+ log.error('Error connecting with notify listener', err);
200
+ if (err instanceof Error && err.stack) {
201
+ log.debug(err.stack);
202
+ }
203
+ if (!this.stopped) {
204
+ setTimeout(() => this.listen(), 5000);
205
+ }
206
+ return;
207
+ }
208
+ if (this.stopped) {
209
+ release();
210
+ return;
211
+ }
212
+ this.listenClient = client;
213
+ this.listenRelease = release;
214
+ client.on('notification', () => {
215
+ log.info('a NEW scheduled JOB!');
216
+ if (this.doNextTimer) {
217
+ // Must be idle, do something!
218
+ this.doNext(client);
204
219
  }
220
+ });
221
+ client.query('LISTEN "scheduled_jobs:insert"');
222
+ client.on('error', (e) => {
205
223
  if (this.stopped) {
206
224
  release();
207
225
  return;
208
226
  }
209
- this.listenClient = client;
210
- this.listenRelease = release;
211
- client.on('notification', () => {
212
- log.info('a NEW scheduled JOB!');
213
- if (this.doNextTimer) {
214
- // Must be idle, do something!
215
- this.doNext(client);
216
- }
217
- });
218
- client.query('LISTEN "scheduled_jobs:insert"');
219
- client.on('error', (e) => {
220
- if (this.stopped) {
221
- release();
222
- return;
223
- }
224
- log.error('Error with database notify listener', e);
225
- if (e instanceof Error && e.stack) {
226
- log.debug(e.stack);
227
- }
228
- release();
229
- if (!this.stopped) {
230
- this.listen();
231
- }
232
- });
233
- log.info(`${this.workerId} connected and looking for scheduled jobs...`);
234
- this.doNext(client);
235
- };
236
- this.pgPool.connect(listenForChanges);
227
+ log.error('Error with database notify listener', e);
228
+ if (e instanceof Error && e.stack) {
229
+ log.debug(e.stack);
230
+ }
231
+ release();
232
+ if (!this.stopped) {
233
+ this.listen();
234
+ }
235
+ });
236
+ log.info(`${this.workerId} connected and looking for scheduled jobs...`);
237
+ this.doNext(client);
237
238
  }
238
239
  async stop() {
239
240
  this.stopped = true;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@constructive-io/job-scheduler",
3
- "version": "2.0.0",
3
+ "version": "2.0.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",
@@ -39,7 +39,7 @@
39
39
  "@constructive-io/job-pg": "^2.0.0",
40
40
  "@constructive-io/job-utils": "^2.0.0",
41
41
  "@pgpmjs/logger": "^2.1.0",
42
- "node-schedule": "1.3.2"
42
+ "node-schedule": "^2.1.1"
43
43
  },
44
- "gitHead": "b2daeefe49cdefb3d01ea63cf778fb9b847ab5fe"
44
+ "gitHead": "c05d6bd3dfa36690aefe21079042666dfae801a1"
45
45
  }