Skip to content

Commit d8945b1

Browse files
authored
Merge pull request #7 from ajna-finance/42-crash-handlers
42: Handle crashes/rejections; use synchronous logs to prevent loss on crash
2 parents 9ea51db + 188d6ed commit d8945b1

File tree

4 files changed

+35
-7
lines changed

4 files changed

+35
-7
lines changed

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ Due to LUP and HTP shifting dynamically with pool activity, the in-range boundar
100100
* `Move(fromBucket, toBucket, amount)` - vault function and event that shifts liquidity directly between buckets. The keeper uses it when consolidating out-of-range buckets into the `optimalBucket` without touching the Buffer.
101101
* `MoveFromBuffer(toBucket, amount)` - vault function and event that moves liquidity out of the Buffer into a bucket. The keeper calls this to drain Buffer surplus or to place funds into the `optimalBucket`.
102102
* `MoveToBuffer(fromBucket, amount)` - vault function and event that withdraws liquidity from a bucket into the Buffer; the keeper uses it to top up the Buffer or cover a deficit.
103-
* Off-chain logs (via keeper `log` and `handleTransaction`)
104-
* Run-level:
103+
* Off-chain logs (pino-formatted JSON; filterable by event field)
104+
* Logger events:
105105
* `keeper_run_succeeded` - final state with buffer total, buffer target, current price, and optimal bucket.
106106
* `keeper_run_failed` - run aborted with error details.
107107
* `keeper_stopping` - process shutdown (SIGINT/SIGTERM).
@@ -110,6 +110,11 @@ Due to LUP and HTP shifting dynamically with pool activity, the in-range boundar
110110
* `tx_failed` - failed tx with phase (`send`, `fail`, `revert`), hash, receipt, and context.
111111
* Warnings:
112112
* `buffer_imbalance` - emitted when the Buffer total does not match the computed bufferTarget after rebalancing (indicating a residual surplus/deficit).
113+
* `price_query_failed` - query failed for the first of the two configured price feeds.
114+
* Other errors:
115+
* `subgraph_query_failed` - query for open auctions via configured subgraph threw an error.
116+
* `uncaught_exception` - an unhandled error crashed the keeper process.
117+
* `unhandled_rejection` - an unhandled promise rejection crashed the keeper process.
113118

114119
## Local Set Up
115120

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import { setUpCrashHandlers } from './utils/logger';
12
import { startScheduler } from './utils/scheduler';
23
import { run } from './keeper';
34

5+
setUpCrashHandlers();
46
startScheduler(run);

src/oracle/price.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export async function getPrice(): Promise<bigint> {
2525
}
2626
} catch (err) {
2727
log.warn(
28-
{ event: 'price_query', err, tag },
28+
{ event: 'price_query_failed', err, tag },
2929
'primary price source failed, trying secondary source',
3030
);
3131
}

src/utils/logger.ts

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,28 @@
11
import pino from 'pino';
22
import { env } from './env.ts';
33

4-
export const log = pino({
5-
level: env.LOG_LEVEL ?? 'info',
6-
redact: ['env.PRIVATE_KEY', 'env.ORACLE_API_KEY', 'env.RPC_URL'],
7-
});
4+
export const log = pino(
5+
{
6+
level: env.LOG_LEVEL ?? 'info',
7+
redact: ['env.PRIVATE_KEY', 'env.ORACLE_API_KEY', 'env.RPC_URL'],
8+
},
9+
pino.destination({ sync: true }),
10+
);
11+
12+
export function setUpCrashHandlers() {
13+
process.on('uncaughtException', (err) => {
14+
log.fatal({ event: 'uncaught_exception', err }, 'uncaughtException, process exiting');
15+
process.exit(1);
16+
});
17+
18+
process.on('unhandledRejection', (reason) => {
19+
log.fatal(
20+
{
21+
event: 'unhandled_rejection',
22+
err: reason instanceof Error ? reason : new Error(String(reason)),
23+
},
24+
'unhandledRejection, process exiting',
25+
);
26+
process.exit(1);
27+
});
28+
}

0 commit comments

Comments
 (0)