Skip to content

Add initial delay when loading python functions #8239

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

Merged
merged 7 commits into from
Feb 21, 2025
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
- Add initial delay when loading python functions (#8239)
- Enforce webframeworks enablement only on webframeworks sites (#8168)
13 changes: 13 additions & 0 deletions src/deploy/functions/runtimes/discovery/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,22 @@ describe("detectFromPort", () => {
code: "ECONNREFUSED",
});

nock("http://127.0.0.1:8080").get("/__/functions.yaml").times(3).replyWithError({
message: "Almost there",
code: "ETIMEDOUT",
});

nock("http://127.0.0.1:8080").get("/__/functions.yaml").reply(200, YAML_TEXT);

const parsed = await discovery.detectFromPort(8080, "project", "nodejs16");
expect(parsed).to.deep.equal(BUILD);
});

it("retries when request times out", async () => {
nock("http://127.0.0.1:8081").get("/__/functions.yaml").delay(1_000).reply(200, YAML_TEXT);
nock("http://127.0.0.1:8080").get("/__/functions.yaml").reply(200, YAML_TEXT);

const parsed = await discovery.detectFromPort(8080, "project", "nodejs16", 0, 500);
expect(parsed).to.deep.equal(BUILD);
});
});
15 changes: 12 additions & 3 deletions src/deploy/functions/runtimes/discovery/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

const TIMEOUT_OVERRIDE_ENV_VAR = "FUNCTIONS_DISCOVERY_TIMEOUT";

export function getFunctionDiscoveryTimeout(): number {

Check warning on line 18 in src/deploy/functions/runtimes/discovery/index.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Missing JSDoc comment
return +(process.env[TIMEOUT_OVERRIDE_ENV_VAR] || 0) * 1000; /* ms */
}

Expand All @@ -23,22 +23,22 @@
* Converts the YAML retrieved from discovery into a Build object for param interpolation.
*/
export function yamlToBuild(
yaml: any,

Check warning on line 26 in src/deploy/functions/runtimes/discovery/index.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unexpected any. Specify a different type
project: string,
region: string,
runtime: Runtime,
): build.Build {
try {
if (!yaml.specVersion) {

Check warning on line 32 in src/deploy/functions/runtimes/discovery/index.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe member access .specVersion on an `any` value
throw new FirebaseError("Expect manifest yaml to specify a version number");
}
if (yaml.specVersion === "v1alpha1") {

Check warning on line 35 in src/deploy/functions/runtimes/discovery/index.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe member access .specVersion on an `any` value
return v1alpha1.buildFromV1Alpha1(yaml, project, region, runtime);
}
throw new FirebaseError(
"It seems you are using a newer SDK than this version of the CLI can handle. Please update your CLI with `npm install -g firebase-tools`",
);
} catch (err: any) {

Check warning on line 41 in src/deploy/functions/runtimes/discovery/index.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unexpected any. Specify a different type
throw new FirebaseError("Failed to parse build specification", { children: [err] });
}
}
Expand All @@ -53,9 +53,9 @@
): Promise<build.Build | undefined> {
let text: string;
try {
text = await exports.readFileAsync(path.join(directory, "functions.yaml"), "utf8");

Check warning on line 56 in src/deploy/functions/runtimes/discovery/index.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe assignment of an `any` value

Check warning on line 56 in src/deploy/functions/runtimes/discovery/index.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe member access .readFileAsync on an `any` value

Check warning on line 56 in src/deploy/functions/runtimes/discovery/index.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe call of an `any` typed value
} catch (err: any) {

Check warning on line 57 in src/deploy/functions/runtimes/discovery/index.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unexpected any. Specify a different type
if (err.code === "ENOENT") {

Check warning on line 58 in src/deploy/functions/runtimes/discovery/index.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe member access .code on an `any` value
logger.debug("Could not find functions.yaml. Must use http discovery");
} else {
logger.debug("Unexpected error looking for functions.yaml file:", err);
Expand All @@ -75,6 +75,7 @@
port: number,
project: string,
runtime: Runtime,
initialDelay = 0,
timeout = 10_000 /* 10s to boot up */,
): Promise<build.Build> {
let res: Response;
Expand All @@ -84,13 +85,21 @@
}, getFunctionDiscoveryTimeout() || timeout);
});

// Initial delay to wait for admin server to boot.
if (initialDelay > 0) {
await new Promise((resolve) => setTimeout(resolve, initialDelay));
}

const url = `http://127.0.0.1:${port}/__/functions.yaml`;
while (true) {
try {
res = await Promise.race([fetch(`http://127.0.0.1:${port}/__/functions.yaml`), timedOut]);
res = await Promise.race([fetch(url), timedOut]);
break;
} catch (err: any) {
// Allow us to wait until the server is listening.
if (err?.code === "ECONNREFUSED") {
if (
err?.name === "FetchError" ||
["ECONNREFUSED", "ECONNRESET", "ETIMEDOUT"].includes(err?.code)
) {
continue;
}
throw err;
Expand Down
7 changes: 6 additions & 1 deletion src/deploy/functions/runtimes/python/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,12 @@ export class Delegate implements runtimes.RuntimeDelegate {
});
const killProcess = await this.serveAdmin(adminPort, envs);
try {
discovered = await discovery.detectFromPort(adminPort, this.projectId, this.runtime);
discovered = await discovery.detectFromPort(
adminPort,
this.projectId,
this.runtime,
500 /* initialDelay, python startup is slow */,
);
} finally {
await killProcess();
}
Expand Down
Loading