Skip to content
This repository was archived by the owner on Jan 21, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 2 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
12 changes: 9 additions & 3 deletions src/gitlab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,13 @@ export const runCommandInGitlabPipeline = async (ctx: Context, task: Task): Prom
*/
let wasBranchRegistered = false;
const waitForBranchMaxTries = 3;
const waitForBranchRetryDelay = 1024;
const waitForBranchRetryDelayMs = 2000;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm mentally bothered that it is 2000 and no 2048 haha

But yeah, it's two seconds so it's a rounder number


const branchPresenceUrl = `${gitlabProjectApi}/repository/branches/${branchNameUrlEncoded}`;

// add small preventive delay, as checking right-away most probably would cause a retry
await millisecondsDelay(waitForBranchRetryDelayMs);

for (let waitForBranchTryCount = 0; waitForBranchTryCount < waitForBranchMaxTries; waitForBranchTryCount++) {
logger.debug({ branchPresenceUrl }, `Sending request to see if the branch for task ${task.id} is ready`);
const response = await fetch(branchPresenceUrl, { headers: { "PRIVATE-TOKEN": gitlab.accessToken } });
Expand All @@ -132,8 +136,9 @@ export const runCommandInGitlabPipeline = async (ctx: Context, task: Task): Prom
{ branchPresenceUrl, task, response },
`Branch of task ${task.id} was not found. Waiting before retrying...`,
);
await millisecondsDelay(waitForBranchRetryDelay);
await millisecondsDelay(waitForBranchRetryDelayMs);
} else if (response.ok) {
logger.debug({ branchNameUrlEncoded, response }, `Found branch ${branchNameUrlEncoded} for task ${task.id}`);
wasBranchRegistered = true;
break;
} else {
Expand All @@ -143,7 +148,7 @@ export const runCommandInGitlabPipeline = async (ctx: Context, task: Task): Prom

if (!wasBranchRegistered) {
throw new Error(
`Task's branch was not registered on GitLab after ${waitForBranchMaxTries * waitForBranchRetryDelay}ms`,
`Task's branch was not registered on GitLab after ${waitForBranchMaxTries * waitForBranchRetryDelayMs}ms`,
);
}

Expand All @@ -158,6 +163,7 @@ export const runCommandInGitlabPipeline = async (ctx: Context, task: Task): Prom
.keys({ id: Joi.number().required(), project_id: Joi.number().required() })
.options({ allowUnknown: true }),
);

logger.info({ pipeline, task }, `Created pipeline for task ${task.id}`);

const jobFetchUrl = `${gitlabProjectApi}/pipelines/${pipeline.id}/jobs`;
Expand Down
6 changes: 5 additions & 1 deletion src/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,11 @@ export const queueTask = async (
const afterTaskRun = (result: CommandOutput | null) => {
const wasAlive = taskIsAlive;

logger.debug(result, "AfterTaskRun handler");
if (result instanceof Error) {
logger.error(result, "AfterTaskRun returned an error");
} else {
logger.debug(result, "AfterTaskRun handler");
}

void terminate().catch((error) => {
logger.error(error, "Failed to terminate task on afterTaskRun");
Expand Down
10 changes: 8 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,19 @@ export class Err<T> {
constructor(public value: T) {}
}

/**
* @throws Joi.ValidationError
*/
export const validatedFetch = async <T>(
response: ReturnType<typeof fetch>,
fetchFn: ReturnType<typeof fetch>,
schema: Joi.AnySchema,
{ decoding }: { decoding: "json" } = { decoding: "json" },
): Promise<T> => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const body = await (async () => {
switch (decoding) {
case "json": {
return await (await response).json();
return await (await fetchFn).json();
}
default: {
const exhaustivenessCheck: never = decoding;
Expand All @@ -88,10 +91,13 @@ export const validatedFetch = async <T>(
}
}
})();

const validation = schema.validate(body);

if (validation.error) {
throw validation.error;
}

return validation.value as T;
};

Expand Down