"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ScheduleTaskHandler = exports.ScheduleTaskDefinition = void 0;
const error_1 = require("../../error");
const magic_function_1 = require("../magic.function");
const modules_1 = require("../modules");
const utils_1 = require("../utils");
/**
 * 动态异步方法
 * @param script
 * @return {*}
 * @constructor
 */
const ScheduleTaskDefinition = (script) => {
    return (0, utils_1.ScriptFunction)(script, [
        'ctx',
        'importModule',
        'importFunction',
        'BizError',
        'BizErrorEnum',
        'db',
        'cache',
        'logger',
    ]);
};
exports.ScheduleTaskDefinition = ScheduleTaskDefinition;
/**
 * 定时任务处理器
 * @param ctx 上下文
 * @param scheduleTaskDefinition 执行脚本
 */
const ScheduleTaskHandler = async (ctx, scheduleTaskDefinition) => {
    const requestContext = ctx.requestContext;
    const db = await requestContext.getAsync(modules_1.SQLModule);
    const cache = await requestContext.getAsync(modules_1.CacheModule);
    const magicFunction = await requestContext.getAsync(magic_function_1.MagicFunction);
    const logger = ctx.getLogger('magic');
    const importModule = async (moduleName) => {
        return await requestContext.getAsync(moduleName);
    };
    try {
        //执行script
        return await (0, utils_1.executeScriptFunction)(scheduleTaskDefinition, ctx, importModule, magicFunction.getFunction.bind(magicFunction), error_1.BizError, error_1.BizErrorEnum, db, cache, logger);
    }
    finally {
        await db.release();
    }
};
exports.ScheduleTaskHandler = ScheduleTaskHandler;
