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) ⇒ RecurringZoneUpdater

Returns a new instance of RecurringZoneUpdater.



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

def initialize(source, destination, frequency)
  @source      = source
  @destination = destination
  @frequency   = frequency

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

Instance Method Details

#health_problemsObject



47
48
49
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
# File 'lib/dnsync/recurring_zone_updater.rb', line 47

def health_problems
  thread    = @thread.value
  running   = @running.value
  updated   = last_updated
  exception = @last_exception.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
    problems << "Last update failed with #{exception.class}: #{exception.message}"
  end

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

#healthy?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/dnsync/recurring_zone_updater.rb', line 43

def healthy?
  health_problems.blank?
end

#joinObject



35
36
37
38
39
40
41
# File 'lib/dnsync/recurring_zone_updater.rb', line 35

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

  self
end

#last_updatedObject



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

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)


82
83
84
85
# File 'lib/dnsync/recurring_zone_updater.rb', line 82

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

#startObject



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

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

  @running.value = true

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

#stopObject



30
31
32
33
# File 'lib/dnsync/recurring_zone_updater.rb', line 30

def stop
  @running.value = false
  self
end