Implement task retrying to recording task attempts

This commit is contained in:
Eric Allam
2023-06-13 15:47:59 +01:00
parent e5282fb264
commit 3589d26fb3
26 changed files with 900 additions and 95 deletions
+3 -1
View File
@@ -13,8 +13,8 @@
"dist/index.js.map"
],
"devDependencies": {
"@octokit/webhooks-types": "^6.10.0",
"@octokit/types": "^9.2.3",
"@octokit/webhooks-types": "^6.10.0",
"@trigger.dev/tsconfig": "workspace:*",
"@types/node": "18",
"rimraf": "^3.0.2",
@@ -26,6 +26,8 @@
"build:tsup": "tsup"
},
"dependencies": {
"@octokit/request": "^6.2.5",
"@octokit/request-error": "^4.0.1",
"@octokit/webhooks": "^10.4.0",
"@trigger.dev/sdk": "workspace:^2.0.0-next.0",
"octokit": "^2.0.14",
+15 -1
View File
@@ -16,10 +16,12 @@ import { Octokit } from "octokit";
import { clientFactory } from "./clientFactory";
import { createOrgEventSource, createRepoEventSource } from "./sources";
import { tasks } from "./tasks";
import { RequestRequestOptions } from "@octokit/types";
export type GithubIntegrationOptions = {
id: string;
token?: string;
octokitRequest?: RequestRequestOptions;
};
type GithubSources = {
@@ -78,6 +80,10 @@ function createConnectionFromOptions(
if (options.token) {
const client = new Octokit({
auth: options.token,
request: options.octokitRequest,
retry: {
enabled: false,
},
});
return {
@@ -89,7 +95,15 @@ function createConnectionFromOptions(
return {
usesLocalAuth: false,
clientFactory,
clientFactory: (auth) => {
return new Octokit({
auth: auth.accessToken,
request: options.octokitRequest,
retry: {
enabled: false,
},
});
},
tasks,
};
}
+50 -3
View File
@@ -1,6 +1,7 @@
import { Octokit } from "octokit";
import { RequestError } from "@octokit/request-error";
import type { GetResponseDataTypeFromEndpointMethod } from "@octokit/types";
import type { AuthenticatedTask } from "@trigger.dev/sdk";
import { Octokit } from "octokit";
type OctokitClient = InstanceType<typeof Octokit>;
@@ -13,11 +14,37 @@ type GithubAuthenticatedTask<
GetResponseDataTypeFromEndpointMethod<TFunction>
>;
function isRequestError(error: unknown): error is RequestError {
return typeof error === "object" && error !== null && "status" in error;
}
function onError(error: unknown) {
if (!isRequestError(error)) {
return;
}
// Check if this is a rate limit error
if (error.status === 403 && error.response) {
const rateLimitRemaining = error.response.headers["x-ratelimit-remaining"];
const rateLimitReset = error.response.headers["x-ratelimit-reset"];
if (rateLimitRemaining === "0" && rateLimitReset) {
const resetDate = new Date(Number(rateLimitReset) * 1000);
return {
retryAt: resetDate,
error,
};
}
}
}
export const createIssue: GithubAuthenticatedTask<
{ title: string; repo: string },
OctokitClient["rest"]["issues"]["create"]
> = {
run: async (params, client) => {
onError,
run: async (params, client, task) => {
const [owner, repo] = params.repo.split("/");
return client.rest.issues
@@ -42,6 +69,13 @@ export const createIssue: GithubAuthenticatedTask<
text: params.title,
},
],
retry: {
limit: 3,
factor: 2,
minTimeoutInMs: 500,
maxTimeoutInMs: 30000,
randomize: true,
},
};
},
};
@@ -50,6 +84,7 @@ export const createIssueComment: GithubAuthenticatedTask<
{ body: string; repo: string; issueNumber: number },
OctokitClient["rest"]["issues"]["createComment"]
> = {
onError,
run: async (params, client) => {
const [owner, repo] = params.repo.split("/");
@@ -84,12 +119,16 @@ export const getRepo: GithubAuthenticatedTask<
{ repo: string },
OctokitClient["rest"]["repos"]["get"]
> = {
run: async (params, client) => {
onError,
run: async (params, client, task) => {
const [owner, repo] = params.repo.split("/");
const response = await client.rest.repos.get({
owner,
repo,
headers: {
"x-trigger-attempt": String(task.attempts),
},
});
return response.data;
@@ -126,6 +165,7 @@ export const addIssueCommentReaction: GithubAuthenticatedTask<
},
OctokitClient["rest"]["reactions"]["createForIssueComment"]
> = {
onError,
run: async (params, client) => {
const [owner, repo] = params.repo.split("/");
@@ -195,6 +235,7 @@ export const createIssueCommentWithReaction: GithubAuthenticatedTask<
},
OctokitClient["rest"]["issues"]["createComment"]
> = {
onError,
run: async (params, client, task, io) => {
const comment = await io.runTask(
`Comment on Issue #${params.issueNumber}`,
@@ -255,6 +296,7 @@ export const updateWebhook: GithubAuthenticatedTask<
},
OctokitClient["rest"]["repos"]["updateWebhook"]
> = {
onError,
run: async (params, client) => {
const [owner, repo] = params.repo.split("/");
@@ -300,6 +342,7 @@ export const updateOrgWebhook: GithubAuthenticatedTask<
},
OctokitClient["rest"]["orgs"]["updateWebhook"]
> = {
onError,
run: async (params, client) => {
return client.rest.orgs
.updateWebhook({
@@ -341,6 +384,7 @@ export const createWebhook: GithubAuthenticatedTask<
},
OctokitClient["rest"]["repos"]["createWebhook"]
> = {
onError,
run: async (params, client) => {
const [owner, repo] = params.repo.split("/");
@@ -384,6 +428,7 @@ export const createOrgWebhook: GithubAuthenticatedTask<
},
OctokitClient["rest"]["orgs"]["createWebhook"]
> = {
onError,
run: async (params, client, task) => {
return client.rest.orgs
.createWebhook({
@@ -422,6 +467,7 @@ export const listWebhooks: GithubAuthenticatedTask<
},
OctokitClient["rest"]["repos"]["listWebhooks"]
> = {
onError,
run: async (params, client) => {
const [owner, repo] = params.repo.split("/");
@@ -452,6 +498,7 @@ export const listOrgWebhooks: GithubAuthenticatedTask<
},
OctokitClient["rest"]["orgs"]["listWebhooks"]
> = {
onError,
run: async (params, client) => {
return client.rest.orgs
.listWebhooks({