Class BaseScheduler

java.lang.Object
ortus.boxlang.runtime.async.tasks.BaseScheduler
All Implemented Interfaces:
IScheduler
Direct Known Subclasses:
BoxScheduler

public class BaseScheduler extends Object implements IScheduler
The Async Scheduler is in charge of registering scheduled tasks, starting them, monitoring them and shutting them down if needed. Each scheduler is bound to an scheduled executor class. The scheduled executor will be named {name}-scheduler
  • Field Details

    • tasks

      protected LinkedHashMap<String,TaskRecord> tasks
      An ordered struct of all the tasks this scheduler manages
    • executor

      protected ExecutorRecord executor
      The Scheduled Executor we are bound to
    • timezone

      protected ZoneId timezone
      The timezone for the scheduler and the tasks it creates and manages
    • asyncService

      protected AsyncService asyncService
      The async service we are bound to
    • started

      protected Boolean started
      Is the scheduler started?
    • startedAt

      protected Instant startedAt
      When the scheduler was started
    • name

      protected String name
      The name of this scheduler
    • logger

      protected final BoxLangLogger logger
      Logger
    • context

      protected IBoxContext context
      The request context that started this scheduler
  • Constructor Details

    • BaseScheduler

      public BaseScheduler()
      Zero arg constructor with basic defaults: Auto-generated name, system default timezone and the default async service
    • BaseScheduler

      public BaseScheduler(String name)
      Create a new scheduler with a name and the default system timezone.
      Parameters:
      name - The name of the scheduler
    • BaseScheduler

      public BaseScheduler(String name, IBoxContext context)
      Create a new scheduler with a name and the default system timezone.
      Parameters:
      name - The name of the scheduler
      context - The context that started this scheduler
    • BaseScheduler

      public BaseScheduler(String name, ZoneId timezone)
      Create a new scheduler with a name and a specific timezone
      Parameters:
      name - The name of the scheduler
      timezone - The timezone for the scheduler and the tasks it creates and manages
    • BaseScheduler

      public BaseScheduler(String name, ZoneId timezone, IBoxContext context)
      Create a new scheduler with a name and a specific timezone
      Parameters:
      name - The name of the scheduler
      timezone - The timezone for the scheduler and the tasks it creates and manages
      context - The context that started this scheduler
  • Method Details

    • setContext

      public BaseScheduler setContext(IBoxContext context)
      Set the scheduler context manually
    • getContext

      public IBoxContext getContext()
      Get the scheduler context
    • configure

      public void configure()
      Usually where concrete implementations add their tasks and configs
      Specified by:
      configure in interface IScheduler
    • xtask

      public ScheduledTask xtask(String name)
      Register a new task in this scheduler but disable it immediately. This is useful when debugging tasks and have the easy ability to disable them.
      Parameters:
      name - The name of this task
      Returns:
      The registered and disabled Scheduled Task
    • xtask

      public ScheduledTask xtask(String name, String group)
      Register a new task in this scheduler but disable it immediately. This is useful when debugging tasks and have the easy ability to disable them.
      Parameters:
      name - The name of this task
      group - The group of this task
      Returns:
      The registered and disabled Scheduled Task
    • task

      public ScheduledTask task(String name)
      Register a new task in this scheduler that will be executed once the `startup()` is fired or manually via the run() method of the task. The group will be empty.
      Parameters:
      name - The name of this task
      Returns:
      a ScheduledTask object so you can work on the registration of the task
    • task

      public ScheduledTask task(String name, String group)
      Register a new task in this scheduler that will be executed once the `startup()` is fired or manually via the run() method of the task.
      Parameters:
      name - The name of this task
      Returns:
      a ScheduledTask object so you can work on the registration of the task
    • startup

      public BaseScheduler startup()
      Startup this scheduler and all of it's scheduled tasks
      Specified by:
      startup in interface IScheduler
    • restart

      public BaseScheduler restart(boolean force, long timeout)
      Restart the scheduler by shutting it down and starting it up again
      Specified by:
      restart in interface IScheduler
      Parameters:
      force - If true, it forces all shutdowns this is usually true when doing reinits
      timeout - The timeout in seconds to wait for the shutdown of all tasks, defaults to 30 or whatever you set using the setShutdownTimeout()
      Returns:
    • clearTasks

      public BaseScheduler clearTasks()
      Clear all tasks from the scheduler. Usually done by a restart
      Returns:
      The scheduler object
    • startupTask

      public TaskRecord startupTask(ScheduledTask task)
      Startup manullay the passed in task
      Parameters:
      task - The task to startup
      Returns:
      The task record
    • startupTask

      public TaskRecord startupTask(String taskName)
      Startup a specific task by name
      Parameters:
      taskName - The name of the task
      Returns:
      The task record
    • shutdown

      public BaseScheduler shutdown(boolean force, long timeout)
      Shutdown this scheduler by calling the executor to shutdown and disabling all tasks
      Specified by:
      shutdown in interface IScheduler
      Parameters:
      force - If true, it forces all shutdowns this is usually true when doing reinits
      timeout - The timeout in seconds to wait for the shutdown of all tasks, defaults to 30 or whatever you set using the setShutdownTimeout() method
    • shutdown

      public BaseScheduler shutdown(boolean force)
      Shutdown this scheduler by calling the executor to shutdown and disabling all tasks using the default timeout
      Specified by:
      shutdown in interface IScheduler
      Parameters:
      force - If true, it forces all shutdowns this is usually true when doing reinits
      Returns:
      The scheduler object
    • shutdown

      public BaseScheduler shutdown()
      Shutdown this scheduler by calling the executor to shutdown and disabling all tasks We do not force and we use the default timeout
      Specified by:
      shutdown in interface IScheduler
      Returns:
      The scheduler object
    • onShutdown

      public void onShutdown()
      Called before the scheduler is going to be shutdown
      Specified by:
      onShutdown in interface IScheduler
    • onStartup

      public void onStartup()
      Called after the scheduler has registered all schedules
      Specified by:
      onStartup in interface IScheduler
    • onAnyTaskError

      public void onAnyTaskError(ScheduledTask task, Exception exception)
      Called whenever ANY task fails
      Specified by:
      onAnyTaskError in interface IScheduler
      Parameters:
      task - The task that got executed
      exception - The exception object
    • onAnyTaskSuccess

      public void onAnyTaskSuccess(ScheduledTask task, Optional<?> result)
      Called whenever ANY task succeeds
      Specified by:
      onAnyTaskSuccess in interface IScheduler
      Parameters:
      task - The task that got executed
      result - The result (if any) that the task produced
    • beforeAnyTask

      public void beforeAnyTask(ScheduledTask task)
      Called before ANY task runs
      Specified by:
      beforeAnyTask in interface IScheduler
      Parameters:
      task - The task about to be executed
    • afterAnyTask

      public void afterAnyTask(ScheduledTask task, Optional<?> result)
      Called after ANY task runs
      Specified by:
      afterAnyTask in interface IScheduler
      Parameters:
      task - The task that got executed
      result - The result (if any) that the task produced
    • getTaskStats

      public IStruct getTaskStats()
      Builds out a report for all the registered tasks in this scheduler. The key is the task name and the value is a struct with the task stats.
      Specified by:
      getTaskStats in interface IScheduler
      Returns:
      A struct with the report: { taskName: { stats } }
    • getRegisteredTasks

      public List<String> getRegisteredTasks()
      Get an array of all the tasks managed by this scheduler
    • hasTask

      public boolean hasTask(String name)
      Check if a task is registered in this scheduler
      Parameters:
      name - The name of the task
      Returns:
      true if registered, false if not
    • getTaskRecord

      public TaskRecord getTaskRecord(String name)
      Get's a task record from the collection by name
      Parameters:
      name - The name of the task
      Returns:
      The task record object
    • removeTask

      public BaseScheduler removeTask(String name)
      Removes a task from the scheduler. It tries to cancel the task first and then removes it from the scheduler. if the task has a future, then it will try to cancel it with interrupt.
      Parameters:
      name - The name of the task
      Returns:
      The scheduler object
    • getTasks

      public Map<String,TaskRecord> getTasks()
      Get the registered tasks in this scheduler
      Returns:
      the tasks
    • hasStarted

      public Boolean hasStarted()
      Has this scheduler been started?
      Specified by:
      hasStarted in interface IScheduler
      Returns:
      true if started, false if not
    • isRunning

      public Boolean isRunning()
      Is it running: Alias to hasStarted()
      Returns:
      true if running, false if not
    • getStartedAt

      public Instant getStartedAt()
      Get when it was started. If not started, then it will return null
    • getSchedulerName

      public String getSchedulerName()
      Get the scheduler name
      Specified by:
      getSchedulerName in interface IScheduler
      Returns:
      the name
    • setSchedulerName

      public BaseScheduler setSchedulerName(String name)
      Set the scheduler name
      Specified by:
      setSchedulerName in interface IScheduler
      Parameters:
      name - the name to set
      Returns:
      the scheduler object
    • getTimezone

      public ZoneId getTimezone()
      Get the timezone
      Specified by:
      getTimezone in interface IScheduler
      Returns:
      the timezone
    • setTimezone

      public BaseScheduler setTimezone(ZoneId timezone)
      Set the scheduler's timezone
      Specified by:
      setTimezone in interface IScheduler
      Parameters:
      timezone - the timezone to set
      Returns:
      the scheduler object
    • setTimezone

      public BaseScheduler setTimezone(String timezone)
      Set the scheduler's timezone as a string, we will convert it to a ZoneId object
      Parameters:
      timezone - the timezone to set as a string
    • setDefaultTimezone

      public BaseScheduler setDefaultTimezone()
      Set the default timezone into the task
      Returns:
      Scheduler
    • getAsyncService

      public AsyncService getAsyncService()
      Get the Aysnc Service
      Returns:
      the asyncService
    • setExecutor

      public BaseScheduler setExecutor(ExecutorRecord executor)
      Set the exectutor record for this scheduler
      Parameters:
      executor - the executor to set
      Returns:
      the scheduler object
    • getExecutor

      public ExecutorRecord getExecutor()
      Get the executor record
      Returns:
      the executor record
    • getLogger

      public BoxLangLogger getLogger()
      Get the logger