Hangfire:任务定时调度


 Hangfire带管理后台的一个任务调度,免费开源、扩展包PRO收费

Quick Start

1.新建空的.Net core web项目,添加Nuget包

Install-Package Hangfire

2.修改Startup.cs

public class Startup
    {
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddHangfire(x => x.UseSqlServerStorage("Data Source=.;Initial Catalog=HangFire;User ID=sa;Password=sasa")); services.AddHangfireServer();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseHangfireServer(); app.UseHangfireDashboard();

            app.Run(context =>
            {
                return context.Response.WriteAsync("Hello from ASP.NET Core!");
            });
        }
    }

至此即可运行查看效果https://localhost:44319/hangfire;当然现在还没有任务被调度。

 

任务调度:

    [Route("[controller]/[action]")]
    [ApiController]
    public class DemoController : Controller
    {

        #region BackgroundJob
        [HttpGet]
        public string One()
        {
            //基于任务队列

            //任务执行一次
            var jobId = BackgroundJob.Enqueue(() => Console.WriteLine($"OneTime:{DateTime.Now}"));
            return "OK," + jobId;
        }
        [HttpGet]
        public string DelayDemo()
        {
            //延时任务

            //任务延时两分钟执行
            Hangfire.BackgroundJob.Schedule(() => Console.WriteLine($"Delay two minute:{DateTime.Now}"), TimeSpan.FromMinutes(2));
            return "OK";
        }
        [HttpGet]
        public string ContinueJobDemo()
        {
            //连续执行任务
            var jobId = BackgroundJob.Schedule(() => Console.WriteLine($"Delay two minute:{DateTime.Now}"), TimeSpan.FromMinutes(2));

            BackgroundJob.ContinueJobWith(jobId, () => Console.WriteLine("Continuation!"));
            return "OK";
        }
        #endregion


        [HttpGet]
        public string CronDemo()
        {
            //Cron方式
            //任务每分钟执行一次
            RecurringJob.AddOrUpdate(() => Console.WriteLine($"Every minute:{DateTime.Now}"), Cron.Minutely());
            return "OK";

        }

        public string BatchJobDemo()
        {
            //PRO 版本 支持
            //var batchId =Batch.StartNew(x =>
            //{
            //    x.Enqueue(() => Console.WriteLine("Job 1"));
            //    x.Enqueue(() => Console.WriteLine("Job 2"));
            //});

            //Batch.ContinueWith(batchId, x =>
            //{
            //    x.Enqueue(() => Console.WriteLine("Last Job"));
            //});
            return "OK";
        }

    }

 

 

 

 

 

 

 

 

 

 

 

资源:

GitHub:https://github.com/HangfireIO/Hangfire

http://hangfire.io/

http://docs.hangfire.io/en/latest/


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM