forked from winton/stasis
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Daemon working with beanstalk, spec for custom plugin
- Loading branch information
Showing
7 changed files
with
25 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
stasis: | ||
beanstalk-client: ~>1.1.0 | ||
directory_watcher: ~>1.4.0 | ||
rake: >=0.8.7 | ||
rocco: ~>0.6 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,36 @@ | ||
Stasis::Gems.activate %w(eventmachine yajl-ruby) | ||
require 'eventmachine' | ||
Stasis::Gems.activate %w(beanstalk-client yajl-ruby) | ||
|
||
require 'beanstalk-client' | ||
require 'yajl' | ||
|
||
class Stasis | ||
class Daemon | ||
|
||
def initialize(dir, port) | ||
@port = port | ||
|
||
last_retry = nil | ||
retries = 0 | ||
def initialize(options={}) | ||
puts "\nStarting Stasis daemon (beanstalk @ #{options[:beanstalk].join(', ')})..." | ||
|
||
puts "\nStarting Stasis daemon on port #{port}..." | ||
beanstalk = Beanstalk::Pool.new(options[:beanstalk]) | ||
|
||
begin | ||
EM.epoll if EM.epoll? | ||
EM.run do | ||
EM.start_server '0.0.0.0', port, Tcp | ||
while true | ||
sleep(1.0 / 1000.0) | ||
job = beanstalk.reserve | ||
data = Yajl::Parser.parse(job.body) | ||
puts data.inspect | ||
job.delete | ||
end | ||
rescue Interrupt | ||
shut_down | ||
rescue Exception => e | ||
puts "\nError: #{e.message}" | ||
puts "\t#{e.backtrace.join("\n\t")}" | ||
|
||
if retries >= 10 && Time.now - last_retry < 10 | ||
shut_down | ||
else | ||
retries += 1 | ||
last_retry = Time.now | ||
end | ||
retry | ||
end | ||
end | ||
|
||
def shut_down | ||
puts "\nShutting down Stasis daemon on port #{@port}..." | ||
puts "\nShutting down Stasis daemon..." | ||
exit | ||
end | ||
|
||
module Tcp | ||
def self.parser | ||
@parser ||= Yajl::Parser.new(:symbolize_keys => true) | ||
end | ||
|
||
def parser | ||
self.class.parser | ||
end | ||
|
||
def receive_data(data) | ||
puts parser.parse(data).inspect | ||
send_data "OK\n" | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters