Scheduled tasks are actions which are scheduled to happen at a certain point in time or with specific intervals - e.g. hourly, daily, or weekly. DynamicWeb comes with a number of standard add-ins for doing standard things - like scheduling emails, building indexes, or executing an SQL statement.
To create a scheduled task add-in you inherit from the BaseScheduledTaskAddIn class and override the Run-method.
[AddInName("SystemName"), AddInLabel("Name"), AddInDescription("Description")]
public class MyCustomScheduledTaskAddIn : BaseScheduledTaskAddIn
{
public override bool Run()
{
//Do your thing!
//Indicate that the execution of the add-in has finished succesfully
return true;
}
}
Example: Run SQL add-in
Below you can see an example of a scheduled task add-in. This specific add-in is used to execute an SQL statement against the database. When you create a scheduled task add-in there are a couple of things that you need to be aware of:
- Inherit from the base class - As mentioned above, your custom class should inherit from the BaseScheduledTaskAddin-class. This base class only has one method - the Run()-method - which must be overridden. It should return a boolean indicating how the execution fo the add-in went - true for success and false for failure.
- Add attributes - Next you need to add attributes to the class to provide information to DynamicWeb - specifically:
[AddInName]is used to provide a SystemName for this specific add-in. The SystemName should be unique, and will be the used when this add-in is selected[AddInLabel]is used to provide the actual name for the add-in. This is the name that will be shown in the add in selectors. If[AddInLabel]is not added to the class, the[AddInName]will be used as the name[AddInDescription]is used to provide a description to the provider, which will also be shown in the add in selector together with the[AddInLabel]
- Add parameters - If you want the people using your add-in to provide some form of input to the add-in, for example an endpoint URL or some authentication details - you can use so-called configurable add-in parameters to create input fields. In our example we want to add a single parameter to the add-in so the user can supply the SQL statement they want to execute. To do so we use:
[AddInParameter]to provide the label in the UI, so users will know what to enter in the field[AddInParameterEditor]to tell DynamicWeb which type of editor we want to use. The second parameter is used to configure the selected editor. You can see the full list of editors and their configuration options here
[AddInName(".RunSqlAddIn"), AddInLabel("Run SQL add-in"), AddInDescription("Execute SQL scheduled task add-in")]
public class RunSqlScheduledTaskAddIn : BaseScheduledTaskAddIn
{
[AddInParameter("SQL Query"), AddInParameterEditor(typeof(TextParameterEditor), "TextArea=True;style=height:60px;")]
public string? SqlQuery { get; set; }
/// <summary>
/// Main method in ScheduledTask Addin - is run when scheduled Task is run
/// </summary>
/// <returns></returns>
public override bool Run()
{
ILogger Logger = LogManager.Current.GetLogger("ScheduledTasks", Task?.LogFileName ?? "");
bool result = true;
try
{
if (!string.IsNullOrEmpty(SqlQuery))
{
int rowCount = Database.ExecuteNonQuery(SqlQuery);
}
}
catch (Exception ex)
{
result = false;
Logger.Error(string.Format("Scheduled task failed on executing sql: {0} with the message: {1}.", SqlQuery, ex.Message), ex);
}
return result;
}
}