import { Singleton, Container, Scope, Inject } from "typescript-ioc";
import * as schedule from 'node-schedule'
import { Config } from '../config/Config'
import { RedisUtil } from '../redis/RedisUtil';
import { log4js as logger } from '../log4js/logger'

@Singleton
export class DefaultVersionSchedule {

    async init() {
        let jobIsRunning = false;
        schedule.scheduleJob('0/5 * * * * ?', async () => {
            if (jobIsRunning) {
                return
            }
            try {
                jobIsRunning = true
                logger.info('定时拉取默认版本信息开始。。。')
                const redisUtil = Container.get(RedisUtil)
                let newDefaultVersions = await redisUtil.get('KT_EC_DEFAULT_VERSIONS')
                let oldDefaultVersions = JSON.stringify(Config.getDefaultVersions())
                if (!newDefaultVersions) {
                    logger.info('newDefaultVersions值为空')
                    return
                }
                if (oldDefaultVersions === newDefaultVersions) {
                    logger.info('默认版本没有变化，不用更新。。。')
                    return
                }
                Config.setDefaultVersions(JSON.parse(newDefaultVersions))
            } finally {
                jobIsRunning = false
            }
        });
    }
}