/// <summary> /// 保存最后读取的插件配置信息 /// </summary> /// <param name="sp"></param> /// <param name="lst"></param> private static void SaveLastPluginInfo(SchedulePluginsInfo sp, ref List <SchedulePluginsInfo> lst) { if (sp != null) { lst.Add(sp); } }
public static List <SchedulePluginsInfo> Read(string path) { List <SchedulePluginsInfo> lst = new List <SchedulePluginsInfo>(); using (StreamReader sr = new StreamReader(path, Encoding.Default)) { string line; SchedulePluginsInfo sp = null; while ((line = sr.ReadLine()) != null) { line = line.Trim(); if (string.IsNullOrEmpty(line)) { continue; } bool isStop = false; ReadNoteLine(line); ReadAssemblyInfo(line, ref isStop, ref sp, ref lst); ReadField(line, ref isStop, "Id", ref sp); ReadField(line, ref isStop, "Name", ref sp); ReadField(line, ref isStop, "EventsName", ref sp); ReadField(line, ref isStop, "NotifyUrl", ref sp); } SaveLastPluginInfo(sp, ref lst); return(lst); } }
public bool AddJobPlugin(SchedulePluginsInfo jobPlugin) { lock (lockPad) { if (jobPlugin == null) { return(false); } if (Assemblies.Exists(c => c.Equals(jobPlugin.AssemblyInfo, StringComparison.OrdinalIgnoreCase))) { return(false); } if (jobPlugin.Id == 0) { bool successed = pluginService.Save(jobPlugin); if (!successed) { return(false); } ConfigFileHelper.Write(Path.Combine(Directory, "config.ini"), jobPlugin); } Assemblies.Add(jobPlugin.AssemblyInfo); if (this.AppDomain == null) { this.AppDomain = new AppDomainInfo(Directory); } return(true); } }
public void Execute(IJobExecutionContext context) { Guid jobID = (Guid)context.JobDetail.JobDataMap["JOBS"]; ScheduleJobsInfo job = jobsService.FindOne(jobID); if (job == null) { return; } logger.Info("Execute:" + job.ScheduleId.ToString()); SchedulePluginsInfo schedule = pluginService.FindOne(job.ScheduleId); string assembly = schedule == null ? "" : schedule.AssemblyInfo; if (string.IsNullOrEmpty(assembly)) { return; } IPlugins plugin = LoadPlugins.Instance.CreateInstance(assembly); if (plugin == null) { logger.Warn("实例化插件:" + assembly + "失败"); //如果插件不存在,//标记未读取,以便下一次插件加载后可以读取任务 jobsService.UpdateStatus(jobID, JobStatus.WaitToRun); RemoveQuartzJob(context); return; } jobsService.UpdateStatus(jobID, JobStatus.Running);//标记任务正在执行 try { if (job.Mode == JobMode.OnTimeEveryTime) { plugin.Execute(job.Id, schedule.NotifyUrl, job.Mode, null, OnPluginTaskCompleted);//执行任务 } else { TaskExecuteState state = new TaskExecuteState(); plugin.Execute(job.Id, schedule.NotifyUrl, job.Mode, state, null);//执行任务 state.Wait(); OnPluginTaskCompleted(context, job, plugin.State.Successed, plugin.State.OperationException != null ? plugin.State.OperationException.Message : ""); } } catch (Exception ex) { jobsService.UpdateStatus(jobID, JobStatus.Failed); logger.Error(ex); } }
/// <summary> /// 读取插件程序集信息 /// </summary> /// <param name="line"></param> private static void ReadAssemblyInfo(string line, ref bool isStop, ref SchedulePluginsInfo sp, ref List <SchedulePluginsInfo> lst) { if (isStop) { return; } if (!line.StartsWith("[") || !line.EndsWith("]")) { return; } SaveLastPluginInfo(sp, ref lst); sp = new SchedulePluginsInfo() { AssemblyInfo = line.Trim('[', ']') }; isStop = true; }
public static void Write(string path, SchedulePluginsInfo sp) { string txt = string.Empty; using (StreamReader sr = new StreamReader(path, Encoding.Default)) { txt = sr.ReadToEnd(); } txt = txt.Replace("\r\n\r\n", "\r\n\r\n\r\n"); int index = txt.IndexOf(sp.AssemblyInfo, StringComparison.OrdinalIgnoreCase); if (index == -1) { logger.Error(new Exception(sp.AssemblyInfo + "不存在于" + path)); return; } int end = txt.IndexOf('[', index + sp.AssemblyInfo.Length + 3); if (end == -1) { end = txt.Length - 1; } int i_start = txt.IndexOf("Id=", index + sp.AssemblyInfo.Length + 3, StringComparison.OrdinalIgnoreCase); if (i_start == -1 || i_start > end) { i_start = index + sp.AssemblyInfo.Length + 3; txt = txt.Insert(i_start, "\r\nId=" + sp.Id.ToString() + "\r\n"); } else { int i_end = txt.IndexOf("\r\n", i_start); txt = txt.Remove(i_start, i_end - i_start); txt = txt.Insert(i_start, "\r\nId=" + sp.Id.ToString() + "\r\n"); } txt = txt.Replace("\r\n\r\n", "\r\n"); using (StreamWriter sw = new StreamWriter(path, false, Encoding.Default)) { sw.WriteLine(txt); } }
/// <summary> /// 读取字段信息 /// </summary> /// <param name="line"></param> /// <param name="fieldName"></param> /// <param name="sp"></param> private static void ReadField(string line, ref bool isStop, string fieldName, ref SchedulePluginsInfo sp) { if (isStop) { return; } if (sp == null) { return; } if (!line.StartsWith(fieldName + "=", StringComparison.OrdinalIgnoreCase)) { return; } string val = line.Substring(fieldName.Length + 1).Trim(); if (string.IsNullOrEmpty(val)) { return; } PropertyInfo propertyInfo = typeof(SchedulePluginsInfo).GetProperty(fieldName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty | BindingFlags.SetProperty); if (propertyInfo == null) { return; } propertyInfo.SetValue(sp, Convert.ChangeType(val, propertyInfo.PropertyType)); isStop = true; }
public bool Save(SchedulePluginsInfo info) { return(repository.Save(info)); }