Giter Site home page Giter Site logo

lulzzz / dntscheduler.core Goto Github PK

View Code? Open in Web Editor NEW

This project forked from vahidn/dntscheduler.core

0.0 2.0 0.0 817 KB

DNTScheduler.Core is a lightweight ASP.NET Core's background tasks runner and scheduler

License: Apache License 2.0

C# 98.72% Batchfile 0.27% CSS 1.01%

dntscheduler.core's Introduction

DNTScheduler.Core

DNTScheduler.Core is a lightweight ASP.NET Core's background tasks runner and scheduler.

Install via NuGet

To install DNTScheduler, run the following command in the Package Manager Console:

PM> Install-Package DNTScheduler.Core

You can also view the package page on NuGet.

Usage

  • After installing the DNTScheduler.Core package, to define a new task, create a new class that implements the IScheduledTask interface:
namespace DNTScheduler.TestWebApp.ScheduledTasks
{
    public class DoBackupTask : IScheduledTask
    {
        private readonly ILogger<DoBackupTask> _logger;

        public DoBackupTask(ILogger<DoBackupTask> logger)
        {
            _logger = logger;
        }

        public bool IsShuttingDown { get; set; }

        public async Task RunAsync()
        {
            if (this.IsShuttingDown)
            {
                return;
            }

            _logger.LogInformation("Running Do Backup");

            await Task.Delay(TimeSpan.FromMinutes(3)).ConfigureAwait(false);
        }
    }
}

The RunAsync method represents the task's logic.

  • To register this new task, call services.AddDNTScheduler(); method in your Startup class:
namespace DNTScheduler.TestWebApp
{
    public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();
            services.AddDirectoryBrowser();

            services.AddDNTScheduler(options =>
            {
                // DNTScheduler needs a ping service to keep it alive. Set it to false if you don't need it.
                // options.AddPingTask = true;

                options.AddScheduledTask<DoBackupTask>(
                    runAt: utcNow =>
                    {
                        var now = utcNow.AddHours(3.5);
                        return now.Day % 3 == 0 && now.Hour == 0 && now.Minute == 1 && now.Second == 1;
                    },
                    order: 2);
            });
        }

AddDNTScheduler method, adds this new task to the list of the defined tasks. Also its first parameter defines the custom logic of the running intervals of this task. It's a callback method that will be called every second and provides the utcNow value. If it returns true, the job will be executed. If you have multiple jobs at the same time, the order parameter's value indicates the order of their execution.

  • Finally to start running the registered tasks, call app.UseDNTScheduler() method in your Startup class:
public void Configure(IApplicationBuilder app)
{
    app.UseDNTScheduler();

Please follow the DNTScheduler.TestWebApp sample for more details.

dntscheduler.core's People

Contributors

vahidn avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.