示例#1
0
        private void StartTimer()
        {
            //初始化Timer
            _pluginTimer = new Timer(Excute, null, -1, -1);
            //设置标志位
            _cancel = false;
            ExecuteRecord record;
            int           executeDelaySecond;

            DebugLogEx(string.Format("StartTimer,PluginConfig.ResetTimer {0}", _pluginConfig.ResetTimer));
            //如果 ResetTimerWhenStartUp 为false 并且可以获取到上次执行时间
            //则根据上次执行时间推算得出开始执行的延迟时间
            var findRecod = PluginExecuteHelper.TryReadRecord(_pluginConfig.TypeKey, out record);

            if (!_pluginConfig.ResetTimer &&
                findRecod &&
                _pluginConfig.CollectionInterval > 60)
            {
                var now        = DateTime.Now;
                var spanSecond = (int)(now - record.LastExecuteTime).TotalSeconds;
                executeDelaySecond = spanSecond > _pluginConfig.CollectionInterval
                    ? 0
                    : _pluginConfig.CollectionInterval - spanSecond;
                DebugLogEx(
                    string.Format(
                        "StartTimer,Use ExecuteDelay In ExecuteRecord: Now {0},LastExecuteTime {1}",
                        now, record.LastExecuteTime));
            }
            else
            {
                //如果 ResetTimerWhenStartUp 为true
                //或者 获取不到 LastExecuteTime
                //或者 CollectionInterval 小等于 60秒
                //则使用 ExecuteDelay 作为延迟
                //如果没有 使用插件中定义的延迟时间
                executeDelaySecond = _pluginConfig.ExecuteDelay;
                DebugLogEx("StartTimer,Use PluginConfig.ExecuteDelay");
            }
            DebugLogEx(
                string.Format(
                    "StartTimer,Execute Delay Time:{0} ,Interval= {1}", executeDelaySecond,
                    _pluginConfig.CollectionInterval));
            //启动执行
            var dueTime = new TimeSpan(executeDelaySecond * TimeSpan.TicksPerSecond);
            var period  = new TimeSpan(_pluginConfig.CollectionInterval * TimeSpan.TicksPerSecond);

            _pluginTimer.Change(dueTime, period);
            //20161123 by guodp for 注册插件 记录插件执行记录
            PluginExecuteHelper.Register(_pluginConfig.TypeKey);
        }
示例#2
0
 /// <summary>
 ///     数据主方法
 /// </summary>
 private void Excute(object sender)
 {
     DebugLogEx(string.Format("Excute"));
     using (
         var scope =
             PluginExecuteHelper.GetExecuteScope(_pluginConfig.TypeKey))
     {
         try
         {
             if (_cancel)
             {
                 DebugLogEx(string.Format("Excute Canceled"));
                 scope.Cancel();
                 return;
             }
             //检查是否可以执行
             if (!CheckExecutable())
             {
                 scope.Miss();
                 return;
             }
             try
             {
                 Interlocked.Increment(ref _runningCount);
                 _executing = true;
                 InnerExcute();
                 scope.Success();
             }
             catch
             {
                 scope.Fail();
                 throw;
             }
             finally
             {
                 _executing = false;
                 Interlocked.Decrement(ref _runningCount);
             }
         }
         catch (Exception ex)
         {
             ExceptionLog(string.Format("Excute Error"), ex);
         }
     }
 }