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

1.1 KB
Raw Blame

import { isNil } from '@activepieces/core-utils'
import dayjs from 'dayjs'
import { FastifyBaseLogger } from 'fastify'
import { redisHelper } from '../../database/redis'
import { redisConnections } from '../../database/redis-connections'

const DELETE_STALE_RUN_METADATA_KEY = 'delete_stale_run_metadata'

export const deleteStaleRunMetadata = (log: FastifyBaseLogger) => ({
    async run(): Promise<void> {
        const redisConnection = await redisConnections.useExisting()
        const isMigrated = await redisConnection.get(DELETE_STALE_RUN_METADATA_KEY)
        if (!isNil(isMigrated)) {
            log.info('[deleteStaleRunMetadata] Already migrated, skipping')
            return
        }
        const keys = await redisHelper.scanAll(redisConnection, 'runs_metadata:*')
        log.info({ count: keys.length }, '[deleteStaleRunMetadata] Found stale run metadata keys')
        for (const key of keys) {
            await redisConnection.expire(key, dayjs.duration(10, 'minute').asSeconds())
        }
        await redisConnection.set(DELETE_STALE_RUN_METADATA_KEY, 'true')
    },
})