docs
Systems
Execution
SchedulerSystem
Public API Reference

This reference lists the public and protected members. The system declares no C# event members. Notifications go through the EventBus, and the events are described under SchedulerEventFactory and SchedulerEventVerb.

SchedulerCommandFactory

Static class (namespace SaffronLibrary.Prime.Systems.Scheduler). Builds Scheduler-domain commands and sends them with BusUtility.Send.

public static class SchedulerCommandFactory
{
    public static void ScheduleSingleEntityhModifier(IScheduledJob modifierJob);
    public static void ScheduleDualEntityModifier(IScheduledJob modifierJob);
    public static void ScheduleAttributeParamModifier(IScheduledJob modifierJob, string value = "");
    public static void ScheduleCallback(IScheduledJob callback);
    public static void PauseScheduler();
    public static void ResumeScheduler();
    public static void CancelScheduledJobs();
}
  • ScheduleSingleEntityhModifier: sends a Schedule command with the Subject set to Job and the Job param set to modifierJob. The method name contains a typo ("Entityh") and is kept as written.
  • ScheduleDualEntityModifier: same body as ScheduleSingleEntityhModifier.
  • ScheduleAttributeParamModifier: as above, and also sets CoreParamKeys.StringValue to value.
  • ScheduleCallback: same body as the two modifier methods.
  • PauseScheduler: sends a Pause command.
  • ResumeScheduler: sends a Resume command.
  • CancelScheduledJobs: sends a Cancel command with the Subject set to Job.

SchedulerCommandVerbs

public enum SchedulerCommandVerbs
{
    Schedule,
    Pause,
    Resume,
    Cancel
}

Verbs for Scheduler-domain commands. Sent as the Verb of a Command.

SchedulerKeyNames

public static class SchedulerKeyNames
{
    public const string SchedulerSubject;
    public const string ScheduledJob;
}
  • SchedulerSubject: the key name core.scheduler.subject.
  • ScheduledJob: the key name core.scheduler.job.

The constants are built from a private base name and a private nested schema class.

SchedulerKeys

public static class SchedulerKeys
{
    public static readonly ParamKey<IScheduledJob> Job;
    public static readonly ParamKey<SchedulerSubject> Subject;
}
  • Job: carries an IScheduledJob in a command's params.
  • Subject: carries a SchedulerSubject in a command's params.

SchedulerSubject

public enum SchedulerSubject
{
    Job
}

The subject of a Scheduler message. Job refers to a job managed by SchedulerManager.

SchedulerEventFactory

public static class SchedulerEventFactory
{
    public static void OnSchedulerPaused();
    public static void OnSchedulerResumed();
}
  • OnSchedulerPaused: raises a Paused event on the Scheduler domain through BusUtility.Raise.
  • OnSchedulerResumed: raises a Resumed event on the Scheduler domain through BusUtility.Raise.

Callers should use this factory rather than constructing scheduler events directly, as its summary states.

SchedulerEventVerb

Declared in the SaffronLibrary.Prime.Systems.Contracts namespace, although SchedulerEventFactory lives in the Scheduler namespace.

public enum SchedulerEventVerb
{
    Paused = 0,
    Resumed = 1,
}

Verbs for Scheduler-domain events.

CallbackJob

public class CallbackJob : IScheduledJob
{
    public JobMetadata JobMetaData { get; set; }
    public Action Callback;
 
    public CallbackJob(JobMetadata jobMetaData, Action callback);
 
    public bool Execute();
}
  • JobMetaData: the job's scheduling metadata.
  • Callback: the action to run when the job executes.
  • Constructor: stores both arguments.
  • Execute: invokes Callback if it is not null, then returns true.

CallbackJob does not override SetJobID, so the interface default (a no-op) applies.

IScheduledJob

public interface IScheduledJob
{
    public JobMetadata JobMetaData { get; set; }
    public bool Execute();
    void SetJobID(int jobID) { }
}
  • JobMetaData: gets or sets the scheduling metadata for the job.
  • Execute: runs the job's logic. Returns true if execution succeeded.
  • SetJobID: receives the job's unique ID before execution. The default implementation does nothing.

SchedulerConstants

Static class holding the job record, the ID counter, and the thresholds.

public static class SchedulerConstants
{
    public class Job
    {
        public int Id { get; private set; }
        public float Delay { get; private set; }
        public float Interval { get; private set; }
        public int RepeatCount { get; private set; }
        public Config Config { get; private set; }
        public Params Param { get; private set; }
 
        public Job(int id, Config config, Params param);
 
        public void ResetDelay();
        public void DecrementRepeatCount();
        public void DecrementDelay(float deltaTime);
    }
 
    public static int JobIDCounter = 1;
 
    public static class Threshold
    {
        public static int Delay = 0;
        public static int Interval = 0;
        public static int RepeatCount = 1;
    }
}

Job members:

  • Id: the unique ID assigned by the scheduler.
  • Delay: seconds left until the job is next due.
  • Interval: seconds between repeats. Zero means the job runs once.
  • RepeatCount: how many more times the job will run.
  • Config: the configuration the job was created with.
  • Param: the params of the command that scheduled the job.
  • Constructor: copies the ID and params, and sets Delay, Interval, and RepeatCount from the config.
  • ResetDelay: sets Delay to Interval.
  • DecrementRepeatCount: reduces RepeatCount by one.
  • DecrementDelay: subtracts deltaTime from Delay.

JobIDCounter holds the next ID to hand out, starting at 1.

Threshold fields:

  • Delay: the lowest allowed delay. A job is due when its delay is at or below this value.
  • Interval: the lowest allowed interval. An interval equal to this value means the job does not repeat.
  • RepeatCount: the lowest allowed repeat count. A job is removed once its count drops below this value.

Config

public struct Config
{
    public float Delay { get; set; }
    public float Interval { get; set; }
    public int RepeatCount { get; set; }
    public Event Event { get; set; }
    public ICommand Command { get; set; }
 
    public Config(float delay, float interval, int repeatCount = int.MaxValue);
    public Config(Event e, ICommand cmd, float delay, float interval, int repeatCount = int.MaxValue);
    public Config(Event e, float delay, float interval, int repeatCount = int.MaxValue);
    public Config(ICommand cmd, float delay, float interval, int repeatCount = int.MaxValue);
}
  • Delay: seconds before the job first runs.
  • Interval: seconds between repeats. Zero means the job runs once.
  • RepeatCount: how many times the job runs. Defaults to int.MaxValue.
  • Event: an optional event for the job to use. The scheduler does not read it.
  • Command: an optional command for the job to use. The scheduler does not read it.

The four constructors differ only in whether they take an Event, an ICommand, both, or neither. Fields not provided are set to null.

JobMetadata

public struct JobMetadata
{
    public Config Config;
    public string Tag;
 
    public JobMetadata(Config config, string tag = "");
}
  • Config: the timing and behavior configuration for the job.
  • Tag: an optional string for debugging, logging, or filtering.
  • Constructor: sets both fields. Tag defaults to an empty string.

SchedulerFlowController

public class SchedulerFlowController : FlowController
{
    protected override Domain Domain { get; }
    protected SchedulerManager schedulerManager;
 
    public override void Init();
    protected override void HandleCommand(ICommand cmd);
}
  • Domain: returns Domain.Scheduler.
  • schedulerManager: the manager that receives commands and ticks. Must be assigned before Init runs.
  • Init: calls base.Init, registers schedulerManager with the update system through UpdateCommandFactory.RegisterUpdatable, and subscribes OnEvent to the Data domain on the EventBus.
  • HandleCommand: passes the command to schedulerManager.ProcessCommand.

The class also has a private OnEvent(Event e) method, which passes the event to schedulerManager.ProcessEvent, and a private eventBus property, which reads the EventBus from ServiceLocator on each access.

SchedulerManager

public abstract class SchedulerManager : IUpdateable
{
    protected Dictionary<int, SchedulerConstants.Job> jobs;
 
    public void ProcessCommand(ICommand cmd);
    public void Tick();
    public void OnUpdate();
    public abstract void ProcessEvent(Event e);
}
  • jobs: all active jobs, keyed by ID.
  • ProcessCommand: routes Schedule, Pause, Resume, and Cancel verbs. Pause and Resume set the paused flag and raise the matching event. Unknown verbs are ignored.
  • Tick: returns early if paused, if time scale is at or below zero, or if there are no jobs. Otherwise it finds due jobs and executes them.
  • OnUpdate: implements IUpdateable by calling Tick.
  • ProcessEvent: abstract. Each project implements it to handle events forwarded from the Data domain.

Private members include the isPaused flag, HandleSchedule, HandleCancel, and ExecuteJob.

HandleSchedule returns early if the Subject param is missing. It then reads the Job param and validates its config. Valid jobs are stored in jobs.

HandleCancel clears jobs when the subject is Job. If the subject param is missing, the out value is the default of SchedulerSubject, which is Job, so the jobs are still cleared.

SchedulerService

public static class SchedulerService
{
    public static bool ValidateSchedulerStruct(Config scheduleConfig);
 
    public static List<int> FindDueJobs(
        Dictionary<int, SchedulerConstants.Job> jobs,
        float deltaTime);
 
    public static void HandleJobCompletion(
        SchedulerConstants.Job job,
        Dictionary<int, SchedulerConstants.Job> jobs);
}
  • ValidateSchedulerStruct: returns false and logs an error if the repeat count is below the threshold, or if the delay or interval is below its threshold. Otherwise returns true.
  • FindDueJobs: decrements each job's delay by deltaTime and returns the IDs of jobs whose delay is at or below zero.
  • HandleJobCompletion: removes the job if its repeat count is below the threshold. Otherwise, if the interval is greater than zero, resets the delay. Otherwise, if the interval equals zero, removes the job. Removals are logged.

SchedulerUtility

public static class SchedulerUtility
{
    public static int IncrementJobIDCounter();
    public static SchedulerConstants.Job CreateScheduledJob(int jobId, Config schedulerStruct, Params param);
    public static void DecrementJobTimer(SchedulerConstants.Job job, float deltaTime);
    public static bool IsJobDue(SchedulerConstants.Job jobs);
}
  • IncrementJobIDCounter: returns the current value of JobIDCounter, then increments it.
  • CreateScheduledJob: creates a SchedulerConstants.Job.
  • DecrementJobTimer: calls DecrementDelay on the job.
  • IsJobDue: returns true if the job's delay is at or below the delay threshold.

Events and notifications

The SchedulerSystem declares no C# events. Scheduler notifications go out on the EventBus:

  • SchedulerEventVerb.Paused is raised when a Pause command is processed.
  • SchedulerEventVerb.Resumed is raised when a Resume command is processed.

Data-domain events are received on the EventBus and forwarded to SchedulerManager.ProcessEvent.