'use strict';

/**
 *
 *
 * @author Mai
 * @date
 * @version
 */

module.exports = {
    schedule: {
        // interval: '30s', // 30 seconds, for dev
        interval: '5m', // 5 min, for opr
        type: 'worker',
    },
    async task (ctx) {
        if (!ctx.app.awaitRefresh || ctx.app.awaitRefresh.length === 0) return;

        const lib = await ctx.service.quotaLib.getDataById(ctx.app.awaitRefresh.shift());
        if (!lib) return;

        const nsp = ctx.app.io.of('/lib');
        try {
            // test error
            // throw '1';
            await ctx.service.quotaLib.refreshLib(lib);

            nsp.emit('refreshed', {
                id: lib.id,
                filename: lib.filename,
                success: true,
                count: ctx.app.awaitRefresh.length,
            });
        } catch(err) {
            console.log(err);
            nsp.emit('refreshed', {
                id: lib.id,
                filename: lib.filename,
                success: false,
                count: ctx.app.awaitRefresh.length,
            });
        }
    }
};