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(gcf-utils): cache Cloud Run service URL #4487

Merged
merged 1 commit into from
Sep 22, 2022
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
6 changes: 6 additions & 0 deletions packages/gcf-utils/src/gcf-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ export class GCFBootstrapper {
taskTargetName: string;
taskCaller: string;
flowControlDelayInSeconds: number;
cloudRunURL: string | undefined;

constructor(options?: BootstrapperOptions) {
options = {
Expand Down Expand Up @@ -356,6 +357,7 @@ export class GCFBootstrapper {
this.taskTargetName = options.taskTargetName || this.functionName;
this.taskCaller = options.taskCaller || DEFAULT_TASK_CALLER;
this.flowControlDelayInSeconds = DEFAULT_FLOW_CONTROL_DELAY_IN_SECOND;
this.cloudRunURL = undefined;
}

async loadProbot(
Expand Down Expand Up @@ -1070,8 +1072,12 @@ export class GCFBootstrapper {
// https://us-central1-repo-automation-bots.cloudfunctions.net/merge_on_green
return `https://${location}-${projectId}.cloudfunctions.net/${botName}`;
} else if (this.taskTargetEnvironment === 'run') {
if (this.cloudRunURL) {
return this.cloudRunURL;
}
const url = await this.getCloudRunUrl(projectId, location, botName);
if (url) {
this.cloudRunURL = url;
return url;
}
throw new Error(`Unable to find url for Cloud Run service: ${botName}`);
Expand Down
12 changes: 11 additions & 1 deletion packages/gcf-utils/test/gcf-bootstrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1791,7 +1791,7 @@ describe('GCFBootstrapper', () => {
});
});

it('queues a Cloud Run URL', async () => {
it('queues a Cloud Run URL with caching', async () => {
const bootstrapper = new GCFBootstrapper({
projectId: 'my-project',
functionName: 'my-function-name',
Expand Down Expand Up @@ -1831,6 +1831,16 @@ describe('GCFBootstrapper', () => {
sinon.assert.calledOnceWithExactly(getServiceStub as any, {
name: 'projects/my-project/locations/my-location/services/my-function-name',
});
// Make sure the Cloud Run service URL is cached.
await bootstrapper.enqueueTask({
body: JSON.stringify({installation: {id: 1}}),
id: 'some-request-id',
name: 'event.name',
});
const getServiceCalls = getServiceStub.getCalls();
assert.equal(getServiceCalls.length, 1);
const createTaskCalls = createTask.getCalls();
assert.equal(createTaskCalls.length, 2);
});

it('queues a Cloud Run URL with underscored bot name', async () => {
Expand Down