'use strict';

const cron = require('node-cron');
const MarketService = require('../services/conversionService');
const logger = require('../utils/logger');

let running = false;
let task = null;

function start() {
    if (task) return;

    task = cron.schedule('*/10 * * * *', async () => {
        if (running) return;
        running = true;

        try {
            await MarketService.updateMarketCron();
        } catch (err) {
            logger.error(`[MARKET] updateMarketCron — ${err.message}`);
        } finally {
            running = false;
        }
    });
}

function stop() {
    task?.stop();
    task = null;
}

module.exports = { start, stop };