Coravel regular tasks based on the number of things Device Statistics

Source: based on the number of things the device Coravel regular tasks of statistics

Coravel regular tasks based on the number of things Device Statistics


1 application background

The total number of things in the system, all devices need to calculate the bottom end, in Dashboard display, a display of the total. The total number of statistics including offline, alarms, etc. outage data needs corresponding display.


2 compares the timing of various libraries

2.1 TaskScheduler

TaskScheduler library only supports .net, and requires a combination of windows Task Scheduler to call, pass.

2.2 Fluent Scheduler

Fluent Scheduler supports only .net, pass.

2.3 Quartz.net

Framework itself is too heavy, and the use of complex, pass.

2.4 Hang Fire

Quartz many relatively lightweight, also simple to use, and pages can have access to observe the implementation of the mandate, but there is a fatal flaw that can only support more minutes and timing tasking because Hangfire use of open source components NCrontab, with crontab instructions on a similar linux. In this application, the total number of pages in 1 minute updates, can not stand. pass

hangfire 7.0 or later has support Cron expressions and-second granularity of time
here thanks to friends @ Name = "Jaly"

2.5 Coravel

Framework light, easy to use, support the second level timing task. Coravel Pro can connect to the database task scheduling, Coravel Pro can support web visualization, the implementation of the mandate by the web display. Background suitable for this application.


3 Coravel general use

3.1 Nuget installation

Installation Coravel call the library layer.

3.2 Dependency Injection

Dependency injection ConfigureServices in the method startup.cs
services.AddScheduler();

3.3 scheduler

In the method startup.cs Configure the two timing chain configuration tasks

  var provider = app.ApplicationServices;     
  provider.UseScheduler(scheduler =>
       {//配置任务1方法
           scheduler.Schedule(() => Console.WriteLine("Every second during the week."))
           //工作日每隔1秒输出
           .EverySecond()
           .Weekday();
       });         
  provider.UseScheduler(scheduler =>
  {//配置任务2方法
      scheduler.Schedule(() => Console.WriteLine("Every 5 second during the week."))
      //工作日每隔5秒输出
      .EverySeconds(5)
      .Weekday();
  });

3.4 to run the program, observe the output

From the results can be seen in FIG. 1 every second print job output; task 2 every 5 seconds printout correctly.

3.5 Cron Expressions

Coravel support Cron Expressions, may need to set Cron expressions depending on the application scenario

  • * * * * * run every minute
  • 00 13 * * * run at 1:00 pm daily
  • 00 1,2,3 * * * run at 1:00 pm, 2:00 pm and 3:00 pm daily
  • 00 1-3 * * * same as above
  • 00 /2 * * run every two hours on the hour

3.6 Error Support

Coravel released on June 28, 2018, just getting started is not mature, I'm working-second timed task of debugging time error.
See # 91 https://github.com/jamesmh/coravel/issues/91
of use within a few hours of work time to solve the problem, also released to nuget2.5.1. Very moved.

3.7 Other functions support

In addition, Coravel also supports task queues, caching, multicast event, e-mail and so on. Task queue can be used according to the readership, as the case may start a new one, the cache is not recommended to use this component, available Easycache. Nor it is recommended to use this event multicast library, reference https://www.cnblogs.com/JerryMouseLi/p/11012839.html. Mail nor recommended for this component.

3.8 Coravel Pro

Coravel Pro可以连接数据库进行任务调度,Coravel Pro可以支持web可视化,将任务执行情况通过web显示出来。在这里不做详细介绍,有需要的读者可自行研究。


4 Coravel的松耦合使用(含总页数统计)

4.1 依赖注入自定义类

ConfigureServices中对松耦合的类Statistic进行依赖注入

 services.AddTransient<Statistic>();

4.2配置调度器

在startup.cs中的Configure方法中配置自定义松耦合任务

    var provider = app.ApplicationServices;     
    provider.UseScheduler(scheduler =>
         {
             scheduler.Schedule<Statistic>()
             .EverySecond()
             .Weekday();
         });   

4.3 编写松耦合任务的代码

详细说明,见代码注释。

using Coravel.Invocable;//需要引用此类库来进行自定义任务
using IBMS.Infrastruct.UoW;
using System;
using System.Threading.Tasks;

 public class StatisticTask: IInvocable
    {
        private readonly IStatisticsServices _statisticsServices;
        private readonly IIPBoxServices _iPBoxServices;


        public StatisticTask(IStatisticsServices statisticsServices, IIPBoxServices iPBoxServices,)
        {
            _statisticsServices = statisticsServices;
            _iPBoxServices = iPBoxServices;
        }
        public  async Task Invoke()
        {
            var _ipboxCount = await _iPBoxServices.CountAsync();//查询设备总数

            var response    = await _statisticsServices.GetAsync(1);//统计表取出
            response.IpboxTotal = (int)_ipboxCount;//存入统计表缓存

            await _statisticsServices.UpdateAsync(response);//更新统计表

        }

    }

注意:1.编写的任务一定要在 Invoke中,这属于固定格式;public async Task Invoke() {};2. 需要引入以下库:using Coravel.Invocable;


5. 结果验证

5.1 前端显示总数数据库记录总数相等,并且数据可以实时更新。


6 其他

计算总设备总页数的计算可以在每次查询时生成,如下

var IPBoxCount =  _unitOfWork.IPBoxRepository.Count();
return Json(new { pageModel, IPBoxCount });

7 小结

这里需要VUE前端以Axios形式来定时获取统计表,后续会改成后端Coravel定时计算好之后,以SignalR形式主动推送到前端进行总览显示。

本文主要推荐Coravel这个定时工具的使用,让Coravel这个优雅的工具被更多人知晓使用。


Guess you like

Origin www.cnblogs.com/lonelyxmas/p/11964830.html