@@ -33,9 +33,10 @@ async function terminateReplicator(
3333 source_db : connection ,
3434 lock
3535 } ) ;
36- console . log ( 'terminating' , stream . slot_name ) ;
36+
37+ micro . logger . info ( `Terminating replication slot ${ stream . slot_name } ` ) ;
3738 await stream . terminate ( ) ;
38- console . log ( 'terminated' , stream . slot_name ) ;
39+ micro . logger . info ( `Terminated replication slot ${ stream . slot_name } ` ) ;
3940 } finally {
4041 await lock . release ( ) ;
4142 }
@@ -76,16 +77,32 @@ async function terminateReplicators(
7677export async function teardown ( runnerConfig : utils . RunnerConfig ) {
7778 const config = await utils . loadConfig ( runnerConfig ) ;
7879 const mongoDB = storage . createPowerSyncMongo ( config . storage ) ;
79- await db . mongo . waitForAuth ( mongoDB . db ) ;
80+ try {
81+ micro . logger . info ( `Waiting for auth` ) ;
82+ await db . mongo . waitForAuth ( mongoDB . db ) ;
8083
81- const bucketStorage = new storage . MongoBucketStorage ( mongoDB , { slot_name_prefix : config . slot_name_prefix } ) ;
82- const connection = config . connection ;
84+ const bucketStorage = new storage . MongoBucketStorage ( mongoDB , { slot_name_prefix : config . slot_name_prefix } ) ;
85+ const connection = config . connection ;
8386
84- if ( connection ) {
85- await terminateReplicators ( bucketStorage , connection ) ;
86- }
87+ micro . logger . info ( `Terminating replication slots` ) ;
8788
88- const database = mongoDB . db ;
89- await database . dropDatabase ( ) ;
90- await mongoDB . client . close ( ) ;
89+ if ( connection ) {
90+ await terminateReplicators ( bucketStorage , connection ) ;
91+ }
92+
93+ const database = mongoDB . db ;
94+ micro . logger . info ( `Dropping database ${ database . namespace } ` ) ;
95+ await database . dropDatabase ( ) ;
96+ micro . logger . info ( `Done` ) ;
97+ await mongoDB . client . close ( ) ;
98+
99+ // If there was an error connecting to postgress, the process may stay open indefinitely.
100+ // This forces an exit.
101+ // We do not consider those errors a teardown failure.
102+ process . exit ( 0 ) ;
103+ } catch ( e ) {
104+ micro . logger . error ( `Teardown failure` , e ) ;
105+ await mongoDB . client . close ( ) ;
106+ process . exit ( 1 ) ;
107+ }
91108}
0 commit comments