예제 #1
0
 public ServiceCommandExecuteResult(ServiceCommand command)
 {
     this.Name      = command.GetType().Name;
     this.StartTime = command.CommandStartTime.Value;
     this.Duration  = command.CommandEndTime.Value - command.CommandStartTime.Value;
 }
예제 #2
0
        private void Start()
        {
            while (true)
            {
                if (_IsSuspend == 1)
                {
                    _AutoResetEvent.WaitOne();
                    continue;
                }
                var l = new List <ServiceCommand>();
                while (_CommandList.TryDequeue(out var cm))
                {
                    if (cm == null)
                    {
                        continue;
                    }
                    l.Add(cm);
                }

                var            now = DateTimeOffset.Now;
                DateTimeOffset?minNextStartTime = null;
                foreach (var cm in l)
                {
                    //Not execute command until schedule time will come.
                    if (cm.ScheduleTime > now)
                    {
                        _CommandList.Enqueue(cm);
                        if (minNextStartTime == null || minNextStartTime > cm.ScheduleTime)
                        {
                            minNextStartTime = cm.ScheduleTime;
                        }
                        continue;
                    }
                    try
                    {
                        var sw = Stopwatch.StartNew();
                        cm.CommandStartTime = DateTimeOffset.Now;
                        _CurrentCommand     = cm;
                        cm.Execute();
                        cm.CommandEndTime = DateTimeOffset.Now;
                        sw.Stop();

                        Interlocked.Increment(ref _ExecutedCommandCount);
                        Interlocked.Add(ref _ExecutedSeconds, sw.ElapsedTicks / TimeSpan.TicksPerSecond);
                        if (_PreviousResetTime.Day != now.Day)
                        {
                            Interlocked.Exchange(ref _ExecutedCommandCount, 0);
                            Interlocked.Exchange(ref _ExecutedSeconds, 0);
                        }
                        _PreviousResetTime = now;
                        this.Executed?.Invoke(this, new ServiceCommandEventArgs(cm, null));
                    }
                    catch (Exception ex)
                    {
                        cm.CommandEndTime = DateTimeOffset.Now;
                        try
                        {
                            this.Error?.Invoke(this, new ServiceCommandEventArgs(cm, ex));
                        }
                        catch { }
                    }
                    finally
                    {
                        _CurrentCommand = null;
                    }
                    if (this.ThreadSleepSecondsPerCommand > 0)
                    {
                        Thread.Sleep(this.ThreadSleepSecondsPerCommand);
                    }
                }
                _PreviousCommandList = l;

                if (minNextStartTime.HasValue)
                {
                    var ts = minNextStartTime.Value - DateTimeOffset.Now;
                    if (ts.TotalMilliseconds > 0)
                    {
                        _AutoResetEvent.WaitOne((Int32)ts.TotalMilliseconds);
                    }
                    else
                    {
                        continue;
                    }
                }
                else
                {
                    _AutoResetEvent.WaitOne();
                }
            }
        }
예제 #3
0
 public void AddCommand(ServiceCommand command)
 {
     _CommandList.Enqueue(command);
     _AutoResetEvent.Set();
 }
예제 #4
0
 public ServiceCommandEventArgs(ServiceCommand command, Exception exception)
 {
     this.Command   = command;
     this.Exception = exception;
 }