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

3.3 KB
Raw Blame
import { isNil } from '@activepieces/core-utils'
import { ApEnvironment, WorkerGroupScope } from '@activepieces/shared'
import dayjs from 'dayjs'
import utc from 'dayjs/plugin/utc'
import { pubsub } from '../../helper/pubsub'
import { system } from '../../helper/system/system'
import { AppSystemProp } from '../../helper/system/system-props'
import { workerMachineCache } from './machine-cache'

dayjs.extend(utc)

const isTestingEnvironment = system.get(AppSystemProp.ENVIRONMENT) === ApEnvironment.TESTING

export function parseWorkerConcurrency(value: string | undefined): number {
    const parsed = Number(value)
    return Number.isInteger(parsed) && parsed > 0 ? parsed : 1
}

// In-memory live-capacity ( aka sum of WORKER_CONCURRENCY in a group) view of the machine cache, read by the
// rate limiter on dispatch. Cached without a TTL and invalidated only when the set of
// workers changes — a new worker connects or a worker disconnects (see machine-service
// onConnection/onDisconnect). Ordinary heartbeats from known workers don't change capacity.
let capacitySnapshot: WorkerCapacitySnapshot | null = null


export const workerCapacity = {
    // Subscribe to cross-instance invalidations. The snapshot is a per-process memo of the Redis-backed
    // machine cache; a worker connect/disconnect is only observed by the one API instance holding that
    // socket, so it broadcasts here and every other instance drops its memo (else it routes/rate-limits
    // against a worker set that's stale on this box until it happens to handle its own worker event).
    async setup(): Promise<void> {
        if (isTestingEnvironment) {
            return
        }
        await pubsub.subscribe(WORKER_CAPACITY_INVALIDATION_CHANNEL, () => {
            capacitySnapshot = null
        })
    },
    async get(): Promise<WorkerCapacitySnapshot> {
        if (!isNil(capacitySnapshot)) {
            return capacitySnapshot
        }
        const allWorkers = await workerMachineCache().find()
        const offlineThreshold = dayjs().subtract(60, 'seconds').utc()
        const projectGroups = new Map<string, PoolCapacity>()
        const shared: PoolCapacity = { slots: 0 }
        for (const worker of allWorkers) {
            if (!dayjs(worker.updated).isAfter(offlineThreshold)) {
                continue
            }
            const slots = parseWorkerConcurrency(worker.information.workerProps.WORKER_CONCURRENCY)
            if (worker.workerGroupScope === WorkerGroupScope.PROJECT && !isNil(worker.workerGroupId) && worker.workerGroupId.length > 0) {
                const current = projectGroups.get(worker.workerGroupId) ?? { slots: 0 }
                projectGroups.set(worker.workerGroupId, { slots: current.slots + slots })
            }
            else if (isNil(worker.workerGroupScope)) {
                shared.slots += slots
            }
        }
        capacitySnapshot = { projectGroups, shared }
        return capacitySnapshot
    },
    async invalidate(): Promise<void> {
        capacitySnapshot = null
        if (!isTestingEnvironment) {
            await pubsub.publish(WORKER_CAPACITY_INVALIDATION_CHANNEL, '1')
        }
    },
}

export const WORKER_CAPACITY_INVALIDATION_CHANNEL = 'worker-capacity-invalidation'

export type PoolCapacity = {
    slots: number
}

export type WorkerCapacitySnapshot = {
    projectGroups: Map<string, PoolCapacity>
    shared: PoolCapacity
}