Skip to content
This repository has been archived by the owner on May 28, 2021. It is now read-only.

Commit

Permalink
Merge pull request #83 from connext/schedule-cleanup-client
Browse files Browse the repository at this point in the history
scheduled cleanup
  • Loading branch information
pedrouid authored Aug 19, 2020
2 parents 13fa4f3 + 52b1422 commit da64ee4
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ export default class Client {
private subscriber: Subscriber;
private connecting = false;
private logLevel: number;
private cleanUpInterval: NodeJS.Timeout | undefined;
private cleaningUp = false;

constructor(opts: InitClientManagerOptions) {
this.subscriber = new Subscriber(opts.logger, opts.store);
Expand All @@ -42,6 +44,8 @@ export default class Client {
rootStoreDir: string,
opts: InternalConnectOptions,
): Promise<IConnextClient> {
if (this.cleaningUp) throw new Error(`Client is cleaning up`);

if (this.connecting) {
throw new Error(`Client is connecting`);
}
Expand Down Expand Up @@ -71,9 +75,25 @@ export default class Client {
return client;
} finally {
this.connecting = false;
this.scheduleCleanup();
}
}

public scheduleCleanup(): void {
if (typeof this.cleanUpInterval !== "undefined") clearInterval(this.cleanUpInterval);
this.cleanUpInterval = setInterval(
this.cleanupRegistryApps,
86_400_000, // 24 hours
);
}

public async cleanupRegistryApps(): Promise<void> {
const client = this.getClient();
this.cleaningUp = true;
await (client as connext.ConnextClient).cleanupRegistryApps();
this.cleaningUp = false;
}

public getConfig(): RouteMethods.GetConfigResponse {
const client = this.getClient();
const config = {
Expand Down Expand Up @@ -308,6 +328,7 @@ export default class Client {
if (!this.client) {
throw new Error("Client is not initialized");
}
if (this.cleaningUp) throw new Error(`Client is cleanning up`);
return this.client;
}
}

0 comments on commit da64ee4

Please sign in to comment.