Skip to content

Commit

Permalink
fix(console): some resources are marked as started even though they a…
Browse files Browse the repository at this point in the history
…re stopped (#6870)

This changeset fixes the running state of constructs that don't have any kind of runtime code.

The Console UI now understands that some resources may not have a running state at all, and the Simulator sets the running state of new resources as `stopped`.

Fixes #6858. Supersedes #6867.
  • Loading branch information
skyrpex authored Jul 9, 2024
1 parent fddc8a9 commit 723bf12
Show file tree
Hide file tree
Showing 17 changed files with 58 additions and 56 deletions.
Original file line number Diff line number Diff line change
@@ -1,44 +1,48 @@
import type { ResourceRunningState } from "@winglang/sdk/lib/simulator/simulator.js";

import type { ConstructTreeNodeMap } from "../utils/constructTreeNodeMap.js";

type RunningState = "starting" | "started" | "stopping" | "stopped" | "error";
type RunningState = ResourceRunningState;

const RunningStateOrder = {
started: 0,
stopped: 1,
starting: 2,
stopping: 3,
error: 4,
undefined: 2,
starting: 3,
stopping: 4,
error: 5,
} as const;

const getRunningStateOrder = (
runningState: RunningState | undefined,
): number => {
return RunningStateOrder[runningState ?? "undefined"];
};

export const getHierarchichalRunningState = (
path: string,
nodeMap: ConstructTreeNodeMap,
): RunningState => {
): RunningState | undefined => {
const node = nodeMap.get(path);
if (!node) {
return "stopped";
}

const runningState = (node?.resourceConfig?.attrs?.["runningState"] ??
"started") as RunningState;
const runningState = node?.runningState;

if (node.children?.length > 0) {
if (node?.children?.length && node?.children?.length > 0) {
const childrenRunningStates = node.children.map((child) =>
getHierarchichalRunningState(child, nodeMap),
);

// eslint-disable-next-line unicorn/no-array-reduce
const highestRunningState = childrenRunningStates.reduce<RunningState>(
(highest, current) => {
return RunningStateOrder[current] > RunningStateOrder[highest]
? current
: highest;
},
"started",
);

return RunningStateOrder[highestRunningState] >
RunningStateOrder[runningState]
const highestRunningState = childrenRunningStates.reduce<
RunningState | undefined
>((highest, current) => {
return getRunningStateOrder(current) > getRunningStateOrder(highest)
? current
: highest;
}, "started");

return getRunningStateOrder(highestRunningState) >
getRunningStateOrder(runningState)
? highestRunningState
: runningState;
}
Expand Down
2 changes: 1 addition & 1 deletion apps/wing-console/console/server/src/router/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export interface ExplorerItem {
type?: string;
childItems?: ExplorerItem[];
display?: NodeDisplay;
hierarchichalRunningState: ResourceRunningState;
hierarchichalRunningState: ResourceRunningState | undefined;
}

export interface MapItem extends ConstructTreeNode {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import type { BaseResourceSchema, Simulator } from "../wingsdk.js";
import type { ResourceRunningState } from "@winglang/sdk/lib/simulator/simulator.js";

import type { Simulator } from "../wingsdk.js";

import type { ConstructInfo, ConstructTreeNode } from "./construct-tree.js";

Expand Down Expand Up @@ -26,7 +28,7 @@ export interface Node {
attributes: Record<string, any> | undefined;
children: string[];
display?: NodeDisplay;
resourceConfig?: BaseResourceSchema;
runningState?: ResourceRunningState | undefined;
}

export interface ConstructTreeNodeRecord {
Expand Down Expand Up @@ -95,7 +97,7 @@ export function buildConstructTreeNodeMap(
attributes: node.attributes,
constructInfo: node.constructInfo,
display: node.display,
resourceConfig: simulator.tryGetResourceConfig(node.path),
runningState: simulator.tryGetResourceRunningState(node.path),
};

if (!parent) {
Expand Down
30 changes: 24 additions & 6 deletions libs/wingsdk/src/simulator/simulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import { PolicySchema } from "../target-sim/schema-resources";

const LOCALHOST_ADDRESS = "127.0.0.1";
const HANDLE_ATTRIBUTE = "handle";
const RUNNING_STATE_ATTRIBUTE = "runningState";

/**
* If an API call is made to a resource with name as the caller, any permissions
Expand Down Expand Up @@ -223,17 +222,21 @@ export class Simulator {
// merged in when calling `getResourceConfig()`.
private state: Record<string, ResourceState> = {};

// keeps the running state of all resources.
private runningState: Record<string, ResourceRunningState> = {};

constructor(props: SimulatorProps) {
const simdir = resolve(props.simfile);
this.statedir = props.stateDir ?? join(simdir, ".state");
this._model = this._loadApp(simdir);

this._running = "stopped";
this._handles = new HandleManager();
this._policyRegistry = new PolicyRegistry();
this._traces = new Array();
this._traceSubscribers = new Array();
this._resourceLifecyleSubscribers = new Array();

this._model = this._loadApp(simdir);
}

private _loadApp(simdir: string): Model {
Expand Down Expand Up @@ -277,6 +280,15 @@ export class Simulator {
const connections = readJsonSync(connectionJson).connections;
const graph = new Graph(Object.values(schema.resources));

// initialize new resources to "stopped" state.
for (const node of graph.nodes) {
if (this.tryGetResourceRunningState(node.path) !== undefined) {
continue;
}

this.setResourceRunningState(node.path, "stopped");
}

return { schema, tree, connections, simdir, graph };
}

Expand Down Expand Up @@ -413,10 +425,7 @@ export class Simulator {
path: string,
runningState: ResourceRunningState
) {
this.state[path].attrs = {
...this.state[path].attrs,
[RUNNING_STATE_ATTRIBUTE]: runningState,
};
this.runningState[path] = runningState;

for (const subscriber of this._resourceLifecyleSubscribers) {
subscriber.callback({ path, runningState });
Expand Down Expand Up @@ -600,6 +609,15 @@ export class Simulator {
return config;
}

/**
* Get the running state of a resource.
*/
public tryGetResourceRunningState(
path: string
): ResourceRunningState | undefined {
return this.runningState[path];
}

/**
* Obtain a resource's state directory path.
* @param path The resource path
Expand Down
1 change: 0 additions & 1 deletion libs/wingsdk/test/target-sim/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ test("create an api", async () => {
expect(s.getResourceConfig("/my_api")).toEqual({
attrs: {
handle: expect.any(String),
runningState: expect.any(String),
url: expect.any(String),
},
path: "root/my_api",
Expand Down
1 change: 0 additions & 1 deletion libs/wingsdk/test/target-sim/bucket.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ test("create a bucket", async () => {
expect(s.getResourceConfig("/my_bucket")).toEqual({
attrs: {
handle: expect.any(String),
runningState: expect.any(String),
},
path: "root/my_bucket",
addr: expect.any(String),
Expand Down
1 change: 0 additions & 1 deletion libs/wingsdk/test/target-sim/function.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ test("create a function", async () => {
expect(s.getResourceConfig("/my_function")).toEqual({
attrs: {
handle: expect.any(String),
runningState: expect.any(String),
},
path: "root/my_function",
addr: expect.any(String),
Expand Down
1 change: 0 additions & 1 deletion libs/wingsdk/test/target-sim/on-deploy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ test("create an OnDeploy", async () => {
expect(s.getResourceConfig("/my_on_deploy")).toEqual({
attrs: {
handle: expect.any(String),
runningState: expect.any(String),
},
deps: ["root/my_on_deploy/Function"],
path: "root/my_on_deploy",
Expand Down
1 change: 0 additions & 1 deletion libs/wingsdk/test/target-sim/queue.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ test("create a queue", async () => {
expect(s.getResourceConfig("/my_queue")).toEqual({
attrs: {
handle: expect.any(String),
runningState: expect.any(String),
},
path: "root/my_queue",
addr: expect.any(String),
Expand Down
1 change: 0 additions & 1 deletion libs/wingsdk/test/target-sim/redis.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ test("create a Redis resource", async () => {
expect(redisResource).toEqual({
attrs: {
handle: expect.any(String),
runningState: expect.any(String),
},
path: "root/my_redis",
addr: expect.any(String),
Expand Down
3 changes: 0 additions & 3 deletions libs/wingsdk/test/target-sim/schedule.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ test("create a schedule", async () => {
expect(s.getResourceConfig("/my_schedule")).toEqual({
attrs: {
handle: expect.any(String),
runningState: expect.any(String),
},
path: "root/my_schedule",
addr: expect.any(String),
Expand Down Expand Up @@ -67,7 +66,6 @@ test("schedule with one task using rate of 10m", async () => {
expect(s.getResourceConfig("/my_schedule")).toEqual({
attrs: {
handle: expect.any(String),
runningState: expect.any(String),
},
path: "root/my_schedule",
addr: expect.any(String),
Expand Down Expand Up @@ -96,7 +94,6 @@ test("schedule with one task using rate of 3h", async () => {
expect(s.getResourceConfig("/my_schedule")).toEqual({
attrs: {
handle: expect.any(String),
runningState: expect.any(String),
},
path: "root/my_schedule",
addr: expect.any(String),
Expand Down
1 change: 0 additions & 1 deletion libs/wingsdk/test/target-sim/secret.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ describe("secrets", () => {
expect(s.getResourceConfig("/my_secret")).toEqual({
attrs: {
handle: expect.any(String),
runningState: expect.any(String),
},
path: "root/my_secret",
addr: expect.any(String),
Expand Down
3 changes: 0 additions & 3 deletions libs/wingsdk/test/target-sim/service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ test("create a service with on start method", async () => {
expect(s.getResourceConfig("/my_service")).toEqual({
attrs: {
handle: expect.any(String),
runningState: expect.any(String),
},
path: "root/my_service",
addr: expect.any(String),
Expand Down Expand Up @@ -53,7 +52,6 @@ test("create a service with a on stop method", async () => {
expect(s.getResourceConfig("/my_service")).toEqual({
attrs: {
handle: expect.any(String),
runningState: expect.any(String),
},
path: "root/my_service",
addr: expect.any(String),
Expand Down Expand Up @@ -95,7 +93,6 @@ test("create a service without autostart", async () => {
expect(s.getResourceConfig("/my_service")).toEqual({
attrs: {
handle: expect.any(String),
runningState: expect.any(String),
},
path: "root/my_service",
addr: expect.any(String),
Expand Down
7 changes: 0 additions & 7 deletions libs/wingsdk/test/target-sim/table.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ test("create a table", async () => {
expect(s.getResourceConfig("/my_table")).toEqual({
attrs: {
handle: expect.any(String),
runningState: expect.any(String),
},
path: "root/my_table",
addr: expect.any(String),
Expand Down Expand Up @@ -59,7 +58,6 @@ test("insert row", async () => {
expect(s.getResourceConfig("/my_table")).toEqual({
attrs: {
handle: expect.any(String),
runningState: expect.any(String),
},
path: "root/my_table",
addr: expect.any(String),
Expand Down Expand Up @@ -107,7 +105,6 @@ test("get row", async () => {
expect(s.getResourceConfig("/my_table")).toEqual({
attrs: {
handle: expect.any(String),
runningState: expect.any(String),
},
path: "root/my_table",
addr: expect.any(String),
Expand Down Expand Up @@ -154,7 +151,6 @@ test("tryGet row", async () => {
expect(s.getResourceConfig("/my_table")).toEqual({
attrs: {
handle: expect.any(String),
runningState: expect.any(String),
},
path: "root/my_table",
addr: expect.any(String),
Expand Down Expand Up @@ -200,7 +196,6 @@ test("update row", async () => {
expect(s.getResourceConfig("/my_table")).toEqual({
attrs: {
handle: expect.any(String),
runningState: expect.any(String),
},
path: "root/my_table",
addr: expect.any(String),
Expand Down Expand Up @@ -245,7 +240,6 @@ test("list table", async () => {
expect(s.getResourceConfig("/my_table")).toEqual({
attrs: {
handle: expect.any(String),
runningState: expect.any(String),
},
path: "root/my_table",
addr: expect.any(String),
Expand Down Expand Up @@ -352,7 +346,6 @@ test("can add row in preflight", async () => {
expect(s.getResourceConfig("/my_table")).toEqual({
attrs: {
handle: expect.any(String),
runningState: expect.any(String),
},
path: "root/my_table",
addr: expect.any(String),
Expand Down
1 change: 0 additions & 1 deletion libs/wingsdk/test/target-sim/test.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ test("create a test", async () => {
expect(s.getResourceConfig("/env0/test:my_test/Handler")).toEqual({
attrs: {
handle: expect.any(String),
runningState: expect.any(String),
},
path: "root/env0/test:my_test/Handler",
addr: expect.any(String),
Expand Down
1 change: 0 additions & 1 deletion libs/wingsdk/test/target-sim/topic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ test("create a topic", async () => {
expect(s.getResourceConfig("/my_topic")).toEqual({
attrs: {
handle: expect.any(String),
runningState: expect.any(String),
},
path: "root/my_topic",
addr: expect.any(String),
Expand Down
4 changes: 2 additions & 2 deletions libs/wingsdk/test/target-sim/website.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ test("api.url is resolved in website config", async () => {
expect(s.getResourceConfig("/website")).toEqual({
attrs: {
handle: expect.any(String),
runningState: expect.any(String),

url: expect.any(String),
},
path: "root/website",
Expand Down Expand Up @@ -144,7 +144,7 @@ test("multiple tokens are resolved in website config", async () => {
expect(s.getResourceConfig("/website")).toEqual({
attrs: {
handle: expect.any(String),
runningState: expect.any(String),

url: expect.any(String),
},
path: "root/website",
Expand Down

0 comments on commit 723bf12

Please sign in to comment.