Class BaseScheduledTaskAddIn
- Namespace
- Dynamicweb.Scheduling
- Assembly
- Dynamicweb.Core.dll
Extensibility point for the code executed by a scheduled task. Every scheduled task (Task) is
bound to a subclass of this type through AddInTypeName, and when the scheduler runs the
task, the add-in is instantiated and its Run() method is invoked. Built-in implementations include
the URL, SQL and log-cleanup task add-ins (see the Dynamicweb.Scheduling.ScheduledTaskAddIns namespace).
public abstract class BaseScheduledTaskAddIn : ConfigurableAddIn
- Inheritance
-
BaseScheduledTaskAddIn
- Derived
- Inherited Members
Remarks
How to create a custom scheduled task add-in: Subclass this class, implement Run(), decorate the class with AddInLabelAttribute (and optionally AddInDescriptionAttribute) to control how it appears when picking a task type in administration, and expose any configuration as public properties decorated with AddInParameterAttribute and an editor attribute — this class derives from ConfigurableAddIn, so parameters are rendered in the task editor and persisted as XML on the task automatically. Deploy the assembly with the solution and the add-in is discovered through AddInManager and becomes selectable as the type of a scheduled task.
Things to be careful of:
- A new instance is created through AddInManager for every run, so the class must have a public parameterless constructor and must not rely on instance state surviving between runs. Before Run() is called, the framework populates ScheduledTaskName, Task, HostUrl and CancellationToken, and loads the saved parameter values via LoadParametersFromXml(string) — none of these are available in the constructor.
- Exceptions thrown from Run() do not crash the scheduler: the job catches them,
writes them to the task log and the event log, and records the run as failed. Prefer catching expected errors
yourself, logging them and returning
false, so the log tells the operator what went wrong. - The returned value is stored as the task's last result and is visible in administration;
when the task is a sub-task in a task group, returning
falsecan stop the remaining sub-tasks from running, so only returntruewhen the work actually succeeded. - The task's configured timeout is enforced from the outside: CancellationToken is cancelled when the timeout elapses or the run is cancelled from administration, and the worker thread may be interrupted (raising ThreadInterruptedException in blocking calls). Long-running implementations should observe CancellationToken at safe points to stop gracefully.
- The scheduler typically executes tasks outside an HTTP request context, so do not depend on a current request or session; use HostUrl when an absolute URL to the site is needed.
using System;
using System.IO;
using Dynamicweb.Core;
using Dynamicweb.Extensibility.AddIns;
using Dynamicweb.Extensibility.Editors;
using Dynamicweb.Scheduling;
[AddInLabel("Cleanup export files")]
[AddInDescription("Deletes export files older than the configured number of days")]
public class ExportCleanupScheduledTaskAddIn : BaseScheduledTaskAddIn
{
public ExportCleanupScheduledTaskAddIn()
{
IsCancellationTokenUsed = true;
}
[AddInParameter("Folder"), AddInParameterEditor(typeof(TextParameterEditor), "")]
public string Folder { get; set; } = "/Files/System/Exports";
[AddInParameter("Older than (days)"), AddInParameterEditor(typeof(IntegerNumberParameterEditor), "minValue=1")]
public int Days { get; set; } = 30;
public override bool Run()
{
var path = SystemInformation.MapPath(Folder);
if (!Directory.Exists(path))
return false;
var cutoff = DateTime.Now.AddDays(-Days);
foreach (var file in new DirectoryInfo(path).GetFiles())
{
CancellationToken.ThrowIfCancellationRequested();
if (file.LastWriteTime < cutoff)
file.Delete();
}
return true;
}
}
Constructors
BaseScheduledTaskAddIn()
Initializes a new instance of the add-in. Called through AddInManager for every task run and by the task editor when rendering the parameter UI; context properties and saved parameters are not yet populated at this point.
protected BaseScheduledTaskAddIn()
Fields
ScheduledTaskName
The name of the scheduled task being executed. Set by the framework before Run() is called;
null outside a task run (for example while the task editor renders the parameter UI).
public string? ScheduledTaskName
Field Value
Properties
CancellationToken
Gets or sets the token that is cancelled when the task's configured timeout elapses or the run is cancelled from administration. Set by the framework before Run() is called; long-running implementations should observe it at safe points and stop when cancellation is requested.
public CancellationToken CancellationToken { get; set; }
Property Value
HostUrl
Gets or sets the base URL of the site the task runs for. Set by the framework before Run() is
called; use it to build absolute URLs, since scheduled tasks typically execute outside an HTTP request
context. May be null when no host URL is known.
public Uri? HostUrl { get; set; }
Property Value
IsCancellationTokenUsed
Gets or sets a value indicating that the implementation supports cooperative cancellation, i.e. that
Run() observes CancellationToken and stops when it is signalled. Set it to
true in the subclass (typically in the constructor) when that is the case; the default is
false.
public bool IsCancellationTokenUsed { get; set; }
Property Value
Task
Gets or sets the scheduled task being executed. Set by the framework before Run() is called;
null outside a task run.
public Task? Task { get; set; }
Property Value
Methods
Run()
Performs the work of the scheduled task. Called once per task run, after the framework has populated the context properties and loaded the saved parameter values.
public abstract bool Run()
Returns
- bool
truewhen the work succeeded; otherwisefalse. The value is logged and stored as the task's last result. Exceptions that escape this method are caught by the framework, logged, and recorded as a failed run.