Skip to content

Commit 94340c7

Browse files
authored
[server/db] resume periodic deleter on quorum re-election (#17092)
1 parent 1bbcd3b commit 94340c7

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

components/gitpod-db/src/periodic-deleter.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,18 @@ export class PeriodicDbDeleter {
1616
@inject(GitpodTableDescriptionProvider) protected readonly tableProvider: GitpodTableDescriptionProvider;
1717
@inject(TypeORM) protected readonly typeORM: TypeORM;
1818

19-
start() {
19+
start(shouldRunFn: () => Promise<boolean>) {
2020
log.info("[PeriodicDbDeleter] Start ...");
21-
this.sync().catch((err) => log.error("[PeriodicDbDeleter] sync failed", err));
21+
this.sync(shouldRunFn).catch((err) => log.error("[PeriodicDbDeleter] sync failed", err));
2222
}
2323

24-
protected async sync() {
24+
protected async sync(shouldRunFn: () => Promise<boolean>) {
2525
const doSync = async () => {
26+
const shouldRun = await shouldRunFn();
27+
if (!shouldRun) {
28+
return;
29+
}
30+
2631
const tickID = new Date().toISOString();
2732
log.info("[PeriodicDbDeleter] Starting to collect deleted rows.", {
2833
periodicDeleterTickId: tickID,

components/server/src/server.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -334,13 +334,13 @@ export class Server<C extends GitpodClient, S extends GitpodServer> {
334334
if (!this.config.runDbDeleter) {
335335
return;
336336
}
337-
const areWeLeader = await this.qorum.areWeLeader();
338-
if (areWeLeader) {
339-
log.info("[PeriodicDbDeleter] Current instance is leader, starting periodic deleter.");
340-
this.periodicDbDeleter.start();
341-
} else {
342-
log.info("[PeriodicDbDeleter] Current instance is not the leader, periodic deleter will not run.");
343-
}
337+
this.periodicDbDeleter.start(async () => {
338+
const areWeLeader = await this.qorum.areWeLeader();
339+
log.info(
340+
"[PeriodicDbDeleter]" + areWeLeader ? "Deleter should run." : "Current instance is not the leader",
341+
);
342+
return areWeLeader;
343+
});
344344
}
345345

346346
protected async registerRoutes(app: express.Application) {

0 commit comments

Comments
 (0)