類似Windows排程的一個套件,不過他有Dashboard可以看
可以用在商業用途
簡單來說如果你需要定時的執行某一段程式就可以使用這個套件來幫你完成。
| 版本 | 價格 | 
|---|---|
| Open | Free | 
| Startup | 500 / year | 
| Business | 1,500 / yea | 
| Enterprise | 4,500/ year | 

安裝環境NET6
主要安裝兩個套件
目前範例是將hangfire的紀錄存在記憶體之中
先註冊AddHangfire及AddHangfireServer,並且在MideeleWave的地方加入UseHangfireDashboard

Backgroud job方法
        // GET: api/<HangfireDemoController>
        [HttpGet]
        public IActionResult OneNigh()
        {
            // 第一招射後不理
            // fire and got:站台啟動後只會執行一次
            BackgroundJob.Enqueue(() => Debug.WriteLine("fire and got"));
            return Ok("射後不理");
        }
        [HttpGet]
        public IActionResult DelaySchedule()
        {
            Debug.WriteLine($"API現在時間:{DateTime.Now}");
            BackgroundJob.Schedule(
                () => Debug.WriteLine($"由HangFire排程發送,時間:{DateTime.Now}"),
                TimeSpan.FromSeconds(3));
            return Ok();
        }
        [HttpGet]
        public IActionResult TimedRepeat()
        {
            Debug.WriteLine($"API現在時間:{DateTime.Now}");
            //【 秒 分 時 日 月 周 年 】,其中年是可選型別,也就是說他如果在不設定年分的情況下是每年。
            RecurringJob.AddOrUpdate(
                 () => Debug.WriteLine($"API現在時間:{DateTime.Now}")
                 , "15 7 * * *");
            return Ok();
        }
先取得jobId再使用ContinueJobWith,來撰寫前一分任務執行完後要做的事情。
        // GET: api/<HangfireDemoController>
        [HttpGet]
        public IActionResult Continuations()
        {
            // 第一招射後不理
            // fire and got:站台啟動後只會執行一次
            var jobid = BackgroundJob.Enqueue(() => Debug.WriteLine("fire and got"));
           
            BackgroundJob.ContinueJobWith(jobid,()=> Debug.WriteLine("two and got"));
            return Ok("接續射後不理");
        }
關於各類型架構、Dashboard權限設定、工作全重設定…等等之後會專門寫一篇文章跟大家講解
Hangfire 官方網站
過去整理的Hangfire文章
本篇已同步發表至個人部落格
https://moushih.com/2022ithome05/