Skip to content

Commit

Permalink
Adding retry mechanism into ops
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasia committed Jun 16, 2024
1 parent 734c944 commit 531993e
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 5 deletions.
3 changes: 3 additions & 0 deletions packages/ops/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
"@credbull/api": "workspace:^",
"@credbull/contracts": "workspace:^",
"@supabase/supabase-js": "^2.39.1",
"axios": "^1.7.2",
"axios-retry": "^4.4.0",
"axis": "^1.0.0",
"bottleneck": "^2.19.5",
"compression": "^1.7.4",
"crypto": "^1.0.1",
Expand Down
24 changes: 20 additions & 4 deletions packages/ops/src/utils/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Database } from '@credbull/api';
import { SupabaseClient, createClient } from '@supabase/supabase-js';
import axios from 'axios';
import axiosRetry from 'axios-retry';
import crypto from 'crypto';
import { Wallet, providers } from 'ethers';
import { SiweMessage, generateNonce } from 'siwe';
Expand All @@ -10,6 +12,19 @@ export const emailSchemaOptional = z.string().email().nullish().or(z.literal('')
export const addressSchema = z.string().regex(/^(0x)?[0-9a-fA-F]{40,40}$/);
export const upsideVaultSchema = z.union([addressSchema, z.string().regex(/^self$/)]).optional();

// Configure axios to use axios-retry
axiosRetry(axios, {
retries: 3, // Number of retries
retryDelay: (retryCount) => {
console.log(`Retry attempt: ${retryCount}`);
return retryCount * 300; // Time in ms between retries
},
retryCondition: (error) => {
// Retry on network errors or 5xx status codes
return axiosRetry.isNetworkOrIdempotentRequestError(error) || error.response.status >= 500;
},
});

const supabaseConfigSchema = z.object({
services: z.object({ supabase: z.object({ url: z.string().url() }) }),
secret: z.object({
Expand Down Expand Up @@ -96,19 +111,20 @@ export const login = async (
let signIn;

try {
signIn = await fetch(`${config.api.url}/auth/api/sign-in`, { method: 'POST', body, ...headers() });
signIn = await axios.post(`${config.api.url}/auth/api/sign-in`, { body, ...headers() });
} catch (error) {
console.error('Network error or server is down:', error);
throw error;
}

if (!signIn.ok) {
if (signIn.status != 200) {
console.error(`HTTP error! status: ${signIn.status}`);
throw new Error(`Failed to login: ${signIn.statusText}`);
}

const data = await signIn.json();
const data = await signIn.data;
console.log(`sign in response: ${JSON.stringify(data)}`);

return data;
};

Expand All @@ -117,7 +133,7 @@ const linkWalletConfigSchema = z.object({ app: z.object({ url: z.string().url()
export const linkWalletMessage = async (config: any, signer: Wallet) => {
linkWalletConfigSchema.parse(config);

let appUrl = new URL(config.app.url);
const appUrl = new URL(config.app.url);
const chainId = await signer.getChainId();
const preMessage = new SiweMessage({
domain: appUrl.host,
Expand Down
30 changes: 29 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,9 @@ __metadata:
"@types/node": ^20.10.5
"@typescript-eslint/eslint-plugin": ^6.16.0
"@typescript-eslint/parser": ^6.16.0
axios: ^1.7.2
axios-retry: ^4.4.0
axis: ^1.0.0
bottleneck: ^2.19.5
compression: ^1.7.4
crypto: ^1.0.1
Expand Down Expand Up @@ -7246,7 +7249,18 @@ __metadata:
languageName: node
linkType: hard

"axios@npm:^1.5.1, axios@npm:^1.6.1, axios@npm:^1.6.5":
"axios-retry@npm:^4.4.0":
version: 4.4.0
resolution: "axios-retry@npm:4.4.0"
dependencies:
is-retry-allowed: ^2.2.0
peerDependencies:
axios: 0.x || 1.x
checksum: 19b005bbd3e956080b27ee54fad658dbf7663fa6e884110a638b9f53db50a34c4870648143c8d9397354fedd0ede920693caa1e7c6a762d9477b4cbb48ca22f2
languageName: node
linkType: hard

"axios@npm:^1.5.1, axios@npm:^1.6.1, axios@npm:^1.6.5, axios@npm:^1.7.2":
version: 1.7.2
resolution: "axios@npm:1.7.2"
dependencies:
Expand All @@ -7257,6 +7271,13 @@ __metadata:
languageName: node
linkType: hard

"axis@npm:^1.0.0":
version: 1.0.0
resolution: "axis@npm:1.0.0"
checksum: 6c1d151252a33ff12c7f390ff9168a44c95e5caa55e6f0441e8f4be8b90990903e511dbb4e0e26dda905def56111dac22df4f1023904447cbdfc4d27ff48b1cf
languageName: node
linkType: hard

"axobject-query@npm:^3.2.1":
version: 3.2.1
resolution: "axobject-query@npm:3.2.1"
Expand Down Expand Up @@ -12854,6 +12875,13 @@ __metadata:
languageName: node
linkType: hard

"is-retry-allowed@npm:^2.2.0":
version: 2.2.0
resolution: "is-retry-allowed@npm:2.2.0"
checksum: 3d1103a9290b5d03626756a41054844633eac78bc5d3e3a95b13afeae94fa3cfbcf7f0b5520d83f75f48a25ce7b142fdbac4217dc4b0630f3ea55e866ec3a029
languageName: node
linkType: hard

"is-set@npm:^2.0.2, is-set@npm:^2.0.3":
version: 2.0.3
resolution: "is-set@npm:2.0.3"
Expand Down

0 comments on commit 531993e

Please sign in to comment.