/*
 * Housekeeping
 * Created by: Michael Pastushkov <michael@pastushkov.com>
 */

const config = require('config')
const utils = require('../utils/utils');
const { CronJob } = require('cron');

const DEFAULT_CRON_TIME = '0 0 * * * *';

function init() {

    cronTime = config.housekeeping.cronTime || DEFAULT_CRON_TIME;

    if (cronTime == 'off')
        return;

    const job = CronJob.from({
        cronTime: cronTime,
        onTick: cleanup,
        start: true,
    });

    if (config.housekeeping.cleanup_on_start)
        cleanup();

    utils.log(`Housekeeping started: ${cronTime}`);

}

async function cleanup() {
    try {
        utils.log('Cleanup ...');
    } catch (e) {
        utils.log_error(e);
    }
}

module.exports.init = init;