UNPKG

@lxdhub/dbsync

Version:

Display, search and copy LXD-images using a web interface.

132 lines 6.03 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); } }; Object.defineProperty(exports, "__esModule", { value: true }); const db_1 = require("@lxdhub/db"); const common_1 = require("@nestjs/common"); const alias_1 = require("./alias"); const architecture_1 = require("./architecture"); const image_1 = require("./image"); const image_availability_1 = require("./image-availability"); const operating_system_1 = require("./operating-system"); const os_arch_1 = require("./os-arch"); const remote_1 = require("./remote"); const sync_run_1 = require("./sync-run"); const app_tokens_1 = require("./app.tokens"); let AppService = /** * The app services orchastrates the database synchronizer */ class AppService { /** * Initializes the app service */ constructor(remoteService, operatingSystemService, architectureService, imageService, aliasService, osArchService, imageAvailabilityService, databaseService, syncRunService, settings) { this.remoteService = remoteService; this.operatingSystemService = operatingSystemService; this.architectureService = architectureService; this.imageService = imageService; this.aliasService = aliasService; this.osArchService = osArchService; this.imageAvailabilityService = imageAvailabilityService; this.databaseService = databaseService; this.syncRunService = syncRunService; this.settings = settings; this.logger = new common_1.Logger('Database Synchronizer'); } async run() { if (this.settings.force) { this.syncRunService.resetAllSyncStates(); } else { const currentlyRunningSyncs = await this.syncRunService.getCurrentlyRunningSyncs(); if (currentlyRunningSyncs.length) { const logMessage = `There are currently ${currentlyRunningSyncs.length} other synchronization tasks running.\n` + currentlyRunningSyncs .map(sync => `- Sync #${sync.id} started at ${sync.started}`) .join('\n') + `You can enforce this database synchronization run by adding the "FORCE=true" environment variable.`; this.logger.error(logMessage); throw new Error(logMessage); } } await this.syncRunService.startSyncRun(this.syncRun.id); this.logger.log(`==> Started synchronization #${this.syncRun.id}`); // Run the synchronizers await this.remoteService.synchronize(); await this.imageService.synchronize(); await this.operatingSystemService.synchronize(); await this.architectureService.synchronize(); await this.aliasService.synchronize(); await this.osArchService.synchronize(); await this.imageAvailabilityService.synchronize(); } /** * Start the database synchronizer. */ async synchronize() { this.syncRun = await this.syncRunService.createSyncRun(); this.logger.log(`==> Starting database synchronization #${this.syncRun.id}`); /** * If we don't handle rejected promises, then tye sync run will * crash the node process. We need to fail the sync run * otherwise the next syncrun won't start. */ process.on('unhandledRejection', async (reason, promise) => { const error = `Unhandled Rejection at: ${promise}, reason: ${reason}`; this.logger.error(error); // fail the syncrun await this.syncRunService.failSyncRun(this.syncRun.id, error); }); try { await this.run(); await this.syncRunService.finishSyncRun(this.syncRun.id); } catch (ex) { await this.syncRunService.failSyncRun(this.syncRun.id, ex ? ex.message : ''); } // Closes the database connection this.logger.log(`==> Finished database synchronization #${this.syncRun.id}`); } async onApplicationShutdown(signal) { this.logger.error(`Database sync shutting down ${signal}`); try { await this.databaseService.connection.connect(); await this.syncRunService.failSyncRun(this.syncRun.id, `Database synchronization shutdown ${signal}`); await this.databaseService.closeConnection(); } catch (ex) { this.logger.error(`Failed gracefully shutting down dbsync: ${ex.message}`); } process.exit(1); } }; AppService = __decorate([ common_1.Injectable() /** * The app services orchastrates the database synchronizer */ , __param(9, common_1.Inject(app_tokens_1.SETTINGS)), __metadata("design:paramtypes", [remote_1.RemoteService, operating_system_1.OperatingSystemService, architecture_1.ArchitectureService, image_1.ImageService, alias_1.AliasService, os_arch_1.OsArchService, image_availability_1.ImageAvailabilityService, db_1.DatabaseService, sync_run_1.SyncRunService, Object]) ], AppService); exports.AppService = AppService; //# sourceMappingURL=app.service.js.map