oruchankural / background-job

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Quartz.NET Scheduler with ASP.NET Core

This is a basic example of using Quartz.NET as a scheduler in an ASP.NET Core application to process data in batches. It schedules jobs to run at different times, 2 minutes apart, and ensures that jobs of the same type do not run concurrently.

Prerequisites

  • .NET 5 or later
  • Visual Studio or Visual Studio Code (optional)

Getting Started

  1. Clone or download this repository.

  2. Open the project in Visual Studio or Visual Studio Code (optional).

  3. Run the application.

  4. Use a tool like Postman or cURL to make a POST request to /api/data with a JSON array of Data objects to trigger the jobs.

  5. Check the terminal logs.

image

Project Structure

  • Program.cs: Main entry point for the application.
  • Startup.cs: Configuration of services and middleware.
  • DataController.cs: Defines the API endpoint to trigger jobs.
  • Jobs/InsertLogToDatabase.cs: The Quartz.NET job that processes data in batches.
  • Models/Data.cs: A sample data model.
  • appsettings.json: Configuration settings (not used in this example).
  • requestBody.json: Sample request body

Best Practices

  • Use dependency injection to manage the Quartz scheduler as a singleton service.
  • Avoid hardcoding values like job names or intervals. Use variables or configuration files for such values.
  • Use JobKey to uniquely identify jobs. In this example, each job has a unique name with an index.
  • Set a unique identity for each job and use it consistently when creating triggers.
  • Start jobs at different times to avoid concurrent execution.
  • Use [DisallowConcurrentExecution] to prevent jobs of the same type from running concurrently.
  • Implement error handling and logging in your jobs as needed.
  • Monitor and manage your Quartz.NET scheduler in a production environment.
  • Keep the Startup.cs file clean by moving the scheduler configuration to a separate class or extension method for better maintainability.

Feel free to customize and expand upon this example to meet your specific requirements.

About


Languages

Language:C# 100.0%