Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/rotten-candles-float.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@powersync/service-core': patch
---

Fix teardown command not terminating after some errors.
39 changes: 28 additions & 11 deletions packages/service-core/src/runner/teardown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ async function terminateReplicator(
source_db: connection,
lock
});
console.log('terminating', stream.slot_name);

micro.logger.info(`Terminating replication slot ${stream.slot_name}`);
await stream.terminate();
console.log('terminated', stream.slot_name);
micro.logger.info(`Terminated replication slot ${stream.slot_name}`);
} finally {
await lock.release();
}
Expand Down Expand Up @@ -76,16 +77,32 @@ async function terminateReplicators(
export async function teardown(runnerConfig: utils.RunnerConfig) {
const config = await utils.loadConfig(runnerConfig);
const mongoDB = storage.createPowerSyncMongo(config.storage);
await db.mongo.waitForAuth(mongoDB.db);
try {
micro.logger.info(`Waiting for auth`);
await db.mongo.waitForAuth(mongoDB.db);

const bucketStorage = new storage.MongoBucketStorage(mongoDB, { slot_name_prefix: config.slot_name_prefix });
const connection = config.connection;
const bucketStorage = new storage.MongoBucketStorage(mongoDB, { slot_name_prefix: config.slot_name_prefix });
const connection = config.connection;

if (connection) {
await terminateReplicators(bucketStorage, connection);
}
micro.logger.info(`Terminating replication slots`);

const database = mongoDB.db;
await database.dropDatabase();
await mongoDB.client.close();
if (connection) {
await terminateReplicators(bucketStorage, connection);
}

const database = mongoDB.db;
micro.logger.info(`Dropping database ${database.namespace}`);
await database.dropDatabase();
micro.logger.info(`Done`);
await mongoDB.client.close();

// If there was an error connecting to postgress, the process may stay open indefinitely.
// This forces an exit.
// We do not consider those errors a teardown failure.
process.exit(0);
} catch (e) {
micro.logger.error(`Teardown failure`, e);
await mongoDB.client.close();
process.exit(1);
}
}