@@ -199,59 +199,55 @@ export default class Scheduler {
199199 }
200200 }
201201 }
202- listen ( ) {
202+ async listen ( ) : Promise < void > {
203203 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 ;
204+ let client : PoolClient ;
205+ let release : ( ) => void ;
206+ try {
207+ client = await this . pgPool . connect ( ) ;
208+ release = ( ) => client . release ( ) ;
209+ } catch ( err ) {
210+ log . error ( 'Error connecting with notify listener' , err ) ;
211+ if ( err instanceof Error && err . stack ) {
212+ log . debug ( err . stack ) ;
213+ }
214+ if ( ! this . stopped ) {
215+ setTimeout ( ( ) => this . listen ( ) , 5000 ) ;
220216 }
217+ return ;
218+ }
219+ if ( this . stopped ) {
220+ release ( ) ;
221+ return ;
222+ }
223+ this . listenClient = client ;
224+ this . listenRelease = release ;
225+ client . on ( 'notification' , ( ) => {
226+ log . info ( 'a NEW scheduled JOB!' ) ;
227+ if ( this . doNextTimer ) {
228+ // Must be idle, do something!
229+ this . doNext ( client ) ;
230+ }
231+ } ) ;
232+ client . query ( 'LISTEN "scheduled_jobs:insert"' ) ;
233+ client . on ( 'error' , ( e : unknown ) => {
221234 if ( this . stopped ) {
222235 release ( ) ;
223236 return ;
224237 }
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 ) ;
238+ log . error ( 'Error with database notify listener' , e ) ;
239+ if ( e instanceof Error && e . stack ) {
240+ log . debug ( e . stack ) ;
241+ }
242+ release ( ) ;
243+ if ( ! this . stopped ) {
244+ this . listen ( ) ;
245+ }
246+ } ) ;
247+ log . info (
248+ `${ this . workerId } connected and looking for scheduled jobs...`
249+ ) ;
250+ this . doNext ( client ) ;
255251 }
256252
257253 async stop ( ) : Promise < void > {
0 commit comments