"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ProcessAndSendLogsTask = void 0;
const Di_1 = require("../../../../ioc/Di");
const ModuleContainerToken_1 = require("../../../../ioc/ModuleContainerToken");
const CoreToken_1 = require("@golemio/core/dist/helpers/ioc/CoreToken");
const ioc_1 = require("@golemio/core/dist/integration-engine/ioc");
const workers_1 = require("@golemio/core/dist/integration-engine/workers");
const redis_semaphore_1 = require("@golemio/core/dist/shared/redis-semaphore");
const LogDateTimeUtils_1 = require("../helpers/LogDateTimeUtils");
const DEFAULT_BATCH_SIZE = 1000;
const DEFAULT_SEND_LOGS_LOCK_TIMEOUT = 60 * 1000;
class ProcessAndSendLogsTask extends workers_1.AbstractEmptyTask {
    constructor(queuePrefix) {
        super(queuePrefix);
        this.queueName = "processAndSendLogs";
        this.queueTtl = 59 * 1000; // 59 seconds
        this.LOCK_KEY_PHRASE = "ropidGTFSProcessAndSendLogsTask";
        const config = Di_1.PidContainer.resolve(CoreToken_1.CoreToken.SimpleConfig);
        this.logRepository = Di_1.PidContainer.resolve(ModuleContainerToken_1.ModuleContainerToken.PresetLogRepository);
        this.monitoringService = Di_1.PidContainer.resolve(ModuleContainerToken_1.ModuleContainerToken.RopidMonitoringService);
        this.redisClient = Di_1.PidContainer.resolve(ioc_1.ContainerToken.RedisConnector);
        this.logger = Di_1.PidContainer.resolve(CoreToken_1.CoreToken.Logger);
        const batchSize = config.getValue("env.ROPID_PRESET_LOG_BATCH_SIZE", DEFAULT_BATCH_SIZE.toString());
        this.batchSize = Number.parseInt(batchSize);
        const lockTimeout = config.getValue("env.ROPID_PRESET_SEND_LOGS_LOCK_TIMEOUT", DEFAULT_SEND_LOGS_LOCK_TIMEOUT.toString());
        this.lockTimeout = Number.parseInt(lockTimeout);
        this.refreshInterval = this.lockTimeout * 0.8; // recommended
    }
    async execute() {
        const mutex = this.createMutex();
        const lockAcquired = await mutex.tryAcquire();
        if (!lockAcquired) {
            this.logger.error(`${this.constructor.name}: unable to obtain mutex lock.`);
            return;
        }
        try {
            const logEntries = await this.logRepository.findUnprocessed();
            if (logEntries.length === 0) {
                this.logger.warn(`${this.constructor.name}: no logs found to send to the ROPID monitoring system`);
                return;
            }
            // Process logs in batches to avoid exceeding the maximum allowed size of the request
            for (let i = 0; i < logEntries.length; i += this.batchSize) {
                try {
                    const batchedLogs = logEntries.slice(i, i + this.batchSize);
                    await this.processBatch(batchedLogs);
                }
                catch (err) {
                    this.logger.warn(err, `${this.constructor.name}: error while processing DCIP system logs`);
                }
            }
        }
        finally {
            await mutex.release();
        }
    }
    async processBatch(logEntries) {
        let logIds = [];
        let logData = [];
        for (const logEntry of logEntries) {
            const { device_alias, received_at } = logEntry.toJSON();
            logIds.push(logEntry.get("id"));
            logData.push({
                deviceAlias: device_alias,
                receivedAt: LogDateTimeUtils_1.LogDateTimeUtils.parseISONoFractionalFromDate(received_at),
            });
        }
        await this.monitoringService.sendLogsToMonitoringSystem(logData);
        this.logger.info(`${this.constructor.name}: ${logData.length} logs sent to the ROPID monitoring system`);
        await this.logRepository.markAsProcessed(logIds);
    }
    createMutex() {
        return new redis_semaphore_1.Mutex(this.redisClient.getConnection(), this.LOCK_KEY_PHRASE, {
            acquireAttemptsLimit: 1,
            lockTimeout: this.lockTimeout,
            refreshInterval: this.refreshInterval,
            onLockLost: (err) => {
                this.logger.error(err, `${this.constructor.name}: mutex lock was lost`);
            },
        });
    }
}
exports.ProcessAndSendLogsTask = ProcessAndSendLogsTask;
//# sourceMappingURL=ProcessAndSendLogsTask.js.map