Skip to content

Commit

Permalink
Add process exist check.
Browse files Browse the repository at this point in the history
Fixed OSX "ps" command.
  • Loading branch information
Marcel Schlegel committed Jun 12, 2015
1 parent 874e919 commit 06796fd
Showing 1 changed file with 11 additions and 18 deletions.
29 changes: 11 additions & 18 deletions lib/sassconf/sass_executor.rb
Expand Up @@ -29,48 +29,41 @@ def create_all_argument_strings(argument_value_hash, argument_hash)
end

def execute(argument_string)
Util.pre_check((argument_string.is_string? and argument_string.is_not_nil_or_empty?), 'Argument string is no string, nil or empty.')
Util.pre_check((argument_string.string? and argument_string.not_nil_or_empty?), 'Argument string is no string, nil or empty.')

@pid = spawn(SASS_PROCESS % [argument_string, @sass_input, @sass_output])
logger.info("Spawn Sass process: #{@pid}")
end

def detach_and_kill
unless @pid.nil?
if @pid.integer? && Util.process_exists?(@pid)
logger.info("Detach Sass process: #{@pid}")
Process.detach(@pid)
out, status = if Util.windows? then
logger.info("Find child processes on MS-DOS")
Open3.capture2("wmic process where (ParentProcessId=#{@pid.to_s}) get ProcessId")
else
logger.info("Find child processes on UNIX")
Open3.capture2('ps', 'h', '--ppid', @pid.to_s, '-o', 'pid')
end

logger.info("Kill process: #{@pid}")
Process.kill('KILL', @pid)
out.each_line do |elem|
pid = elem.to_i;
unless pid == 0
Process.kill('KILL', pid)
logger.info("Killed child process: #{pid}")
end

Util.process_childs(@pid) do |pid|
Process.kill('KILL', pid)
logger.info("Killed child process: #{pid}")
end
end
end

def wait
logger.info("Wait for Sass process: #{@pid}")
Process.wait(@pid) unless @pid.nil?
Process.wait(@pid) if @pid.integer? && Util.process_exists?(@pid)
end

private
def create_string(argument_type, argument_hash)
Util.pre_check(argument_type.is_string?, 'Argument type is no string.')
Util.pre_check((argument_hash.is_hash? and !argument_hash.nil?), 'Argument hash is no hash or nil.')
Util.pre_check(argument_type.string?, 'Argument type is no string.')
Util.pre_check(argument_hash.hash?, 'Argument hash is no hash.')

logger.info("Create argument string from hash: #{argument_hash}")
argument_hash.each { |key, value| argument_hash[key] = String.empty if value == :no_value }
argument_hash.reduce('') { |arg_string, (key, value)| arg_string.concat((argument_type % [key, value])) }.strip
end
end
end

0 comments on commit 06796fd

Please sign in to comment.