Skip to content

Commit

Permalink
feat: ✨ Add retry logic and error handeling
Browse files Browse the repository at this point in the history
service is persistent and does not error out
  • Loading branch information
Jacob Gad committed Mar 16, 2023
1 parent 6aaef13 commit 06990c2
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 23 deletions.
24 changes: 16 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Record } from './schemas';
import { updatedDNSRecordSchema, createdDNSRecord } from './schemas';
import { recordSchema } from './schemas';
import { log, vercelAxios } from './utils';
import { log, retry, vercelAxios } from './utils';
import env from './env';
import { z } from 'zod';

Expand Down Expand Up @@ -60,14 +60,22 @@ async function createDNSRecord(name: string, value: string) {
}

async function main() {
const publicIp = await getPublicIp();
const records = await getDNSRecords();
try {
const publicIp = await retry(getPublicIp);
const records = await retry(getDNSRecords);

env.subdomains.forEach(async (subdomain) => {
const record = records?.find((record) => record.name === subdomain);
if (!record) await createDNSRecord(subdomain, publicIp);
if (record && record.value !== publicIp) await updateDNSRecord(record, publicIp);
});
env.subdomains.forEach(async (subdomain) => {
const record = records?.find((record) => record.name === subdomain);
if (!record) await retry(() => createDNSRecord(subdomain, publicIp));
if (record && record.value !== publicIp) await retry(() => updateDNSRecord(record, publicIp));
});
} catch (error) {
log({
status: 'ERROR',
function: 'main',
error: 'Failed after 3 attempts, will try again in 30 min',
});
}
}

main();
Expand Down
4 changes: 2 additions & 2 deletions src/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/* eslint-disable no-console */
import { retry } from 'utils';
import { retry } from './utils';

function getValue() {
return new Promise<string>((resolve, reject) =>
Math.random() < 0.1 ? resolve('SUCCESS') : reject('ERROR')
Math.random() < 0 ? resolve('SUCCESS') : reject('ERROR')
);
}

Expand Down
24 changes: 11 additions & 13 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,15 @@ export function log(props: ConsoleError) {
}
}

export function retry<T>(func: () => Promise<T>, attempt = 0): Promise<T> {
return new Promise(
(resolve) =>
setTimeout(async () => {
try {
const value = await func();
resolve(value);
} catch (error) {
console.error('ERROR:', func.name, 'attempt', attempt, '->', error);
resolve(retry(func, attempt + 1));
}
}, 1000 * 10) //10 seconds
);
export function retry<T>(func: () => Promise<T>, maxAttempt = 3, currentAttempt = 1): Promise<T> {
return new Promise(async (resolve, reject) => {
try {
const value = await func();
resolve(value);
} catch (error) {
console.error('ERROR:', func.name, 'attempt', currentAttempt, '->', error);
if (currentAttempt >= maxAttempt) return reject(error);
resolve(retry(func, maxAttempt, currentAttempt + 1));
}
});
}

0 comments on commit 06990c2

Please sign in to comment.