Skip to content
This repository has been archived by the owner on Feb 7, 2018. It is now read-only.

Commit

Permalink
Merge branch 'master' of https://github.com/bdurand/cocaine
Browse files Browse the repository at this point in the history
  • Loading branch information
mike-burns committed Mar 16, 2012
2 parents f77c1b5 + 98dff3d commit cccc67a
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 4 deletions.
3 changes: 3 additions & 0 deletions README.md
Expand Up @@ -119,6 +119,9 @@ But you don't have to, as you saw above where it doesn't use this. But you CAN l
Cocaine::CommandLine.logger = Logger.new(STDOUT) Cocaine::CommandLine.logger = Logger.new(STDOUT)
Cocaine::CommandLine.new("date").run # => Logs this -> Command :: date Cocaine::CommandLine.new("date").run # => Logs this -> Command :: date
``` ```
## POSIX Spawn

You can potentially increase performance by installing the posix-spawn gem (https://rubygems.org/gems/posix-spawn). This gem can keep your application's heap from being copied when forking command line processesG. For applications with large heaps the gain can be significant. To include posix-spawn, simply add it to your Gemfile or, if you don't use bundler, install the gem.


## License ## License


Expand Down
1 change: 1 addition & 0 deletions cocaine.gemspec
Expand Up @@ -20,5 +20,6 @@ Gem::Specification.new do |s|
s.add_development_dependency('bourne') s.add_development_dependency('bourne')
s.add_development_dependency('mocha') s.add_development_dependency('mocha')
s.add_development_dependency('rake') s.add_development_dependency('rake')
s.add_development_dependency('posix-spawn')
end end


11 changes: 10 additions & 1 deletion lib/cocaine/command_line.rb
@@ -1,5 +1,13 @@
module Cocaine module Cocaine
class CommandLine class CommandLine
# Check for posix-spawn gem. If it is available it will prevent the invoked processes
# from getting a copy of the ruby heap which can lead to significant performance gains.
begin
require 'posix/spawn'
rescue LoadError => e
# posix-spawn gem not available
end

class << self class << self
attr_accessor :path, :logger attr_accessor :path, :logger
end end
Expand All @@ -14,6 +22,7 @@ def initialize(binary, params = "", options = {})
@swallow_stderr = @options.delete(:swallow_stderr) @swallow_stderr = @options.delete(:swallow_stderr)
@expected_outcodes = @options.delete(:expected_outcodes) @expected_outcodes = @options.delete(:expected_outcodes)
@expected_outcodes ||= [0] @expected_outcodes ||= [0]
extend(POSIX::Spawn) if defined?(POSIX::Spawn)
end end


def command def command
Expand All @@ -29,7 +38,7 @@ def run
begin begin
with_modified_path do with_modified_path do
@logger.info("\e[32mCommand\e[0m :: #{command}") if @logger @logger.info("\e[32mCommand\e[0m :: #{command}") if @logger
output = self.class.send(:'`', command) output = send(:'`', command)
end end
rescue Errno::ENOENT rescue Errno::ENOENT
raise Cocaine::CommandNotFoundError raise Cocaine::CommandNotFoundError
Expand Down
33 changes: 30 additions & 3 deletions spec/cocaine/command_line_spec.rb
Expand Up @@ -113,15 +113,15 @@


it "runs the command it's given and return the output" do it "runs the command it's given and return the output" do
cmd = Cocaine::CommandLine.new("convert", "a.jpg b.png", :swallow_stderr => false) cmd = Cocaine::CommandLine.new("convert", "a.jpg b.png", :swallow_stderr => false)
cmd.class.stubs(:"`").with("convert a.jpg b.png").returns(:correct_value) cmd.stubs(:"`").with("convert a.jpg b.png").returns(:correct_value)
with_exitstatus_returning(0) do with_exitstatus_returning(0) do
cmd.run.should == :correct_value cmd.run.should == :correct_value
end end
end end


it "raises a CommandLineError if the result code from the command isn't expected" do it "raises a CommandLineError if the result code from the command isn't expected" do
cmd = Cocaine::CommandLine.new("convert", "a.jpg b.png", :swallow_stderr => false) cmd = Cocaine::CommandLine.new("convert", "a.jpg b.png", :swallow_stderr => false)
cmd.class.stubs(:"`").with("convert a.jpg b.png").returns(:correct_value) cmd.stubs(:"`").with("convert a.jpg b.png").returns(:correct_value)
with_exitstatus_returning(1) do with_exitstatus_returning(1) do
lambda do lambda do
cmd.run cmd.run
Expand All @@ -134,7 +134,7 @@
"a.jpg b.png", "a.jpg b.png",
:expected_outcodes => [0, 1], :expected_outcodes => [0, 1],
:swallow_stderr => false) :swallow_stderr => false)
cmd.class.stubs(:"`").with("convert a.jpg b.png").returns(:correct_value) cmd.stubs(:"`").with("convert a.jpg b.png").returns(:correct_value)
with_exitstatus_returning(1) do with_exitstatus_returning(1) do
lambda do lambda do
cmd.run cmd.run
Expand Down Expand Up @@ -184,4 +184,31 @@
cmd = Cocaine::CommandLine.new("echo", "'Logging!'", :logger => nil) cmd = Cocaine::CommandLine.new("echo", "'Logging!'", :logger => nil)
lambda { cmd.run }.should_not raise_error lambda { cmd.run }.should_not raise_error
end end

describe "command execution" do
it "should use the ` method to invoke the command line" do
cmd = Cocaine::CommandLine.new("echo", "hello")
cmd.stubs(:`).with(anything).returns(nil)
cmd.run
cmd.should have_received(:`).with("echo hello")
end

it "should use POSIX::Spawn to create processes if it is available" do
cmd = Cocaine::CommandLine.new("echo", "hello")
cmd.method(:`).owner.should == POSIX::Spawn
cmd.run.chomp.should == "hello"
end

it "should use the default Kernel spawning to create processes if POSIX::Spawn is not available" do
spawn = POSIX::Spawn
POSIX.send(:remove_const, :Spawn)
begin
cmd = Cocaine::CommandLine.new("echo", "hello")
cmd.method(:`).owner.should == Kernel
cmd.run.chomp.should == "hello"
ensure
POSIX.const_set(:Spawn, spawn)
end
end
end
end end

0 comments on commit cccc67a

Please sign in to comment.