/** * Job Management Types * Defines interfaces for job state management, retry strategies, and job operations */ import { IActionRequest, INotificationRequest, IStorageRequest, IPublishRequest } from '../types/processor.types'; /** * Job status throughout its lifecycle */ export declare enum JobStatus { SCHEDULED = "scheduled", QUEUED = "queued", RUNNING = "running", COMPLETED = "completed", FAILED = "failed", CANCELLED = "cancelled", PAUSED = "paused" } /** * Job types that can be scheduled */ export declare enum JobType { ACTION = "action", NOTIFICATION = "notification", DATABASE_ACTION = "database_action", DATABASE_OPERATION = "database_operation", GRAPH_ACTION = "graph_action", GRAPH_OPERATION = "graph_operation", WORKFLOW = "workflow", STORAGE = "storage", PUBLISH = "publish" } /** * Retry configuration for jobs */ export interface IRetryConfig { /** Initial delay in milliseconds before first retry (default: 1000) */ initialDelay?: number; /** Maximum delay in milliseconds between retries (default: 300000 - 5 minutes) */ maxDelay?: number; /** Multiplier for exponential backoff (default: 2) */ backoffMultiplier?: number; /** Only retry on these error types */ retryableErrors?: string[]; /** Never retry on these error types */ nonRetryableErrors?: string[]; /** Add jitter to prevent thundering herd */ jitter?: boolean; /** Jitter percentage (0-1, default: 0.3) */ jitterPercent?: number; } /** * Schedule configuration for jobs */ export interface IJobSchedule { /** When to start the job (Unix timestamp in ms or ISO date string) */ start_at?: number | string; /** Cron expression for recurring jobs */ cron?: string; /** Interval in milliseconds for recurring jobs */ every?: number; /** Maximum number of times to run (for recurring jobs) */ limit?: number; /** Stop recurring after this date */ endDate?: number | string; /** Timezone for cron expressions */ tz?: string; } /** * Job execution record */ export interface IJobExecution { /** Execution number (1-indexed) */ number: number; /** When execution started */ started_at: number; /** When execution completed */ completed_at?: number; /** Duration in milliseconds */ duration_ms?: number; /** Execution status */ status: 'completed' | 'failed'; /** Error message if failed */ error?: string; /** Error code if failed */ error_code?: string; /** Result data if completed */ result?: Record; } /** * Complete job record stored in Redis */ export interface IJob { /** Unique job ID */ id: string; /** Current job status */ status: JobStatus; /** Job type (action, notification, etc.) */ type: JobType; /** Namespace that created this job */ namespace: string; /** Product tag */ product: string; /** Environment slug */ env: string; /** Job event/tag identifier */ event: string; /** App/database/graph tag (depending on type) */ app?: string; /** Scheduled start time */ scheduled_at: number; /** When job started executing */ started_at?: number; /** When job completed */ completed_at?: number; /** Whether this is a recurring job */ recurring: boolean; /** Cron expression (if recurring) */ cron?: string; /** Interval in ms (if recurring) */ every?: number; /** Next scheduled run time */ next_run_at?: number; /** Total execution count */ execution_count: number; /** Maximum executions limit */ limit?: number; /** End date for recurring job */ end_date?: number; /** Timezone for cron */ tz?: string; /** Maximum retry attempts */ retries: number; /** Current retry count */ retry_count: number; /** Retry configuration */ retry_config?: IRetryConfig; /** Last error message */ last_error?: string; /** Last error code */ last_error_code?: string; /** Job input data */ input: Record; /** Job result (if completed) */ result?: Record; /** Session info */ session?: { tag: string; token: string; }; /** Cache tag */ cache?: string; /** Workspace ID */ workspace_id: string; /** Created timestamp */ created_at: number; /** Last updated timestamp */ updated_at: number; /** Cancellation reason (if cancelled) */ cancel_reason?: string; } /** * Job execution history */ export interface IJobHistory { /** Job ID */ job_id: string; /** Total executions */ total_executions: number; /** Successful executions */ successful_executions: number; /** Failed executions */ failed_executions: number; /** Execution records */ executions: IJobExecution[]; } /** * Options for listing jobs */ export interface IJobListOptions { /** Filter by status */ status?: JobStatus | JobStatus[]; /** Filter by recurring */ recurring?: boolean; /** Filter by product */ product?: string; /** Filter by environment */ env?: string; /** Filter by job type/namespace */ namespace?: string; /** Maximum results */ limit?: number; /** Offset for pagination */ offset?: number; /** Start date filter (Unix timestamp or ISO string) */ from?: number | string; /** End date filter (Unix timestamp or ISO string) */ to?: number | string; } /** * Job list result */ export interface IJobListResult { /** List of jobs */ jobs: IJob[]; /** Total count (before pagination) */ total: number; /** Applied limit */ limit: number; /** Applied offset */ offset: number; } /** * Job statistics */ export interface IJobStats { /** Total jobs */ total: number; /** Scheduled jobs */ scheduled: number; /** Queued jobs */ queued: number; /** Running jobs */ running: number; /** Completed jobs */ completed: number; /** Failed jobs */ failed: number; /** Cancelled jobs */ cancelled: number; /** Paused jobs */ paused: number; /** Jobs completed on first try */ completed_first_try: number; /** Jobs completed after retry */ completed_after_retry: number; /** Success rate (0-1) */ success_rate: number; /** Average retry count for completed jobs */ avg_retry_count: number; } /** * Result of dispatching a job */ export interface IDispatchResult { /** Unique job ID for tracking */ job_id: string; /** Job status */ status: JobStatus.SCHEDULED | JobStatus.QUEUED; /** Scheduled start time */ scheduled_at: number; /** Whether this is a recurring job */ recurring: boolean; /** Next run time for recurring jobs */ next_run_at?: number; } /** * Webhook configuration for job events */ export interface IJobWebhookConfig { /** Webhook URL */ url: string; /** Events to trigger webhook */ events: JobWebhookEvent[]; /** Secret for signing payloads */ secret?: string; /** Additional headers */ headers?: Record; } /** * Job webhook events */ export type JobWebhookEvent = 'job.scheduled' | 'job.started' | 'job.completed' | 'job.failed' | 'job.cancelled' | 'job.paused' | 'job.resumed' | 'job.retrying'; /** * Webhook payload */ export interface IJobWebhookPayload { /** Event type */ event: JobWebhookEvent; /** Event timestamp */ timestamp: number; /** Job data */ job: { id: string; status: JobStatus; namespace: string; product: string; env: string; event: string; scheduled_at: number; execution_count?: number; error?: string; error_code?: string; result?: Record; }; } /** * Options for retrying a job */ export interface IRetryOptions { /** Delay before retry in milliseconds */ delay?: number; /** Override retry configuration */ retryConfig?: IRetryConfig; } /** * Options for cancelling a job */ export interface ICancelOptions { /** Reason for cancellation */ reason?: string; } /** * Options for rescheduling a job */ export interface IRescheduleOptions { /** New start time */ start_at?: number | string; /** New cron expression (for recurring jobs) */ cron?: string; /** New interval (for recurring jobs) */ every?: number; /** New timezone */ tz?: string; } /** * Base dispatch input with common options */ export interface IJobDispatchOptions { /** Number of retries on failure */ retries?: number; /** Retry configuration */ retryConfig?: IRetryConfig; /** Schedule configuration */ schedule?: IJobSchedule; /** Session info */ session?: { tag: string; token: string; }; /** Cache tag */ cache?: string; } /** * Action dispatch input */ export interface IActionJobDispatch extends IJobDispatchOptions { env: string; product: string; app: string; event: string; input: IActionRequest; } /** * Notification dispatch input */ export interface INotificationJobDispatch extends IJobDispatchOptions { env: string; product: string; notification: string; event: string; input: INotificationRequest; } /** * Database operation dispatch input */ export interface IDatabaseOperationJobDispatch extends IJobDispatchOptions { env: string; product: string; database: string; operation: string; input: Record; } /** * Feature dispatch input */ export interface IFeatureJobDispatch extends IJobDispatchOptions { env: string; product: string; feature: string; input: Record; } /** * Storage dispatch input */ export interface IStorageJobDispatch extends IJobDispatchOptions { env: string; product: string; storage: string; event: string; input: IStorageRequest; } /** * Publish dispatch input */ export interface IPublishJobDispatch extends IJobDispatchOptions { env: string; product: string; broker: string; event: string; input: IPublishRequest; } /** * Graph operation dispatch input */ export interface IGraphOperationJobDispatch extends IJobDispatchOptions { env: string; product: string; graph: string; operation: string; input: Record; } /** * Redis key prefixes for job storage */ export declare const JOB_REDIS_KEYS: { /** Job data: job:{workspace_id}:{job_id} */ readonly JOB: "job"; /** Job index by status: job_status:{workspace_id}:{status} */ readonly JOB_STATUS: "job_status"; /** Job index by product: job_product:{workspace_id}:{product} */ readonly JOB_PRODUCT: "job_product"; /** Job execution history: job_history:{workspace_id}:{job_id} */ readonly JOB_HISTORY: "job_history"; /** Webhook config: job_webhook:{workspace_id} */ readonly JOB_WEBHOOK: "job_webhook"; /** Job stats: job_stats:{workspace_id}:{product} */ readonly JOB_STATS: "job_stats"; }; /** * Default job configuration */ export declare const JOB_DEFAULTS: { /** Default max retries */ readonly MAX_RETRIES: 0; /** Default initial retry delay (1 second) */ readonly INITIAL_DELAY: 1000; /** Default max retry delay (5 minutes) */ readonly MAX_DELAY: 300000; /** Default backoff multiplier */ readonly BACKOFF_MULTIPLIER: 2; /** Default jitter percentage */ readonly JITTER_PERCENT: 0.3; /** Job data TTL in seconds (90 days) */ readonly JOB_TTL: number; /** Job history TTL in seconds (30 days) */ readonly HISTORY_TTL: number; /** Max history entries per job */ readonly MAX_HISTORY_ENTRIES: 100; };