Class: WebChecker::Workflow

Inherits:
Object
  • Object
show all
Defined in:
lib/web_checker/workflow.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Workflow

Returns a new instance of Workflow.



9
10
11
12
13
14
15
# File 'lib/web_checker/workflow.rb', line 9

def initialize(options)
  @options = options.dup
  WebChecker::Notifications.instance.setup(@options)
  @options['threshold'] = @options['threshold'].map(&:to_i)
  @checker = WebChecker::Http.new(options['url'])
  @attempts = 0
end

Instance Attribute Details

#attemptsObject (readonly)

Returns the value of attribute attempts.



7
8
9
# File 'lib/web_checker/workflow.rb', line 7

def attempts
  @attempts
end

#checkerObject (readonly)

Returns the value of attribute checker.



7
8
9
# File 'lib/web_checker/workflow.rb', line 7

def checker
  @checker
end

Instance Method Details

#cancelObject



35
36
37
# File 'lib/web_checker/workflow.rb', line 35

def cancel
  EventMachine.cancel_timer(@timer) if self.instance_variable_defined?(:@timer) && EventMachine.reactor_running?
end

#runObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/web_checker/workflow.rb', line 17

def run
  if @checker.accessible?
    WebChecker::Notifications.instance.notify if @attempts > 0
    @attempts = 0
  else
    @attempts += 1
    if @options['threshold'].include?(@attempts)
      WebChecker::Notifications.instance.notify_broken(@attempts)
    end
  end

  if EventMachine.reactor_running?
    @timer = EventMachine.add_timer(@options['refresh_rate']) do
      run
    end
  end
end