Class: Ductr::Scheduler

Inherits:
Object
  • Object
show all
Extended by:
Annotable
Defined in:
lib/ductr/scheduler.rb

Overview

Base class to declare event driven scheduling. Example using the schedule trigger:

class MyScheduler < Ductr::Scheduler
  trigger :schedule, every: "10min"
  def every_ten_minutes
    MyJob.perform_later
  end
end

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeScheduler

Parses trigger annotations, creates triggers if needed and calls #add on trigger instances.



69
70
71
72
73
74
75
76
77
# File 'lib/ductr/scheduler.rb', line 69

def initialize
  self.class.annotated_methods.each do |method|
    annotation = method.find_annotation(:trigger)
    trigger = find_trigger(*annotation.params.reverse)
    callable = self.method(method.name)

    trigger.add(callable, annotation.options)
  end
end

Class Method Details

.startvoid

This method returns an undefined value.

Calls #start on all created triggers.



52
53
54
# File 'lib/ductr/scheduler.rb', line 52

def start
  triggers.values.each(&:start)
end

.stopvoid

This method returns an undefined value.

Calls #stop on all created triggers.



61
62
63
# File 'lib/ductr/scheduler.rb', line 61

def stop
  triggers.values.each(&:stop)
end

.trigger(trigger_type, adapter_name = nil, **trigger_options) ⇒ void

This method returns an undefined value.

Annotation to define a trigger method

Examples:

A schedule trigger

trigger :schedule, every: "10min"
def every_ten_minutes
  MyJob.perform_later
end

Parameters:

  • trigger_type (Symbol)

    The trigger’s type

  • adapter_name (Symbol) (defaults to: nil)

    The trigger’s adapter (if any)

  • **trigger_options (Hash<Symbol: Object>)

    The options to pass to the trigger

See Also:

  • chosen trigger documentation for further information.


35
# File 'lib/ductr/scheduler.rb', line 35

annotable :trigger

.triggersHash<Symbol: Trigger>

All trigger instances are stored in a singleton hash, avoiding to create the same trigger multiple times.

Returns:

  • (Hash<Symbol: Trigger>)

    The singleton hash containing all trigger instances



43
44
45
# File 'lib/ductr/scheduler.rb', line 43

def triggers
  @triggers ||= {}
end