Skip to content

Commit 8baeee9

Browse files
committed
Added closeable decorators to all services needing closing
1 parent 20f7908 commit 8baeee9

File tree

17 files changed

+104
-24
lines changed

17 files changed

+104
-24
lines changed

packages/api/src/graphql/GraphqlSequencerModule.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import assert from "node:assert";
22

3-
import { SequencerModule } from "@proto-kit/sequencer";
3+
import { Closeable, closeable, SequencerModule } from "@proto-kit/sequencer";
44
import {
55
ChildContainerProvider,
66
Configurable,
@@ -29,9 +29,10 @@ export interface GraphqlModulesDefintion<
2929
config?: ModulesConfig<GraphQLModules>;
3030
}
3131

32+
@closeable()
3233
export class GraphqlSequencerModule<GraphQLModules extends GraphqlModulesRecord>
3334
extends ModuleContainer<GraphQLModules>
34-
implements Configurable<unknown>, SequencerModule<unknown>
35+
implements Configurable<unknown>, SequencerModule<unknown>, Closeable
3536
{
3637
public static from<GraphQLModules extends GraphqlModulesRecord>(
3738
definition: GraphqlModulesDefintion<GraphQLModules>
@@ -93,4 +94,10 @@ export class GraphqlSequencerModule<GraphQLModules extends GraphqlModulesRecord>
9394
}
9495
await this.graphqlServer.startServer();
9596
}
97+
98+
public async close() {
99+
if (this.graphqlServer !== undefined) {
100+
await this.graphqlServer.close();
101+
}
102+
}
96103
}

packages/api/src/graphql/GraphqlServer.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,18 @@ export class GraphqlServer extends SequencerModule<GraphqlServerOptions> {
145145
});
146146
}
147147

148-
public close() {
149-
this.server?.close();
148+
public async close() {
149+
if (this.server !== undefined) {
150+
const { server } = this;
151+
152+
await new Promise<void>((res) => {
153+
server.close((error) => {
154+
if (error !== undefined) {
155+
log.error(error);
156+
}
157+
res();
158+
});
159+
});
160+
}
150161
}
151162
}

packages/deployment/src/queue/BullQueue.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
InstantiatedQueue,
77
TaskQueue,
88
AbstractTaskQueue,
9+
closeable,
910
} from "@proto-kit/sequencer";
1011

1112
import { InstantiatedBullQueue } from "./InstantiatedBullQueue";
@@ -28,13 +29,15 @@ interface BullWorker extends Closeable {
2829
/**
2930
* TaskQueue implementation for BullMQ
3031
*/
32+
@closeable()
3133
export class BullQueue
3234
extends AbstractTaskQueue<BullQueueConfig>
33-
implements TaskQueue
35+
implements TaskQueue, Closeable
3436
{
3537
private activePromise?: Promise<void>;
3638

3739
private activeWorkers: Record<string, BullWorker> = {};
40+
3841
private activeJobs = 0;
3942

4043
public createWorker(
@@ -131,4 +134,10 @@ export class BullQueue
131134
public async start() {
132135
noop();
133136
}
137+
138+
public async close() {
139+
await this.closeQueues();
140+
141+
// Closing of active workers is handled by the LocalTaskWorkerModule
142+
}
134143
}

packages/indexer/test/GeneratedResolverFactoryGraphqlModule.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ describe("GeneratedResolverFactoryGraphqlModule", () => {
139139
}
140140
});
141141

142-
afterAll(() => {
143-
indexer.resolve("GraphqlServer").close();
142+
afterAll(async () => {
143+
await indexer.resolve("GraphqlServer").close();
144144
});
145145
});

packages/persistance/src/PrismaRedisDatabase.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
SequencerModule,
44
StorageDependencyMinimumDependencies,
55
Database,
6+
closeable,
67
} from "@proto-kit/sequencer";
78
import { ChildContainerProvider } from "@proto-kit/common";
89
import { PrismaClient } from "@prisma/client";
@@ -25,6 +26,7 @@ export interface PrismaRedisCombinedConfig {
2526
}
2627

2728
@sequencerModule()
29+
@closeable()
2830
export class PrismaRedisDatabase
2931
extends SequencerModule<PrismaRedisCombinedConfig>
3032
implements PrismaConnection, RedisConnection, Database

packages/persistance/test-integration/PrismaBlockProduction.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ describe("prisma integration", () => {
4646
};
4747

4848
const teardown = async () => {
49-
await appChain.sequencer.resolve("Database").close();
49+
await appChain.sequencer.close();
5050
};
5151

5252
describe("produce fuzzed block", () => {

packages/sdk/src/appChain/AppChain.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,5 +348,9 @@ export class AppChain<
348348
.resolve(WorkerReadyModule)
349349
.waitForReady();
350350
}
351+
352+
public async close() {
353+
await this.sequencer.close();
354+
}
351355
}
352356
/* eslint-enable @typescript-eslint/consistent-type-assertions */

packages/sequencer/src/protocol/production/trigger/TimedBlockTrigger.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { inject, injectable } from "tsyringe";
22
import { injectOptional, log } from "@proto-kit/common";
33
import gcd from "compute-gcd";
44

5-
import { Closeable } from "../../../sequencer/builder/Closeable";
5+
import { closeable, Closeable } from "../../../sequencer/builder/Closeable";
66
import { BatchProducerModule } from "../BatchProducerModule";
77
import { Mempool } from "../../../mempool/Mempool";
88
import { BlockQueue } from "../../../storage/repositories/BlockStorage";
@@ -32,6 +32,7 @@ export interface TimedBlockTriggerEvent extends BlockEvents {
3232
}
3333

3434
@injectable()
35+
@closeable()
3536
export class TimedBlockTrigger
3637
extends BlockTriggerBase<TimedBlockTriggerConfig, TimedBlockTriggerEvent>
3738
implements BlockTrigger, Closeable

packages/sequencer/src/sequencer/SequencerStartupModule.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,14 @@ import {
2121
import { VerificationKeyService } from "../protocol/runtime/RuntimeVerificationKeyService";
2222

2323
import { SequencerModule, sequencerModule } from "./builder/SequencerModule";
24+
import { Closeable, closeable } from "./builder/Closeable";
2425

2526
@sequencerModule()
26-
export class SequencerStartupModule extends SequencerModule {
27+
@closeable()
28+
export class SequencerStartupModule
29+
extends SequencerModule
30+
implements Closeable
31+
{
2732
public constructor(
2833
private readonly flowCreator: FlowCreator,
2934
@inject("Protocol")
@@ -144,4 +149,8 @@ export class SequencerStartupModule extends SequencerModule {
144149

145150
log.info("Protocol circuits compiled successfully, commencing startup");
146151
}
152+
153+
public async close() {
154+
await this.registrationFlow.close();
155+
}
147156
}

packages/sequencer/src/sequencer/executor/Sequencer.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,9 @@ export class Sequencer<Modules extends SequencerModulesRecord>
9292
log.info("Closing sequencer...");
9393
const closeables = this.container.resolveAll<Closeable>("Closeable");
9494
await Promise.all(
95-
closeables.map(async (closeable) => await closeable.close())
95+
closeables.map(async (closeable) => {
96+
await closeable.close();
97+
})
9698
);
9799
log.info("Sequencer closed");
98100
}

0 commit comments

Comments
 (0)