Skip to content

Commit

Permalink
fix: Job cf task polling and scale worker instances
Browse files Browse the repository at this point in the history
  • Loading branch information
apburnes committed Oct 17, 2024
1 parent cd4f911 commit d57dd1a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .cloudgov/manifest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ applications:
- type: worker
command: yarn start-workers
disk_quota: 5G
instances: 1
instances: 2
memory: 2G
services:
- app-((env))-uaa-client
Expand Down
54 changes: 34 additions & 20 deletions api/utils/cfApiClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,33 +143,47 @@ class CloudFoundryAPIClient {
guid,
{ attempt = 1, totalAttempts = 240, sleepInterval = 15000 } = {}
) {
if (attempt > totalAttempts) {
await this.cancelTask(guid);
const totalMins = ((sleepInterval / 1000) * totalAttempts) / 60;
let current = attempt;
let isPolling = true;
let error = null;
let state = null;

while (isPolling) {
if (current > totalAttempts) {
// eslint-disable-next-line no-await-in-loop
await this.cancelTask(guid);
const totalMins = ((sleepInterval / 1000) * totalAttempts) / 60;

error = new Error(`Task timed out after ${totalMins} minutes`);
isPolling = false;
break;
}

throw new Error(`Task timed out after ${totalMins} minutes`);
}
// eslint-disable-next-line no-await-in-loop
const response = await this.fetchTaskByGuid(guid);

const response = await this.fetchTaskByGuid(guid);
if (!response) {
error = new Error('Task not found');
isPolling = false;
break;
}

if (!response) {
throw new Error('Task not found');
}
if (['SUCCEEDED', 'FAILED'].includes(response.state)) {
state = response.state;
isPolling = false;
break;
}

if (['SUCCEEDED', 'FAILED'].includes(response.state)) {
return {
state: response.state,
};
current += 1;
// eslint-disable-next-line no-await-in-loop
await wait(sleepInterval);
}

const nextAttempt = attempt + 1;
await wait(sleepInterval);
if (error) {
throw error;
}

return this.pollTaskStatus(guid, {
attempt: nextAttempt,
totalAttempts,
sleepInterval,
});
return { state };
}

async startBuildTask(
Expand Down

0 comments on commit d57dd1a

Please sign in to comment.