From 2f411e253a809a70a7f23b1684a6d436c9357e4e Mon Sep 17 00:00:00 2001 From: LongX Date: Thu, 24 Jan 2019 15:35:40 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=9F=BA=E7=A1=80=E4=BB=BB?= =?UTF-8?q?=E5=8A=A1=E7=9B=B8=E5=85=B3=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Interfaces/Job/BaseListJobExcutor.cs | 88 +++++++++++++ OSS.Common/Interfaces/Job/BaseTimerTrigger.cs | 117 ++++++++++++++++++ OSS.Common/Interfaces/Job/IJobExecutor.cs | 18 +++ 3 files changed, 223 insertions(+) create mode 100644 OSS.Common/Interfaces/Job/BaseListJobExcutor.cs create mode 100644 OSS.Common/Interfaces/Job/BaseTimerTrigger.cs create mode 100644 OSS.Common/Interfaces/Job/IJobExecutor.cs diff --git a/OSS.Common/Interfaces/Job/BaseListJobExcutor.cs b/OSS.Common/Interfaces/Job/BaseListJobExcutor.cs new file mode 100644 index 0000000..a02b3c3 --- /dev/null +++ b/OSS.Common/Interfaces/Job/BaseListJobExcutor.cs @@ -0,0 +1,88 @@ +using System.Collections.Generic; + +namespace OSS.Common.Interfaces.Job +{ + /// + /// 列表循环处理任务执行 + /// 从 GetExcuteSource() 获取执行数据源,循环并通过 ExcuteItem() 执行个体任务,直到没有数据源返回 + /// 如果执行时间过长,重复触发时 当前任务还在进行中,则不做任何处理 + /// + public abstract class BaseListJobExcutor : IJobExecutor + { + /// + /// 运行状态 + /// + public bool IsRuning { get; protected set; } + + + private int triggerTimes { get; set; } = 0; + + /// + /// 开始任务 + /// + public void StartJob() + { + // 任务依然在执行中,不需要再次唤起 + if (IsRuning) + return; + + IsRuning = true; + IList list = null; // 结清实体list + + OnBegin(triggerTimes++); + do + { + for (var i = 0; IsRuning && i < list?.Count; i++) + { + ExcuteItem(list[i], i); + } + list = GetExcuteSource(); + } while (IsRuning && list?.Count > 0); + OnEnd(triggerTimes); + IsRuning = false; + } + + /// + /// 获取list数据源 + /// + /// + protected virtual IList GetExcuteSource() + { + return null; + } + + /// + /// 个体任务执行 + /// + /// 单个实体 + /// 在数据源中的索引 + protected virtual void ExcuteItem(IType item, int index) + { + } + + /// + /// 结束任务 + /// + public void StopJob() + { + IsRuning = false; + } + + + + /// + /// 此轮任务开始 + /// + protected virtual void OnBegin(int triggerTimes) + { + + } + + /// + /// 此轮任务结束 + /// + protected virtual void OnEnd(int triggerTimes) + { + } + } +} diff --git a/OSS.Common/Interfaces/Job/BaseTimerTrigger.cs b/OSS.Common/Interfaces/Job/BaseTimerTrigger.cs new file mode 100644 index 0000000..8579067 --- /dev/null +++ b/OSS.Common/Interfaces/Job/BaseTimerTrigger.cs @@ -0,0 +1,117 @@ +using System; +using System.Threading; +using System.Threading.Tasks; +using OSS.Common.Plugs.LogPlug; + +namespace OSS.Common.Interfaces.Job +{ + /// + /// 定时器基础类 + /// + public class BaseTimerTrigger:IDisposable + { + private Timer _timer; + + private readonly TimeSpan _dueTime; + private readonly TimeSpan _periodTime; + + private readonly IJobExecutor _jobExcutor; + + /// + /// 构造函数 + /// + /// 到期开始执行时间 + /// 间隔时间 + /// 任务执行者 + protected BaseTimerTrigger(TimeSpan dueTime, TimeSpan periodTime, IJobExecutor jobExcutor) + { + _dueTime = dueTime; + _periodTime = periodTime; + _jobExcutor = jobExcutor; + } + + + #region 基础方法 + + /// + /// 配置并触发定时器 + /// + /// + private void StartTimerTrigger() + { + if (_timer == null) + _timer = new Timer(ExcuteJob, _jobExcutor, _dueTime, _periodTime); + else + _timer.Change(_dueTime, _periodTime); + } + + /// + /// 停止定时器 + /// + private void StopTimerTrigger() + { + _timer?.Change(Timeout.Infinite, Timeout.Infinite); + } + + + + /// + public void Dispose() + { + _timer?.Dispose(); + } + + #endregion + + /// + /// 系统级任务执行启动 + /// + /// + /// + public virtual Task StartAsync(CancellationToken cancellationToken) + { + try + { + StartTimerTrigger(); + } + catch (Exception e) + { + LogUtil.Error($"启动定时任务({nameof(GetType)})时出错,信息:{e}", string.Empty, "System_TimerJob"); + } + + return Task.CompletedTask; + } + + /// + /// 系统级任务执行关闭 + /// + /// + /// + public virtual Task StopAsync(CancellationToken cancellationToken) + { + try + { + _jobExcutor.StopJob(); + StopTimerTrigger(); + } + catch (Exception e) + { + LogUtil.Error($"停止定时任务({nameof(GetType)})时出错,信息:{e}", String.Empty, "System_TimerJob"); + } + return Task.CompletedTask; + } + + private void ExcuteJob(object obj) + { + try + { + var excutor = obj as IJobExecutor; + excutor?.StartJob(); + } + catch (Exception e) + { + LogUtil.Error($"执行任务({nameof(GetType)})时出错,信息:{e}", String.Empty, "System_TimerJob"); + } + } +} +} diff --git a/OSS.Common/Interfaces/Job/IJobExecutor.cs b/OSS.Common/Interfaces/Job/IJobExecutor.cs new file mode 100644 index 0000000..1f990cb --- /dev/null +++ b/OSS.Common/Interfaces/Job/IJobExecutor.cs @@ -0,0 +1,18 @@ +namespace OSS.Common.Interfaces.Job +{ + /// + /// 任务 提供者 接口 + /// + public interface IJobExecutor + { + /// + /// 开始任务 + /// + void StartJob(); + + /// + /// 结束任务 + /// + void StopJob(); + } +}