forked from Xeio/IdleCodeRedeemer
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbackfillManager.ts
More file actions
143 lines (123 loc) · 4.16 KB
/
backfillManager.ts
File metadata and controls
143 lines (123 loc) · 4.16 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
import { and, eq, sql } from 'drizzle-orm';
import { db } from './db';
import { backfillOperations, type BackfillOperation } from './schema/index';
class BackfillManager {
// Global backfill lock to prevent concurrent operations
private backfillInProgress = false;
isBackfillInProgress(): boolean {
return this.backfillInProgress;
}
async startBackfill(discordId: string): Promise<number> {
if (this.backfillInProgress) {
throw new Error(
'A backfill operation is already in progress. Please wait for it to complete.'
);
}
this.backfillInProgress = true;
try {
const result = db
.insert(backfillOperations)
.values({ initiatedBy: discordId, status: 'in_progress' })
.returning({ id: backfillOperations.id })
.get();
return result?.id ?? 0;
} catch (err) {
this.backfillInProgress = false;
throw err;
}
}
async canUserInitiateBackfill(discordId: string): Promise<boolean> {
const lastBackfill = db
.select({ completedAt: backfillOperations.completedAt, startedAt: backfillOperations.startedAt })
.from(backfillOperations)
.where(and(eq(backfillOperations.initiatedBy, discordId), eq(backfillOperations.status, 'completed')))
.orderBy(sql`${backfillOperations.completedAt} DESC`)
.limit(1)
.get();
if (!lastBackfill) return true;
const timestamp = lastBackfill.completedAt ?? lastBackfill.startedAt;
if (!timestamp) return true;
const lastTime = new Date(timestamp).getTime();
if (isNaN(lastTime)) return true;
const now = Date.now();
return now - lastTime >= 60 * 60 * 1000;
}
async getLastBackfill(): Promise<BackfillOperation | undefined> {
return db
.select()
.from(backfillOperations)
.where(eq(backfillOperations.status, 'completed'))
.orderBy(sql`${backfillOperations.completedAt} DESC`)
.limit(1)
.get() as BackfillOperation | undefined;
}
async shouldRunStartupBackfill(): Promise<boolean> {
const lastBackfill = await this.getLastBackfill();
if (!lastBackfill) return true;
const timestamp = lastBackfill.completedAt ?? lastBackfill.startedAt;
if (!timestamp) return true;
const lastTime = new Date(timestamp).getTime();
if (isNaN(lastTime)) return true;
return Date.now() - lastTime >= 6 * 60 * 60 * 1000;
}
async updateBackfill(
operationId: number,
codesFound: number,
codesRedeemed: number,
status: 'completed' | 'failed'
): Promise<void> {
try {
db.update(backfillOperations)
.set({ codesFound, codesRedeemed, status, completedAt: sql`CURRENT_TIMESTAMP` })
.where(eq(backfillOperations.id, operationId))
.run();
} finally {
this.backfillInProgress = false;
}
}
async getBackfillById(operationId: number): Promise<BackfillOperation | undefined> {
return db
.select()
.from(backfillOperations)
.where(eq(backfillOperations.id, operationId))
.get() as BackfillOperation | undefined;
}
async hasUserBackfillOperations(discordId: string): Promise<boolean> {
const row = db
.select({ id: backfillOperations.id })
.from(backfillOperations)
.where(eq(backfillOperations.initiatedBy, discordId))
.limit(1)
.get();
return row !== undefined;
}
async hasUserActiveBackfill(discordId: string): Promise<boolean> {
const row = db
.select({ id: backfillOperations.id })
.from(backfillOperations)
.where(
and(
eq(backfillOperations.initiatedBy, discordId),
eq(backfillOperations.status, 'in_progress')
)
)
.limit(1)
.get();
return row !== undefined;
}
async deleteUserBackfillOperations(discordId: string): Promise<number> {
const rows = db
.select({ id: backfillOperations.id })
.from(backfillOperations)
.where(eq(backfillOperations.initiatedBy, discordId))
.all();
const total = rows.length;
if (total > 0) {
db.delete(backfillOperations)
.where(eq(backfillOperations.initiatedBy, discordId))
.run();
}
return total;
}
}
export const backfillManager = new BackfillManager();