예제 #1
0
        /// <summary>
        /// 执行任务
        /// </summary>
        /// <param name="task"></param>
        private static void DoJob(TaskInfo task)
        {
            try
            {
                task.AsyncOperationResult = task.TaskInstance.ExecuteTask(task.FolderOfTask, task.TimeTagOfNowOperation);
            }
            catch (Exception ex)
            {
                task.ErrorTimesOfNowOperation = task.ErrorTimesOfNowOperation + 1;
                //失败执行写入日志
                ServiceLog.SaveLog("执行任务(DoJob):" + task.TaskName + "出错!" + DateTime.Now.ToString(CultureInfo.InvariantCulture) + "\r\n" + ex.Message + "\r\n" + ex.StackTrace + "\r\n");
                return;
            }

            var taskConfig = task.ConfigOfTask;

            if (taskConfig.LoopType == TaskLoopType.NotLoop)
            {
                task.HaveExecuteIfNotLoop = task.AsyncOperationResult.IsSuccess;
            }

            if (task.AsyncOperationResult.IsSuccess)
            {
                task.ErrorTimesOfNowOperation = 0;
                task.IsSuccessOfNowOperation  = true;
                //成功执行写入日志
                ServiceLog.SaveLog("执行任务:" + task.TaskName + "成功!" + DateTime.Now.ToString(CultureInfo.InvariantCulture) + "\r\n");
            }
            else
            {
                task.ErrorTimesOfNowOperation = task.ErrorTimesOfNowOperation + 1;
                //失败执行写入日志
                ServiceLog.SaveLog("执行任务:" + task.TaskName + "失败!" + DateTime.Now.ToString(CultureInfo.InvariantCulture) + "\r\n");
            }
        }
예제 #2
0
        protected override void OnStart(string[] args)
        {
            ServiceLog.SaveLog("服务启动!" + DateTime.Now.ToString(CultureInfo.InvariantCulture) + "\r\n");

            //初始化任务信息
            TaskPolling.InitTaskInfos();
            //启动轮询进程
            Thread wt = new Thread(new ThreadStart(TaskPolling.PollingThreadEntrance));

            wt.Start();
        }
예제 #3
0
        /// <summary>
        /// 执行任务的回调函数
        /// </summary>
        /// <param name="result"></param>
        private static void  CallBackOfDoJob(IAsyncResult result)
        {
            TaskInfo task = (TaskInfo)result.AsyncState;

            try
            {
                DoJobDelegate del = (DoJobDelegate)task.ExternOperationObject;
                del.EndInvoke(result);
            }
            catch (Exception ex)
            {
                task.ErrorTimesOfNowOperation = task.ErrorTimesOfNowOperation + 1;
                //失败执行写入日志
                ServiceLog.SaveLog("执行任务(CallBackOfDoJob):" + task.TaskName + "失败!" + DateTime.Now.ToString() + "\r\n" + ex.Message + "\r\n" + ex.StackTrace + "\r\n");
            }
            finally
            {
                task.IsExecuting     = false;
                task.LastExecuteTime = DateTime.Now;
            }
        }
예제 #4
0
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        static void Main()
        {
            try
            {
                //获取运行路径
                RuntimeInfo.ApplicationFolder = System.AppDomain.CurrentDomain.BaseDirectory;
                if (!RuntimeInfo.ApplicationFolder.EndsWith("\\"))
                {
                    RuntimeInfo.ApplicationFolder += "\\";
                }

                ServiceBase[] ServicesToRun;
                ServicesToRun = new ServiceBase[] { new Service1() };
                ServiceBase.Run(ServicesToRun);
            }
            catch (Exception ex)
            {
                string msg = "#---------------------------------------------------------- \r\n"
                             + DateTime.Now.ToString(CultureInfo.InvariantCulture) + "\r\nScheduledTask.Service定时任务计划出错,详细错误信息为:" + ex;
                ServiceLog.SaveLog(msg);
            }
        }
예제 #5
0
        /// <summary>
        /// 初始化任务信息
        /// </summary>
        public static void InitTaskInfos()
        {
            //加载定时任务
            string strTaskAssembly = System.Configuration.ConfigurationManager.AppSettings["OATaskAssemblys"];

            string[] taskAssemblys = strTaskAssembly.Split('|');
            for (int t = 0; t < taskAssemblys.Length; t++)
            {
                try
                {
                    string[] items    = taskAssemblys[t].Split(',');
                    TaskInfo loopInfo = new TaskInfo();
                    loopInfo.FolderOfTask = RuntimeInfo.ApplicationFolder;
                    if (!loopInfo.FolderOfTask.EndsWith(@"\"))
                    {
                        loopInfo.FolderOfTask += @"\";
                    }
                    AppDomain loopDomain = AppDomain.CreateDomain(items[0], null, new AppDomainSetup()
                    {
                        ApplicationName   = items[0],
                        ApplicationBase   = loopInfo.FolderOfTask + items[0],
                        ConfigurationFile = items[0] + ".config"
                    });
                    loopInfo.FolderOfTask = loopInfo.FolderOfTask + items[0] + @"\";
                    string assemblyPath  = loopInfo.FolderOfTask + items[1];
                    string taskClassName = items[2];
                    loopInfo.DomainToRun  = loopDomain;
                    loopInfo.TaskInstance = (ITask)loopDomain.CreateInstanceFromAndUnwrap(assemblyPath, taskClassName);
                    loopInfo.ConfigOfTask = loopInfo.TaskInstance.Init(loopInfo.FolderOfTask);
                    loopInfo.TaskName     = loopInfo.ConfigOfTask.TaskName;
                    TaskInfoList.Add(loopInfo);
                }
                catch (Exception ex)
                {
                    ServiceLog.SaveLog("获取定时任务配置:" + taskAssemblys[t] + "的初始化任务信息时失败:" + ex.Message);
                }
            }
        }
예제 #6
0
        /// <summary>
        /// 轮询线程的入口
        /// </summary>
        public static void PollingThreadEntrance()
        {
            while (RuntimeInfo.ContinueWork)
            {
                for (int i = 0; i < 60 && RuntimeInfo.ContinueWork; i++)
                {
                    Thread.Sleep(1000);
                }
                //每隔1分钟进行一次调度
                if (RuntimeInfo.ContinueWork)
                {
                    for (int i = 0; i < TaskInfoList.Count; i++)
                    {
                        TaskInfo theTaskInfo = TaskInfoList[i];
                        if (theTaskInfo.IsExecuting)
                        {
                            continue;
                        }

                        var    taskConfig = theTaskInfo.ConfigOfTask;
                        string timeTag    = GetTimeTagOfLatestOperation(theTaskInfo);

                        if (string.IsNullOrEmpty(theTaskInfo.TimeTagOfNowOperation))
                        {
                            theTaskInfo.TimeTagOfNowOperation = timeTag;
                        }

                        StringBuilder logBuffer = new StringBuilder();
                        logBuffer.Append("调度任务:本次计算所得的调度时间为[").Append(timeTag).Append("],");
                        logBuffer.Append("任务已执行的调度时间为[").Append(theTaskInfo.TimeTagOfNowOperation).AppendLine("]。");

                        bool     doJob = false;
                        TimeSpan tsToLastExecute;
                        switch (taskConfig.LoopType)
                        {
                        case TaskLoopType.NotLoop:
                            if (!theTaskInfo.HaveExecuteIfNotLoop)
                            {
                                doJob = true;
                            }
                            break;

                        case TaskLoopType.SecondInterval:
                            tsToLastExecute = DateTime.Now.Subtract(theTaskInfo.LastExecuteTime);
                            if (tsToLastExecute.TotalSeconds > taskConfig.LoopInterval)
                            {
                                doJob = true;
                            }
                            break;

                        case TaskLoopType.PerDay:
                        case TaskLoopType.PerHour:
                        case TaskLoopType.PerMonth:
                        case TaskLoopType.PerWeek:
                            if (timeTag != theTaskInfo.TimeTagOfNowOperation)
                            {
                                theTaskInfo.TimeTagOfNowOperation = timeTag;
                                doJob = true;
                            }
                            else if (!theTaskInfo.IsSuccessOfNowOperation && theTaskInfo.ErrorTimesOfNowOperation < taskConfig.TryTimesIfFailure)
                            {
                                tsToLastExecute = DateTime.Now.Subtract(theTaskInfo.LastExecuteTime);
                                if (tsToLastExecute.TotalSeconds > taskConfig.TryIntervalAfterFailure)
                                {
                                    logBuffer.AppendLine("当前为失败后重试");
                                    doJob = true;
                                }
                            }
                            break;

                        default:
                            break;
                        }

                        if (doJob)
                        {
                            logBuffer.AppendLine("经过计算,本次轮询将执行任务新的轮次");
                            DoJobDelegate del = new DoJobDelegate(DoJob);
                            theTaskInfo.IsExecuting           = true;
                            theTaskInfo.ExternOperationObject = del;
                            del.BeginInvoke(theTaskInfo, new AsyncCallback(CallBackOfDoJob), theTaskInfo);
                        }
                        else
                        {
                            logBuffer.AppendLine("本次轮询不执行任务");
                        }

                        ServiceLog.SaveDebugLog(theTaskInfo.TaskName, logBuffer.ToString());
                    }
                }
            }
        }
예제 #7
0
 protected override void OnStop()
 {
     RuntimeInfo.ContinueWork = false;
     ServiceLog.SaveLog("服务结束!" + DateTime.Now.ToString(CultureInfo.InvariantCulture) + "\r\n");
 }