firenxis-god 0.11.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (123) hide show
  1. data/Announce.txt +135 -0
  2. data/History.txt +393 -0
  3. data/README.txt +59 -0
  4. data/Rakefile +142 -0
  5. data/bin/god +132 -0
  6. data/ext/god/.gitignore +5 -0
  7. data/ext/god/extconf.rb +55 -0
  8. data/ext/god/kqueue_handler.c +125 -0
  9. data/ext/god/netlink_handler.c +168 -0
  10. data/god.gemspec +164 -0
  11. data/lib/god.rb +701 -0
  12. data/lib/god/behavior.rb +52 -0
  13. data/lib/god/behaviors/clean_pid_file.rb +21 -0
  14. data/lib/god/behaviors/clean_unix_socket.rb +21 -0
  15. data/lib/god/behaviors/notify_when_flapping.rb +51 -0
  16. data/lib/god/cli/command.rb +256 -0
  17. data/lib/god/cli/run.rb +172 -0
  18. data/lib/god/cli/version.rb +23 -0
  19. data/lib/god/compat19.rb +36 -0
  20. data/lib/god/condition.rb +96 -0
  21. data/lib/god/conditions/always.rb +23 -0
  22. data/lib/god/conditions/complex.rb +86 -0
  23. data/lib/god/conditions/cpu_usage.rb +80 -0
  24. data/lib/god/conditions/degrading_lambda.rb +52 -0
  25. data/lib/god/conditions/disk_usage.rb +32 -0
  26. data/lib/god/conditions/file_mtime.rb +28 -0
  27. data/lib/god/conditions/flapping.rb +128 -0
  28. data/lib/god/conditions/http_response_code.rb +168 -0
  29. data/lib/god/conditions/lambda.rb +25 -0
  30. data/lib/god/conditions/memory_usage.rb +82 -0
  31. data/lib/god/conditions/process_exits.rb +72 -0
  32. data/lib/god/conditions/process_running.rb +74 -0
  33. data/lib/god/conditions/tries.rb +44 -0
  34. data/lib/god/configurable.rb +57 -0
  35. data/lib/god/contact.rb +114 -0
  36. data/lib/god/contacts/campfire.rb +121 -0
  37. data/lib/god/contacts/email.rb +136 -0
  38. data/lib/god/contacts/jabber.rb +75 -0
  39. data/lib/god/contacts/prowl.rb +57 -0
  40. data/lib/god/contacts/scout.rb +55 -0
  41. data/lib/god/contacts/twitter.rb +51 -0
  42. data/lib/god/contacts/webhook.rb +73 -0
  43. data/lib/god/dependency_graph.rb +41 -0
  44. data/lib/god/diagnostics.rb +37 -0
  45. data/lib/god/driver.rb +206 -0
  46. data/lib/god/errors.rb +24 -0
  47. data/lib/god/event_handler.rb +108 -0
  48. data/lib/god/event_handlers/dummy_handler.rb +13 -0
  49. data/lib/god/event_handlers/kqueue_handler.rb +17 -0
  50. data/lib/god/event_handlers/netlink_handler.rb +13 -0
  51. data/lib/god/logger.rb +109 -0
  52. data/lib/god/metric.rb +59 -0
  53. data/lib/god/process.rb +363 -0
  54. data/lib/god/registry.rb +32 -0
  55. data/lib/god/simple_logger.rb +59 -0
  56. data/lib/god/socket.rb +107 -0
  57. data/lib/god/sugar.rb +47 -0
  58. data/lib/god/sys_logger.rb +45 -0
  59. data/lib/god/system/portable_poller.rb +42 -0
  60. data/lib/god/system/process.rb +50 -0
  61. data/lib/god/system/slash_proc_poller.rb +92 -0
  62. data/lib/god/task.rb +503 -0
  63. data/lib/god/timeline.rb +25 -0
  64. data/lib/god/trigger.rb +43 -0
  65. data/lib/god/watch.rb +188 -0
  66. data/test/configs/child_events/child_events.god +44 -0
  67. data/test/configs/child_events/simple_server.rb +3 -0
  68. data/test/configs/child_polls/child_polls.god +37 -0
  69. data/test/configs/child_polls/simple_server.rb +12 -0
  70. data/test/configs/complex/complex.god +59 -0
  71. data/test/configs/complex/simple_server.rb +3 -0
  72. data/test/configs/contact/contact.god +108 -0
  73. data/test/configs/contact/simple_server.rb +3 -0
  74. data/test/configs/daemon_events/daemon_events.god +37 -0
  75. data/test/configs/daemon_events/simple_server.rb +8 -0
  76. data/test/configs/daemon_events/simple_server_stop.rb +11 -0
  77. data/test/configs/daemon_polls/daemon_polls.god +17 -0
  78. data/test/configs/daemon_polls/simple_server.rb +6 -0
  79. data/test/configs/degrading_lambda/degrading_lambda.god +31 -0
  80. data/test/configs/degrading_lambda/tcp_server.rb +15 -0
  81. data/test/configs/lifecycle/lifecycle.god +25 -0
  82. data/test/configs/matias/matias.god +50 -0
  83. data/test/configs/real.rb +59 -0
  84. data/test/configs/running_load/running_load.god +16 -0
  85. data/test/configs/stop_options/simple_server.rb +12 -0
  86. data/test/configs/stop_options/stop_options.god +39 -0
  87. data/test/configs/stress/simple_server.rb +3 -0
  88. data/test/configs/stress/stress.god +15 -0
  89. data/test/configs/task/logs/.placeholder +0 -0
  90. data/test/configs/task/task.god +26 -0
  91. data/test/configs/test.rb +61 -0
  92. data/test/helper.rb +141 -0
  93. data/test/suite.rb +6 -0
  94. data/test/test_behavior.rb +18 -0
  95. data/test/test_campfire.rb +23 -0
  96. data/test/test_condition.rb +50 -0
  97. data/test/test_conditions_disk_usage.rb +50 -0
  98. data/test/test_conditions_http_response_code.rb +109 -0
  99. data/test/test_conditions_process_running.rb +40 -0
  100. data/test/test_conditions_tries.rb +67 -0
  101. data/test/test_contact.rb +109 -0
  102. data/test/test_dependency_graph.rb +62 -0
  103. data/test/test_driver.rb +11 -0
  104. data/test/test_email.rb +34 -0
  105. data/test/test_event_handler.rb +80 -0
  106. data/test/test_god.rb +570 -0
  107. data/test/test_handlers_kqueue_handler.rb +16 -0
  108. data/test/test_jabber.rb +29 -0
  109. data/test/test_logger.rb +55 -0
  110. data/test/test_metric.rb +72 -0
  111. data/test/test_process.rb +247 -0
  112. data/test/test_prowl.rb +15 -0
  113. data/test/test_registry.rb +15 -0
  114. data/test/test_socket.rb +34 -0
  115. data/test/test_sugar.rb +42 -0
  116. data/test/test_system_portable_poller.rb +17 -0
  117. data/test/test_system_process.rb +30 -0
  118. data/test/test_task.rb +246 -0
  119. data/test/test_timeline.rb +37 -0
  120. data/test/test_trigger.rb +59 -0
  121. data/test/test_watch.rb +279 -0
  122. data/test/test_webhook.rb +15 -0
  123. metadata +362 -0
@@ -0,0 +1,75 @@
1
+ # Send a notice to a Jabber address.
2
+ #
3
+ # host - The String hostname of the Jabber server.
4
+ # port - The Integer port of the Jabber server (default: 5222).
5
+ # from_jid - The String Jabber ID of the sender.
6
+ # password - The String password of the sender.
7
+ # to_jid - The String Jabber ID of the recipient.
8
+ # subject - The String subject of the message (default: "God Notification").
9
+
10
+ CONTACT_DEPS[:jabber] = ['xmpp4r']
11
+ CONTACT_DEPS[:jabber].each do |d|
12
+ require d
13
+ end
14
+
15
+ module God
16
+ module Contacts
17
+
18
+ class Jabber < Contact
19
+ class << self
20
+ attr_accessor :host, :port, :from_jid, :password, :to_jid, :subject
21
+ attr_accessor :format
22
+ end
23
+
24
+ self.port = 5222
25
+ self.subject = 'God Notification'
26
+
27
+ self.format = lambda do |message, time, priority, category, host|
28
+ text = "Message: #{message}\n"
29
+ text += "Host: #{host}\n" if host
30
+ text += "Priority: #{priority}\n" if priority
31
+ text += "Category: #{category}\n" if category
32
+ text
33
+ end
34
+
35
+ attr_accessor :host, :port, :from_jid, :password, :to_jid, :subject
36
+
37
+ def valid?
38
+ valid = true
39
+ valid &= complain("Attribute 'host' must be specified", self) unless arg(:host)
40
+ valid &= complain("Attribute 'port' must be specified", self) unless arg(:port)
41
+ valid &= complain("Attribute 'from_jid' must be specified", self) unless arg(:from_jid)
42
+ valid &= complain("Attribute 'to_jid' must be specified", self) unless arg(:to_jid)
43
+ valid &= complain("Attribute 'password' must be specified", self) unless arg(:password)
44
+ valid
45
+ end
46
+
47
+ def notify(message, time, priority, category, host)
48
+ body = Jabber.format.call(message, time, priority, category, host)
49
+
50
+ message = ::Jabber::Message.new(arg(:to_jid), body)
51
+ message.set_type(:normal)
52
+ message.set_id('1')
53
+ message.set_subject(arg(:subject))
54
+
55
+ jabber_id = ::Jabber::JID.new("#{arg(:from_jid)}/God")
56
+
57
+ client = ::Jabber::Client.new(jabber_id)
58
+ client.connect(arg(:host), arg(:port))
59
+ client.auth(arg(:password))
60
+ client.send(message)
61
+ client.close
62
+
63
+ self.info = "sent jabber message to #{self.to_jid}"
64
+ rescue Object => e
65
+ if e.respond_to?(:message)
66
+ applog(nil, :info, "failed to send jabber message to #{arg(:to_jid)}: #{e.message}")
67
+ else
68
+ applog(nil, :info, "failed to send jabber message to #{arg(:to_jid)}: #{e.class}")
69
+ end
70
+ applog(nil, :debug, e.backtrace.join("\n"))
71
+ end
72
+
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,57 @@
1
+ # Send a notice to Prowl (http://prowl.weks.net/).
2
+ #
3
+ # apikey - The String API key.
4
+
5
+ CONTACT_DEPS[:prowl] = ['prowly']
6
+ CONTACT_DEPS[:prowl].each do |d|
7
+ require d
8
+ end
9
+
10
+ module God
11
+ module Contacts
12
+ class Prowl < Contact
13
+
14
+ class << self
15
+ attr_accessor :apikey
16
+ end
17
+
18
+ def valid?
19
+ valid = true
20
+ valid &= complain("Attribute 'apikey' must be specified", self) if self.apikey.nil?
21
+ valid
22
+ end
23
+
24
+ attr_accessor :apikey
25
+
26
+ def notify(message, time, priority, category, host)
27
+ result = Prowly.notify do |n|
28
+ n.apikey = arg(:apikey)
29
+ n.priority = map_priority(priority.to_i)
30
+ n.application = category || "God"
31
+ n.event = "on " + host.to_s
32
+ n.description = message.to_s + " at " + time.to_s
33
+ end
34
+
35
+ if result.succeeded?
36
+ self.info = "sent prowl notification to #{self.name}"
37
+ else
38
+ self.info = "failed to send prowl notification to #{self.name}: #{result.message}"
39
+ end
40
+ rescue Object => e
41
+ applog(nil, :info, "failed to send prowl notification to #{self.name}: #{e.message}")
42
+ applog(nil, :debug, e.backtrace.join("\n"))
43
+ end
44
+
45
+ def map_priority(priority)
46
+ case priority
47
+ when 1 then Prowly::Notification::Priority::EMERGENCY
48
+ when 2 then Prowly::Notification::Priority::HIGH
49
+ when 3 then Prowly::Notification::Priority::NORMAL
50
+ when 4 then Prowly::Notification::Priority::MODERATE
51
+ when 5 then Prowly::Notification::Priority::VERY_LOW
52
+ else Prowly::Notification::Priority::NORMAL
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,55 @@
1
+ # Send a notice to Scout (http://scoutapp.com/).
2
+ #
3
+ # client_key - The String client key.
4
+ # plugin_id - The String plugin id.
5
+
6
+ require 'net/http'
7
+ require 'uri'
8
+
9
+ module God
10
+ module Contacts
11
+
12
+ class Scout < Contact
13
+ class << self
14
+ attr_accessor :client_key, :plugin_id
15
+ attr_accessor :format
16
+ end
17
+
18
+ self.format = lambda do |message, priority, category, host|
19
+ text = "Message: #{message}\n"
20
+ text += "Host: #{host}\n" if host
21
+ text += "Priority: #{priority}\n" if priority
22
+ text += "Category: #{category}\n" if category
23
+ return text
24
+ end
25
+
26
+ attr_accessor :client_key, :plugin_id
27
+
28
+ def valid?
29
+ valid = true
30
+ valid &= complain("Attribute 'client_key' must be specified", self) unless arg(:client_key)
31
+ valid &= complain("Attribute 'plugin_id' must be specified", self) unless arg(:plugin_id)
32
+ valid
33
+ end
34
+
35
+ def notify(message, time, priority, category, host)
36
+ data = {
37
+ :client_key => arg(:client_key),
38
+ :plugin_id => arg(:plugin_id),
39
+ :format => 'xml',
40
+ 'alert[subject]' => message,
41
+ 'alert[body]' => Scout.format.call(message, priority, category, host)
42
+ }
43
+
44
+ uri = URI.parse('http://scoutapp.com/alerts/create')
45
+ Net::HTTP.post_form(uri, data)
46
+
47
+ self.info = "sent scout alert to plugin ##{plugin_id}"
48
+ rescue => e
49
+ applog(nil, :info, "failed to send scout alert to plugin ##{plugin_id}: #{e.message}")
50
+ applog(nil, :debug, e.backtrace.join("\n"))
51
+ end
52
+ end
53
+
54
+ end
55
+ end
@@ -0,0 +1,51 @@
1
+ # Send a notice to a Twitter account (http://twitter.com/).
2
+ #
3
+ # consumer_token - The String OAuth consumer token (defaults to God's
4
+ # existing consumer token).
5
+ # consumer_secret - The String OAuth consumer secret (defaults to God's
6
+ # existing consumer secret).
7
+ # access_token - The String OAuth access token.
8
+ # access_secret - The String OAuth access secret.
9
+
10
+ CONTACT_DEPS[:twitter] = ['twitter']
11
+ CONTACT_DEPS[:twitter].each do |d|
12
+ require d
13
+ end
14
+
15
+ module God
16
+ module Contacts
17
+ class Twitter < Contact
18
+ class << self
19
+ attr_accessor :consumer_token, :consumer_secret,
20
+ :access_token, :access_secret
21
+ end
22
+
23
+ self.consumer_token = 'gOhjax6s0L3mLeaTtBWPw'
24
+ self.consumer_secret = 'yz4gpAVXJHKxvsGK85tEyzQJ7o2FEy27H1KEWL75jfA'
25
+
26
+ def valid?
27
+ valid = true
28
+ valid &= complain("Attribute 'consumer_token' must be specified", self) unless arg(:consumer_token)
29
+ valid &= complain("Attribute 'consumer_secret' must be specified", self) unless arg(:consumer_secret)
30
+ valid &= complain("Attribute 'access_token' must be specified", self) unless arg(:access_token)
31
+ valid &= complain("Attribute 'access_secret' must be specified", self) unless arg(:access_secret)
32
+ valid
33
+ end
34
+
35
+ attr_accessor :consumer_token, :consumer_secret,
36
+ :access_token, :access_secret
37
+
38
+ def notify(message, time, priority, category, host)
39
+ oauth = ::Twitter::OAuth.new(arg(:consumer_token), arg(:consumer_secret))
40
+ oauth.authorize_from_access(arg(:access_token), arg(:access_secret))
41
+
42
+ ::Twitter::Base.new(oauth).update(message)
43
+
44
+ self.info = "sent twitter update"
45
+ rescue => e
46
+ applog(nil, :info, "failed to send twitter update: #{e.message}")
47
+ applog(nil, :debug, e.backtrace.join("\n"))
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,73 @@
1
+ # Send a notice to a webhook.
2
+ #
3
+ # url - The String webhook URL.
4
+ # format - The Symbol format [ :form | :json ] (default: :form).
5
+
6
+ require 'net/http'
7
+ require 'uri'
8
+
9
+ CONTACT_DEPS[:webhook] = ['json']
10
+ CONTACT_DEPS[:webhook].each do |d|
11
+ require d
12
+ end
13
+
14
+ module God
15
+ module Contacts
16
+
17
+ class Webhook < Contact
18
+ class << self
19
+ attr_accessor :url, :format
20
+ end
21
+
22
+ self.format = :form
23
+
24
+ def valid?
25
+ valid = true
26
+ valid &= complain("Attribute 'url' must be specified", self) unless arg(:url)
27
+ valid &= complain("Attribute 'format' must be one of [ :form | :json ]", self) unless [:form, :json].include?(arg(:format))
28
+ valid
29
+ end
30
+
31
+ attr_accessor :url, :format
32
+
33
+ def notify(message, time, priority, category, host)
34
+ data = {
35
+ :message => message,
36
+ :time => time,
37
+ :priority => priority,
38
+ :category => category,
39
+ :host => host
40
+ }
41
+
42
+ uri = URI.parse(arg(:url))
43
+ http = Net::HTTP.new(uri.host, uri.port)
44
+
45
+ req = nil
46
+ res = nil
47
+
48
+ case arg(:format)
49
+ when :form
50
+ req = Net::HTTP::Post.new(uri.path)
51
+ req.set_form_data(data)
52
+ when :json
53
+ req = Net::HTTP::Post.new(uri.path)
54
+ req.body = data.to_json
55
+ end
56
+
57
+ res = http.request(req)
58
+
59
+ case res
60
+ when Net::HTTPSuccess
61
+ self.info = "sent webhook to #{arg(:url)}"
62
+ else
63
+ self.info = "failed to send webhook to #{arg(:url)}: #{res.error!}"
64
+ end
65
+ rescue Object => e
66
+ applog(nil, :info, "failed to send email to #{arg(:url)}: #{e.message}")
67
+ applog(nil, :debug, e.backtrace.join("\n"))
68
+ end
69
+
70
+ end
71
+
72
+ end
73
+ end
@@ -0,0 +1,41 @@
1
+ module God
2
+ class DependencyGraph
3
+ attr_accessor :nodes
4
+
5
+ def initialize
6
+ self.nodes = {}
7
+ end
8
+
9
+ def add(a, b)
10
+ node_a = self.nodes[a] || Node.new(a)
11
+ node_b = self.nodes[b] || Node.new(b)
12
+
13
+ node_a.add(node_b)
14
+
15
+ self.nodes[a] ||= node_a
16
+ self.nodes[b] ||= node_b
17
+ end
18
+ end
19
+ end
20
+
21
+ module God
22
+ class DependencyGraph
23
+ class Node
24
+ attr_accessor :name
25
+ attr_accessor :dependencies
26
+
27
+ def initialize(name)
28
+ self.name = name
29
+ self.dependencies = []
30
+ end
31
+
32
+ def add(node)
33
+ self.dependencies << node unless self.dependencies.include?(node)
34
+ end
35
+
36
+ def has_node?(node)
37
+ (self == node) || self.dependencies.any { |x| x.has_node?(node) }
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,37 @@
1
+ def start_dike
2
+ require 'dike'
3
+ Thread.new do
4
+ Dike.logfactory File.join(File.dirname(__FILE__), *%w[.. .. logs])
5
+ loop do
6
+ Dike.finger
7
+ sleep(1)
8
+ end
9
+ end
10
+ end
11
+
12
+ class BleakHouseDiagnostic
13
+ LOG_FILE = File.join(File.dirname(__FILE__), *%w[.. .. logs bleak.log])
14
+
15
+ def self.install
16
+ require 'snapshot'
17
+ self.spin
18
+ end
19
+
20
+ def self.snapshot
21
+ @count ||= 0
22
+ filename = "/tmp/god-bleak-%s-%03i.dump" % [Process.pid,@count]
23
+ STDERR.puts "** BleakHouse: working..."
24
+ BleakHouse.ext_snapshot(filename, 3)
25
+ STDERR.puts "** BleakHouse: complete\n** Bleakhouse: Run 'bleak #{filename}' to analyze."
26
+ @count += 1
27
+ end
28
+
29
+ def self.spin(delay = 60)
30
+ Thread.new do
31
+ loop do
32
+ sleep(delay)
33
+ self.snapshot
34
+ end
35
+ end
36
+ end
37
+ end
data/lib/god/driver.rb ADDED
@@ -0,0 +1,206 @@
1
+ require 'monitor'
2
+
3
+ # ruby 1.9 specific fixes
4
+ unless RUBY_VERSION < '1.9'
5
+ require 'god/compat19'
6
+ end
7
+
8
+ module God
9
+ class TimedEvent
10
+ include Comparable
11
+
12
+ attr_accessor :at
13
+
14
+ # Instantiate a new TimedEvent that will be triggered after the specified delay
15
+ # +delay+ is the number of seconds from now at which to trigger
16
+ #
17
+ # Returns TimedEvent
18
+ def initialize(delay = 0)
19
+ self.at = Time.now + delay
20
+ end
21
+
22
+ def due?
23
+ Time.now >= self.at
24
+ end
25
+
26
+ def <=>(other)
27
+ self.at <=> other.at
28
+ end
29
+ end # DriverEvent
30
+
31
+ class DriverEvent < TimedEvent
32
+ attr_accessor :condition, :task
33
+
34
+ def initialize(delay, task, condition)
35
+ super delay
36
+ self.task = task
37
+ self.condition = condition
38
+ end
39
+
40
+ def handle_event
41
+ @task.handle_poll(@condition)
42
+ end
43
+ end # DriverEvent
44
+
45
+ class DriverOperation < TimedEvent
46
+ attr_accessor :task, :name, :args
47
+
48
+ def initialize(task, name, args)
49
+ super(0)
50
+ self.task = task
51
+ self.name = name
52
+ self.args = args
53
+ end
54
+
55
+ # Handle the next queued operation that was issued asynchronously
56
+ #
57
+ # Returns nothing
58
+ def handle_event
59
+ @task.send(@name, *@args)
60
+ end
61
+ end
62
+
63
+ class DriverEventQueue
64
+ def initialize
65
+ @shutdown = false
66
+ @events = []
67
+ @monitor = Monitor.new
68
+ @resource = @monitor.new_cond
69
+ @events.taint
70
+ self.taint
71
+ end
72
+
73
+ #
74
+ # Wake any sleeping threads after setting the sentinel
75
+ #
76
+ def shutdown
77
+ @shutdown = true
78
+ @monitor.synchronize do
79
+ @resource.broadcast
80
+ end
81
+ end
82
+
83
+ #
84
+ # Sleep until the queue has something due
85
+ #
86
+ def pop
87
+ @monitor.synchronize do
88
+ if @events.empty?
89
+ raise ThreadError, "queue empty" if @shutdown
90
+ @resource.wait
91
+ else !@events.first.due?
92
+ delay = @events.first.at - Time.now
93
+ @resource.wait(delay) if delay > 0
94
+ end
95
+
96
+ @events.shift
97
+ end
98
+ end
99
+
100
+ alias shift pop
101
+ alias deq pop
102
+
103
+ #
104
+ # Add an event to the queue, wake any waiters if what we added needs to
105
+ # happen sooner than the next pending event
106
+ #
107
+ def push(event)
108
+ @monitor.synchronize do
109
+ @events << event
110
+ @events.sort!
111
+
112
+ # If we've sorted the events and found the one we're adding is at
113
+ # the front, it will likely need to run before the next due date
114
+ @resource.signal if @events.first == event
115
+ end
116
+ end
117
+
118
+ alias << push
119
+ alias enq push
120
+
121
+ def empty?
122
+ @events.empty?
123
+ end
124
+
125
+ def clear
126
+ @events.clear
127
+ end
128
+
129
+ def length
130
+ @events.length
131
+ end
132
+
133
+ alias size length
134
+ end
135
+
136
+
137
+ class Driver
138
+ attr_reader :thread
139
+
140
+ # Instantiate a new Driver and start the scheduler loop to handle events
141
+ # +task+ is the Task this Driver belongs to
142
+ #
143
+ # Returns Driver
144
+ def initialize(task)
145
+ @task = task
146
+ @events = God::DriverEventQueue.new
147
+
148
+ @thread = Thread.new do
149
+ loop do
150
+ begin
151
+ @events.pop.handle_event
152
+ rescue ThreadError => e
153
+ # queue is empty
154
+ break
155
+ rescue Object => e
156
+ message = format("Unhandled exception in driver loop - (%s): %s\n%s",
157
+ e.class, e.message, e.backtrace.join("\n"))
158
+ applog(nil, :fatal, message)
159
+ end
160
+ end
161
+ end
162
+ end
163
+
164
+ # Check if we're in the driver context
165
+ #
166
+ # Returns true if in driver thread
167
+ def in_driver_context?
168
+ Thread.current == @thread
169
+ end
170
+
171
+ # Clear all events for this Driver
172
+ #
173
+ # Returns nothing
174
+ def clear_events
175
+ @events.clear
176
+ end
177
+
178
+ # Shutdown the DriverEventQueue threads
179
+ #
180
+ # Returns nothing
181
+ def shutdown
182
+ @events.shutdown
183
+ end
184
+
185
+ # Queue an asynchronous message
186
+ # +name+ is the Symbol name of the operation
187
+ # +args+ is an optional Array of arguments
188
+ #
189
+ # Returns nothing
190
+ def message(name, args = [])
191
+ @events.push(DriverOperation.new(@task, name, args))
192
+ end
193
+
194
+ # Create and schedule a new DriverEvent
195
+ # +condition+ is the Condition
196
+ # +delay+ is the number of seconds to delay (default: interval defined in condition)
197
+ #
198
+ # Returns nothing
199
+ def schedule(condition, delay = condition.interval)
200
+ applog(nil, :debug, "driver schedule #{condition} in #{delay} seconds")
201
+
202
+ @events.push(DriverEvent.new(delay, @task, condition))
203
+ end
204
+ end # Driver
205
+
206
+ end # God