ASP.Net MVC with Hangfire

定时任务迁移到定时任务平台,移除项目中使用的Hangfire。

代码做备份,以防后续用到

using System;
using System.Threading.Tasks;
using Microsoft.Owin;
using Owin;
using Hangfire;
using System.Diagnostics;
using HiP.RFQ.BLL;
using Hangfire.Dashboard;

[assembly: OwinStartup(typeof(HiP.RFQ.UI2.Startup))]

namespace HiP.RFQ.UI2
{
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=316888

            GlobalConfiguration.Configuration.UseSqlServerStorage("HangfireDBConnection");
            app.UseHangfireServer();
            //app.UseHangfireDashboard();

            var options = new DashboardOptions
            {
                Authorization = new[] { new HangfireAuthorizationFilter() },
                IgnoreAntiforgeryToken=true,
                DisplayStorageConnectionString=true,
                IsReadOnlyFunc=context=>false,
            };
            app.UseHangfireDashboard("/hangfire", options);

            ////支持基于队列的任务处理:任务执行不是同步的,而是放到一个持久化队列中,以便马上把请求控制权返回给调用者。
            //var jobId = BackgroundJob.Enqueue(() => WriteLog("队列任务"));

            ////延迟任务执行:不是马上调用方法,而是设定一个未来时间点再来执行。
            //BackgroundJob.Schedule(() => WriteLog("延时任务"), TimeSpan.FromSeconds(10));
#if DEBUG

            Console.WriteLine("DEBUG:11111111111");
#else
           //循环任务执行:一行代码添加重复执行的任务,其内置了常见的时间循环模式,也可基于CRON表达式来设定复杂的模式。
            RecurringJob.AddOrUpdate(() => new QuotationService().NotifyBDDirectorforQuotationApprovingJob(), Cron.Daily(7,30), TimeZoneInfo.Local); //注意最小单位是分钟
           //RecurringJob.AddOrUpdate(() => new UserCompanyRoleService().SyncOMGMRoleMemberFromWF(), Cron.Daily(7,30), TimeZoneInfo.Local); //注意最小单位是分钟
#endif


            ////延续性任务执行:类似于.NET中的Task,可以在第一个任务执行完之后紧接着再次执行另外的任务
            //BackgroundJob.ContinueJobWith(jobId, () => WriteLog("连续任务"));
        }
        public void WriteLog(string msg)
        {
            Debug.WriteLine($"Hangfire于{DateTime.Now}执行了任务[{msg}]");
        }
    }


    public class HangfireAuthorizationFilter : IDashboardAuthorizationFilter
    {
        //这里需要配置权限规则
        public bool Authorize(DashboardContext context)
        {
            return new UserService().IsAdmin();
        }
    }
}

 

posted @ 2020-02-18 13:25  春秋一梦  阅读(505)  评论(0编辑  收藏  举报