Skip to content

Commit

Permalink
feat(console): wing console application reset capability (#6665)
Browse files Browse the repository at this point in the history
Resolves #6124
  • Loading branch information
polamoros authored Jun 14, 2024
1 parent 02b0fd0 commit 06d3a02
Show file tree
Hide file tree
Showing 10 changed files with 324 additions and 175 deletions.
5 changes: 5 additions & 0 deletions apps/wing-console/console/server/src/expressServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import type { LogInterface } from "./utils/LogInterface.js";

export interface CreateExpressServerOptions {
simulatorInstance(): Promise<simulator.Simulator>;
restartSimulator(): Promise<void>;
testSimulatorInstance(): Promise<simulator.Simulator>;
consoleLogger: ConsoleLogger;
errorMessage(): string | undefined;
Expand Down Expand Up @@ -55,6 +56,7 @@ export interface CreateExpressServerOptions {

export const createExpressServer = async ({
simulatorInstance,
restartSimulator,
testSimulatorInstance,
consoleLogger,
errorMessage,
Expand Down Expand Up @@ -87,6 +89,9 @@ export const createExpressServer = async ({
async simulator() {
return await simulatorInstance();
},
async restartSimulator() {
return await restartSimulator();
},
async testSimulator() {
return await testSimulatorInstance();
},
Expand Down
3 changes: 3 additions & 0 deletions apps/wing-console/console/server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,9 @@ export const createConsoleServer = async ({
simulatorInstance() {
return simulator.instance();
},
restartSimulator() {
return simulator.reload();
},
errorMessage() {
return lastErrorMessage;
},
Expand Down
9 changes: 9 additions & 0 deletions apps/wing-console/console/server/src/router/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@ export const createAppRouter = () => {
config: ctx.layoutConfig,
};
}),
"app.reset": createProcedure.mutation(async ({ ctx }) => {
ctx.logger.verbose("Resetting simulator...", "console", {
messageType: "info",
});
await ctx.restartSimulator();
ctx.logger.verbose("Simulator reset.", "console", {
messageType: "info",
});
}),
"app.logsFilters": createProcedure.query(async ({ ctx }) => {
const simulator = await ctx.simulator();

Expand Down
1 change: 1 addition & 0 deletions apps/wing-console/console/server/src/utils/createRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export interface RouterMeta {

export interface RouterContext {
simulator(): Promise<simulator.Simulator>;
restartSimulator(): Promise<void>;
testSimulator(): Promise<simulator.Simulator>;
appDetails(): Promise<{
wingVersion: string | undefined;
Expand Down
14 changes: 14 additions & 0 deletions apps/wing-console/console/server/src/utils/simulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export interface Simulator {
instance(statedir?: string): Promise<simulator.Simulator>;
start(simfile: string): Promise<void>;
stop(): Promise<void>;
reload(): Promise<void>;
on<T extends keyof SimulatorEvents>(
event: T,
listener: (event: SimulatorEvents[T]) => void | Promise<void>,
Expand Down Expand Up @@ -91,6 +92,16 @@ export const createSimulator = (props?: CreateSimulatorProps): Simulator => {
}
};

const reload = async () => {
if (instance) {
await events.emit("starting", { instance });
await instance.reload(true);
await events.emit("started");
} else {
throw new Error("Simulator not started");
}
};

return {
async instance() {
return (
Expand All @@ -103,6 +114,9 @@ export const createSimulator = (props?: CreateSimulatorProps): Simulator => {
async stop() {
await instance?.stop();
},
async reload() {
await reload();
},
on(event, listener) {
events.on(event, listener);
},
Expand Down
Loading

0 comments on commit 06d3a02

Please sign in to comment.