-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Expand file tree
/
Copy pathcancellation.ts
More file actions
50 lines (45 loc) · 1.21 KB
/
cancellation.ts
File metadata and controls
50 lines (45 loc) · 1.21 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
import { getRedisClient } from '@/lib/core/config/redis'
const KEY_PREFIX = 'execution:cancel:'
const TTL_SECONDS = 300
const TTL_MS = TTL_SECONDS * 1000
const memoryStore = new Map<string, number>()
export async function requestCancellation(executionId: string): Promise<boolean> {
const redis = getRedisClient()
if (redis) {
try {
await redis.set(`${KEY_PREFIX}${executionId}`, '1', 'EX', TTL_SECONDS)
return true
} catch {
return false
}
}
memoryStore.set(executionId, Date.now() + TTL_MS)
return true
}
export async function isCancellationRequested(executionId: string): Promise<boolean> {
const redis = getRedisClient()
if (redis) {
try {
return (await redis.exists(`${KEY_PREFIX}${executionId}`)) === 1
} catch {
return false
}
}
const expiry = memoryStore.get(executionId)
if (!expiry) return false
if (Date.now() > expiry) {
memoryStore.delete(executionId)
return false
}
return true
}
export async function clearCancellation(executionId: string): Promise<void> {
const redis = getRedisClient()
if (redis) {
try {
await redis.del(`${KEY_PREFIX}${executionId}`)
} catch {}
return
}
memoryStore.delete(executionId)
}