"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CheckForNewDataTask = void 0;
const Di_1 = require("../../../ioc/Di");
const RopidGtfsContainerToken_1 = require("../../../ioc/RopidGtfsContainerToken");
const CoreToken_1 = require("@golemio/core/dist/helpers/ioc/CoreToken");
const integration_engine_1 = require("@golemio/core/dist/integration-engine");
const workers_1 = require("@golemio/core/dist/integration-engine/workers");
const CheckForNewDataMessageValidation_1 = require("./schema/CheckForNewDataMessageValidation");
class CheckForNewDataTask extends workers_1.AbstractTask {
    constructor(queuePrefix) {
        super(queuePrefix);
        this.queueName = "checkForNewData";
        this.queueTtl = 19 * 60 * 1000; // 19 minutes
        this.schema = CheckForNewDataMessageValidation_1.CheckForNewDataMessageValidation;
        this.logger = Di_1.RopidGtfsContainer.resolve(CoreToken_1.CoreToken.Logger);
        this.metadataRepository = Di_1.RopidGtfsContainer.resolve(RopidGtfsContainerToken_1.RopidGtfsContainerToken.RopidGtfsMetadataRepository);
    }
    async execute(data) {
        let forceRefresh = !!data.forceRefresh;
        let outdatedDatasetCount = 0;
        const ropidGtfsFactory = Di_1.RopidGtfsContainer.resolve(RopidGtfsContainerToken_1.RopidGtfsContainerToken.RopidGtfsFactory);
        const datasetMap = ropidGtfsFactory.getDatasetMap();
        const datasetList = Object.keys(datasetMap);
        for (const dataset of datasetList) {
            const serverLastModified = await datasetMap[dataset].datasource.getLastModified();
            const dbLastModified = await this.metadataRepository.getLastModified(dataset);
            const isDeployed = await this.metadataRepository.isDeployed(dataset);
            if (serverLastModified !== dbLastModified.lastModified || !isDeployed) {
                outdatedDatasetCount++;
            }
        }
        if (outdatedDatasetCount !== datasetList.length) {
            this.logger.warn(`RopidGTFS datasets are out of sync`);
        }
        if (outdatedDatasetCount > 0) {
            // NOTE: We are sending all datasets to download even if only some of them are outdated
            // to avoid unwanted side effects (such as empty precomputed tables)
            await integration_engine_1.QueueManager.sendMessageToExchange(this.queuePrefix, "downloadDatasets", {
                datasets: datasetList,
            });
        }
        else {
            this.logger.info("RopidGTFS datasets are up to date");
            if (forceRefresh) {
                await integration_engine_1.QueueManager.sendMessageToExchange(this.queuePrefix, "refreshPrecomputedTables", {});
            }
        }
        await integration_engine_1.QueueManager.sendMessageToExchange(this.queuePrefix, "saveStaticData", {});
    }
}
exports.CheckForNewDataTask = CheckForNewDataTask;
//# sourceMappingURL=CheckForNewDataTask.js.map