Version
Google Translate

Quartz Background Job Manager

Quartz is an advanced background job manager. You can integrate Quartz with the VCP to use it instead of the default background job manager. In this way, you can use the same background job API for Quartz and your code will be independent of Quartz. If you like, you can directly use Quartz's API, too.

See the background jobs document to learn how to use the background job system. This document only shows how to install and configure the Quartz integration.

Installation

It is suggested to use the VCP CLI to install this package.

Using the VCP CLI

Open a command line window in the folder of the project (.csproj file) and type the following command:

vcp add-package Verto.Vcp.BackgroundJobs.Quartz

If you haven't done it yet, you first need to install the VCP CLI. For other installation options, see the package description page.

Manual Installation

If you want to manually install;

  1. Add the Verto.Vcp.BackgroundJobs.Quartz NuGet package to your project:

    Install-Package Verto.Vcp.BackgroundJobs.Quartz
    
  2. Add the VcpBackgroundJobsQuartzModule to the dependency list of your module:

[DependsOn(
    //...other dependencies
    typeof(VcpBackgroundJobsQuartzModule) //Add the new module dependency
    )]
public class YourModule : VcpModule
{
}

Configuration

Quartz is a very configurable library,and the VCP provides VcpQuartzOptions for this. You can use the PreConfigure method in your module class to pre-configure this option. VCP will use it when initializing the Quartz module. For example:

[DependsOn(
    //...other dependencies
    typeof(VcpBackgroundJobsQuartzModule) //Add the new module dependency
    )]
public class YourModule : VcpModule
{
    public override void PreConfigureServices(ServiceConfigurationContext context)
    {
        var configuration = context.Services.GetConfiguration();

        PreConfigure<VcpQuartzOptions>(options =>
        {
            options.Properties = new NameValueCollection
            {
                ["quartz.jobStore.dataSource"] = "BackgroundJobsDemoApp",
                ["quartz.jobStore.type"] = "Quartz.Impl.AdoJobStore.JobStoreTX, Quartz",
                ["quartz.jobStore.tablePrefix"] = "QRTZ_",
                ["quartz.serializer.type"] = "json",
                ["quartz.dataSource.BackgroundJobsDemoApp.connectionString"] = configuration.GetConnectionString("Quartz"),
                ["quartz.dataSource.BackgroundJobsDemoApp.provider"] = "SqlServer",
                ["quartz.jobStore.driverDelegateType"] = "Quartz.Impl.AdoJobStore.SqlServerDelegate, Quartz",
            };
        });
    }
}

Starting from VCP 3.1 version, we have added Configurator to VcpQuartzOptions to configure Quartz. For example:

[DependsOn(
    //...other dependencies
    typeof(VcpBackgroundJobsQuartzModule) //Add the new module dependency
    )]
public class YourModule : VcpModule
{
    public override void PreConfigureServices(ServiceConfigurationContext context)
    {
        var configuration = context.Services.GetConfiguration();

        PreConfigure<VcpQuartzOptions>(options =>
        {
            options.Configurator = configure =>
            {
                configure.UsePersistentStore(storeOptions =>
                {
                    storeOptions.UseProperties = true;
                    storeOptions.UseJsonSerializer();
                    storeOptions.UseSqlServer(configuration.GetConnectionString("Quartz"));
                    storeOptions.UseClustering(c =>
                    {
                        c.CheckinMisfireThreshold = TimeSpan.FromSeconds(20);
                        c.CheckinInterval = TimeSpan.FromSeconds(10);
                    });
                });
            };
        });
    }
}

You can choose the way you favorite to configure Quaratz.

Quartz stores job and scheduling information in memory by default. In the example, we use the pre-configuration of options pattern to change it to the database. For more configuration of Quartz, please refer to the Quartz's documentation.

Exception handling

Default exception handling strategy

When an exception occurs in the background job,VCP provide the default handling strategy retrying once every 3 seconds, up to 3 times. You can change the retry count and retry interval via VcpBackgroundJobQuartzOptions options:

[DependsOn(
    //...other dependencies
    typeof(VcpBackgroundJobsQuartzModule) //Add the new module dependency
    )]
public class YourModule : VcpModule
{
    public override void ConfigureServices(ServiceConfigurationContext context)
    {
        Configure<VcpBackgroundJobQuartzOptions>(options =>
        {
            options.RetryCount = 1;
            options.RetryIntervalMillisecond = 1000;
        });
    }
}

Customize exception handling strategy

You can customize the exception handling strategy via VcpBackgroundJobQuartzOptions options:

[DependsOn(
    //...other dependencies
    typeof(VcpBackgroundJobsQuartzModule) //Add the new module dependency
    )]
public class YourModule : VcpModule
{
    public override void ConfigureServices(ServiceConfigurationContext context)
    {
        Configure<VcpBackgroundJobQuartzOptions>(options =>
        {
            options.RetryStrategy = async (retryIndex, executionContext, exception) =>
            {
                // customize exception handling
            };
        });
    }
}
In this document