Skip to content

Commit

Permalink
refactor: lift unhandled error handling to main
Browse files Browse the repository at this point in the history
  • Loading branch information
Codex- committed Oct 2, 2024
1 parent 3cf422e commit 1d16afc
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 70 deletions.
46 changes: 33 additions & 13 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,41 @@ import * as api from "./api.ts";
import { getWorkflowId, returnDispatch } from "./return-dispatch.ts";
import { getBranchName, logInfoForBranchNameResult } from "./utils.ts";

(async (): Promise<void> => {
const startTime = Date.now();
async function action(): Promise<void> {
try {
const startTime = Date.now();

const config = getConfig();
api.init(config);
const config = getConfig();
api.init(config);

const workflowId = await getWorkflowId(config);
const workflowId = await getWorkflowId(config);

// Dispatch the action
await api.dispatchWorkflow(config.distinctId);
// Dispatch the action
await api.dispatchWorkflow(config.distinctId);

// Attempt to get the branch from config ref
core.info("Attempt to extract branch name from ref...");
const branch = getBranchName(config.ref);
logInfoForBranchNameResult(branch, config.ref);
// Attempt to get the branch from config ref
core.info("Attempt to extract branch name from ref...");
const branch = getBranchName(config.ref);
logInfoForBranchNameResult(branch, config.ref);

await returnDispatch(config, startTime, branch, workflowId);
})();
await returnDispatch(config, startTime, branch, workflowId);
} catch (error) {
if (error instanceof Error) {
const failureMsg = `Failed: An unhandled error has occurred: ${error.message}`;
core.setFailed(failureMsg);
core.error(failureMsg);
core.debug(error.stack ?? "");
} else {
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
const failureMsg = `Failed: An unknown error has occurred: ${error}`;
core.setFailed(failureMsg);
core.error(failureMsg);
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
core.debug(error as any);
}
}
}

if (!process.env.VITEST) {
await action();
}
96 changes: 39 additions & 57 deletions src/return-dispatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,71 +118,53 @@ export async function returnDispatch(
branch: BranchNameResult,
workflowId: number,
): Promise<void> {
try {
const timeoutMs = config.workflowTimeoutSeconds * 1000;
let attemptNo = 0;
let elapsedTime = Date.now() - startTime;
core.info("Attempt to extract run ID from steps...");
while (elapsedTime < timeoutMs) {
attemptNo++;
elapsedTime = Date.now() - startTime;

core.debug(`Attempting to fetch Run IDs for Workflow ID ${workflowId}`);

// Get all runs for a given workflow ID
const fetchWorkflowRunIds = await api.retryOrTimeout(
() => api.getWorkflowRunIds(workflowId, branch),
Math.max(constants.WORKFLOW_FETCH_TIMEOUT_MS, timeoutMs),
);
if (fetchWorkflowRunIds.timeout) {
core.debug(
`Timed out while attempting to fetch Workflow Run IDs, waited ${Date.now() - startTime}ms`,
);
break;
}

const workflowRunIds = fetchWorkflowRunIds.value;
const timeoutMs = config.workflowTimeoutSeconds * 1000;
let attemptNo = 0;
let elapsedTime = Date.now() - startTime;
core.info("Attempt to extract run ID from steps...");
while (elapsedTime < timeoutMs) {
attemptNo++;
elapsedTime = Date.now() - startTime;

core.debug(`Attempting to fetch Run IDs for Workflow ID ${workflowId}`);

// Get all runs for a given workflow ID
const fetchWorkflowRunIds = await api.retryOrTimeout(
() => api.getWorkflowRunIds(workflowId, branch),
Math.max(constants.WORKFLOW_FETCH_TIMEOUT_MS, timeoutMs),
);
if (fetchWorkflowRunIds.timeout) {
core.debug(
`Attempting to get step names for Run IDs: [${workflowRunIds.join(", ")}]`,
`Timed out while attempting to fetch Workflow Run IDs, waited ${Date.now() - startTime}ms`,
);
break;
}

const idRegex = new RegExp(config.distinctId);
const workflowRunIds = fetchWorkflowRunIds.value;
core.debug(
`Attempting to get step names for Run IDs: [${workflowRunIds.join(", ")}]`,
);

const result = await attemptToFindRunId(idRegex, workflowRunIds);
if (result.found) {
core.info(
"Successfully identified remote Run:\n" +
` Run ID: ${result.value.id}\n` +
` URL: ${result.value.url}`,
);
core.setOutput(ActionOutputs.runId, result.value.id);
core.setOutput(ActionOutputs.runUrl, result.value.url);
core.debug(`Completed in ${Date.now() - startTime}ms`);
return;
}
const idRegex = new RegExp(config.distinctId);

const result = await attemptToFindRunId(idRegex, workflowRunIds);
if (result.found) {
core.info(
`Exhausted searching IDs in known runs, attempt ${attemptNo}...`,
"Successfully identified remote Run:\n" +
` Run ID: ${result.value.id}\n` +
` URL: ${result.value.url}`,
);

await sleep(constants.WORKFLOW_JOB_STEPS_RETRY_MS);
core.setOutput(ActionOutputs.runId, result.value.id);
core.setOutput(ActionOutputs.runUrl, result.value.url);
core.debug(`Completed in ${Date.now() - startTime}ms`);
return;
}

core.error("Failed: Timeout exceeded while attempting to get Run ID");
core.setFailed("Timeout exceeded while attempting to get Run ID");
} catch (error) {
if (error instanceof Error) {
const failureMsg = `Failed: An unhandled error has occurred: ${error.message}`;
core.setFailed(failureMsg);
core.error(failureMsg);
core.debug(error.stack ?? "");
} else {
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
const failureMsg = `Failed: An unknown error has occurred: ${error}`;
core.setFailed(failureMsg);
core.error(failureMsg);
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
core.debug(error as any);
}
core.info(`Exhausted searching IDs in known runs, attempt ${attemptNo}...`);

await sleep(constants.WORKFLOW_JOB_STEPS_RETRY_MS);
}

core.error("Failed: Timeout exceeded while attempting to get Run ID");
core.setFailed("Timeout exceeded while attempting to get Run ID");
}

0 comments on commit 1d16afc

Please sign in to comment.