Example #1
0
 protected override TaskResult Work()
 {
     var rst = new TaskResult();
     var helper = new IpHelper(oldIp);
     var ip = helper.GetIp();
     if (helper.IpChanged)
     {
         var sw = new Stopwatch();
         sw.Start();
         var ok = BatchVast(ip);
         sw.Stop();
         if (ok)
         {
             rst.Result = TaskResultType.Succeed;
             rst.Message = $"已将所有泛解析更新了IP地址指向[effecteds:<{_ups}>,change: <{oldIp}> -> <{ip}>][elapsed:<{sw.Elapsed}>]";
             oldIp = ip;
         }
         else
         {
             rst.Result = TaskResultType.Failed;
             rst.Message = $"更新IP地址失败[change: <{oldIp}> -> <{ip}>][elapsed:<{sw.Elapsed}>]";
             Log.Warn($"更新地址失败[change: <{oldIp}> -> <{ip}>][elapsed:<{sw.Elapsed}>]");
         }
     }
     else
     {
         rst.Result = TaskResultType.Unknow;
         rst.Message = "IP地址未变,不用更新。";
     }
     return rst;
 }
Example #2
0
        /// <summary>
        /// 工作运行
        /// Note:两次间隔有100豪秒左右的误差
        /// </summary>
        public virtual void Working()
        {
            try
            {
                TaskWorker.Change(Timeout.Infinite, Timeout.Infinite); //暂停计时。
                if (IsRemoving)
                {
                    Log.Warn("任务被标起为移除,回调中断,且任务不会被再次调用。");
                    return;
                }

                //执行今日任务
                _runTimes++;
                var now = SystemTime.Now();
                //Log.Info($"[{this}] 第{_runTimes}次执行开始。[{now:HH:mm:ss ffff}] ◇");
                ChangeStatus(TaskRunStatusType.Working);
                var val = new TaskResult();
                try
                {
                    val = WorkHandler(); //同步委托,任务执行[可能较耗时]
                }
                catch (Exception ex)
                {
                    Log.Error($"执行任务<{Task}>时发生异常:", ex);
                    //throw;
                    val.Result = TaskResultType.Error;
                    val.ExtendMessage = ex.Message;
                }
                finally
                {
                    ChangeStatus(TaskRunStatusType.Worked);
                    Task.Execution.LastRun = now;
                }

                var runSpan = SystemTime.Now() - now;
                Log.Info($"[{this}] 第{_runTimes}次执行结果[{val.Result} : {val.Message}] [Execution:{runSpan}]");

                //Note:工作完成后的状态处理
                //Note:注意,这里的错误次数实际上是执行失败的次数
                if (val.Result.HasFlag(TaskResultType.Error))
                {
                    var sleepInterval = ((TimeSpan)Task.WorkSetting.SleepInterval);
                    Task.Execution.SleepTimes++;
                    Log.Warn($"[{this}] 状态更新[{val.Result}],休眠次数++ ,准备[{sleepInterval}]后再次执行");
                    TaskWorker.Change(sleepInterval, TimeSpan.FromMilliseconds(-1));
                    return;
                }
                else
                {
                    Task.Execution.RunTimes++;
                    var runInterval = Task.GetNextInterval();
                    if (runInterval == null)
                    {
                        ChangeStatus(TaskRunStatusType.Removing);
                        Log.Debug($"[{this}] 下次运行时间为null,当前任务停止。");
                        return;
                    }
                    if (runInterval.Value.TotalMilliseconds > WorkerInterval * 5)
                    {
                        ChangeStatus(TaskRunStatusType.Removing);
                        Log.Debug($"[{this}] 下次运行时间{runInterval},超过5倍工作线程间隔,暂时移除执行队列。当前任务停止。");
                        return;
                    }

                    //var runInterval = runTime.Value.Subtract(now);
                    SaveExecution();
                    Log.Debug($"[{this}]第{_runTimes}次执行结束。 运行成功[Times:{Task.Execution.RunTimes}] ,准备[{runInterval}]后再次执行 ◆");
                    TaskWorker.Change(runInterval.Value, TimeSpan.FromMilliseconds(-1)); //Note:更改计时器约50多毫秒
                }

                #region 根据任务配置做出相应动作

                //本次任务已完成,Note:只有本次任务达到所设条件才算是正常完成,正常完成后才更新最后成功完成的时间。
                if ((Task.Execution.RunTimes >= Task.WorkSetting.Times && Task.WorkSetting.Times > 0) ||
                    (val.Result.HasFlag(TaskResultType.Finished)))
                {
                    //Task.Meta.Execution.LastSucceedRun = PathDate ?? now;   //Note:可自动补全点
                    //Task.Meta.Execution.RunStatus = TaskRunStatusType.TodayComplete;
                    ChangeStatus(TaskRunStatusType.Removing);
                    Log.Debug($"■ [{this}] ({Task.Execution.LastSucceedRun})完成。■");
                    return;
                }

                //根据设定,一旦有错误发生。立即停止
                if (val.Result.HasFlag(TaskResultType.Error) && Task.WorkSetting.ErrorWay == ErrorWayType.Stop)
                {
                    ChangeStatus(TaskRunStatusType.Removing);
                    Log.Info($"▲ [{this}] 根据设定Stop,发生了错误一次,等待移除。▲");
                    return;
                }

                //根据设定,有错误时。休眠超期后停止
                if (Task.Execution.SleepTimes >= Task.WorkSetting.SleepInterval.Times &&
                    Task.WorkSetting.SleepInterval.Times > 0 && Task.WorkSetting.ErrorWay == ErrorWayType.TryAndStop)
                {
                    ChangeStatus(TaskRunStatusType.Removing);
                    Log.Info($"▲ [{this}] 根据设定Sleep,发生了错误{Task.Execution.SleepTimes}次,等待移除。▲");
                    return;
                }

                #endregion

            }
            catch (Exception ex)
            {
                //Note:异常发生后停止该任务,不管任何原因
                Log.Error($"[{this}] 执行异常,停止执行。", ex);
                Stop();
                //throw;
            }
            finally
            {
            }
        }
Example #3
0
        /// <summary>
        /// 工作运行
        /// Note:两次间隔有100豪秒左右的误差
        /// </summary>
        public virtual void Working()
        {
            try
            {
                TaskWorker.Change(Timeout.Infinite, Timeout.Infinite); //暂停计时。
                if (IsRemoving)
                {
                    Log.Warn("任务被标起为移除,回调中断,且任务不会被再次调用。");
                    return;
                }

                //执行今日任务
                _runTimes++;
                var now = SystemTime.Now();
                //Log.Info($"[{this}] 第{_runTimes}次执行开始。[{now:HH:mm:ss ffff}] ◇");
                ChangeStatus(TaskRunStatusType.Working);
                var val = new TaskResult();
                try
                {
                    val = WorkHandler(); //同步委托,任务执行[可能较耗时]
                }
                catch (Exception ex)
                {
                    Log.Error($"执行任务<{Task}>时发生异常:", ex);
                    //throw;
                    val.Result        = TaskResultType.Error;
                    val.ExtendMessage = ex.Message;
                }
                finally
                {
                    ChangeStatus(TaskRunStatusType.Worked);
                    Task.Execution.LastRun = now;
                }

                var runSpan = SystemTime.Now() - now;
                Log.Info($"[{this}] 第{_runTimes}次执行结果[{val.Result} : {val.Message}] [Execution:{runSpan}]");

                //Note:工作完成后的状态处理
                //Note:注意,这里的错误次数实际上是执行失败的次数
                if (val.Result.HasFlag(TaskResultType.Error))
                {
                    var sleepInterval = ((TimeSpan)Task.WorkSetting.SleepInterval);
                    Task.Execution.SleepTimes++;
                    Log.Warn($"[{this}] 状态更新[{val.Result}],休眠次数++ ,准备[{sleepInterval}]后再次执行");
                    TaskWorker.Change(sleepInterval, TimeSpan.FromMilliseconds(-1));
                    return;
                }
                else
                {
                    Task.Execution.RunTimes++;
                    var runInterval = Task.GetNextInterval();
                    if (runInterval == null)
                    {
                        ChangeStatus(TaskRunStatusType.Removing);
                        Log.Debug($"[{this}] 下次运行时间为null,当前任务停止。");
                        return;
                    }
                    if (runInterval.Value.TotalMilliseconds > WorkerInterval * 5)
                    {
                        ChangeStatus(TaskRunStatusType.Removing);
                        Log.Debug($"[{this}] 下次运行时间{runInterval},超过5倍工作线程间隔,暂时移除执行队列。当前任务停止。");
                        return;
                    }

                    //var runInterval = runTime.Value.Subtract(now);
                    SaveExecution();
                    Log.Debug($"[{this}]第{_runTimes}次执行结束。 运行成功[Times:{Task.Execution.RunTimes}] ,准备[{runInterval}]后再次执行 ◆");
                    TaskWorker.Change(runInterval.Value, TimeSpan.FromMilliseconds(-1)); //Note:更改计时器约50多毫秒
                }

                #region 根据任务配置做出相应动作

                //本次任务已完成,Note:只有本次任务达到所设条件才算是正常完成,正常完成后才更新最后成功完成的时间。
                if ((Task.Execution.RunTimes >= Task.WorkSetting.Times && Task.WorkSetting.Times > 0) ||
                    (val.Result.HasFlag(TaskResultType.Finished)))
                {
                    //Task.Meta.Execution.LastSucceedRun = PathDate ?? now;   //Note:可自动补全点
                    //Task.Meta.Execution.RunStatus = TaskRunStatusType.TodayComplete;
                    ChangeStatus(TaskRunStatusType.Removing);
                    Log.Debug($"■ [{this}] ({Task.Execution.LastSucceedRun})完成。■");
                    return;
                }

                //根据设定,一旦有错误发生。立即停止
                if (val.Result.HasFlag(TaskResultType.Error) && Task.WorkSetting.ErrorWay == ErrorWayType.Stop)
                {
                    ChangeStatus(TaskRunStatusType.Removing);
                    Log.Info($"▲ [{this}] 根据设定Stop,发生了错误一次,等待移除。▲");
                    return;
                }

                //根据设定,有错误时。休眠超期后停止
                if (Task.Execution.SleepTimes >= Task.WorkSetting.SleepInterval.Times &&
                    Task.WorkSetting.SleepInterval.Times > 0 && Task.WorkSetting.ErrorWay == ErrorWayType.TryAndStop)
                {
                    ChangeStatus(TaskRunStatusType.Removing);
                    Log.Info($"▲ [{this}] 根据设定Sleep,发生了错误{Task.Execution.SleepTimes}次,等待移除。▲");
                    return;
                }

                #endregion
            }
            catch (Exception ex)
            {
                //Note:异常发生后停止该任务,不管任何原因
                Log.Error($"[{this}] 执行异常,停止执行。", ex);
                Stop();
                //throw;
            }
            finally
            {
            }
        }