Skip to content

Commit

Permalink
fix(gcf-utils): cache Cloud Run service URL (#4487)
Browse files Browse the repository at this point in the history
Because it doesn't change within a single deployment lifecycle.

fixes #4469
  • Loading branch information
Takashi Matsuo authored Sep 22, 2022
1 parent 2ee342c commit a908803
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
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

0 comments on commit a908803

Please sign in to comment.