Browse files

worker.rb: Add interface for accessing sockets

  • Loading branch information...
1 parent ef4bcd5 commit c2edca3340c949da8a60b7de7abe224d6a26320a @gdb gdb committed Sep 26, 2012
Showing with 25 additions and 0 deletions.
  1. +25 −0 lib/einhorn/worker.rb
View
25 lib/einhorn/worker.rb
@@ -78,6 +78,31 @@ def self.ack!(discovery=:env, arg=nil)
true
end
+ def self.socket(number=0)
+ fds = einhorn_fds
+ fds ? fds[number] : nil
+ end
+
+ def self.socket!(number=0)
+ unless fds = einhorn_fds
+ raise "No EINHORN_FDS provided in environment. Are you running under Einhorn?"
+ end
+
+ unless number < fds.length
+ raise "Only #{fds.length} FDs available, but FD #{number} was requested"
+ end
+
+ fds[number]
+ end
+
+ def self.einhorn_fds
+ unless raw_fds = ENV['EINHORN_FDS']
+ return nil
+ end
+
+ raw_fds.split(' ').map {|fd| Integer(fd)}
+ end
+
# Call this to handle graceful shutdown requests to your app.
def self.graceful_shutdown(&blk)
Signal.trap('USR2', &blk)

0 comments on commit c2edca3

Please sign in to comment.