UNPKG

mm_os

Version:

这是超级美眉服务端框架,用于快速构建应用程序。

110 lines (102 loc) 2.2 kB
const Index = require('mm_machine').Index; const Drive = require('./drive'); /** * 任务类 * @extends {Index} * @class */ class Task extends Index { /** * 构造函数 * @param {Object} scope 作用域 * @param {String} title 标题 * @constructor */ constructor(scope, title) { super(scope, __dirname); this.Drive = Drive; this.type = "task"; this.title = title; // 更新配置并重载脚本 this.mode = 3; } } /** * 执行任务 * @param {String} name 要执行的名称 */ Task.prototype.run = async function(name) { var lt = this.list; if (name) { for (var i = 0, o; o = lt[i++];) { if (o.config.state === 1 && o.config.name === name) { try { await o.run(); } catch (error) { $.log.error("定时任务错误", o.name, error); } break; } } } else { for (var i = 0, o; o = lt[i++];) { if (o.config.state === 1) { try { await o.run(); } catch (error) { $.log.error("定时任务错误", o.name, error); } } } } }; /** * 加载项 * @param {String} dir 文件路径 * @param {Object} cg 配置参数 * @param {String} file 配置文件 */ Task.prototype.load_item = async function(dir, cg, file) { var _this = this; var drive = new this.Drive(dir, this.dir_base.fullname()); drive.mode = this.mode; if (cg) { await drive.exec('load_config', file, cg.name); await drive.exec('set_config', cg); } else { await drive.exec('load_config', file); } drive.admin = function() { return _this; }; this.list.push(drive); return drive; }; exports.Task = Task; /** * Task模板池 */ if (!$.pool.task) { $.pool.task = {}; } /** * Task管理器,用于创建缓存 * @task {String} scope 作用域 * @param {string} title 标题 * @return {Object} 返回一个缓存类 */ function task_admin(scope, title) { if (!scope) { scope = $.val.scope + ''; } var obj = $.pool.task[scope]; if (!obj) { $.pool.task[scope] = new Task(scope, title); obj = $.pool.task[scope]; } return obj; } /** * @module 导出Task管理器 */ $.task_admin = task_admin;