/// <summary> /// 初始化脚本 /// </summary> /// <param name="scriptFile"></param> private bool InitScript(string scriptFile) { try { if (this._needInit) { // 初始化执行脚本 this._innerSE.Execute(FileUnity.ReadConfigFile(scriptFile)); this._innerSE.Execute(@"function ______tryRun(uri){ try{ var result = run(uri); if(result == undefined || result == 1 || result == true){ return 'OK'; } else{ return 'parse is not return 1 or true'; } }catch(e){ return e.message; }}"); this._needInit = false; } return(true); } catch (Exception ex) { LoggerProxy.Error(LogSource, string.Format("call InitScript error,scriptFile:'{0}'.{1}.", scriptFile, ex.Message), ex); } return(false); }
/// <summary> /// 工作线程执行方法 /// </summary> private void ThreadWork() { // 绑定Invoker执行上下文 this.BindContext(); do { this._notify.WaitOne(); try { if (this._needInit) { // 初始化执行脚本 this._innerSE.Execute(FileUnity.ReadConfigFile(this.CurrSetting.ScriptFile)); this._needInit = false; } // 执行任务 var execRest = this._innerSE.Invoke("parse", this.CurrSetting.Url); if (int.TryParse(execRest as string, out int rest) && rest == 1) { this.Result.Success = true; } } catch (Exception ex) { // todo: 记录错误信息 } finally { this.Status = TaskInvokerStatus.Stop; this._callBack?.Invoke(this); } }while (true); }
/// <summary> /// 执行初始化脚本 /// </summary> private void ExecuteInitScript() { var script = FileUnity.ReadConfigFile(this.ConfigFile); this._mainSE.Execute(script); this._script = this._mainSE.Invoke("config"); if (this._script == null) { throw new Exception("Task Init Error, the \"config\" method has no return value."); } var strSet = Serializer.JsonSerialize(this._script); if (string.IsNullOrWhiteSpace(strSet)) { throw new Exception("Task Init Error, the \"config\" method return value is empty."); } this.TaskSetting = Serializer.JsonDeserialize <TaskSetting>(strSet); if (!TaskInvokerStorage.Instance.AddRoot(new TaskInvokerStorageEntity() { ParentId = 0, Script = this.TaskSetting.Script, TaskId = this.TaskId, Url = this.TaskSetting.Url })) { throw new Exception("Task Init Error, add rootTask failed."); } }
public TaskInvoker(string scriptFile) { this._innerSE = new V8ScriptEngine(); this._innerSE.LoadSystemModules(); this.Context = new TaskInvokerContext(); this.Context.ExecutePath = FileUnity.GetDrectory(scriptFile); this.Context.Engine = this._innerSE; this.BindContext(); this._needInit = true; this.InitScript(scriptFile); }
/// <summary> /// 初始化任务生成工厂 /// </summary> /// <param name="cfgFile">任务工厂配置文件</param> /// <param name="isTest">是否为测试任务</param> public Task(string cfgFile) { this.ExecutePath = FileUnity.GetDrectory(cfgFile); this._freeSE = new Queue <TaskInvoker>(); var script = FileUnity.ReadConfigFile(cfgFile); if (string.IsNullOrEmpty(script)) { throw new Exception("task init error, the config file content is empty."); } this.TaskSetting = Serializer.JsonDeserialize <TaskSetting>(script); this.InitTaskStorage(); this._lock = new Semaphore(this.TaskSetting.Parallel, this.TaskSetting.Parallel); }
/// <summary> /// 初始化某个目录下定义的所有任务 /// </summary> /// <param name="cfgFolder">配置文件目录</param> public static void InitTasks(string cfgFolder) { cfgFolder = Snail.IO.PathUnity.GetFullPath(cfgFolder); if (string.IsNullOrEmpty(cfgFolder)) { throw new Exception("cannot found the folder with path : '" + cfgFolder + "'"); } FileUnity.Each(new DirectoryInfo(cfgFolder), (file) => { if (file.Name.Equals("task.js")) { InitTask(file.FullName); } }); }
/// <summary> /// 初始化任务生成工厂 /// </summary> /// <param name="cfgFile">任务工厂配置文件</param> public Task(string cfgFile) { this.ExecutePath = FileUnity.GetDrectory(cfgFile); this.ConfigFile = cfgFile; this._mainSE = new V8ScriptEngine(); this._mainSE.LoadSystemModules(); this._freeSE = new Queue <TaskInvoker>(); this._http = new HttpModule(); this._mainSE.AddHostObject("http", this._http); // 临时绑定上下文到当前线程中,该引用会被下一个初始化任务覆盖 this.Context = new TaskContext(); this.Context.ExecutePath = this.ExecutePath; this.Context.HttpClient = this._http; this.Context.Engine = this._mainSE; this.Context.BindContext(); this.ExecuteInitScript(); this.Context.TaskId = this.TaskId; this._lock = new Semaphore(this.TaskSetting.Parallel, this.TaskSetting.Parallel); }