Skip to content

Commit

Permalink
feat: Wait longer and only print one retry message for deploying a fu…
Browse files Browse the repository at this point in the history
…nction app (#283)
  • Loading branch information
tbarlow12 committed Sep 13, 2019
1 parent 357a609 commit 1ffbd69
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
33 changes: 29 additions & 4 deletions src/services/functionAppService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ jest.mock("@azure/arm-resources");
jest.mock("./azureBlobStorageService");
import { AzureBlobStorageService } from "./azureBlobStorageService"
import configConstants from "../config";
import { Utils } from "../shared/utils";

describe("Function App Service", () => {
const app = MockFactory.createTestSite();
Expand All @@ -41,8 +42,10 @@ describe("Function App Service", () => {
setting2: "value2",
}

beforeAll(() => {
const axiosMock = new MockAdapter(axios);
let axiosMock: MockAdapter;

beforeEach(() => {
axiosMock = new MockAdapter(axios);

// Master Key
axiosMock.onGet(masterKeyUrl).reply(200, { value: masterKey });
Expand All @@ -64,9 +67,7 @@ describe("Function App Service", () => {
"serviceName.zip": "contents",
}
}, { createCwd: true, createTmp: true });
});

beforeEach(() => {
WebSiteManagementClient.prototype.webApps = {
get: jest.fn(() => app),
deleteFunction: jest.fn(),
Expand Down Expand Up @@ -152,6 +153,30 @@ describe("Function App Service", () => {
expect(await service.listFunctions(app)).toEqual(functionsResponse.map((f) => f.properties));
});

it("list functions with retry", async () => {
axiosMock.onGet(listFunctionsUrl).reply(200, { value: [] });
const service = createService();

const originalRunWithRetry = Utils.runWithRetry;
Utils.runWithRetry = jest.fn(() => {
return {
data: {
value: []
}
}
}) as any;

await service.listFunctions(app);
const runWithRetryCalls = (Utils.runWithRetry as any).mock.calls;
Utils.runWithRetry = originalRunWithRetry;

expect(runWithRetryCalls).toHaveLength(1);
const call = runWithRetryCalls[0];
await expect(call[0]()).rejects.toThrow();
expect(call[1]).toEqual(30);
expect(call[2]).toEqual(30000);
});

describe("Deployments", () => {
const expectedDeployment: ArmDeployment = {
parameters: {},
Expand Down
11 changes: 8 additions & 3 deletions src/services/functionAppService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import configConstants from "../config";

export class FunctionAppService extends BaseService {
private static readonly retryCount: number = 30;
private static readonly retryInterval: number = 5000;
private static readonly retryInterval: number = 30000;
private webClient: WebSiteManagementClient;
private blobService: AzureBlobStorageService;

Expand Down Expand Up @@ -94,13 +94,18 @@ export class FunctionAppService extends BaseService {
Guard.null(functionApp);

const getTokenUrl = `${this.baseUrl}${functionApp.id}/functions?api-version=2016-08-01`;
let retries = 0;
try {
const response = await Utils.runWithRetry(async () => {
const listFunctionsResponse = await this.sendApiRequest("GET", getTokenUrl);

if (listFunctionsResponse.status !== 200 || listFunctionsResponse.data.value.length === 0) {
this.log("-> Function App not ready. Retrying...");
throw new Error(JSON.stringify(listFunctionsResponse.data, null, 2));
this.log(`-> Function App not ready. Retry ${retries++} of ${FunctionAppService.retryCount}...`);
const response = JSON.stringify(listFunctionsResponse.data, null, 2);
throw new Error(
`The function app is taking longer than usual to be provisioned. Please try again soon.
Response error data: \n${response}`
);
}

return listFunctionsResponse;
Expand Down

0 comments on commit 1ffbd69

Please sign in to comment.