UNPKG

@vessl-ai/mcpctl-control-plane

Version:

Vessl MCP Control Plane service for managing distributed jobs.

146 lines 5.89 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __param = (this && this.__param) || function (paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } }; var AppCacheService_1; Object.defineProperty(exports, "__esModule", { value: true }); exports.AppCacheService = void 0; const cache_manager_1 = require("@nestjs/cache-manager"); const common_1 = require("@nestjs/common"); const config_1 = require("@nestjs/config"); const cacheable_1 = require("cacheable"); const fs_1 = require("fs"); const fs = require("fs/promises"); const path = require("path"); let AppCacheService = AppCacheService_1 = class AppCacheService { cacheManager; configService; logger = new common_1.Logger(AppCacheService_1.name); MEMORY_CACHE_FILE = 'memory-cache.json'; constructor(cacheManager, configService) { this.cacheManager = cacheManager; this.configService = configService; this.restore(); } isOnlyMemoryStore() { return (this.cacheManager.stores.some((store) => store.store instanceof cacheable_1.CacheableMemory) && this.cacheManager.stores.length === 1); } async restore() { if (this.isOnlyMemoryStore()) { this.logger.log('Restoring cache...'); const backupDir = this.configService.get('cache')?.defaultCache.backupDir; if (!backupDir) { this.logger.error('No backup directory found'); return; } const backupPath = path.join(backupDir, this.MEMORY_CACHE_FILE); if (!(0, fs_1.existsSync)(backupPath)) { this.logger.error('No backup file found'); return; } const entries = JSON.parse(await fs.readFile(backupPath, 'utf8')); for (const [key, value] of Object.entries(entries)) { await this.cacheManager.set(key, value); } this.logger.log('Cache restored'); } } async backup() { this.logger.log('Backing up cache...'); if (this.isOnlyMemoryStore()) { this.logger.log('Backing up memory cache since only it is used'); const store = this.cacheManager.stores.filter((store) => store.store instanceof cacheable_1.CacheableMemory)[0]; if (!store) { this.logger.error('No memory store found'); return; } if (!store.iterator) { this.logger.error('No iterator found'); return; } const entries = {}; for await (const [key, value] of store.iterator(undefined)) { entries[key] = value; } const backupDir = this.configService.get('cache')?.defaultCache.backupDir; if (!backupDir) { this.logger.error('No backup directory found'); return; } if (!(0, fs_1.existsSync)(backupDir)) { await fs.mkdir(backupDir, { recursive: true }); } const backupPath = path.join(backupDir, this.MEMORY_CACHE_FILE); await fs.writeFile(backupPath, JSON.stringify(entries, null, 2)); this.logger.log('Memory cache backed up'); } } async get(key) { return this.cacheManager.get(key); } async mget(keys) { return this.cacheManager.mget(keys); } async ttl(key) { return this.cacheManager.ttl(key); } async set(key, value, ttl) { await this.cacheManager.set(key, value, ttl); return value; } async mset(list) { await this.cacheManager.mset(list); return list; } async del(key) { return this.cacheManager.del(key); } async mdel(keys) { return this.cacheManager.mdel(keys); } async clear() { return this.cacheManager.clear(); } on(event, listener) { return this.cacheManager.on(event, listener); } off(event, listener) { return this.cacheManager.off(event, listener); } async disconnect() { return this.cacheManager.disconnect(); } get cacheId() { return this.cacheManager.cacheId; } get stores() { return this.cacheManager.stores; } async wrap(key, fnc, ttl, refreshThreshold) { const safeTtl = typeof ttl === 'number' || (typeof ttl === 'function' && ttl.length === 1) ? ttl : undefined; const safeRefresh = typeof refreshThreshold === 'number' || (typeof refreshThreshold === 'function' && refreshThreshold.length === 1) ? refreshThreshold : undefined; return this.cacheManager.wrap(key, fnc, safeTtl, safeRefresh); } }; exports.AppCacheService = AppCacheService; exports.AppCacheService = AppCacheService = AppCacheService_1 = __decorate([ (0, common_1.Injectable)(), __param(0, (0, common_1.Inject)(cache_manager_1.CACHE_MANAGER)), __metadata("design:paramtypes", [cache_manager_1.Cache, config_1.ConfigService]) ], AppCacheService); //# sourceMappingURL=appcache.service.js.map