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
cd29b035
Commit
cd29b035
authored
Mar 14, 2011
by
John E. Vincent
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
breaking out to modular agents. need to DRY up
parent
cb98e067
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
89 additions
and
23 deletions
+89
-23
noah-watcher.rb
bin/noah-watcher.rb
+14
-23
dummy_agent.rb
lib/noah/agents/dummy_agent.rb
+29
-0
http_agent.rb
lib/noah/agents/http_agent.rb
+37
-0
watchers.rb
lib/noah/models/watchers.rb
+9
-0
No files found.
bin/noah-watcher.rb
View file @
cd29b035
...
...
@@ -12,6 +12,8 @@ begin
require
'eventmachine'
require
'em-http-request'
require
'noah'
require
'noah/agents/http_agent'
require
'noah/agents/dummy_agent'
require
'json'
rescue
LoadError
puts
HELP
...
...
@@ -23,31 +25,16 @@ LOGGER = Logger.new(STDOUT)
class
EventMachine
::
NoahAgent
include
EM
::
Deferrable
Noah
::
Agents
::
HttpAgent
.
register
Noah
::
Agents
::
DummyAgent
.
register
@@watchers
=
Noah
::
Watcher
.
watch_list
@@agents
=
Noah
::
Watchers
.
agents
def
initialize
@logger
=
LOGGER
@logger
.
debug
(
"Initializing with
#{
@@watchers
.
size
}
registered watches"
)
@logger
.
debug
(
"
#{
@@agents
}
agents registered"
)
if
EventMachine
.
reactor_running?
@worker
=
EM
.
spawn
{
|
event
,
message
,
watch_list
|
logger
=
LOGGER
logger
.
info
(
"Worker initiated"
)
logger
.
debug
(
"got event on http worker:
#{
event
}
"
)
matches
=
watch_list
.
find_all
{
|
w
|
event
=~
/^
#{
Base64
.
decode64
(
w
)
}
/
}
logger
.
debug
(
"Found
#{
matches
.
size
}
matches for
#{
event
}
"
)
EM
::
Iterator
.
new
(
matches
).
each
do
|
watch
,
iter
|
p
,
ep
=
Base64
.
decode64
(
watch
).
split
(
"|"
)
logger
.
info
(
"Sending message to:
#{
ep
}
for pattern:
#{
p
}
"
)
http
=
EM
::
HttpRequest
.
new
(
ep
,
:connection_timeout
=>
2
,
:inactivity_timeout
=>
4
).
post
:body
=>
message
http
.
callback
{
logger
.
info
(
"Message posted to
#{
ep
}
successfully"
)
}
http
.
errback
{
logger
.
error
(
"Something went wrong"
)
}
iter
.
next
end
}
self
.
succeed
(
"Succeed callback"
)
else
logger
.
fatal
(
"Must be inside a reactor!"
)
...
...
@@ -58,19 +45,24 @@ class EventMachine::NoahAgent
@@watchers
.
size
end
def
http_worker
@http_worker
end
def
reread_watchers
@logger
.
debug
(
"Found new watches"
)
@logger
.
debug
(
"Current watch count:
#{
@@watchers
.
size
}
"
)
@@watchers
=
Noah
::
Watcher
.
watch_list
@logger
.
debug
(
"New watch count:
#{
@@watchers
.
size
}
"
)
#@logger.debug(@@watchers)
end
def
broker
(
msg
)
# This is just for testing for now
e
,
m
=
msg
.
split
(
"|"
)
be
=
Base64
.
encode64
(
e
).
gsub
(
"
\n
"
,
""
)
@worker
.
notify
e
,
m
,
@@watchers
.
clone
EM
::
Iterator
.
new
(
@@agents
).
each
do
|
agent
,
iter
|
agent
.
send
(
:notify
,
e
,
m
,
@@watchers
.
clone
)
iter
.
next
end
end
end
...
...
@@ -93,7 +85,6 @@ EventMachine.run do
r
.
on
(
:pmessage
)
do
|
pattern
,
event
,
message
|
noah
.
reread_watchers
if
event
=~
/^\/\/noah\/watcher\/.*/
master_channel
.
push
"
#{
event
}
|
#{
message
}
"
logger
.
debug
(
"Saw[
#{
event
}
]"
)
end
sub
=
master_channel
.
subscribe
{
|
msg
|
...
...
lib/noah/agents/dummy_agent.rb
0 → 100644
View file @
cd29b035
module
Noah::Agents
class
DummyAgent
include
EM
::
Deferrable
PREFIX
=
"dummy"
NAME
=
"dummy"
def
self
.
register
Noah
::
Watchers
.
register_agent
(
self
)
end
def
self
.
notify
(
event
,
message
,
watch_list
)
logger
=
LOGGER
logger
.
info
(
"
#{
NAME
}
: Worker initiated"
)
logger
.
debug
(
"
#{
NAME
}
: got event -
#{
event
}
"
)
matches
=
watch_list
.
find_all
{
|
w
|
event
=~
/^
#{
Base64
.
decode64
(
w
)
}
/
}
logger
.
debug
(
"
#{
NAME
}
: Found
#{
matches
.
size
}
possible matches for
#{
event
}
"
)
EM
::
Iterator
.
new
(
matches
).
each
do
|
watch
,
iter
|
p
,
ep
=
Base64
.
decode64
(
watch
).
split
(
"|"
)
if
ep
=~
/^
#{
PREFIX
}
/
logger
.
info
(
"
#{
NAME
}
: Sending message to:
#{
ep
}
for pattern:
#{
p
}
"
)
logger
.
debug
(
"
#{
NAME
}
: message received:
#{
message
}
"
)
end
iter
.
next
end
end
end
end
lib/noah/agents/http_agent.rb
0 → 100644
View file @
cd29b035
require
'logger'
module
Noah::Agents
class
HttpAgent
include
EM
::
Deferrable
PREFIX
=
"http"
NAME
=
"http"
def
self
.
register
Noah
::
Watchers
.
register_agent
(
self
)
end
def
self
.
notify
(
event
,
message
,
watch_list
)
logger
=
LOGGER
logger
.
info
(
"
#{
NAME
}
: Worker initiated"
)
logger
.
debug
(
"
#{
NAME
}
: got event -
#{
event
}
"
)
matches
=
watch_list
.
find_all
{
|
w
|
event
=~
/^
#{
Base64
.
decode64
(
w
)
}
/
}
logger
.
debug
(
"
#{
PREFIX
}
: Found
#{
matches
.
size
}
possible matches for
#{
event
}
"
)
EM
::
Iterator
.
new
(
matches
).
each
do
|
watch
,
iter
|
p
,
ep
=
Base64
.
decode64
(
watch
).
split
(
"|"
)
if
ep
=~
/^
#{
PREFIX
}
/
logger
.
info
(
"
#{
NAME
}
: Sending message to (
#{
ep
}
) for pattern (
#{
p
}
)"
)
http
=
EM
::
HttpRequest
.
new
(
ep
,
:connection_timeout
=>
2
,
:inactivity_timeout
=>
4
).
post
:body
=>
message
http
.
callback
{
logger
.
info
(
"
#{
NAME
}
: Message posted to
#{
ep
}
successfully"
)
}
http
.
errback
{
logger
.
error
(
"
#{
NAME
}
: Something went wrong with
#{
ep
}
"
)
}
iter
.
next
end
end
end
end
end
lib/noah/models/watchers.rb
View file @
cd29b035
...
...
@@ -55,8 +55,17 @@ module Noah
end
class
Watchers
@@agents
=
[]
def
self
.
all
(
options
=
{})
options
.
empty?
?
Watcher
.
all
.
sort
:
Watcher
.
find
(
options
).
sort
end
def
self
.
register_agent
(
agent_class
)
@@agents
<<
agent_class
end
def
self
.
agents
@@agents
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