Skip to content
Closed
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 packages/api/src/graphql/GraphqlSequencerModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
ModuleContainer,
ModulesConfig,
ModulesRecord,
noop,
TypedClass,
} from "@proto-kit/common";

Expand Down Expand Up @@ -74,4 +75,8 @@ export class GraphqlSequencerModule<GraphQLModules extends GraphqlModulesRecord>
}
void this.graphqlServer.startServer();
}

public async close(): Promise<void> {
noop();
}
}
4 changes: 4 additions & 0 deletions packages/sdk/src/appChain/AppChain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,4 +352,8 @@ export class AppChain<
// this.runtime.start();
await this.sequencer.start();
}

public async close(): Promise<void> {
await this.sequencer.close();
}
}
13 changes: 11 additions & 2 deletions packages/sdk/test/graphql/run-graphql.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
import { startServer } from "./graphql";
import { sleep } from "@proto-kit/common";

import { AppChain } from "../../src";

import { startServer } from "./graphql";

describe("run graphql", () => {
let appchain: AppChain<any, any, any, any>;

afterAll(async () => {
await appchain.close();
});

it("run", async () => {
const server = await startServer();
appchain = await startServer();
await sleep(1000000000);
}, 1000000000);
});
11 changes: 10 additions & 1 deletion packages/sequencer/src/sequencer/builder/SequencerModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
StaticConfigurableModule,
TypedClass,
Presets,
NoConfig,
NoConfig, noop
} from "@proto-kit/common";
import { injectable } from "tsyringe";

Expand All @@ -24,6 +24,15 @@ export abstract class SequencerModule<
* That means that you mustn't await server.start() for example.
*/
public abstract start(): Promise<void>;

/**
* Close() is called by the sequencer when the appchain or the sequencer
* is in the process of stopping.
* Possible usages are stopping of API services, closing of database connections, ...
*/
public async close(): Promise<void> {
noop();
}
}

/**
Expand Down
25 changes: 24 additions & 1 deletion packages/sequencer/src/sequencer/executor/Sequencer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ import {
ModuleContainerDefinition,
log,
} from "@proto-kit/common";
import { Runtime, RuntimeModulesRecord, MethodIdFactory } from "@proto-kit/module";
import {
Runtime,
RuntimeModulesRecord,
MethodIdFactory,
} from "@proto-kit/module";
import { Protocol, ProtocolModulesRecord } from "@proto-kit/protocol";
import { DependencyContainer, injectable } from "tsyringe";

Expand Down Expand Up @@ -87,4 +91,23 @@ export class Sequencer<Modules extends SequencerModulesRecord>
await sequencerModule.start();
}
}

public async close(): Promise<void> {
let counter = 0;

const { modules } = this.definition;
const totalModules = Object.keys(modules).length;

for (const moduleName in modules) {
const sequencerModule = this.resolve(moduleName);

log.info(
`Closing sequencer module ${moduleName} (${
sequencerModule.constructor.name
}) ${++counter}/${totalModules}`
);
// eslint-disable-next-line no-await-in-loop
await sequencerModule.close();
}
}
}