Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(wrangler): Improvements to --init-from-dash #5374

Merged
merged 1 commit into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/eighty-peas-clean.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"wrangler": patch
---

fix: Improvements to `--init-from-dash`

Adds user-specified CPU limit to `wrangler.toml` if one exists. Excludes `usage_model` from `wrangler.toml` in all cases, since this field is deprecated and no longer used.
79 changes: 38 additions & 41 deletions packages/wrangler/src/__tests__/init.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { msw } from "./helpers/msw";
import { runInTempDir } from "./helpers/run-in-tmp";
import { runWrangler } from "./helpers/run-wrangler";
import type { RawConfig } from "../config";
import type { UserLimits } from "../config/environment";
import type { PackageManager } from "../package-manager";

/**
Expand Down Expand Up @@ -2575,6 +2576,7 @@ describe("init", () => {
],
customDomains = [],
workersDev = true,
limits,
}: {
id?: string;
usage_model?: string;
Expand All @@ -2585,6 +2587,7 @@ describe("init", () => {
routes?: unknown[];
customDomains?: unknown[];
workersDev?: boolean;
limits?: UserLimits;
} = {}) {
return {
schedules,
Expand All @@ -2603,6 +2606,7 @@ describe("init", () => {
created_on: "1987-09-27",
migration_tag: "some-migration-tag",
usage_model,
limits,
compatibility_date,
tail_consumers: [{ service: "listener" }],
},
Expand Down Expand Up @@ -2643,7 +2647,6 @@ describe("init", () => {

const mockConfigExpected: RawConfig = {
workers_dev: true,
usage_model: "bundled",
main: "src/index.js",
compatibility_date: "1987-09-27",
name: "isolinear-optical-chip",
Expand Down Expand Up @@ -3007,7 +3010,6 @@ describe("init", () => {
main = \\"src/index.js\\"
compatibility_date = \\"1987-09-27\\"
workers_dev = false
usage_model = \\"bundled\\"

[[routes]]
pattern = \\"delta.quadrant\\"
Expand Down Expand Up @@ -3123,7 +3125,6 @@ describe("init", () => {
main = \\"src/index.js\\"
compatibility_date = \\"1987-09-27\\"
workers_dev = true
usage_model = \\"bundled\\"

[[routes]]
pattern = \\"delta.quadrant\\"
Expand Down Expand Up @@ -3257,23 +3258,48 @@ describe("init", () => {
},
});
});
it("should ignore usage_model = standard", async () => {

it("should include user limits", async () => {
worker = makeWorker({
id: "isolinear-optical-chip",
usage_model: "standard",
limits: {
cpu_ms: 75,
},
});

await expect(
downloadWorker("LCARS", "isolinear-optical-chip")
).resolves.toMatchObject({
config: {
...mockConfigExpected,
main: "index.js",
usage_model: undefined,
const { config } = await downloadWorker(
"LCARS",
"isolinear-optical-chip"
);
expect(config).toMatchObject({
...mockConfigExpected,
main: "index.js",
limits: {
cpu_ms: 75,
},
});
});

it.each(["bundled", "unbound", "standard"])(
"should ignore usage_model = %s",
async (usage_model) => {
worker = makeWorker({
id: "isolinear-optical-chip",
usage_model,
});

const { config } = await downloadWorker(
"LCARS",
"isolinear-optical-chip"
);
expect(config).toMatchObject({
...mockConfigExpected,
main: "index.js",
});
expect(config.usage_model).toBeUndefined();
}
);

it("should use fallback compatibility date if none is upstream", async () => {
worker = makeWorker({
id: "isolinear-optical-chip",
Expand Down Expand Up @@ -3387,7 +3413,6 @@ describe("init", () => {
compatibility_date: "1988-08-07",
main: "src/index.js",
workers_dev: true,
usage_model: "bundled",
name: "isolinear-optical-chip",
tail_consumers: [{ service: "listener" }],
}),
Expand Down Expand Up @@ -3489,34 +3514,6 @@ describe("init", () => {
},
});
});

it("should have an explicit usage model for non-standard users", async () => {
mockConfirm(
{
text: "Would you like to use git to manage this Worker?",
result: false,
},
{
text: "No package.json found. Would you like to create one?",
result: true,
}
);

await runWrangler(
"init isolinear-optical-chip --from-dash memory-crystal --no-delegate-c3"
);

expect(std.out).toContain("cd isolinear-optical-chip");

checkFiles({
items: {
"isolinear-optical-chip/wrangler.toml": wranglerToml({
...mockConfigExpected,
usage_model: "bundled",
}),
},
});
});
});
});
});
Expand Down
10 changes: 4 additions & 6 deletions packages/wrangler/src/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ export type ServiceMetadataRes = {
created_on: string;
migration_tag: string;
usage_model: "bundled" | "unbound";
limits: {
cpu_ms: number;
};
compatibility_date: string;
compatibility_flags: string[];
last_deployed_from?: "wrangler" | "dash" | "api";
Expand Down Expand Up @@ -896,7 +899,6 @@ async function getWorkerConfig(
workersDev,
serviceEnvMetadata,
cronTriggers,
standard,
] = await Promise.all([
fetchResult<WorkerMetadata["bindings"]>(
`/accounts/${accountId}/workers/services/${workerName}/environments/${serviceEnvironment}/bindings`
Expand All @@ -918,7 +920,6 @@ async function getWorkerConfig(
fetchResult<CronTriggersRes>(
`/accounts/${accountId}/workers/scripts/${workerName}/schedules`
),
fetchResult<StandardRes>(`/accounts/${accountId}/workers/standard`),
]).catch((e) => {
throw new Error(
`Error Occurred ${e}: Unable to fetch bindings, routes, or services metadata from the dashboard. Please try again later.`
Expand Down Expand Up @@ -956,14 +957,11 @@ async function getWorkerConfig(
new Date().toISOString().substring(0, 10),
compatibility_flags: serviceEnvMetadata.script.compatibility_flags,
...(allRoutes.length ? { routes: allRoutes } : {}),
usage_model:
standard?.standard == true
? undefined
: serviceEnvMetadata.script.usage_model,
placement:
serviceEnvMetadata.script.placement_mode === "smart"
? { mode: "smart" }
: undefined,
limits: serviceEnvMetadata.script.limits,
...(durableObjectClassNames.length
? {
migrations: [
Expand Down