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

2.0 KB
Raw Blame
import { FastifyInstance } from 'fastify'
import { FastifyPluginAsyncZod } from 'fastify-type-provider-zod'
import { runsMetadataQueue } from '../flows/flow-run/flow-runs-queue'
import { pubsub } from '../helper/pubsub'
import { system } from '../helper/system/system'
import { AppSystemProp } from '../helper/system/system-props'
import { barrierQueue } from '../waitpoints/barrier-queue'
import { flowEngineWorker } from './engine-controller'
import { setupBullMQBoard } from './job-queue/bullboard'
import { jobBroker } from './job-queue/job-broker'
import { jobQueue } from './job-queue/job-queue'
import { workerMachineController } from './machine/machine-controller'
import { queueMigration } from './migrations/queue-migration-runner'
import { workerGroupController } from './worker-groups/worker-group.controller'
export const workerModule: FastifyPluginAsyncZod = async (app) => {
    await app.register(flowEngineWorker, {
        prefix: '/v1/engine',
    })
    await app.register(workerMachineController, {
        prefix: '/v1/worker-machines',
    })
    await app.register(workerGroupController, {
        prefix: '/v1/worker-groups',
    })
    await jobQueue(app.log).init()

    await runsMetadataQueue(app.log).init()

    await barrierQueue(app.log).init()

    await setupBullMQBoard(app)

    app.addHook('onClose', async () => {
        await jobBroker(app.log).close()
        await runsMetadataQueue(app.log).close()
        await barrierQueue(app.log).close()
        await jobQueue(app.log).close()
        await pubsub.close()
    })
}


// This should be called after the app is booted, to ensure no plugin timeout
export const migrateQueuesAndRunConsumers = async (app: FastifyInstance) => {
    const isCanaryApp = system.getBoolean(AppSystemProp.IS_CANARY_APP) ?? false
    if (isCanaryApp) {
        app.log.info('[migrateQueuesAndRunConsumers] Skipping queue migration on canary app instance')
    }
    else {
        await queueMigration(app.log).run()
    }
    await jobBroker(app.log).init()
}