Class: Herald
- Inherits:
-
Object
- Object
- Herald
- Defined in:
- lib/herald.rb,
lib/herald/item.rb,
lib/herald/version.rb,
lib/herald/watcher.rb,
lib/herald/notifier.rb,
lib/herald/watchers/rss.rb,
lib/herald/notifiers/ping.rb,
lib/herald/notifiers/post.rb,
lib/herald/notifiers/growl.rb,
lib/herald/notifiers/stdout.rb,
lib/herald/watchers/twitter.rb,
lib/herald/watchers/website.rb,
lib/herald/notifiers/callback.rb
Defined Under Namespace
Constant Summary collapse
- VERSION =
"0.2"
- @@heralds =
[]
Instance Attribute Summary collapse
-
#keep_alive ⇒ Object
Returns the value of attribute keep_alive.
-
#subprocess ⇒ Object
Returns the value of attribute subprocess.
-
#watchers ⇒ Object
Returns the value of attribute watchers.
Class Method Summary collapse
- .alive? ⇒ Boolean
-
.clear ⇒ Object
stop all heralds, and remove them from list of herald instances.
-
.demonize! ⇒ Object
just walk away, leaving whatever strays to themselves.
- .heralds ⇒ Object
- .once(&block) ⇒ Object
-
.start ⇒ Object
batch methods.
- .stop ⇒ Object
- .watch(&block) ⇒ Object
- .watch_rss(options = {}, &block) ⇒ Object
-
.watch_twitter(&block) ⇒ Object
shorthand methods.
- .watch_website(options = {}, &block) ⇒ Object
Instance Method Summary collapse
-
#_for(*keywords) ⇒ Object
send keywords to Watchers.
-
#action(type = nil, options = {}, &block) ⇒ Object
send instructions to Watchers.
- #alive? ⇒ Boolean
-
#check(type, options = {}, &block) ⇒ Object
create a new Watcher.
-
#every(time) ⇒ Object
send sleep time to Watchers.
-
#initialize(&block) ⇒ Herald
constructor
instance methods:.
-
#start ⇒ Object
start Watchers.
-
#stop ⇒ Object
(also: #end, #kill)
is there a gentler way of doing it? or have watchers do cleanup tasks on exit? look at GOD.
Constructor Details
#initialize(&block) ⇒ Herald
instance methods:
80 81 82 83 84 85 86 87 88 |
# File 'lib/herald.rb', line 80 def initialize(&block) @watchers = [] @keep_alive = true if block_given? instance_eval(&block) end @@heralds << self self end |
Instance Attribute Details
#keep_alive ⇒ Object
Returns the value of attribute keep_alive.
12 13 14 |
# File 'lib/herald.rb', line 12 def keep_alive @keep_alive end |
#subprocess ⇒ Object
Returns the value of attribute subprocess.
12 13 14 |
# File 'lib/herald.rb', line 12 def subprocess @subprocess end |
#watchers ⇒ Object
Returns the value of attribute watchers.
12 13 14 |
# File 'lib/herald.rb', line 12 def watchers @watchers end |
Class Method Details
.alive? ⇒ Boolean
68 69 70 |
# File 'lib/herald.rb', line 68 def self.alive? @@heralds.any? { |h| h.alive? } end |
.clear ⇒ Object
stop all heralds, and remove them from list of herald instances. mostly useful for clearing @@heralds when testing
56 57 58 59 60 |
# File 'lib/herald.rb', line 56 def self.clear stop() @@heralds.clear true end |
.demonize! ⇒ Object
just walk away, leaving whatever strays to themselves
63 64 65 66 |
# File 'lib/herald.rb', line 63 def self.demonize! @@heralds.clear true end |
.heralds ⇒ Object
72 73 74 |
# File 'lib/herald.rb', line 72 def self.heralds @@heralds end |
.once(&block) ⇒ Object
18 19 20 21 22 23 |
# File 'lib/herald.rb', line 18 def self.once(&block) herald = new() herald.keep_alive = false herald.send(:instance_eval, &block) herald.start end |
.start ⇒ Object
batch methods
37 38 39 40 41 42 43 |
# File 'lib/herald.rb', line 37 def self.start return false if @@heralds.empty? @@heralds.each do |herald| herald.start end true end |
.stop ⇒ Object
45 46 47 48 49 50 51 |
# File 'lib/herald.rb', line 45 def self.stop return false unless Herald.alive? @@heralds.each do |herald| herald.stop end true end |
.watch(&block) ⇒ Object
14 15 16 |
# File 'lib/herald.rb', line 14 def self.watch(&block) new(&block).start end |
.watch_rss(options = {}, &block) ⇒ Object
29 30 31 |
# File 'lib/herald.rb', line 29 def self.watch_rss( = {}, &block) watch() { check(:rss, , &block) } end |
.watch_twitter(&block) ⇒ Object
shorthand methods
26 27 28 |
# File 'lib/herald.rb', line 26 def self.watch_twitter(&block) watch() { check(:twitter, &block) } end |
.watch_website(options = {}, &block) ⇒ Object
32 33 34 |
# File 'lib/herald.rb', line 32 def self.watch_website( = {}, &block) watch() { check(:website, , &block) } end |
Instance Method Details
#_for(*keywords) ⇒ Object
send keywords to Watchers
98 99 100 101 102 103 |
# File 'lib/herald.rb', line 98 def _for(*keywords) @watchers.each do |watcher| watcher._for(*keywords) end self end |
#action(type = nil, options = {}, &block) ⇒ Object
send instructions to Watchers
106 107 108 109 110 111 |
# File 'lib/herald.rb', line 106 def action(type = nil, = {}, &block) @watchers.each do |watcher| watcher.action(type, , &block) end self end |
#alive? ⇒ Boolean
179 180 181 |
# File 'lib/herald.rb', line 179 def alive? !!@subprocess end |
#check(type, options = {}, &block) ⇒ Object
create a new Watcher
91 92 93 94 95 |
# File 'lib/herald.rb', line 91 def check(type, = {}, &block) [:keep_alive] ||= @keep_alive @watchers << Herald::Watcher.new(type, , &block) self end |
#every(time) ⇒ Object
send sleep time to Watchers
114 115 116 117 118 119 |
# File 'lib/herald.rb', line 114 def every(time) @watchers.each do |watcher| watcher.every(time) end self end |
#start ⇒ Object
start Watchers
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/herald.rb', line 122 def start if @watchers.empty? raise "No watchers assigned" end # if herald is already running, first stop stop() if alive? # start watching as a @subprocess @subprocess = fork { @watchers.each do |watcher| watcher.start end # all watchers do their tasks in a new thread. # join all thread in this @subprocess Thread.list.each do |thread| thread.join unless thread == Thread.main end } # if herald process is persistant if @keep_alive Process.detach(@subprocess) else begin # wait before the end of this script # for all watchers to finish their jobs Process.waitpid(@subprocess) # if @subprocess PID does not exist, it will # be due to an error in the subprocess # which has terminated it and waitpid() # will throw an exception rescue Errno::ECHILD => e # do nothing end @subprocess = nil # signal unalive state end self # return instance object end |
#stop ⇒ Object Also known as: end, kill
is there a gentler way of doing it? or have watchers do cleanup tasks on exit? look at GOD
162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/herald.rb', line 162 def stop if @subprocess begin Process.kill("TERM", @subprocess) # if @subprocess PID does not exist, # this will be due to an error in the subprocess # which has terminated it rescue Errno::ESRCH => e # do nothing end end @subprocess = nil self end |