Class: Clockwork::Manager

Inherits:
Object
  • Object
show all
Defined in:
lib/clockwork/manager.rb

Direct Known Subclasses

DatabaseEvents::Manager

Defined Under Namespace

Classes: NoHandlerDefined

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeManager


7
8
9
10
11
12
# File 'lib/clockwork/manager.rb', line 7

def initialize
  @events = []
  @callbacks = {}
  @config = default_configuration
  @handler = nil
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config


5
6
7
# File 'lib/clockwork/manager.rb', line 5

def config
  @config
end

Instance Method Details

#configure {|config| ... } ⇒ Object

Yields:


18
19
20
21
22
23
# File 'lib/clockwork/manager.rb', line 18

def configure
  yield(config)
  if config[:sleep_timeout] < 1
    config[:logger].warn 'sleep_timeout must be >= 1 second'
  end
end

#default_configurationObject


25
26
27
# File 'lib/clockwork/manager.rb', line 25

def default_configuration
  { :sleep_timeout => 1, :logger => Logger.new(STDOUT), :thread => false, :max_threads => 10 }
end

#error_handler(&block) ⇒ Object


35
36
37
38
# File 'lib/clockwork/manager.rb', line 35

def error_handler(&block)
  @error_handler = block if block_given?
  @error_handler
end

#every(period, job, options = {}, &block) ⇒ Object


45
46
47
48
49
50
51
# File 'lib/clockwork/manager.rb', line 45

def every(period, job, options={}, &block)
  if options[:at].respond_to?(:each)
    every_with_multiple_times(period, job, options, &block)
  else
    register(period, job, block, options)
  end
end

#fire_callbacks(event, *args) ⇒ Object


53
54
55
# File 'lib/clockwork/manager.rb', line 53

def fire_callbacks(event, *args)
  @callbacks[event].nil? || @callbacks[event].all? { |h| h.call(*args) }
end

#handle_error(e) ⇒ Object


84
85
86
# File 'lib/clockwork/manager.rb', line 84

def handle_error(e)
  error_handler.call(e) if error_handler
end

#handler(&block) ⇒ Object

Raises:


29
30
31
32
33
# File 'lib/clockwork/manager.rb', line 29

def handler(&block)
  @handler = block if block_given?
  raise NoHandlerDefined unless @handler
  @handler
end

#log(msg) ⇒ Object


88
89
90
# File 'lib/clockwork/manager.rb', line 88

def log(msg)
  config[:logger].info(msg)
end

#log_error(e) ⇒ Object


80
81
82
# File 'lib/clockwork/manager.rb', line 80

def log_error(e)
  config[:logger].error(e)
end

#on(event, options = {}, &block) ⇒ Object


40
41
42
43
# File 'lib/clockwork/manager.rb', line 40

def on(event, options={}, &block)
  raise "Unsupported callback #{event}" unless [:before_tick, :after_tick, :before_run, :after_run].include?(event.to_sym)
  (@callbacks[event.to_sym]||=[]) << block
end

#runObject


57
58
59
60
61
62
63
64
# File 'lib/clockwork/manager.rb', line 57

def run
  log "Starting clock for #{@events.size} events: [ #{@events.map(&:to_s).join(' ')} ]"
  loop do
    tick
    interval = config[:sleep_timeout] - Time.now.subsec + 0.001
    sleep(interval) if interval > 0
  end
end

#thread_available?Boolean


14
15
16
# File 'lib/clockwork/manager.rb', line 14

def thread_available?
  Thread.list.select { |t| t['creator'] == self }.count < config[:max_threads]
end

#tick(t = Time.now) ⇒ Object


66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/clockwork/manager.rb', line 66

def tick(t=Time.now)
  if (fire_callbacks(:before_tick))
    events = events_to_run(t)
    events.each do |event|
      if (fire_callbacks(:before_run, event, t))
        event.run(t)
        fire_callbacks(:after_run, event, t)
      end
    end
  end
  fire_callbacks(:after_tick)
  events
end