Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

4.5 KB
Raw Blame
import { FlowId, FlowRunId, PlatformId, ProjectId } from '@activepieces/core-utils'
import { Flow } from '@activepieces/shared'
import { Job, JobsOptions, JobState } from 'bullmq'
import { Dayjs } from 'dayjs'

export enum SystemJobName {
    PIECES_ANALYTICS = 'pieces-analytics',
    PIECES_SYNC = 'pieces-sync',
    FILE_CLEANUP_TRIGGER = 'file-cleanup-trigger',
    RUN_TELEMETRY = 'run-telemetry',
    DELETE_FLOW = 'delete-flow',
    HARD_DELETE_PROJECT = 'hard-delete-project',
    CREATE_MISSING_PERSONAL_PROJECTS = 'create-missing-personal-projects',
    HARD_DELETE_PLATFORM = 'hard-delete-platform',
    BILLING_USAGE_REPORT = 'billing-usage-report',
    RESUME_DELAY_WAITPOINT = 'resume-delay-waitpoint',
    TOOL_SEARCH_REINDEX = 'tool-search-reindex',
    CHAT_STALE_SWEEP = 'chat-stale-sweep',
    AI_USAGE_COPY = 'ai-usage-copy',
    AI_USAGE_RETENTION = 'ai-usage-retention',
    WAITPOINT_DEADLINE_SWEEP = 'waitpoint-deadline-sweep',
    RUN_BACKGROUND_MIGRATIONS = 'run-background-migrations',
    AUDIT_LOG_RETENTION = 'audit-log-retention',
}

type DeleteFlowDurableSystemJobData =  {
    flow: Flow
    preDeleteDone: boolean
}

type HardDeleteProjectSystemJobData = {
    projectId: ProjectId
    platformId: PlatformId
    preDeletedFlowIds: FlowId[]
}

type CreateMissingPersonalProjectsSystemJobData = {
    platformId: PlatformId
    runId: string
    afterUserId?: string
    onlyUserIds?: string[]
    failedUserIds?: string[]
}

type HardDeletePlatformSystemJobData = {
    platformId: PlatformId
}

type ResumeDelayWaitpointSystemJobData = {
    flowRunId: FlowRunId
    projectId: ProjectId
    waitpointId: string
}

// Scope shape kept inline (structurally equal to tool-search's ReindexScope) so this generic
// job framework does not depend on the tool-search feature module.
type ToolSearchReindexSystemJobData = {
    scope: { type: 'all' } | { type: 'platform', platformId: PlatformId }
}

type SystemJobDataMap = {
    [SystemJobName.PIECES_ANALYTICS]: Record<string, never>
    [SystemJobName.PIECES_SYNC]: Record<string, never>
    [SystemJobName.FILE_CLEANUP_TRIGGER]: Record<string, never>
    [SystemJobName.RUN_TELEMETRY]: Record<string, never>
    [SystemJobName.DELETE_FLOW]: DeleteFlowDurableSystemJobData
    [SystemJobName.HARD_DELETE_PROJECT]: HardDeleteProjectSystemJobData
    [SystemJobName.CREATE_MISSING_PERSONAL_PROJECTS]: CreateMissingPersonalProjectsSystemJobData
    [SystemJobName.HARD_DELETE_PLATFORM]: HardDeletePlatformSystemJobData
    [SystemJobName.BILLING_USAGE_REPORT]: Record<string, never>
    [SystemJobName.RESUME_DELAY_WAITPOINT]: ResumeDelayWaitpointSystemJobData
    [SystemJobName.TOOL_SEARCH_REINDEX]: ToolSearchReindexSystemJobData
    [SystemJobName.CHAT_STALE_SWEEP]: Record<string, never>
    [SystemJobName.AI_USAGE_COPY]: Record<string, never>
    [SystemJobName.AI_USAGE_RETENTION]: Record<string, never>
    [SystemJobName.WAITPOINT_DEADLINE_SWEEP]: Record<string, never>
    [SystemJobName.RUN_BACKGROUND_MIGRATIONS]: Record<string, never>
    [SystemJobName.AUDIT_LOG_RETENTION]: Record<string, never>
}

export type SystemJobData<T extends SystemJobName = SystemJobName> = T extends SystemJobName ? SystemJobDataMap[T] : never

export type SystemJobDefinition<T extends SystemJobName> = {
    name: T
    data: SystemJobData<T>
    jobId: string
}

export type SystemJobHandler<T extends SystemJobName = SystemJobName> = (data: SystemJobData<T>) => Promise<void>

type OneTimeJobSchedule = {
    type: 'one-time'
    date: Dayjs
}

type RepeatedJobSchedule = {
    type: 'repeated'
    cron: string
}

export type JobSchedule = OneTimeJobSchedule | RepeatedJobSchedule

type UpsertJobParams<T extends SystemJobName> = {
    job: SystemJobDefinition<T>
    schedule: JobSchedule
    customConfig?: JobsOptions
}

export type SystemJobSchedule = {
    init(): Promise<void>
    startWorker(): Promise<void>
    upsertJob<T extends SystemJobName>(params: UpsertJobParams<T>): Promise<UpsertJobResult>
    getJob<T extends SystemJobName>(jobId: string): Promise<Job<SystemJobData<T>> | undefined>
    getJobState(jobId: string): Promise<JobState | 'unknown'>
    removeJob(params: { jobId: string }): Promise<void>
    close(): Promise<void>
}

export const systemJobIds = {
    resumeDelay: ({ waitpointId }: { waitpointId: string }): string => `resume-delay-${waitpointId}`,
    legacyResumeDelay: ({ flowRunId }: { flowRunId: FlowRunId }): string => `resume-delay-${flowRunId}`,
}

export type UpsertJobResult = {
    status: 'added' | 'kept' | 'retried' | 'scheduler-upserted'
}