Skip to content

Commit 79a6b7d

Browse files
containers: default max_instances to 20 and fix logging in @cloudflare/cli (#10764)
* default max_instances to 20 * add log level to @cloudflare/cli * fixup link to docs in error * Update packages/containers-shared/src/images.ts Co-authored-by: Edmund Hung <[email protected]> --------- Co-authored-by: Edmund Hung <[email protected]>
1 parent bd5b720 commit 79a6b7d

File tree

9 files changed

+139
-7
lines changed

9 files changed

+139
-7
lines changed

.changeset/fruity-pants-end.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"wrangler": patch
3+
---
4+
5+
containers: default `max_instances` to 20 instead of 1.

.changeset/twenty-carrots-read.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"wrangler": patch
3+
"@cloudflare/cli": patch
4+
---
5+
6+
fix: respect the log level set by wrangler when logging using @cloudflare/cli

packages/cli/index.ts

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,38 @@ export const space = (n = 1) => {
5353
return hidden("\u200A".repeat(n));
5454
};
5555

56+
const LOGGER_LEVELS = {
57+
none: -1,
58+
error: 0,
59+
warn: 1,
60+
info: 2,
61+
log: 3,
62+
debug: 4,
63+
} as const;
64+
65+
export type LoggerLevel = keyof typeof LOGGER_LEVELS;
66+
67+
// Global log level that can be set by consuming packages
68+
let currentLogLevel: LoggerLevel = "log";
69+
70+
export function setLogLevel(level: LoggerLevel) {
71+
currentLogLevel = level;
72+
}
73+
74+
export function getLogLevel(): LoggerLevel {
75+
return currentLogLevel;
76+
}
77+
5678
// Primitive for printing to stdout. Use this instead of
5779
// console.log or printing to stdout directly
5880
export const logRaw = (msg: string) => {
59-
stdout.write(`${msg}\n`);
81+
// treat all log calls as 'log' level logs
82+
const currentLevel = getLogLevel();
83+
84+
// Only output if current log level allows 'log' level messages
85+
if (LOGGER_LEVELS[currentLevel] >= LOGGER_LEVELS.log) {
86+
stdout.write(`${msg}\n`);
87+
}
6088
};
6189

6290
// A simple stylized log for use within a prompt
@@ -237,7 +265,9 @@ export const error = (
237265
extra?: string,
238266
corner = shapes.corners.bl
239267
) => {
240-
if (msg) {
268+
// Only output if current log level allows 'error' level messages
269+
const currentLevel = getLogLevel();
270+
if (msg && LOGGER_LEVELS[currentLevel] >= LOGGER_LEVELS.error) {
241271
stderr.write(
242272
`${gray(corner)} ${status.error} ${dim(msg)}\n${
243273
extra ? space() + extra + "\n" : ""

packages/containers-shared/src/images.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ export async function prepareContainerImagesForDev(args: {
9898
if (!isCloudflareRegistryLink(options.image_uri)) {
9999
throw new UserError(
100100
`Image "${options.image_uri}" is a registry link but does not point to the Cloudflare container registry.\n` +
101-
`To use an existing image from another repository, see https://developers.cloudflare.com/containers/image-management/#using-existing-images`
101+
`To use an existing image from another repository, see https://developers.cloudflare.com/containers/platform-details/image-management/#using-pre-built-container-images`
102102
);
103103
}
104104
const pull = await pullImage(dockerPath, options);

packages/wrangler/src/__tests__/containers/config.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ describe("getNormalizedContainerOptions", () => {
227227
expect(result[0]).toMatchObject({
228228
name: "test-container",
229229
class_name: "TestContainer",
230-
max_instances: 1,
230+
max_instances: 20,
231231
rollout_step_percentage: 100,
232232
});
233233
});

packages/wrangler/src/__tests__/logger.test.ts

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
import { error, logRaw, setLogLevel } from "@cloudflare/cli";
12
import { Logger } from "../logger";
2-
import { mockConsoleMethods } from "./helpers/mock-console";
3+
import { mockCLIOutput, mockConsoleMethods } from "./helpers/mock-console";
34

45
describe("logger", () => {
56
const std = mockConsoleMethods();
@@ -238,4 +239,84 @@ describe("logger", () => {
238239
expect(std.info).toMatchInlineSnapshot(`"This is a once message"`);
239240
});
240241
});
242+
243+
describe("@cloudflare/cli logRaw", () => {
244+
const cliOut = mockCLIOutput();
245+
246+
it("should output at log level", () => {
247+
setLogLevel("log");
248+
logRaw("This is a logRaw message");
249+
expect(cliOut.stdout).toMatchInlineSnapshot(
250+
`"This is a logRaw message\n"`
251+
);
252+
});
253+
254+
it("should not output when log level is set to warn", () => {
255+
setLogLevel("warn");
256+
logRaw("This is a logRaw message");
257+
expect(cliOut.stdout).toMatchInlineSnapshot(`""`);
258+
});
259+
260+
it("should not output when log level is set to error", () => {
261+
setLogLevel("error");
262+
logRaw("This is a logRaw message");
263+
expect(cliOut.stdout).toMatchInlineSnapshot(`""`);
264+
});
265+
266+
it("should not output when log level is set to none", () => {
267+
setLogLevel("none");
268+
logRaw("This is a logRaw message");
269+
expect(cliOut.stdout).toMatchInlineSnapshot(`""`);
270+
});
271+
272+
it("should output when log level is set to debug", () => {
273+
setLogLevel("debug");
274+
logRaw("This is a logRaw message");
275+
expect(cliOut.stdout).toMatchInlineSnapshot(
276+
`"This is a logRaw message\n"`
277+
);
278+
});
279+
});
280+
281+
describe("@cloudflare/cli error", () => {
282+
const cliOut = mockCLIOutput();
283+
284+
it("should output at error level", () => {
285+
setLogLevel("error");
286+
error("This is an error message");
287+
expect(cliOut.stderr).toMatchInlineSnapshot(
288+
`"╰ ERROR This is an error message\n"`
289+
);
290+
});
291+
292+
it("should not output when log level is set to none", () => {
293+
setLogLevel("none");
294+
error("This is an error message");
295+
expect(cliOut.stderr).toMatchInlineSnapshot(`""`);
296+
});
297+
298+
it("should output when log level is set to warn", () => {
299+
setLogLevel("warn");
300+
error("This is an error message");
301+
expect(cliOut.stderr).toMatchInlineSnapshot(
302+
`"╰ ERROR This is an error message\n"`
303+
);
304+
});
305+
306+
it("should output when log level is set to log", () => {
307+
setLogLevel("log");
308+
error("This is an error message");
309+
expect(cliOut.stderr).toMatchInlineSnapshot(
310+
`"╰ ERROR This is an error message\n"`
311+
);
312+
});
313+
314+
it("should output when log level is set to debug", () => {
315+
setLogLevel("debug");
316+
error("This is an error message");
317+
expect(cliOut.stderr).toMatchInlineSnapshot(
318+
`"╰ ERROR This is an error message\n"`
319+
);
320+
});
321+
});
241322
});

packages/wrangler/src/containers/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ export const getNormalizedContainerOptions = async (
8888
const shared: Omit<SharedContainerConfig, "disk_size" | "instance_type"> = {
8989
name: container.name,
9090
class_name: container.class_name,
91-
max_instances: container.max_instances ?? 1,
91+
max_instances: container.max_instances ?? 20,
9292
scheduling_policy: (container.scheduling_policy ??
9393
SchedulingPolicy.DEFAULT) as SchedulingPolicy,
9494
constraints: {

packages/wrangler/src/index.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import assert from "node:assert";
22
import os from "node:os";
33
import { resolve } from "node:path";
44
import { setTimeout } from "node:timers/promises";
5-
import { checkMacOSVersion } from "@cloudflare/cli";
5+
import { checkMacOSVersion, setLogLevel } from "@cloudflare/cli";
66
import { ApiError } from "@cloudflare/containers-shared";
77
import { UserError as ContainersUserError } from "@cloudflare/containers-shared/src/error";
88
import chalk from "chalk";
@@ -64,6 +64,7 @@ import {
6464
dispatchNamespaceRenameCommand,
6565
} from "./dispatch-namespace";
6666
import { docs } from "./docs";
67+
import { getEnvironmentVariableFactory } from "./environment-variables/factory";
6768
import { COMPLIANCE_REGION_CONFIG_UNKNOWN } from "./environment-variables/misc-variables";
6869
import {
6970
CommandLineArgsError,
@@ -1597,6 +1598,14 @@ export async function main(argv: string[]): Promise<void> {
15971598
// Update logger level, before we do any logging
15981599
if (Object.keys(LOGGER_LEVELS).includes(args.logLevel as string)) {
15991600
logger.loggerLevel = args.logLevel as LoggerLevel;
1601+
// Also set the CLI package log level to match
1602+
setLogLevel(args.logLevel as LoggerLevel);
1603+
}
1604+
const envLogLevel = getEnvironmentVariableFactory({
1605+
variableName: "WRANGLER_LOG",
1606+
})()?.toLowerCase();
1607+
if (envLogLevel) {
1608+
setLogLevel(envLogLevel as LoggerLevel);
16001609
}
16011610
// Middleware called for each sub-command, but only want to record once
16021611
if (recordedCommand) {

turbo.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"CLOUDFLARE_CONTAINER_REGISTRY",
1414
"DOCKER_HOST",
1515
"WRANGLER_DOCKER_HOST",
16+
"WRANGLER_LOG",
1617
"WRANGLER_API_ENVIRONMENT"
1718
],
1819
"tasks": {

0 commit comments

Comments
 (0)