forked from InsurNiffy/niff-Stellar-shurance
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin.service.ts
More file actions
56 lines (51 loc) · 1.9 KB
/
Copy pathadmin.service.ts
File metadata and controls
56 lines (51 loc) · 1.9 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
import { Injectable, Logger } from '@nestjs/common';
import { PrismaService } from '../prisma/prisma.service';
import { Queue } from 'bullmq';
import { getBullMQConnection } from '../redis/client';
@Injectable()
export class AdminService {
private readonly logger = new Logger(AdminService.name);
private reindexQueue: Queue;
constructor(private readonly prisma: PrismaService) {
this.reindexQueue = new Queue('reindex', {
connection: getBullMQConnection(),
defaultJobOptions: {
attempts: 3,
backoff: { type: 'exponential', delay: 2_000 },
removeOnComplete: { count: 50 },
removeOnFail: { count: 100 },
},
});
}
/**
* Reset per-network cursor so the next indexer pass starts at `fromLedger`,
* then enqueue a BullMQ job to drive catch-up (see ReindexWorkerService).
*/
async enqueueReindex(fromLedger: number, network: string): Promise<string> {
const lastProcessed = Math.max(0, fromLedger - 1);
await this.prisma.$transaction(async (tx) => {
await tx.ledgerCursor.upsert({
where: { network },
create: { network, lastProcessedLedger: lastProcessed },
update: { lastProcessedLedger: lastProcessed },
});
});
const job = await this.reindexQueue.add(
'reindex',
{ fromLedger, network },
{ jobId: `reindex-${network}-${fromLedger}-${Date.now()}` },
);
this.logger.log(`Reindex job enqueued: ${job.id} network=${network} fromLedger=${fromLedger}`);
return job.id!;
}
async setFeatureFlag(key: string, enabled: boolean, description: string | undefined, actor: string) {
return this.prisma.featureFlag.upsert({
where: { key },
create: { key, enabled, description, updatedBy: actor },
update: { enabled, description, updatedBy: actor },
});
}
async getFeatureFlags() {
return this.prisma.featureFlag.findMany({ orderBy: { key: 'asc' } });
}
}