-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Expand file tree
/
Copy pathcancellation.ts
More file actions
66 lines (57 loc) · 1.84 KB
/
cancellation.ts
File metadata and controls
66 lines (57 loc) · 1.84 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
import { getRedisClient } from '@/lib/core/config/redis'
import { createLogger } from '@/lib/logs/console/logger'
const logger = createLogger('ExecutionCancellation')
const EXECUTION_CANCEL_PREFIX = 'execution:cancel:'
const EXECUTION_CANCEL_EXPIRY = 60 * 60
export function isRedisCancellationEnabled(): boolean {
return getRedisClient() !== null
}
/**
* Mark an execution as cancelled in Redis.
* Returns true if Redis is available and the flag was set, false otherwise.
*/
export async function markExecutionCancelled(executionId: string): Promise<boolean> {
const redis = getRedisClient()
if (!redis) {
return false
}
try {
await redis.set(`${EXECUTION_CANCEL_PREFIX}${executionId}`, '1', 'EX', EXECUTION_CANCEL_EXPIRY)
logger.info('Marked execution as cancelled', { executionId })
return true
} catch (error) {
logger.error('Failed to mark execution as cancelled', { executionId, error })
return false
}
}
/**
* Check if an execution has been cancelled via Redis.
* Returns false if Redis is not available (fallback to local abort signal).
*/
export async function isExecutionCancelled(executionId: string): Promise<boolean> {
const redis = getRedisClient()
if (!redis) {
return false
}
try {
const result = await redis.exists(`${EXECUTION_CANCEL_PREFIX}${executionId}`)
return result === 1
} catch (error) {
logger.error('Failed to check execution cancellation', { executionId, error })
return false
}
}
/**
* Clear the cancellation flag for an execution.
*/
export async function clearExecutionCancellation(executionId: string): Promise<void> {
const redis = getRedisClient()
if (!redis) {
return
}
try {
await redis.del(`${EXECUTION_CANCEL_PREFIX}${executionId}`)
} catch (error) {
logger.error('Failed to clear execution cancellation', { executionId, error })
}
}