예제 #1
0
 public static IApplicationBuilder UseJob(this IApplicationBuilder app)
 {
     AssemblyUtil
     .GetAssemblies("Blog.*.dll", "Sixpence.*.dll")
     .GetTypes()
     .Where(type => !type.IsAbstract && !type.IsInterface && type.GetInterfaces().Contains(typeof(IJob)) && !type.IsDefined(typeof(DynamicJobAttribute), true))
     .Each(type => ServiceContainer.Register(typeof(IJob), type));
     JobHelpers.Start();
     return(app);
 }
예제 #2
0
        /// <summary>
        /// 任务执行
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public Task Execute(IJobExecutionContext context)
        {
            var user = context.JobDetail.JobDataMap.Get("User") as CurrentUserModel;

            return(Task.Factory.StartNew(() =>
            {
                Logger.Debug($"作业:{Name} 开始执行");

                var stopWatch = new Stopwatch();
                stopWatch.Start();
                var broker = PersistBrokerFactory.GetPersistBroker();
                UserIdentityUtil.SetCurrentUser(user);
                try
                {
                    broker.ExecuteTransaction(() =>
                    {
                        Executing(context);
                        // 更新下次执行时间
                        var nextTime = JobHelpers.GetJobNextTime(Name);
                        var nextTimeSql = "";
                        var paramList = new Dictionary <string, object>()
                        {
                            { "@time", DateTime.Now },
                            { "@name", Name }
                        };
                        paramList.Add("@nextTime", nextTime.UtcDateTime);
                        nextTimeSql = ", nextruntime = @nextTime";
                        broker.Execute($"UPDATE job SET lastruntime = @time {nextTimeSql} WHERE name = @name", paramList);
                    });
                }
                catch (Exception e)
                {
                    Logger.Error($"作业:{Name}执行异常", e);
                    throw e;
                }
                stopWatch.Stop();
                Logger.Debug($"作业:{Name} 执行结束,耗时{stopWatch.ElapsedMilliseconds}ms");
            }));
        }