"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AsyncTaskHandler = exports.AsyncTaskDefinition = 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 AsyncTaskDefinition = (script) => {
    return (0, utils_1.ScriptFunction)(script, [
        'ctx',
        'params',
        'job',
        'importModule',
        'importFunction',
        'BizError',
        'BizErrorEnum',
        'db',
        'cache',
        'logger',
    ]);
};
exports.AsyncTaskDefinition = AsyncTaskDefinition;
/**
 * 定时任务处理器
 * @param ctx 上下文
 * @param params 调用数据
 * @param job 任务对象
 * @param asyncTaskFunction 执行脚本
 */
const AsyncTaskHandler = async (ctx, params, job, asyncTaskFunction) => {
    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)(asyncTaskFunction, ctx, params, job, importModule, magicFunction.getFunction.bind(magicFunction), error_1.BizError, error_1.BizErrorEnum, db, cache, logger);
    }
    finally {
        await db.release();
    }
};
exports.AsyncTaskHandler = AsyncTaskHandler;
