Ejemplo n.º 1
0
        private static void OnTimedEvent(object source)
        {
            ScheduleTask task = source as ScheduleTask;

            if (task is MonsterTask)
            {
                MonsterTask monsterTask = task as MonsterTask;
                if (monsterTask.mapCode == 1)
                {
                    long ticks1 = TimeUtil.NOW();
                    SysConOut.WriteLine("----------------------------------时间触发器时间间隔----------------------------" + (ticks1 - ScheduleExecutor2.Instance.Oldticks).ToString());
                    ScheduleExecutor2.Instance.Oldticks = ticks1;
                }
            }
            if (task.InternalLock.TryEnter())
            {
                bool logRunTime = false;
                long nowTicks   = TimeUtil.CurrentTicksInexact;
                try
                {
                    task.run();
                }
                catch (System.Exception ex)
                {
                    LogManager.WriteLog(LogTypes.Error, string.Format("{0}执行时异常,{1}", task.ToString(), ex.ToString()));
                }
                finally
                {
                    logRunTime = task.InternalLock.Leave();
                }

                if (logRunTime)
                {
                    long finishTicks = TimeUtil.CurrentTicksInexact;
                    if (finishTicks - nowTicks > TimeUtil.SECOND)
                    {
                        try
                        {
                            MonsterTask monsterTask = task as MonsterTask;
                            if (null != monsterTask)
                            {
                                LogManager.WriteLog(LogTypes.Error, string.Format("{0} mapCode:{1},subMapCode:{2},执行时间:{3}毫秒"
                                                                                  , task.ToString(), monsterTask.mapCode, monsterTask.subMapCode, finishTicks - nowTicks));
                            }
                            else
                            {
                                LogManager.WriteLog(LogTypes.Error, string.Format("{0}执行时间:{1}毫秒", task.ToString(), finishTicks - nowTicks));
                            }
                        }
                        catch
                        {
                            //写日志异常就不记了
                        }
                    }
                }
            }
        }
Ejemplo n.º 2
0
        private static void OnTimedEvent(object source)
        {
            ScheduleTask task = source as ScheduleTask;

            if (task.InternalLock.TryEnter())
            {
                bool logRunTime = false;
                long nowTicks   = TimeUtil.CurrentTicksInexact;
                try
                {
                    task.run();
                }
                catch (Exception ex)
                {
                    LogManager.WriteLog(LogTypes.Error, string.Format("{0}执行时异常,{1}", task.ToString(), ex.ToString()), null, true);
                }
                finally
                {
                    logRunTime = task.InternalLock.Leave();
                }
                if (logRunTime)
                {
                    long finishTicks = TimeUtil.CurrentTicksInexact;
                    if (finishTicks - nowTicks > 1000L)
                    {
                        try
                        {
                            MonsterTask monsterTask = task as MonsterTask;
                            if (null != monsterTask)
                            {
                                LogManager.WriteLog(LogTypes.Error, string.Format("{0} mapCode:{1},subMapCode:{2},执行时间:{3}毫秒", new object[]
                                {
                                    task.ToString(),
                                    monsterTask.mapCode,
                                    monsterTask.subMapCode,
                                    finishTicks - nowTicks
                                }), null, true);
                            }
                            else
                            {
                                LogManager.WriteLog(LogTypes.Error, string.Format("{0}执行时间:{1}毫秒", task.ToString(), finishTicks - nowTicks), null, true);
                            }
                        }
                        catch
                        {
                        }
                    }
                }
            }
        }
Ejemplo n.º 3
0
        private static void OnTimedEvent(object source)
        {
            ScheduleTask task = source as ScheduleTask;

            if (task.InternalLock.TryEnter())
            {
                try
                {
                    task.run();
                }
                catch (System.Exception ex)
                {
                    LogManager.WriteLog(LogTypes.Error, string.Format("{0}执行时异常,{1}", task.ToString(), ex.ToString()));
                }
                finally
                {
                    task.InternalLock.Leave();
                }
            }
            else
            {
                try
                {
                    MonsterTask monsterTask = task as MonsterTask;
                    if (null != monsterTask)
                    {
                        LogManager.WriteLog(LogTypes.Error, string.Format("{0}自身执行时间超过设定的时间间隔,本次执行取消,mapCode:{1},subMapCode:{2}"
                                                                          , task.ToString(), monsterTask.mapCode, monsterTask.subMapCode));
                    }
                    else
                    {
                        LogManager.WriteLog(LogTypes.Error, string.Format("{0}自身执行时间超过设定的时间间隔,本次执行取消", task.ToString()));
                    }
                }
                catch
                {
                    //写日志异常就不记了
                }
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Recursive method to execute a schedule task and when complete and successful, execute all child tasks in parallel.
        /// </summary>
        private static bool ExecuteScheduleTask(ScheduleTask task)
        {
            var shouldTaskRun = true;
            var taskComplete  = false;

            switch (task.Type)
            {
            case ScheduleTaskType.ManagementAgent:
                if (task.OnlyRunIfPendingExportsExist)
                {
                    Log.Information($"{LoggingPrefix}Evaluating potential execution of '{task.Name}' Management Agent, run profile: '{task.Command}'");
                    shouldTaskRun = PendingExportsInManagementAgent(task.Name);
                }
                if (shouldTaskRun)
                {
                    Log.Information($"{LoggingPrefix}Executing the '{task.Name}' Management Agent, run profile: '{task.Command}'");
                    taskComplete = ExecuteMimRunProfile(task.Name, task.Command, out var retryRequired);
                    if (retryRequired)
                    {
                        task.RetryRequired = true;
                    }
                }
                break;

            case ScheduleTaskType.PowerShell:
                Log.Information($"{LoggingPrefix}Executing '{task.Name}' PowerShell script: '{task.Command}'");
                taskComplete = ExecutePowerShellScript(task.Command);
                break;

            case ScheduleTaskType.VisualBasicScript:
                Log.Information($"{LoggingPrefix}Executing '{task.Name}' Visual Basic script: '{task.Command}'");
                taskComplete = ExecuteVisualBasicScript(task.Command);
                break;

            case ScheduleTaskType.Executable:
                Log.Information($"{LoggingPrefix}Executing '{task.Name}' executable: '{task.Command}'");
                taskComplete = ExecuteExecutable(task.Command, task.Arguments, task.ShowExecutableWindow);
                break;

            case ScheduleTaskType.SqlServer:
                Log.Information($"{LoggingPrefix}Executing '{task.Name}' sql server command: '{task.Command}' on server: '{task.Server}'");
                taskComplete = ExecuteSqlServerCommand(task.Command, task.Server);
                break;

            case ScheduleTaskType.Block:
                Log.Information($"{LoggingPrefix}Executing block '{task.Name}'");
                taskComplete = true;
                break;

            case ScheduleTaskType.ContinuationCondition:
                if (task.ConditionType == ContinuationConditionType.ManagementAgentsHadImports && !ManagementAgentImportsHadChanges)
                {
                    Log.Information(LoggingPrefix + "ContinuationCondition:ManagementAgentsHadImports - No pending imports detected. Task: " + task.ToString());
                    shouldTaskRun = false;
                }
                else
                {
                    taskComplete = true;
                }
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            #region validation
            if (!taskComplete && StopOnIncompletion)
            {
                return(false);
            }

            if (!shouldTaskRun)
            {
                Log.Information(LoggingPrefix + "Logic has determined this and any child tasks should not run. Task: " + task.ToString());
                return(false);
            }

            if (taskComplete || task.ChildTasks.Count <= 0)
            {
                return(ExecuteTasks(task.ChildTasks));
            }
            Log.Warning(LoggingPrefix + "Not executing child tasks due to the last error event. Current task: " + task.ToString());
            return(false);

            #endregion
        }
Ejemplo n.º 5
0
        public void ThreadFunction()
        {
            while (true)
            {
                foreach (var s in TimerDict)
                {
                    ScheduleTask task = s.Key as ScheduleTask;
                    if (task.InternalLock.TryEnter())
                    {
                        bool logRunTime = false;
                        long nowTicks   = TimeUtil.CurrentTicksInexact;
                        try
                        {
                            task.run();
                        }
                        catch (System.Exception ex)
                        {
                            LogManager.WriteLog(LogTypes.Error, string.Format("{0}执行时异常,{1}", task.ToString(), ex.ToString()));
                        }
                        finally
                        {
                            logRunTime = task.InternalLock.Leave();
                        }

                        if (logRunTime)
                        {
                            long finishTicks = TimeUtil.CurrentTicksInexact;
                            if (finishTicks - nowTicks > TimeUtil.SECOND)
                            {
                                try
                                {
                                    MonsterTask monsterTask = task as MonsterTask;
                                    if (null != monsterTask)
                                    {
                                        LogManager.WriteLog(LogTypes.Error, string.Format("{0} mapCode:{1},subMapCode:{2},执行时间:{3}毫秒"
                                                                                          , task.ToString(), monsterTask.mapCode, monsterTask.subMapCode, finishTicks - nowTicks));
                                    }
                                    else
                                    {
                                        LogManager.WriteLog(LogTypes.Error, string.Format("{0}执行时间:{1}毫秒", task.ToString(), finishTicks - nowTicks));
                                    }
                                }
                                catch
                                {
                                    //写日志异常就不记了
                                }
                            }
                        }
                    }
                }
                Thread.Sleep(2000);
            }
        }