fix(aws): Host settings merge saves against the latest config and cache the environment's lifetime cap
Build App (Preview) / compute-version (pull_request) Successful in 38s
Secret Scan / scan (push) Successful in 17s
Secret Scan / scan (pull_request) Successful in 11s
Build App (Preview) / create-release (pull_request) Successful in 7s
Build App (Preview) / test (pull_request) Successful in 8m14s
Build App (Preview) / build-macos (pull_request) Successful in 6m4s
Build App (Preview) / build-windows (pull_request) Successful in 10m43s
Build Container / build-container (pull_request) Successful in 19m45s
Build App (Preview) / build-linux (pull_request) Successful in 16m22s
Build App (Preview) / prune-previews (pull_request) Successful in 9s
Build App (Preview) / compute-version (pull_request) Successful in 38s
Secret Scan / scan (push) Successful in 17s
Secret Scan / scan (pull_request) Successful in 11s
Build App (Preview) / create-release (pull_request) Successful in 7s
Build App (Preview) / test (pull_request) Successful in 8m14s
Build App (Preview) / build-macos (pull_request) Successful in 6m4s
Build App (Preview) / build-windows (pull_request) Successful in 10m43s
Build Container / build-container (pull_request) Successful in 19m45s
Build App (Preview) / build-linux (pull_request) Successful in 16m22s
Build App (Preview) / prune-previews (pull_request) Successful in 9s
A save right after the setup script's blur (a toggle) spread the render-time config and reverted the script; saves now start from the stored config plus every patch saved since. aws_environment_max_hours is cached per connection, region and environment for 10 minutes, so re-mounting the section does not ask AWS again. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
ea37f8434f
commit
0a325cb7f2
4 files changed
+178
-8
No files matched your search
@@ -8,6 +8,7 @@ import HostSection from "./HostSection";
|
||||
import { awsProject, localProject } from "../../../../test/awsFixtures";
|
||||
import type { AppSettings, AuthBridgeStatus, Project } from "../../../../lib/types";
|
||||
import { useAppState } from "../../../../store/appState";
|
||||
import { clearEnvironmentMaxHoursCache } from "../../../../lib/environmentMaxHours";
|
||||
|
||||
const OFF_BRIDGE: AuthBridgeStatus = { enabled: false, active_ports: [], conflicts: [] };
|
||||
const removeAwsResources = vi.fn();
|
||||
@@ -27,7 +28,10 @@ vi.mock("@tauri-apps/plugin-dialog", () => ({ open: vi.fn(async () => null) }));
|
||||
|
||||
const save = vi.fn().mockResolvedValue(true);
|
||||
|
||||
beforeEach(() => vi.clearAllMocks());
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
clearEnvironmentMaxHoursCache();
|
||||
});
|
||||
|
||||
function toggle(name: string) {
|
||||
return screen.getByRole("switch", { name });
|
||||
@@ -376,6 +380,32 @@ describe("HostSection", () => {
|
||||
expect(save.mock.calls[1][0].aws.setup_script).toBeNull();
|
||||
});
|
||||
|
||||
it("a toggle right after the setup script's blur keeps the script", async () => {
|
||||
// The store has not caught up with the first save when the second comes.
|
||||
const p = awsProject();
|
||||
renderHost(p);
|
||||
const box = screen.getByLabelText("Setup script");
|
||||
fireEvent.change(box, { target: { value: "make deps\n" } });
|
||||
fireEvent.blur(box);
|
||||
fireEvent.click(toggle("Spot"));
|
||||
await waitFor(() => expect(save).toHaveBeenCalledTimes(2));
|
||||
expect(save.mock.calls[0][0].aws.setup_script).toBe("make deps\n");
|
||||
expect(save.mock.calls[1][0].aws).toMatchObject({ setup_script: "make deps\n", spot: true });
|
||||
});
|
||||
|
||||
it("starts again from the stored config after a refused save", async () => {
|
||||
save.mockResolvedValueOnce(false);
|
||||
renderHost();
|
||||
const box = screen.getByLabelText("Setup script");
|
||||
fireEvent.change(box, { target: { value: "make deps\n" } });
|
||||
fireEvent.blur(box);
|
||||
await waitFor(() => expect(save).toHaveBeenCalledTimes(1));
|
||||
await act(async () => {});
|
||||
fireEvent.click(toggle("Spot"));
|
||||
await waitFor(() => expect(save).toHaveBeenCalledTimes(2));
|
||||
expect(save.mock.calls[1][0].aws).toMatchObject({ setup_script: null, spot: true });
|
||||
});
|
||||
|
||||
it("refuses a setup script over 32 KiB with the backend's message", () => {
|
||||
renderHost();
|
||||
const box = screen.getByLabelText("Setup script");
|
||||
@@ -413,6 +443,7 @@ describe("HostSection", () => {
|
||||
cleanup();
|
||||
|
||||
for (const none of [0, null]) {
|
||||
clearEnvironmentMaxHoursCache();
|
||||
awsEnvironmentMaxHours.mockResolvedValueOnce(none);
|
||||
renderHost();
|
||||
await waitFor(() => expect(awsEnvironmentMaxHours).toHaveBeenCalled());
|
||||
@@ -422,6 +453,23 @@ describe("HostSection", () => {
|
||||
}
|
||||
});
|
||||
|
||||
it("reads the lifetime cap once per environment across re-mounts", async () => {
|
||||
awsEnvironmentMaxHours.mockResolvedValue(24);
|
||||
renderHost();
|
||||
await screen.findByText(/stops workspaces after 24 h/);
|
||||
cleanup();
|
||||
renderHost();
|
||||
// Shown from the cache, without asking AWS again.
|
||||
await screen.findByText(/stops workspaces after 24 h/);
|
||||
expect(awsEnvironmentMaxHours).toHaveBeenCalledTimes(1);
|
||||
cleanup();
|
||||
|
||||
// Another environment is its own read.
|
||||
renderHost(awsProject({ aws_placement: { region: "eu-west-1", environment: "prod" } }));
|
||||
await waitFor(() => expect(awsEnvironmentMaxHours).toHaveBeenCalledTimes(2));
|
||||
awsEnvironmentMaxHours.mockReset();
|
||||
});
|
||||
|
||||
it("does not let the credentials setting change while the workspace runs", () => {
|
||||
renderHost(awsProject(), true);
|
||||
expect(screen.getByRole("radio", { name: "Copied when you connect" })).toBeDisabled();
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { useEffect, useState } from "react";
|
||||
import { useEffect, useRef, useState } from "react";
|
||||
import type { AwsWorkspaceConfig, HostConnection, Project, RepoSource } from "../../../../lib/types";
|
||||
import * as commands from "../../../../lib/tauri-commands";
|
||||
import { environmentMaxHours } from "../../../../lib/environmentMaxHours";
|
||||
import { useAppState } from "../../../../store/appState";
|
||||
import {
|
||||
COPY_AT_CONNECT_GATEWAY_NOTE,
|
||||
@@ -80,13 +81,13 @@ export default function HostSection({ project, save, disabled }: Props) {
|
||||
const environment = project.aws_placement?.environment;
|
||||
useEffect(() => {
|
||||
setMaxHours(null);
|
||||
if (!project.host_id || !environment) return;
|
||||
if (!project.host_id || !region || !environment) return;
|
||||
let cancelled = false;
|
||||
// Best effort: an unreadable record just shows no cap line.
|
||||
Promise.resolve()
|
||||
.then(() => commands.awsEnvironmentMaxHours(project.id))
|
||||
// Best effort: an unreadable record just shows no cap line. Cached per
|
||||
// environment, so re-opening this section doesn't ask AWS again.
|
||||
environmentMaxHours(project.id, project.host_id, region, environment)
|
||||
.then((h) => {
|
||||
if (!cancelled) setMaxHours(h ?? null);
|
||||
if (!cancelled) setMaxHours(h);
|
||||
})
|
||||
.catch(() => {});
|
||||
return () => {
|
||||
@@ -94,7 +95,35 @@ export default function HostSection({ project, save, disabled }: Props) {
|
||||
};
|
||||
}, [project.id, project.host_id, region, environment]);
|
||||
|
||||
const saveAws = (patch: Partial<AwsWorkspaceConfig>) => save({ aws: { ...aws, ...patch } });
|
||||
// The config the next save starts from: the stored one plus every patch
|
||||
// saved since. A blur's save followed at once by a toggle's (before the
|
||||
// store has the first back) must not spread the render-time `aws` and
|
||||
// revert the first. Re-based on the prop when it changes with no save in
|
||||
// flight.
|
||||
const latest = useRef({ base: project.aws, value: aws, inFlight: 0 });
|
||||
if (latest.current.base !== project.aws && latest.current.inFlight === 0) {
|
||||
latest.current = { base: project.aws, value: aws, inFlight: 0 };
|
||||
}
|
||||
const saveAws = async (patch: Partial<AwsWorkspaceConfig>) => {
|
||||
const l = latest.current;
|
||||
const next = { ...l.value, ...patch };
|
||||
l.value = next;
|
||||
l.inFlight += 1;
|
||||
// A refused save is not the config any more: start again from the store.
|
||||
const refused = () => {
|
||||
if (l.value === next) l.value = l.base ?? defaultAwsWorkspace();
|
||||
};
|
||||
try {
|
||||
const ok = await save({ aws: next });
|
||||
if (!ok) refused();
|
||||
return ok;
|
||||
} catch (e) {
|
||||
refused();
|
||||
throw e;
|
||||
} finally {
|
||||
l.inFlight -= 1;
|
||||
}
|
||||
};
|
||||
|
||||
const hasResources = !!(
|
||||
aws.task_arn ||
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
import { describe, it, expect, vi, beforeEach } from "vitest";
|
||||
|
||||
const awsEnvironmentMaxHours = vi.fn(async (_id: string) => 24 as number | null);
|
||||
vi.mock("./tauri-commands", () => ({
|
||||
awsEnvironmentMaxHours: (id: string) => awsEnvironmentMaxHours(id),
|
||||
}));
|
||||
|
||||
import {
|
||||
MAX_HOURS_TTL_MS,
|
||||
clearEnvironmentMaxHoursCache,
|
||||
environmentMaxHours,
|
||||
} from "./environmentMaxHours";
|
||||
|
||||
describe("environmentMaxHours", () => {
|
||||
beforeEach(() => {
|
||||
awsEnvironmentMaxHours.mockClear();
|
||||
clearEnvironmentMaxHoursCache();
|
||||
});
|
||||
|
||||
it("asks once per connection, region and environment until the TTL passes", async () => {
|
||||
expect(await environmentMaxHours("p1", "c1", "eu-west-1", "dev", 0)).toBe(24);
|
||||
// Another project in the same environment shares the read.
|
||||
expect(await environmentMaxHours("p2", "c1", "eu-west-1", "dev", 1000)).toBe(24);
|
||||
expect(awsEnvironmentMaxHours).toHaveBeenCalledTimes(1);
|
||||
await environmentMaxHours("p1", "c2", "eu-west-1", "dev", 1000);
|
||||
await environmentMaxHours("p1", "c1", "us-east-1", "dev", 1000);
|
||||
await environmentMaxHours("p1", "c1", "eu-west-1", "prod", 1000);
|
||||
expect(awsEnvironmentMaxHours).toHaveBeenCalledTimes(4);
|
||||
await environmentMaxHours("p1", "c1", "eu-west-1", "dev", MAX_HOURS_TTL_MS);
|
||||
expect(awsEnvironmentMaxHours).toHaveBeenCalledTimes(5);
|
||||
});
|
||||
|
||||
it("shares a read in flight and keeps an unreadable record (null)", async () => {
|
||||
awsEnvironmentMaxHours.mockResolvedValueOnce(null);
|
||||
const a = environmentMaxHours("p1", "c1", "eu-west-1", "dev", 0);
|
||||
const b = environmentMaxHours("p1", "c1", "eu-west-1", "dev", 0);
|
||||
expect(await a).toBeNull();
|
||||
expect(await b).toBeNull();
|
||||
expect(awsEnvironmentMaxHours).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("does not keep a rejected read", async () => {
|
||||
awsEnvironmentMaxHours.mockRejectedValueOnce(new Error("Project p1 not found"));
|
||||
await expect(environmentMaxHours("p1", "c1", "eu-west-1", "dev", 0)).rejects.toThrow();
|
||||
expect(await environmentMaxHours("p1", "c1", "eu-west-1", "dev", 1)).toBe(24);
|
||||
expect(awsEnvironmentMaxHours).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,45 @@
|
||||
import * as commands from "./tauri-commands";
|
||||
|
||||
/** How long an environment's lifetime cap is reused before AWS is asked again. */
|
||||
export const MAX_HOURS_TTL_MS = 10 * 60 * 1000;
|
||||
|
||||
interface Entry {
|
||||
at: number;
|
||||
value: Promise<number | null>;
|
||||
}
|
||||
|
||||
/** Keyed by connection, region and environment: the record is the
|
||||
* environment's, so every project placed there shares one read. */
|
||||
const cache = new Map<string, Entry>();
|
||||
|
||||
/**
|
||||
* `aws_environment_max_hours`, cached per (connection, region, environment)
|
||||
* for [`MAX_HOURS_TTL_MS`], so re-mounting Host settings does not read the
|
||||
* environment record from AWS again. A read in flight is shared; a rejected
|
||||
* one is not kept (the next mount asks again).
|
||||
*/
|
||||
export function environmentMaxHours(
|
||||
projectId: string,
|
||||
connectionId: string,
|
||||
region: string,
|
||||
environment: string,
|
||||
now: number = Date.now(),
|
||||
): Promise<number | null> {
|
||||
const key = JSON.stringify([connectionId, region, environment]);
|
||||
const hit = cache.get(key);
|
||||
if (hit && now - hit.at < MAX_HOURS_TTL_MS) return hit.value;
|
||||
const value = Promise.resolve()
|
||||
.then(() => commands.awsEnvironmentMaxHours(projectId))
|
||||
.then((h) => h ?? null);
|
||||
const entry = { at: now, value };
|
||||
cache.set(key, entry);
|
||||
value.catch(() => {
|
||||
if (cache.get(key) === entry) cache.delete(key);
|
||||
});
|
||||
return value;
|
||||
}
|
||||
|
||||
/** For tests. */
|
||||
export function clearEnvironmentMaxHoursCache() {
|
||||
cache.clear();
|
||||
}
|
||||
Reference in new issue
Block a user