@@ -33,9 +33,10 @@ async function terminateReplicator(
33
33
source_db : connection ,
34
34
lock
35
35
} ) ;
36
- console . log ( 'terminating' , stream . slot_name ) ;
36
+
37
+ micro . logger . info ( `Terminating replication slot ${ stream . slot_name } ` ) ;
37
38
await stream . terminate ( ) ;
38
- console . log ( 'terminated' , stream . slot_name ) ;
39
+ micro . logger . info ( `Terminated replication slot ${ stream . slot_name } ` ) ;
39
40
} finally {
40
41
await lock . release ( ) ;
41
42
}
@@ -76,16 +77,32 @@ async function terminateReplicators(
76
77
export async function teardown ( runnerConfig : utils . RunnerConfig ) {
77
78
const config = await utils . loadConfig ( runnerConfig ) ;
78
79
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 ) ;
80
83
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 ;
83
86
84
- if ( connection ) {
85
- await terminateReplicators ( bucketStorage , connection ) ;
86
- }
87
+ micro . logger . info ( `Terminating replication slots` ) ;
87
88
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` ) ;
105
+ await mongoDB . client . close ( ) ;
106
+ process . exit ( 1 ) ;
107
+ }
91
108
}
0 commit comments