"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.RefreshPrecomputedTablesTask = void 0;
const Di_1 = require("../../../../ioc/Di");
const DataCacheManager_1 = require("../../../helpers/DataCacheManager");
const SourceTableSuffixEnum_1 = require("../../../helpers/SourceTableSuffixEnum");
const Di_2 = require("../../../ioc/Di");
const RopidGtfsContainerToken_1 = require("../../../ioc/RopidGtfsContainerToken");
const CoreToken_1 = require("@golemio/core/dist/helpers/ioc/CoreToken");
const workers_1 = require("@golemio/core/dist/integration-engine/workers");
const golemio_errors_1 = require("@golemio/core/dist/shared/golemio-errors");
class RefreshPrecomputedTablesTask extends workers_1.AbstractEmptyTask {
    constructor(queuePrefix) {
        super(queuePrefix);
        this.queueName = "refreshPrecomputedTables";
        this.queueTtl = 59 * 60 * 1000; // 59 minutes
        this.logger = Di_2.RopidGtfsContainer.resolve(CoreToken_1.CoreToken.Logger);
        this.dataCacheManager = DataCacheManager_1.DataCacheManager.getInstance();
        this.precomputeTablesFacade = Di_2.RopidGtfsContainer.resolve(RopidGtfsContainerToken_1.RopidGtfsContainerToken.PrecomputedTablesFacade);
        this.ropidGtfsRepository = Di_2.RopidGtfsContainer.resolve(RopidGtfsContainerToken_1.RopidGtfsContainerToken.RopidGtfsRepository);
        this.ropidGtfsFacade = Di_2.RopidGtfsContainer.resolve(RopidGtfsContainerToken_1.RopidGtfsContainerToken.RopidGtfsFacade);
    }
    async execute() {
        try {
            await this.precomputeTablesFacade.createAndPopulatePrecomputedTmpTables(SourceTableSuffixEnum_1.SourceTableSuffixEnum.Actual);
        }
        catch (err) {
            this.logger.error(err);
            throw new golemio_errors_1.GeneralError("Error while creating RopidGTFS precomputed tables", this.constructor.name);
        }
        const connection = Di_1.PidContainer.resolve(CoreToken_1.CoreToken.PostgresConnector).getConnection();
        const transaction = await connection.transaction();
        try {
            await this.ropidGtfsFacade.replacePrecomputedTables(transaction);
            await transaction.commit();
        }
        catch (err) {
            await transaction.rollback();
            this.logger.error(err);
            throw new golemio_errors_1.GeneralError("Error while replacing RopidGTFS precomputed tables", this.constructor.name);
        }
        finally {
            await this.ropidGtfsRepository.cleanOldTables();
        }
        await this.dataCacheManager.cleanCache();
    }
}
exports.RefreshPrecomputedTablesTask = RefreshPrecomputedTablesTask;
//# sourceMappingURL=RefreshPrecomputedTablesTask.js.map