@mondaydotcomorg/atp-compiler
Version:
Production-ready compiler for transforming async iteration patterns into resumable operations with checkpoint-based state management
81 lines • 2.83 kB
JavaScript
import { CheckpointError, CheckpointOperation } from './errors.js';
const MAX_CHECKPOINT_SIZE = 10 * 1024 * 1024;
const CHECKPOINT_TTL = 3600;
export class CheckpointManager {
cache;
executionId;
prefix;
constructor(executionId, cache, prefix = 'checkpoint') {
this.executionId = executionId;
this.cache = cache;
this.prefix = prefix;
}
async save(checkpoint) {
const key = this.getKey(checkpoint.loopId);
try {
const serialized = JSON.stringify(checkpoint);
if (serialized.length > MAX_CHECKPOINT_SIZE) {
throw new CheckpointError(`Checkpoint size ${serialized.length} exceeds maximum ${MAX_CHECKPOINT_SIZE}`, checkpoint.loopId, CheckpointOperation.SAVE);
}
await this.cache.set(key, checkpoint, CHECKPOINT_TTL);
}
catch (error) {
if (error instanceof CheckpointError) {
throw error;
}
const message = error instanceof Error ? error.message : String(error);
throw new CheckpointError(message, checkpoint.loopId, CheckpointOperation.SAVE);
}
}
async load(loopId) {
const key = this.getKey(loopId);
try {
const checkpoint = await this.cache.get(key);
if (!checkpoint) {
return null;
}
if (checkpoint.completed && checkpoint.completed instanceof Array) {
checkpoint.completed = new Set(checkpoint.completed);
}
return checkpoint;
}
catch (error) {
const message = error instanceof Error ? error.message : String(error);
throw new CheckpointError(message, loopId, CheckpointOperation.LOAD);
}
}
async clear(loopId) {
const key = this.getKey(loopId);
try {
await this.cache.delete(key);
}
catch (error) {
const message = error instanceof Error ? error.message : String(error);
throw new CheckpointError(message, loopId, CheckpointOperation.CLEAR);
}
}
async clearAll() { }
getKey(loopId) {
return `${this.prefix}:${this.executionId}:${loopId}`;
}
getExecutionId() {
return this.executionId;
}
}
let globalCheckpointManager = null;
export function setCheckpointManager(manager) {
globalCheckpointManager = manager;
}
export function getCheckpointManager() {
if (!globalCheckpointManager) {
throw new Error('CheckpointManager not initialized');
}
return globalCheckpointManager;
}
export function clearCheckpointManager() {
globalCheckpointManager = null;
}
export function hasCheckpointManager() {
return globalCheckpointManager !== null;
}
//# sourceMappingURL=checkpoint-manager.js.map