Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
N
Noah
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
Noah
Commits
f7e41c63
Commit
f7e41c63
authored
Mar 06, 2011
by
John E. Vincent
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
I'm pretty happy with this watcher implementation
parent
f15ebc40
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
38 additions
and
42 deletions
+38
-42
noah-watcher.rb
bin/noah-watcher.rb
+20
-27
models.rb
lib/noah/models.rb
+1
-1
watchers.rb
lib/noah/models/watchers.rb
+5
-14
watcher_validations.rb
lib/noah/validations/watcher_validations.rb
+12
-0
No files found.
bin/noah-watcher.rb
View file @
f7e41c63
...
@@ -13,32 +13,24 @@ LOGGER = Logger.new(STDOUT)
...
@@ -13,32 +13,24 @@ LOGGER = Logger.new(STDOUT)
class
EventMachine
::
NoahAgent
class
EventMachine
::
NoahAgent
include
EM
::
Deferrable
include
EM
::
Deferrable
@@watchers
=
Noah
::
Watcher
s
.
all
@@watchers
=
Noah
::
Watcher
.
watch_list
def
initialize
def
initialize
@logger
=
LOGGER
@logger
=
LOGGER
@logger
.
debug
(
"Initializing with
#{
@@watchers
.
size
}
registered watches"
)
@logger
.
debug
(
"Initializing with
#{
@@watchers
.
size
}
registered watches"
)
# Some logic to spawn protocol specific agents
# i.e. http, amqp, xmpp, redis, whatever
if
EventMachine
.
reactor_running?
if
EventMachine
.
reactor_running?
@
http_worker
=
EM
.
spawn
{
|
msg
,
ep
|
@
worker
=
EM
.
spawn
{
|
event
,
message
,
watch_list
|
logger
=
LOGGER
logger
=
LOGGER
logger
.
debug
(
"spawning http processor"
)
logger
.
debug
(
"Worker initiated"
)
logger
.
info
(
"got ep on http worker:
#{
ep
}
"
)
logger
.
info
(
"got event on http worker:
#{
event
}
"
)
logger
.
info
(
"got msg on http worker:
#{
msg
}
"
)
logger
.
info
(
"got message on http worker:
#{
message
}
"
)
}
matches
=
watch_list
.
find_all
{
|
w
|
event
=~
/^
#{
Base64
.
decode64
(
w
)
}
/
}
@redis_worker
=
EM
.
spawn
{
|
msg
|
logger
.
debug
(
"Found
#{
matches
.
size
}
matches for
#{
event
}
"
)
logger
=
LOGGER
EM
::
Iterator
.
new
(
matches
).
each
do
|
watch
,
iter
|
logger
.
debug
(
"spawning redis worker"
)
p
,
ep
=
Base64
.
decode64
(
watch
).
split
(
"|"
)
logger
.
info
(
"got msg on redis worker:
#{
msg
}
"
)
logger
.
info
(
"Sending message to:
#{
ep
}
for pattern:
#{
p
}
"
)
}
iter
.
next
@log_worker
=
EM
.
spawn
{
|
msg
|
end
logger
=
LOGGER
logger
.
debug
(
"spawning logger worker:
#{
msg
}
"
)
}
@amqp_worker
=
EM
.
spawn
{
|
msg
|
logger
=
LOGGER
logger
.
debug
(
"spawning amqp worker:
#{
msg
}
"
)
}
}
else
else
logger
.
fatal
(
"Must be inside a reactor!"
)
logger
.
fatal
(
"Must be inside a reactor!"
)
...
@@ -52,18 +44,18 @@ class EventMachine::NoahAgent
...
@@ -52,18 +44,18 @@ class EventMachine::NoahAgent
def
reread_watchers
def
reread_watchers
@logger
.
debug
(
"Found new watches"
)
@logger
.
debug
(
"Found new watches"
)
@logger
.
debug
(
"Current watch count:
#{
@@watchers
.
size
}
"
)
@logger
.
debug
(
"Current watch count:
#{
@@watchers
.
size
}
"
)
@@watchers
=
Noah
::
Watcher
s
.
all
@@watchers
=
Noah
::
Watcher
.
watch_list
@logger
.
debug
(
"New watch count:
#{
@@watchers
.
size
}
"
)
@logger
.
debug
(
"New watch count:
#{
@@watchers
.
size
}
"
)
@logger
.
debug
(
@@watchers
)
#
@logger.debug(@@watchers)
end
end
def
broker
(
msg
)
def
broker
(
msg
)
# This is just for testing for now
# This is just for testing for now
@logger
.
info
(
msg
.
class
)
@logger
.
warn
(
msg
)
@logger
.
warn
(
msg
)
e
,
m
=
msg
.
split
(
"
\t
"
)
e
,
m
=
msg
.
split
(
"|"
)
be
=
Base64
.
encode64
(
e
).
gsub
(
"
\n
"
,
""
)
@http_worker
.
notify
m
,
e
@logger
.
info
(
"Encoded event:
#{
be
}
"
)
@worker
.
notify
e
,
m
,
@@watchers
.
clone
end
end
end
end
...
@@ -78,11 +70,12 @@ EventMachine.run do
...
@@ -78,11 +70,12 @@ EventMachine.run do
r
.
psubscribe
(
"//noah/*"
)
r
.
psubscribe
(
"//noah/*"
)
r
.
on
(
:pmessage
)
do
|
pattern
,
event
,
message
|
r
.
on
(
:pmessage
)
do
|
pattern
,
event
,
message
|
noah
.
reread_watchers
if
event
=~
/^\/\/noah\/watcher\/.*/
noah
.
reread_watchers
if
event
=~
/^\/\/noah\/watcher\/.*/
master_channel
.
push
"
#{
event
}
\t
#{
message
}
"
master_channel
.
push
"
#{
event
}
|
#{
message
}
"
logger
.
debug
(
"Publishing [
#{
event
}
]"
)
logger
.
debug
(
"Publishing [
#{
event
}
]"
)
end
end
sub
=
master_channel
.
subscribe
{
|
msg
|
sub
=
master_channel
.
subscribe
{
|
msg
|
# We short circuit if we have no watchers
noah
.
broker
(
msg
)
unless
noah
.
watchers
==
0
noah
.
broker
(
msg
)
unless
noah
.
watchers
==
0
}
}
end
end
lib/noah/models.rb
View file @
f7e41c63
...
@@ -69,7 +69,7 @@ module Noah
...
@@ -69,7 +69,7 @@ module Noah
self
.
name
.
nil?
?
name
=
@deleted_name
:
name
=
self
.
name
self
.
name
.
nil?
?
name
=
@deleted_name
:
name
=
self
.
name
# Pulling out dbnum for now. Need to rethink it
# Pulling out dbnum for now. Need to rethink it
#pub_category = "#{db}:noah.#{self.class.to_s}[#{name}].#{meth}"
#pub_category = "#{db}:noah.#{self.class.to_s}[#{name}].#{meth}"
pub_category
=
"
#{
self
.
patternize_me
}
.
#{
meth
}
"
pub_category
=
"
#{
self
.
patternize_me
}
"
Ohm
.
redis
.
publish
(
pub_category
,
self
.
to_hash
.
merge
({
"action"
=>
meth
,
"pubcategory"
=>
pub_category
}).
to_json
)
Ohm
.
redis
.
publish
(
pub_category
,
self
.
to_hash
.
merge
({
"action"
=>
meth
,
"pubcategory"
=>
pub_category
}).
to_json
)
# The following provides a post post-action hook. It allows a class to provide it's own handling after the fact
# The following provides a post post-action hook. It allows a class to provide it's own handling after the fact
...
...
lib/noah/models/watchers.rb
View file @
f7e41c63
...
@@ -21,7 +21,7 @@ module Noah
...
@@ -21,7 +21,7 @@ module Noah
end
end
def
name
def
name
@name
=
Digest
::
SHA1
.
hexdigest
"
#{
endpoint
}#{
pattern
}
"
@name
=
Base64
.
encode64
(
"
#{
pattern
}
|
#{
endpoint
}
"
).
gsub
(
"
\n
"
,
""
)
end
end
def
to_hash
def
to_hash
...
@@ -30,19 +30,10 @@ module Noah
...
@@ -30,19 +30,10 @@ module Noah
end
end
def
self
.
watch_list
def
self
.
watch_list
hsh
=
Hash
.
new
arr
=
[]
watch_list
=
self
.
all
.
sort_by
(
:pattern
)
watches
=
self
.
all
.
sort_by
(
:pattern
)
watch_list
.
each
do
|
watch
|
watches
.
each
{
|
w
|
arr
<<
w
.
name
}
p
=
watch
.
pattern
.
to_sym
arr
e
=
watch
.
endpoint
if
hsh
.
has_key?
(
p
)
hsh
[
p
].
push
(
watch
.
endpoint
)
else
hsh
[
p
]
=
Array
.
new
hsh
[
p
].
push
(
watch
.
endpoint
)
end
end
hsh
end
end
private
private
...
...
lib/noah/validations/watcher_validations.rb
View file @
f7e41c63
...
@@ -9,6 +9,10 @@ module Noah
...
@@ -9,6 +9,10 @@ module Noah
self
.
instance_of?
(
Noah
::
Watcher
)
?
(
assert
endpoint_overrides?
,
error
)
:
(
assert
false
,
"Validation not applicable"
)
self
.
instance_of?
(
Noah
::
Watcher
)
?
(
assert
endpoint_overrides?
,
error
)
:
(
assert
false
,
"Validation not applicable"
)
end
end
def
assert_valid_watch
(
error
=
[
:pattern
,
:invalid_format
])
self
.
instance_of?
(
Noah
::
Watcher
)
?
(
assert
pattern_valid?
,
error
)
:
(
assert
false
,
"Validation not applicable"
)
end
private
private
def
endpoint_covered?
def
endpoint_covered?
watches
=
Watcher
.
all
.
find
(
:endpoint
=>
self
.
endpoint
).
sort
watches
=
Watcher
.
all
.
find
(
:endpoint
=>
self
.
endpoint
).
sort
...
@@ -32,5 +36,13 @@ module Noah
...
@@ -32,5 +36,13 @@ module Noah
return
false
return
false
end
end
def
pattern_valid?
unless
self
.
pattern
.
match
(
/^\/\/noah\/.*\/$/
)
return
false
end
rescue
ArgumentError
return
false
end
end
end
end
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment