Example #1
0
 public ActiveCommandInfo(PotentialCommand potentialCommand, ActiveStaticCommand executingCommand)
 {
     PotentialCommand = potentialCommand;
     ExecutingCommand = executingCommand;
 }
Example #2
0
        public bool ExecuteActiveCommand(PotentialCommand potentialCommand, ActiveStaticCommand activeStaticCommand)
        {
            if (potentialCommand.StopEvent == null)
            {
                return(true);
            }

            lock (_settingsLock)
                switch (potentialCommand.StopEvent.Id)
                {
                case 1:     //DurationStopEvent
                    var sessionInfo =
                        _stopSchedulerSettings.Sessions.FirstOrDefault(
                            x => x.CommandId == potentialCommand.CallbackId);
                    var durationInfo =
                        _stopSchedulerSettings.DurationStopEventInfos.FirstOrDefault(
                            x => x.CommandId == potentialCommand.CallbackId);

                    var duration = TimeSpan.FromTicks(BitConverter.ToInt64(potentialCommand.StopEvent.Parameter, 0));

                    if (sessionInfo != null)
                    {
                        //if the difference is greater than 1 min. We dont directly compare because there might be a very small difference which comes from dividing the seconds in StartupTime
                        if (Math.Abs((StartupTime - sessionInfo.StartupTime).TotalSeconds) > 60)
                        {
                            //we dont execute this command again in a different session
                            _stopSchedulerSettings.Sessions.Remove(sessionInfo);
                            if (durationInfo != null)
                            {
                                _stopSchedulerSettings.DurationStopEventInfos.Remove(durationInfo);
                            }

                            SaveSchedulerSettings();
                            _dynamicCommandStore.RemoveStoredCommand(potentialCommand);
                            return(false);
                        }

                        if (durationInfo != null)
                        {
                            _stopSchedulerSettings.DurationStopEventInfos.Remove(durationInfo);
                            duration = DateTime.Now - durationInfo.StartTime;

                            if (duration < TimeSpan.Zero)
                            {
                                _stopSchedulerSettings.Sessions.Remove(sessionInfo);
                                SaveSchedulerSettings();
                                _dynamicCommandStore.RemoveStoredCommand(potentialCommand);
                                return(false);
                            }
                        }
                    }
                    else
                    {
                        _stopSchedulerSettings.Sessions.Add(new SessionCommandInfo
                        {
                            CommandId   = potentialCommand.CallbackId,
                            StartupTime = StartupTime
                        });
                    }

                    _stopSchedulerSettings.DurationStopEventInfos.Add(new DurationStopEventInfo
                    {
                        CommandId = potentialCommand.CallbackId,
                        StartTime = DateTime.Now
                    });

                    SaveSchedulerSettings();

                    new Timer(StopCommandCallback, activeStaticCommand, duration,
                              TimeSpan.FromMilliseconds(Timeout.Infinite));
                    return(true);

                case 2:     //DateTimeStopEvent
                    var dateTime = DateTime.FromBinary(BitConverter.ToInt64(potentialCommand.StopEvent.Parameter, 0));
                    var timeSpan = dateTime - DateTime.UtcNow;
                    if (timeSpan < TimeSpan.Zero)
                    {
                        _dynamicCommandStore.RemoveStoredCommand(potentialCommand);
                        return(false);
                    }

                    new Timer(StopAndRemoveCommandCallback,
                              new KeyValuePair <PotentialCommand, ActiveStaticCommand>(potentialCommand,
                                                                                       activeStaticCommand),
                              timeSpan, TimeSpan.FromMilliseconds(-1));
                    break;

                case 3:     //ShutdownStopEvent
                    sessionInfo =
                        _stopSchedulerSettings.Sessions.FirstOrDefault(
                            x => x.CommandId == potentialCommand.CallbackId);
                    if (sessionInfo != null)
                    {
                        //if the difference is greater than 1 min. We dont directly compare because there might be a very small difference which comes from dividing the seconds in StartupTime
                        if (Math.Abs((StartupTime - sessionInfo.StartupTime).TotalSeconds) > 60)
                        {
                            //we dont execute this command again in a different session
                            _stopSchedulerSettings.Sessions.Remove(sessionInfo);
                            SaveSchedulerSettings();
                            _dynamicCommandStore.RemoveStoredCommand(potentialCommand);
                            return(false);
                        }
                    }
                    else
                    {
                        _stopSchedulerSettings.Sessions.Add(new SessionCommandInfo
                        {
                            CommandId   = potentialCommand.CallbackId,
                            StartupTime = StartupTime
                        });
                        SaveSchedulerSettings();
                    }

                    return(true);

                default:
                    return(true);
                }

            return(true);
        }