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.3 KB
Raw Blame

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

const DELETE_LEGACY_REDIS_KEYS_KEY = 'delete_legacy_redis_keys_v2'

const LEGACY_PATTERNS = [
    'tasks:project:*',
    'tasks:platform:*',
    'project-usage:*',
    'project-*-usage-tasks:*',
    'platform:*:worker_group_id',
]

export const deleteLegacyRedisKeys = (log: FastifyBaseLogger) => ({
    async run(): Promise<void> {
        const redisConnection = await redisConnections.useExisting()
        const isMigrated = await redisConnection.get(DELETE_LEGACY_REDIS_KEYS_KEY)
        if (!isNil(isMigrated)) {
            log.info('[deleteLegacyRedisKeys] Already migrated, skipping')
            return
        }
        for (const pattern of LEGACY_PATTERNS) {
            const keys = await redisHelper.scanAll(redisConnection, pattern)
            log.info({ pattern, count: keys.length }, '[deleteLegacyRedisKeys] Found legacy keys')
            const BATCH_SIZE = 1000
            for (let i = 0; i < keys.length; i += BATCH_SIZE) {
                await redisConnection.del(...keys.slice(i, i + BATCH_SIZE))
            }
        }
        await redisConnection.set(DELETE_LEGACY_REDIS_KEYS_KEY, 'true')
    },
})