Code Monkey home page Code Monkey logo

aspnetcore-cronjob-demo's Introduction

ASP.NET Core cron job BackgroundService demo

This is a demo app that executes CRON jobs using IHostedService and BackgroundService of ASP.NET Core.

Documentation of ASP.NET Core background services: https://learn.microsoft.com/en-us/dotnet/architecture/microservices/multi-container-microservice-net-applications/background-tasks-with-ihostedservice

  • Job classes are regenerated each time they are executed
  • If the job class implements IDisposable, Dispose() will be called after the execution is finished
  • Dependency injection can be performed in the job class constructor
  • You can specify any parameter to the job class

Customized based on dotnet-labs/ServiceWorkerCronJob.

How to define a CRON job

1. Define a job class that implements IJobWorker

public class CronJob : IJobWorker
{
    public Task DoWorkAsync(CancellationToken cancellationToken)
    {
        // Do work here
    }
}

2. Set up jobs with startup code (Program.cs)

builder.Services.AddCronJob(
    options =>
    {
        options.CronExpression = "*/1 * * * *";
    },
    provider => ActivatorUtilities.CreateInstance<CronJob>(provider));

License

The MIT License(MIT)

aspnetcore-cronjob-demo's People

Contributors

aoisupersix avatar

Stargazers

 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.