Skip to content

Commit

Permalink
feat: implement store close method and handle process termination sig…
Browse files Browse the repository at this point in the history
…nals
  • Loading branch information
kuoruan committed Jan 11, 2025
1 parent b515afa commit b096b83
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 8 deletions.
15 changes: 15 additions & 0 deletions src/server/plugin/Plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,21 @@ export class Plugin implements IPluginMiddleware<any>, IPluginAuth<any> {

const store = createStore(parsedConfig);

// close store on process termination
for (const signal of ["SIGINT", "SIGQUIT", "SIGTERM", "SIGHUP"]) {
process.once(signal, async () => {
try {
debug("Received signal %s, closing store...", signal);

await store.close();

debug("Store closed, good bye!");
} catch (e: any) {
debug("Error closing store: %s", e.message);
}
});
}

const provider = new OpenIDConnectAuthProvider(parsedConfig, store);
const core = new AuthCore(parsedConfig, provider);

Expand Down
4 changes: 4 additions & 0 deletions src/server/store/File.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,8 @@ export default class FileStore extends BaseStore implements Store {

return this.db.getItem(groupsKey);
}

close(): void {
// ignore
}
}
6 changes: 6 additions & 0 deletions src/server/store/InMemory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,10 @@ export default class InMemoryStore extends BaseStore implements Store {

return this.groupsCache.get(userGroupsKey);
}

close(): void {
this.stateCache.clear();
this.userinfoCache.clear();
this.groupsCache.clear();
}
}
12 changes: 4 additions & 8 deletions src/server/store/Redis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,6 @@ export default class RedisStore extends BaseStore implements Store {
this.ttl = ttl;
}
}

this.addProcessExitHandler();
}

private addProcessExitHandler(): void {
process.once("exit", async () => {
await this.redis.quit();
});
}

private async isKeyExists(key: string): Promise<boolean> {
Expand Down Expand Up @@ -129,4 +121,8 @@ export default class RedisStore extends BaseStore implements Store {

return this.redis.lrange(groupsKey, 0, -1);
}

async close(): Promise<void> {
await this.redis.quit();
}
}
3 changes: 3 additions & 0 deletions src/server/store/Store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ export interface Store {

/** get user groups from the store */
getUserGroups?: (key: string, providerId: string) => MaybePromise<string[] | null | undefined>;

/** close the store */
close: () => MaybePromise<void>;
}

export class BaseStore {
Expand Down

0 comments on commit b096b83

Please sign in to comment.