Class: Dnsync::RecurringZoneUpdater

Inherits:
Object
  • Object
show all
Defined in:
lib/dnsync/recurring_zone_updater.rb

Instance Method Summary collapse

Constructor Details

#initialize(source, destination, frequency, grace_period) ⇒ RecurringZoneUpdater

Returns a new instance of RecurringZoneUpdater.



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/dnsync/recurring_zone_updater.rb', line 6

def initialize(source, destination, frequency, grace_period)
  @source       = source
  @destination  = destination
  @frequency    = frequency.to_i
  @grace_period = grace_period.to_i

  @thread          = Atomic.new(nil)
  @running         = Atomic.new(false)
  @last_updated_at = Atomic.new(nil)
  @last_exception  = Atomic.new(nil)
  @failures        = Atomic.new(0)
end

Instance Method Details

#health_problemsObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/dnsync/recurring_zone_updater.rb', line 50

def health_problems
  thread    = @thread.value
  running   = @running.value
  updated   = last_updated
  exception = @last_exception.value
  failures  = @failures.value

  problems = []

  unless running
    problems << "Component not running"
  end

  unless thread && thread.alive?
    problems << "Thread not alive"
  end

  unless recently_updated?(updated)
    if updated
      time_description = "in %0.2f seconds (should have been %d seconds)" % [ updated.to_f, @frequency ]
    else
      time_description = "ever"
    end

    problems << "Successful update hasn't occured #{time_description}"
  end

  if exception && failures > @grace_period
    problems << "Last update failed with #{exception.class}: #{exception.message}"
  end

  unless problems.empty?
    problems.join('; ')
  end
end

#healthy?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/dnsync/recurring_zone_updater.rb', line 46

def healthy?
  health_problems.blank?
end

#joinObject



38
39
40
41
42
43
44
# File 'lib/dnsync/recurring_zone_updater.rb', line 38

def join
  if thread = @thread.value
    thread.join
  end

  self
end

#last_updatedObject



91
92
93
94
95
# File 'lib/dnsync/recurring_zone_updater.rb', line 91

def last_updated
  if at = @last_updated_at.value
    Time.now.to_f - at.to_f
  end
end

#recently_updated?(updated = nil) ⇒ Boolean

Returns:

  • (Boolean)


86
87
88
89
# File 'lib/dnsync/recurring_zone_updater.rb', line 86

def recently_updated?(updated = nil)
  updated ||= last_updated
  updated && updated < (@frequency * @grace_period)
end

#startObject



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/dnsync/recurring_zone_updater.rb', line 19

def start
  if (thread = @thread.value) && thread.alive?
    return self
  end

  @running.value         = true
  @last_updated_at.value = Time.now

  @thread.value = Thread.new do
    Thread.current.abort_on_exception = true
    run
  end
end

#stopObject



33
34
35
36
# File 'lib/dnsync/recurring_zone_updater.rb', line 33

def stop
  @running.value = false
  self
end