Skip to content

BackgroundTasks: Configuration

ronimizy edited this page May 6, 2024 · 7 revisions

Service collection

Scheduler implementation is moved out to separate implementation. As if right not only Hangfire scheduler is supported. You need to install Itmo.Dev.Platform.BackgroundTasks.Hangfire package and call UseHangfireScheduling extension method (as shown below) to register it.

Storage configuration for Persistence and Scheduler is moved out to separate implementations. As if right now only Postgres database is supported. You need to install Itmo.Dev.Platform.BackgroundTasks.Postgres and Itmo.Dev.Platform.BackgroundTasks.Hanfgire.Postgres packages and call UsePostgresPersistence and UsePostgresJobStorage extensions method (as shown below) to register them.

collection.AddPlatformBackgroundTasks(
    tasks => tasks
        .UsePostgresPersistence(
            builder => builder.BindConfiguration("Infrastructure:BackgroundTasks:Persistence"))
        .ConfigureScheduling(
            builder => builder.BindConfiguration("Infrastructure:BackgroundTasks:Scheduling"))
        .UseHangfireScheduling(
            hangfire => hangfire
                .ConfigureOptions(
                    builder => builder.BindConfiguration("Infrastructure:BackgroundTasks:Scheduling:Hangfire"))
                .UsePostgresJobStorage())
        .ConfigureExecution(configuration)
        .AddStateMachine());

Schema

Persistence configuration

{
  "SchemaName": string
}
  • SchemaName
    Name of a PostgreSQL schema to store background task data

Example configuration

"Infrastructure": {
  "BackgroundTasks": {
    "Persistence": {
      "SchemaName": "background_tasks"
    }
  }
}

Scheduling configuration

This configuration section used to configure library scheduler.

{
  "BatchSize": int,
  "PollingDelay": timespan
}
  • BatchSize
    Number of tasks fetched per enqueuing run
  • PollingDelay
    Delay between enqueuing runs

Example configuration

"Infrastructure": {
  "BackgroundTasks": {
    "Scheduling": {
      "BatchSize": 10,
      "PollingDelay": "00:00:05"
    }
  }
}

Scheduling configuration (Hangfire)

This configuration section used to configure Hangfire scheduler, these parameters are passed directly to Hangfire.

{
  "CancellationCheckInterval": timespan,
  "SchedulerRetryCount": int,
  "SchedulerRetryDelays": [int],
  "SchedulerWorkerCount": int
}
  • CancellationCheckInterval
    Delay between cancellation checks
  • SchedulerRetryCount
    Count of retries that hangfire will do, before marking task failed
  • SchedulerRetryDelays
    Delay between hangfire retries, index corresponds to retry number
  • SchedulerWorkerCount
    Count of Hangfire workers

Example configuration

"Infrastructure": {
  "BackgroundTasks": {
    "Scheduling": {
      "Hangfire": {
        "SchedulerRetryCount": 10,
        "SchedulerWorkerCount": 2,
        "SchedulerRetryDelays": [60, 120, 180],
        "SchedulerWorkerCount": 1
      }
    }
  }
}

Execution configuration

{
  "MaxRetryCount": int
}
  • MaxRetryCount
    Count of enqueueing retries for before task is moved into failed state

Example configuration

"Infrastructure": {
  "BackgroundTasks": {
    "Execution": {
      "MaxRetryCount": 5
    }
  }
}