Sana Assistant (online)
Table of Contents

ScheduledTaskExtension reference

All scheduled tasks extensions have to be inherited from the ScheduledTaskExtension class which, in turn, inherits from the core Sana Commerce Cloud Extension class.

Additionally, each scheduled task extension class should be decorated with the ScheduledTaskIdAttribute to define a unique identifier for the task.

Class

Methods

CheckPrerequisitesAsync

Checks the state of the task prerequisites, indicating whether the task can be executed at the current moment. Sana calls this method and passes the instance of TaskPrerequisitesContext and the method returns an instance of TaskPrerequisiteCheckResult that represents the result of the prerequisites check operation.

public override ValueTask<TaskPrerequisiteCheckResult> CheckPrerequisitesAsync(TaskPrerequisitesContext context, CancellationToken cancellationToken)
{
    // Example logic to demonstrate all possible outcomes
    // Replace this with real condition checks as needed
    var someCondition = GetCondition();

    return someCondition switch
    {
        0 => ValueTask.FromResult(TaskPrerequisiteCheckResult.Available),
        1 => ValueTask.FromResult(TaskPrerequisiteCheckResult.NotAvailable),
        2 => ValueTask.FromResult(TaskPrerequisiteCheckResult.TemporaryNotAvailable("This task is temporarily unavailable.")),
        _ => throw new InvalidOperationException("Unexpected prerequisite condition.")
    };
}

ExecuteAsync

Executes the task. Sana calls this method and passes the instance of TaskExecutionContext and the method returns an instance of TaskOutcome that represents the result of the task execution operation.

public override Task<TaskOutcome> ExecuteAsync(TaskExecutionContext context, CancellationToken cancellationToken)
{
    context.Logger.Log("Starting task execution...");

    // Example logic to demonstrate all possible outcomes
    // Replace with your actual logic
    var simulatedResult = GetSimulatedOutcome();

    return simulatedResult switch
    {
        0 => Task.FromResult(TaskOutcome.Succeeded),
        1 => Task.FromResult(TaskOutcome.PartiallySucceeded),
        2 => Task.FromResult(TaskOutcome.Failed),
        _ => throw new InvalidOperationException("Unexpected task outcome.")
    };
}