Scheduled Tasks

EasyAdminBlazor integrates FreeScheduler as its task scheduling engine. Two usage modes are supported:

  1. [Scheduler] attribute: add one line to a static method and it is auto-registered as a scheduled task at startup
  2. Dynamic management via code: inject ISchedulerService and create, pause, resume, or remove tasks anytime

All tasks are visible in the management page at /Admin/TaskScheduler, where you can view execution logs, pause/resume, and run tasks immediately.

Enable the Extension

Install the EasyAdminBlazor.Scheduler package, then call in Program.cs:

builder.AddEasyAdminBlazor(new EasyAdminBlazorOptions
{
    // The assembly that contains methods marked with [Scheduler] must be registered here
    Assemblies = [typeof(Program).Assembly],
    ...
})
    .AddEasyAdminBlazorScheduler();

Option 1: Register Tasks with the [Scheduler] Attribute

Mark any static method with [Scheduler]. The method may take no parameters, or a single IServiceProvider parameter for resolving services:

using EasyAdminBlazor;

public static class MyJobs
{
    // Runs every day at 03:30 (Cron format: second minute hour day month weekday)
    [Scheduler("DailyBackup", "0 30 3 * * ?")]
    public static void DailyBackup()
    {
        // Task logic here
    }

    // Runs every 60 seconds
    [Scheduler("Heartbeat", Interval = SchedulerInterval.Seconds, Argument = "60")]
    public static void Heartbeat()
    {
    }

    // The method may receive IServiceProvider to resolve database, cache, and other services
    [Scheduler("Cleanup", "0 0 4 * * ?")]
    public static void Cleanup(IServiceProvider sp)
    {
        var fsql = sp.GetRequiredService<MainOrmHandle>().Orm;
        // ...
    }
}

Attribute parameters:

Parameter Description
Name Task name shown in the task list; also the unique identifier
Constructor (string name, string cron) Pass a 6-field Cron expression (including seconds)
Interval Trigger interval type, see the SchedulerInterval enum
Argument Interval argument: seconds, a fixed time (e.g. 15:55:59), or a Cron expression
Round Number of executions; -1 means infinite (default)
Status Initial status: Running / Paused / Completed

Supported SchedulerInterval types:

  • Seconds — trigger by seconds, pass the interval in Argument
  • RunOnDay — fixed time every day, e.g. 15:55:59
  • RunOnWeek — fixed time on a weekday, e.g. 2:15:55:59
  • RunOnMonth — fixed time on a day of the month, e.g. 5:15:55:59
  • Custom — custom Cron expression

At startup the framework scans the assemblies registered in Assemblies, registers those methods as tasks, and they appear under /Admin/TaskScheduler.

Option 2: Create and Manage Tasks in Code

Inject ISchedulerService to manage tasks from any page or service:

@inject ISchedulerService Scheduler

// Add a task that runs every day at 15:55:59; returns the task ID
var taskId = Scheduler.AddTask("DailyReport", "body", -1, SchedulerInterval.RunOnDay, "15:55:59");

// Add a one-off task that runs 10 minutes later
Scheduler.AddTempTask(TimeSpan.FromMinutes(10), () =>
{
    // ...
});

// Manage the task
Scheduler.PauseTask(taskId);   // pause
Scheduler.ResumeTask(taskId);  // resume
Scheduler.RunNowTask(taskId);  // run immediately
Scheduler.RemoveTask(taskId);  // remove

Common ISchedulerService methods:

Method Description
AddTask(topic, body, round, interval, argument) Add a recurring task, returns the task ID
AddTempTask(delay, action) Add a one-off delayed task
PauseTask(id) / ResumeTask(id) Pause / resume a task
RunNowTask(id) Run a task immediately
RemoveTask(id) Remove a task
GetTask(id) / GetTasks(...) Get a single task / paginated task list
GetTaskLogs(taskId, ...) Paginated execution logs of a task
FindTask(predicate) Find tasks by condition

Management Page

At /Admin/TaskScheduler you can:

  • View the task list: name, interval, executed rounds, status, last run time, and next run time
  • Pause / resume / run tasks immediately
  • View execution logs (duration, success/failure, exception details)
  • Remove tasks

Notes

  • The Cron expression has 6 fields (second minute hour day month weekday); seconds cannot be omitted
  • Invalid Cron expressions are rejected during validation; legacy bad data degrades to a 5-second retry instead of crashing the scheduler
  • The scheduler always uses the +8 timezone (Asia/Shanghai)
  • Tasks are persisted in the FreeScheduler_task / FreeScheduler_tasklog tables, created automatically
  • The assembly containing [Scheduler] methods must be registered in EasyAdminBlazorOptions.Assemblies
  • Keep task methods lightweight; split long-running operations or use async processing