コード例 #1
0
 public ScheduleMonitorModel()
 {
     if (_db == null)
     {
         _db = new ScheduleMonitorDb();
     }
 }
コード例 #2
0
 public ScheduleMonitorModel(ScheduleMonitorDb db)
 {
     _db = db;
 }
        /// <summary>
        /// The background thread.
        /// </summary>
        private void ThreadProcess()
        {
            while (!m_done.WaitOne(1000, false))
            {
                if (m_running.WaitOne(0, false))
                {
                    using (_db = new ScheduleMonitorDb())
                    {
                        try
                        {
                            if (_db.ClientCommands.Any(x => x.IsScheduled == true && x.IsExecuted == false))
                            {
                                var command = _db.ClientCommands.Where(x => x.IsScheduled && x.IsExecuted == false).OrderBy(c => c.ScheduledTime).First();

                                if (command.ScheduledTime != null && command.ScheduledTime < DateTime.Now)
                                {
                                    var missedCommandLog = _db.ClientCommandLogs.Add(new ClientCommandLog()
                                    {
                                        ClientCommandId = command.ClientCommandId,
                                        LogText         = "Missed the Scheduled command : " + command.Command + " " + DateTime.Now,
                                        LogType         = "MISSED COMMAND"
                                    });
                                    command.IsExecuted = true;

                                    _db.SaveChanges();

                                    Log.Info("Executed the Scheduled command : " + command.Command + " " + DateTime.Now);
                                    continue;
                                }

                                EventLog.WriteEntry("ScheduleRunnerService", DateTime.Now.ToLongTimeString() + " " + command.Command);

                                base.OnCustomCommand(Convert.ToInt32(command.Command));

                                command.IsExecuted = true;

                                var commandLog = _db.ClientCommandLogs.Add(new ClientCommandLog()
                                {
                                    ClientCommandId = command.ClientCommandId,
                                    LogText         = "Executed the Scheduled command : " + command.Command,
                                    LogType         = "OUTPUT"
                                });

                                _db.SaveChanges();

                                Log.Info("Executed the Scheduled command : " + command.Command);
                            }
                        }
                        catch (Exception ex)
                        {
                            EventLog.WriteEntry("ScheduleRunnerService", DateTime.Now.ToLongTimeString() + " " + ExceptionMessageFull(ex));
                            Log.Error(ExceptionMessageFull(ex));
                            break;
                        }
                    }

                    // Log4Net logging methods.
                    //Log.Debug("This is the debug message");
                }
                else
                {
                    // Wait to be signalled
                    m_running.WaitOne();
                }
            }
        }
        protected override void OnCustomCommand(int command)
        {
            try
            {
                base.OnCustomCommand(command);
                System.Diagnostics.EventLog.WriteEntry("ScheduleRunnerService", DateTime.Now.ToLongTimeString() + " Custom command recieved " + command);
                //log4net.LogicalThreadContext.Properties["CommandValue"] = command.ToString();
                var commandString = command.ToString();
                using (var _db = new ScheduleMonitorDb())
                {
                    try
                    {
                        if (_db.ClientCommands.Any(x => x.Command == commandString))
                        {
                            var clientCommand = _db.ClientCommands.First(x => x.Command == commandString);
                            EventLog.WriteEntry("ScheduleRunnerService", DateTime.Now.ToLongTimeString() + " " + clientCommand.Command);

                            //base.OnCustomCommand(command);

                            clientCommand.IsExecuted = true;

                            var commandLog = _db.ClientCommandLogs.Add(new ClientCommandLog()
                            {
                                ClientCommandId = clientCommand.ClientCommandId,
                                LogText         = "Executed the Custom command : " + clientCommand.Command,
                                LogType         = "CUSTOM COMMAND OUTPUT"
                            });
                            EventLog.WriteEntry("ScheduleRunnerService", DateTime.Now.ToLongTimeString() + " Custom command recieved and linked to a client" + clientCommand.Command);

                            _db.SaveChanges();
                        }
                        else
                        {
                            var commandLog = _db.ClientCommandLogs.Add(new ClientCommandLog()
                            {
                                ClientCommandId = null,
                                LogText         = "Executed the Custom command : " + command,
                                LogType         = "CUSTOM COMMAND OUTPUT"
                            });

                            _db.SaveChanges();
                            EventLog.WriteEntry("ScheduleRunnerService", DateTime.Now.ToLongTimeString() + " Custom command recieved " + command);
                        }
                        Log.Info("Executed the Custom command : " + command);
                    }
                    catch (Exception ex)
                    {
                        EventLog.WriteEntry("ScheduleRunnerService", DateTime.Now.ToLongTimeString() + " " + ExceptionMessageFull(ex));
                        Log.Error(ExceptionMessageFull(ex));
                    }
                }

                //using (_db = new ScheduleMonitorDb())
                //{
                //    _db.ClientCommandLogs.Add(new ClientCommandLog
                //    {
                //        ClientCommandId = 103,
                //        LogType = "OUTPUT",
                //        LogText = " Custom command recieved " + command
                //    });

                //    _db.SaveChanges();
                //}

                //Log.Info("Ran custom command : " + command);
            }
            catch (Exception ex)
            {
                Log.Error("Error while running custom command : " + command + " " + ex.Message);
            }
        }