forked from ZyntariHQ/Invoisio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackfill-cli.ts
More file actions
229 lines (209 loc) · 9.3 KB
/
Copy pathbackfill-cli.ts
File metadata and controls
229 lines (209 loc) · 9.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#!/usr/bin/env node
import { NestFactory } from '@nestjs/core';
import { AppModule } from '../src/app.module';
import { BackfillService } from '../src/backfill/backfill.service';
import { Command } from 'commander';
import * as dotenv from 'dotenv';
dotenv.config();
async function bootstrap() {
const app = await NestFactory.createApplicationContext(AppModule, {
logger: ['error', 'warn', 'log'],
});
const backfillService = app.get(BackfillService);
const program = new Command();
program
.name('backfill')
.description('Backfill invoice payment states from Soroban events')
.version('1.0.0');
program
.command('reconcile')
.description('Reconcile historical invoice payments')
.option('-s, --start-ledger <number>', 'Starting ledger', parseInt)
.option('-e, --end-ledger <number>', 'Ending ledger', parseInt)
.option('--dry-run', 'Simulate without writing to database')
.option('--from-last', 'Start from last processed ledger')
.option('--batch-size <number>', 'Batch size for fetching events', parseInt, 100)
.option('--resume-from-run <number>', 'Resume a previous failed/cancelled run by ID', parseInt)
.option('--allow-overlap', 'Allow overlapping range with an existing running/paused run')
.option('--operator <name>', 'Operator identifier (for audit trail on cancel)')
.action(async (options) => {
try {
console.log('\n🚀 Starting backfill reconciliation...\n');
const result = await backfillService.reconcile({
startLedger: options.startLedger,
endLedger: options.endLedger,
dryRun: options.dryRun,
fromLast: options.fromLast,
batchSize: options.batchSize,
resumeFromRunId: options.resumeFromRun,
allowOverlap: options.allowOverlap,
operator: options.operator,
});
console.log('\n✅ Backfill complete!');
console.log(` Run ID: ${result.runId}`);
console.log(` Total Events: ${result.stats.totalEvents}`);
console.log(` ✓ Matched: ${result.stats.matched}`);
console.log(` → Skipped: ${result.stats.skipped}`);
console.log(` ✗ Failed: ${result.stats.failed}`);
if (result.stats.failedEvents.length > 0) {
console.log('\n⚠️ Failed Events:');
for (const failed of result.stats.failedEvents) {
console.log(` - ${failed.invoiceId}: ${failed.error}`);
}
}
console.log('\n📊 Report saved. View with: npm run backfill:report', result.runId);
} catch (error: any) {
console.error('\n❌ Backfill failed:', error?.message || error);
process.exit(1);
} finally {
await app.close();
}
});
program
.command('cancel <runId>')
.description('Stop/cancel a running or pending backfill run')
.option('--operator <name>', 'Operator identifier (audited)')
.option('--note <text>', 'Reason for cancellation (audited)')
.action(async (runId, options) => {
try {
const result = await backfillService.cancelRun({
runId: parseInt(runId, 10),
operator: options.operator,
note: options.note,
});
console.log(`\n🛑 Run ${result.id} marked as ${result.status}`);
console.log(' Use `backfill checkpoint <runId>` to inspect last progress.');
} catch (error: any) {
console.error('Error cancelling run:', error?.message || error);
process.exit(1);
} finally {
await app.close();
}
});
program
.command('checkpoint <runId>')
.description('Inspect the latest checkpoint for a run')
.action(async (runId) => {
try {
const info = await backfillService.getLatestCheckpoint(parseInt(runId, 10));
console.log('\n📍 Checkpoint Status');
console.log('═══════════════════════════════════════');
console.log(` Run ID: ${info.runId}`);
console.log(` Status: ${info.status}`);
console.log(` Last ledger: ${info.lastCheckpointLedger ?? 'N/A'}`);
console.log(` Checkpoint at: ${info.lastCheckpointAt ?? 'N/A'}`);
console.log(` Cursor saved: ${info.lastCheckpointCursor ? 'yes' : 'no'}`);
if (info.checkpoint) {
console.log(`\n Last checkpoint range: ${info.checkpoint.startLedgerRange} → ${info.checkpoint.endLedgerRange}`);
console.log(` Events in batch: ${info.checkpoint.eventsInBatch}`);
console.log(` Events before batch:`);
console.log(` - Processed: ${info.checkpoint.eventsProcessedBefore}`);
console.log(` - Matched: ${info.checkpoint.eventsMatchedBefore}`);
console.log(` - Skipped: ${info.checkpoint.eventsSkippedBefore}`);
console.log(` - Failed: ${info.checkpoint.eventsFailedBefore}`);
if (info.checkpoint.lastEventId) {
console.log(` Last event id: ${info.checkpoint.lastEventId}`);
}
console.log(` Checkpoint saved: ${info.checkpoint.createdAt}`);
} else {
console.log('\n (no checkpoints saved yet)');
}
} catch (error: any) {
console.error('Error fetching checkpoint:', error?.message || error);
process.exit(1);
} finally {
await app.close();
}
});
program
.command('report <runId>')
.description('Get a backfill run report')
.action(async (runId) => {
try {
const report: any = await backfillService.getReport(parseInt(runId, 10));
console.log('\n📊 Backfill Report');
console.log('═══════════════════════════════════════');
console.log(` Run ID: ${report.id}`);
console.log(` Status: ${report.status}`);
console.log(` Contract: ${report.contractId}`);
console.log(` Started: ${report.startedAt}`);
console.log(` Completed: ${report.completedAt || 'N/A'}`);
console.log(` Range: ${report.startLedger} → ${report.endLedger}`);
console.log(` Last Checkpoint Ledger: ${report.lastCheckpointLedger ?? 'N/A'}`);
if (report.lastCheckpointAt) {
console.log(` Last Checkpoint At: ${report.lastCheckpointAt}`);
}
console.log(` Total: ${report.eventsProcessed}`);
console.log(` Matched: ${report.eventsMatched}`);
console.log(` Skipped: ${report.eventsSkipped}`);
console.log(` Failed: ${report.eventsFailed}`);
if (report.cancelledAt) {
console.log(` Cancelled: ${report.cancelledAt} (by ${report.cancelledBy ?? 'unknown'})`);
if (report.cancellationNote) {
console.log(` Cancellation note: ${report.cancellationNote}`);
}
}
if (report.parentRunId) {
console.log(` Resumed from run: ${report.parentRunId}`);
}
if (report.errorMessage) {
console.log(` Error: ${report.errorMessage}`);
}
if (report.checkpoints?.length) {
console.log(`\n Recent checkpoints (${report.checkpoints.length}):`);
for (const cp of report.checkpoints) {
console.log(` - ledger ${Number(cp.checkpointLedger)} @ ${cp.createdAt} (${cp.eventsInBatch} evts)`);
}
}
} catch (error: any) {
console.error('Error fetching report:', error?.message || error);
process.exit(1);
} finally {
await app.close();
}
});
program
.command('stats')
.description('Get processed events statistics')
.action(async () => {
try {
const stats = await backfillService.getStats();
console.log('\n📊 Processed Events Statistics');
console.log('═══════════════════════════════════════');
console.log(` Total: ${stats.total}`);
console.log(` Success: ${stats.success}`);
console.log(` Skipped: ${stats.skipped}`);
console.log(` Failed: ${stats.failed}`);
console.log(` Last Processed Ledger: ${stats.lastProcessedLedger || 'N/A'}`);
} catch (error: any) {
console.error('Error fetching stats:', error?.message || error);
process.exit(1);
} finally {
await app.close();
}
});
program
.command('history [limit]')
.description('Show recent backfill runs')
.action(async (limit) => {
try {
const rows = await backfillService.getHistory(
limit ? parseInt(limit, 10) : undefined,
);
console.log('\n🕓 Backfill History');
console.log('═══════════════════════════════════════');
for (const r of rows as any[]) {
const statusCol = String(r.status).padEnd(10);
const range = `${String(r.startLedger).padStart(8)} → ${String(r.endLedger).padStart(8)}`;
console.log(` #${String(r.id).padStart(4)} ${statusCol} ${range} ${r.startedAt}`);
}
} catch (error: any) {
console.error('Error fetching history:', error?.message || error);
process.exit(1);
} finally {
await app.close();
}
});
await program.parseAsync(process.argv);
}
bootstrap();