d25d482dc2
Publish CLI Package / publish-npm (push) Waiting to run
Publish Python SDK / publish-pypi (push) Waiting to run
Publish TypeScript SDK / publish-npm (push) Waiting to run
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
91 lines
2.8 KiB
TypeScript
91 lines
2.8 KiB
TypeScript
import { createLogger } from '@sim/logger'
|
|
import { getRedisClient } from '@/lib/core/config/redis'
|
|
import { createPubSubChannel, type PubSubChannel } from '@/lib/events/pubsub'
|
|
|
|
const logger = createLogger('ExecutionCancellation')
|
|
|
|
const EXECUTION_CANCEL_PREFIX = 'execution:cancel:'
|
|
const EXECUTION_CANCEL_EXPIRY = 60 * 60
|
|
const EXECUTION_CANCEL_CHANNEL = 'execution:cancel'
|
|
|
|
export interface ExecutionCancelEvent {
|
|
executionId: string
|
|
}
|
|
|
|
export type ExecutionCancellationRecordResult =
|
|
| { durablyRecorded: true; reason: 'recorded' }
|
|
| {
|
|
durablyRecorded: false
|
|
reason: 'redis_unavailable' | 'redis_write_failed'
|
|
}
|
|
|
|
type CancellationGlobal = typeof globalThis & {
|
|
_executionCancelChannel?: PubSubChannel<ExecutionCancelEvent>
|
|
}
|
|
|
|
const _g = globalThis as CancellationGlobal
|
|
|
|
export function getCancellationChannel(): PubSubChannel<ExecutionCancelEvent> {
|
|
if (!_g._executionCancelChannel) {
|
|
_g._executionCancelChannel = createPubSubChannel<ExecutionCancelEvent>({
|
|
channel: EXECUTION_CANCEL_CHANNEL,
|
|
label: 'execution-cancel',
|
|
})
|
|
}
|
|
return _g._executionCancelChannel
|
|
}
|
|
|
|
export function isRedisCancellationEnabled(): boolean {
|
|
return getRedisClient() !== null
|
|
}
|
|
|
|
/** Writes the durable key first, then publishes — so a late subscriber still sees the flag on backstop check. */
|
|
export async function markExecutionCancelled(
|
|
executionId: string
|
|
): Promise<ExecutionCancellationRecordResult> {
|
|
const redis = getRedisClient()
|
|
if (!redis) {
|
|
getCancellationChannel().publish({ executionId })
|
|
return { durablyRecorded: false, reason: 'redis_unavailable' }
|
|
}
|
|
|
|
try {
|
|
await redis.set(`${EXECUTION_CANCEL_PREFIX}${executionId}`, '1', 'EX', EXECUTION_CANCEL_EXPIRY)
|
|
logger.info('Marked execution as cancelled', { executionId })
|
|
getCancellationChannel().publish({ executionId })
|
|
return { durablyRecorded: true, reason: 'recorded' }
|
|
} catch (error) {
|
|
logger.error('Failed to mark execution as cancelled', { executionId, error })
|
|
getCancellationChannel().publish({ executionId })
|
|
return { durablyRecorded: false, reason: 'redis_write_failed' }
|
|
}
|
|
}
|
|
|
|
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
|
|
}
|
|
}
|
|
|
|
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 })
|
|
}
|
|
}
|