"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 });
exports.RefreshPrecomputedTablesTask = void 0;
const RopidGtfsFacade_1 = require("../../../RopidGtfsFacade");
const RopidGtfsRepository_1 = require("../../../data-access/RopidGtfsRepository");
const DataCacheManager_1 = require("../../../helpers/DataCacheManager");
const SourceTableSuffixEnum_1 = require("../../../helpers/SourceTableSuffixEnum");
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");
const tsyringe_1 = require("@golemio/core/dist/shared/tsyringe");
const constants_1 = require("../constants");
const PrecomputedTablesFacade_1 = require("./helpers/PrecomputedTablesFacade");
let RefreshPrecomputedTablesTask = class RefreshPrecomputedTablesTask extends workers_1.AbstractEmptyTask {
    constructor(ropidGtfsRepository, precomputeTablesFacade, ropidGtfsFacade, databaseConnector, logger) {
        super(constants_1.TIMETABLE_WORKER_NAME);
        this.ropidGtfsRepository = ropidGtfsRepository;
        this.precomputeTablesFacade = precomputeTablesFacade;
        this.ropidGtfsFacade = ropidGtfsFacade;
        this.databaseConnector = databaseConnector;
        this.logger = logger;
        this.queueName = "refreshPrecomputedTables";
        this.queueTtl = 59 * 60 * 1000; // 59 minutes
        this.dataCacheManager = DataCacheManager_1.DataCacheManager.getInstance();
    }
    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 = this.databaseConnector.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;
exports.RefreshPrecomputedTablesTask = RefreshPrecomputedTablesTask = __decorate([
    (0, tsyringe_1.injectable)(),
    __param(0, (0, tsyringe_1.inject)(RopidGtfsContainerToken_1.RopidGtfsContainerToken.RopidGtfsRepository)),
    __param(1, (0, tsyringe_1.inject)(RopidGtfsContainerToken_1.RopidGtfsContainerToken.PrecomputedTablesFacade)),
    __param(2, (0, tsyringe_1.inject)(RopidGtfsContainerToken_1.RopidGtfsContainerToken.RopidGtfsFacade)),
    __param(3, (0, tsyringe_1.inject)(CoreToken_1.CoreToken.PostgresConnector)),
    __param(4, (0, tsyringe_1.inject)(CoreToken_1.CoreToken.Logger)),
    __metadata("design:paramtypes", [RopidGtfsRepository_1.RopidGtfsRepository,
        PrecomputedTablesFacade_1.PrecomputedTablesFacade,
        RopidGtfsFacade_1.RopidGtfsFacade, Object, Object])
], RefreshPrecomputedTablesTask);
//# sourceMappingURL=RefreshPrecomputedTablesTask.js.map