Class ChenileScheduler

java.lang.Object
org.chenile.scheduler.ChenileScheduler
All Implemented Interfaces:
InitializingBean

public class ChenileScheduler extends Object implements InitializingBean
Chenile's scheduler. It holds this node's SchedulerDefinitions, schedules them in Quartz and, when one fires, raises its event through TriggerService.

Definitions come from chenile.scheduler.json.package at startup, and from code (or chenile-scheduler-db) through schedule(SchedulerDefinition). Quartz is Spring Boot's: in memory by default, clustered through spring.quartz.*. In a cluster every node must know every definition, so code that schedules must run on every node, for example at startup.

  • Field Details

    • CONTEXT_KEY

      public static final String CONTEXT_KEY
      Scheduler-context key for this node's instance: kept in memory, never persisted.
      See Also:
    • DEFINITION_ID

      public static final String DEFINITION_ID
      Job data: the definition id.
      See Also:
    • TENANT

      public static final String TENANT
      Job data: the definition's tenant, when it has one. With a database per tenant, the node that fires a stored schedule needs it to read the definition from that tenant's database.
      See Also:
    • GROUP

      public static final String GROUP
      See Also:
    • logger

      private static final org.slf4j.Logger logger
    • quartz

      private final org.quartz.Scheduler quartz
    • triggers

      private final TriggerService triggers
    • jsonResources

      private final Resource[] jsonResources
    • tenantScope

      private final TenantScope tenantScope
    • mapper

      private final tools.jackson.databind.ObjectMapper mapper
    • definitions

      private final Map<String, SchedulerDefinition> definitions
    • lookup

      private volatile Function<String, SchedulerDefinition> lookup
  • Constructor Details

  • Method Details

    • afterPropertiesSet

      public void afterPropertiesSet() throws Exception
      Registers this node with Quartz and schedules the JSON definitions.
      Specified by:
      afterPropertiesSet in interface InitializingBean
      Throws:
      Exception
    • schedule

      public void schedule(SchedulerDefinition definition)
      Adds or replaces a definition and (re)schedules it. A disabled definition is kept but removed from Quartz. Replace-if-exists makes this safe when several nodes start together.
    • unschedule

      public void unschedule(String id)
      Forgets the definition and removes it from Quartz.
    • definition

      public SchedulerDefinition definition(String id)
    • definitions

      public Collection<SchedulerDefinition> definitions()
    • lookUpDefinitionsWith

      public void lookUpDefinitionsWith(Function<String, SchedulerDefinition> lookup)
      Where a fire looks first for its definition, before this node's own. chenile-scheduler-db passes a database lookup here: in a cluster, a schedule saved on one node can fire on another, and the database holds the current version.
    • fire

      public void fire(String id, String tenant, Date scheduledFireTime, Date fireTime)
      Raises the definition's event. Called by SchedulerJob on the node Quartz picked. The whole fire runs as the schedule's tenant (see TenantScope), so the database lookup and the trigger reach that tenant's database even though the Quartz thread has no tenant.
    • fireAsTenant

      private void fireAsTenant(String id, Date scheduledFireTime, Date fireTime)
    • read

      private SchedulerDefinition read(Resource resource)
    • validate

      public void validate(SchedulerDefinition definition)
      Checks a definition without scheduling it: id, eventId, cron expression, timezone and x- headers. Throws a BadRequestException (E571). An empty timezone becomes UTC.
    • invalid

      private void invalid(String id, String reason)