Skip to content

Commit 54e9c84

Browse files
authored
Justin/retry finalize (#5451)
* added retries for finalize * organize import
1 parent d6b0dda commit 54e9c84

File tree

1 file changed

+35
-15
lines changed

1 file changed

+35
-15
lines changed

worker/src/lib/managers/WalletManager.ts

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
1-
import {
2-
ALERT_ID,
3-
ALERT_THRESHOLD,
4-
SYNC_STALENESS_THRESHOLD,
5-
Wallet,
6-
} from "../durable-objects/Wallet";
7-
import { SlackAlertManager } from "./SlackAlertManager";
8-
import { err, ok, Result } from "../util/results";
9-
import { isError } from "../../../../packages/common/result";
10-
import { HeliconeProxyRequest } from "../models/HeliconeProxyRequest";
11-
import { WalletKVSync } from "../ai-gateway/WalletKVSync";
1+
import retry from "async-retry";
122
import { DisallowListKVSync } from "../ai-gateway/DisallowListKVSync";
3+
import { WalletKVSync } from "../ai-gateway/WalletKVSync";
4+
import { SYNC_STALENESS_THRESHOLD, Wallet } from "../durable-objects/Wallet";
5+
import { HeliconeProxyRequest } from "../models/HeliconeProxyRequest";
136
import { createDataDogTracer } from "../monitoring/DataDogTracer";
7+
import { err, ok, Result } from "../util/results";
148

159
export class WalletManager {
1610
private env: Env;
@@ -70,10 +64,36 @@ export class WalletManager {
7064
const escrowId = escrowResult.data.reservedEscrowId;
7165

7266
const { clickhouseLastCheckedAt, remainingBalance, staleEscrowsCleared } =
73-
await this.walletStub.finalizeEscrow(
74-
organizationId,
75-
escrowId,
76-
cost ?? 0
67+
await retry(
68+
async (bail) => {
69+
try {
70+
return await this.walletStub.finalizeEscrow(
71+
organizationId,
72+
escrowId,
73+
cost ?? 0
74+
);
75+
} catch (e) {
76+
// Don't retry validation errors
77+
if (
78+
e instanceof Error &&
79+
e.message.includes("cannot be negative")
80+
) {
81+
bail(e);
82+
return undefined as never;
83+
}
84+
throw e;
85+
}
86+
},
87+
{
88+
retries: 3,
89+
minTimeout: 100,
90+
maxTimeout: 1000,
91+
onRetry: (error, attempt) => {
92+
console.warn(
93+
`Retry attempt ${attempt} for finalizeEscrow. Error: ${error}`
94+
);
95+
},
96+
}
7797
);
7898

7999
// Store remaining balance to KV for future optimistic checks

0 commit comments

Comments
 (0)