From 0fe6a3fa4c1228b900f6a516b4893d833dc23fe9 Mon Sep 17 00:00:00 2001 From: Josh Hagins Date: Tue, 7 Apr 2015 15:19:05 -0400 Subject: [PATCH 1/3] Add method Subprocess#spawn_in_background Use ChildProcess to run a process in the background. --- lib/overcommit/subprocess.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lib/overcommit/subprocess.rb b/lib/overcommit/subprocess.rb index 9f556659..51c1cc18 100644 --- a/lib/overcommit/subprocess.rb +++ b/lib/overcommit/subprocess.rb @@ -29,6 +29,18 @@ def spawn(args) Result.new(process.exit_code, out.read, err.read) end + # Spawns a new process in the background using the given array of + # arguments (the first element is the command). + # + # @return [ChildProcess] + def spawn_in_background(args) + process = ChildProcess.build(*args) + + assign_output_streams(process) + + process.start + end + private # @param process [ChildProcess] From 9648d3a930e5a700697df38ee471db35fecb35cf Mon Sep 17 00:00:00 2001 From: Josh Hagins Date: Tue, 7 Apr 2015 15:46:36 -0400 Subject: [PATCH 2/3] Defer to Subprocess for process spawning logic --- lib/overcommit/utils.rb | 5 ++--- spec/overcommit/utils_spec.rb | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/overcommit/utils.rb b/lib/overcommit/utils.rb index 540937f3..6d58be04 100644 --- a/lib/overcommit/utils.rb +++ b/lib/overcommit/utils.rb @@ -124,15 +124,14 @@ def execute(args) # which we do not need to know the result. # # @param args [Array] - # @return [Thread] thread watching the resulting child process + # @return [ChildProcess] thread watching the resulting child process def execute_in_background(args) if args.include?('|') raise Overcommit::Exceptions::InvalidCommandArgs, 'Cannot pipe commands with the `execute_in_background` helper' end - # Dissociate process from parent's input/output streams - Process.detach(Process.spawn({}, *args, [:in, :out, :err] => '/dev/null')) + Subprocess.spawn_in_background(args) end # Calls a block of code with a modified set of environment variables, diff --git a/spec/overcommit/utils_spec.rb b/spec/overcommit/utils_spec.rb index 91bb32ef..258c5c09 100644 --- a/spec/overcommit/utils_spec.rb +++ b/spec/overcommit/utils_spec.rb @@ -153,7 +153,7 @@ end it 'executes the command' do - wait_until { subject.stop? } # Make sure process terminated before checking + wait_until { subject.exited? } # Make sure process terminated before checking File.exist?('some-file').should == true end end From 6ef71ed819b89de27374cfe77af9457d5f63fa76 Mon Sep 17 00:00:00 2001 From: Josh Hagins Date: Tue, 7 Apr 2015 22:00:56 -0400 Subject: [PATCH 3/3] Use Process.detach to run process in the background --- lib/overcommit/subprocess.rb | 11 +++-------- lib/overcommit/utils.rb | 4 ++-- spec/overcommit/utils_spec.rb | 2 +- 3 files changed, 6 insertions(+), 11 deletions(-) diff --git a/lib/overcommit/subprocess.rb b/lib/overcommit/subprocess.rb index 51c1cc18..74edd172 100644 --- a/lib/overcommit/subprocess.rb +++ b/lib/overcommit/subprocess.rb @@ -31,14 +31,9 @@ def spawn(args) # Spawns a new process in the background using the given array of # arguments (the first element is the command). - # - # @return [ChildProcess] - def spawn_in_background(args) - process = ChildProcess.build(*args) - - assign_output_streams(process) - - process.start + def spawn_detached(args) + # Dissociate process from parent's input/output streams + Process.detach(Process.spawn({}, *args, [:in, :out, :err] => '/dev/null')) end private diff --git a/lib/overcommit/utils.rb b/lib/overcommit/utils.rb index 6d58be04..e51ce93a 100644 --- a/lib/overcommit/utils.rb +++ b/lib/overcommit/utils.rb @@ -124,14 +124,14 @@ def execute(args) # which we do not need to know the result. # # @param args [Array] - # @return [ChildProcess] thread watching the resulting child process + # @return [Thread] thread watching the resulting child process def execute_in_background(args) if args.include?('|') raise Overcommit::Exceptions::InvalidCommandArgs, 'Cannot pipe commands with the `execute_in_background` helper' end - Subprocess.spawn_in_background(args) + Subprocess.spawn_detached(args) end # Calls a block of code with a modified set of environment variables, diff --git a/spec/overcommit/utils_spec.rb b/spec/overcommit/utils_spec.rb index 258c5c09..91bb32ef 100644 --- a/spec/overcommit/utils_spec.rb +++ b/spec/overcommit/utils_spec.rb @@ -153,7 +153,7 @@ end it 'executes the command' do - wait_until { subject.exited? } # Make sure process terminated before checking + wait_until { subject.stop? } # Make sure process terminated before checking File.exist?('some-file').should == true end end