class Jackal::Utils::Process

Constants

BLACKLISTED_ENV

Environment variables that should be removed from process environment

DEFAULT_STORAGE_DIRECTORY

Default path for IO tmp files

Attributes

configuration[R]

@return [Smash] manager configuration

storage_directory[R]

@return [String] storage directory path

Public Class Methods

new(config={}) click to toggle source

Create new instance

@param config [Smash] process manager configuration @return [self]

# File lib/jackal/utils/process.rb, line 26
def initialize(config={})
  @base_env = ENV.to_hash
  @configuration = config.to_smash
  @storage_directory = configuration.fetch(
    :storage_directory, DEFAULT_STORAGE_DIRECTORY
  )
  FileUtils.mkdir_p(storage_directory)
end

Public Instance Methods

create_io_tmp(*args) click to toggle source

Temporary IO for logging

@param args [String] argument list joined for filename @return [IO]

# File lib/jackal/utils/process.rb, line 66
def create_io_tmp(*args)
  path = File.join(storage_directory, args.join('-'))
  FileUtils.mkdir_p(File.dirname(path))
  t_file = File.open(path, 'w+')
  t_file.sync
  t_file
end
process(identifier, *command) { |_proc| ... } click to toggle source

Create new process

@param identifier [String] command identifier (compat argument) @param command [String] command in single string or splatted array @yieldparam [ChildProcess] @return [ChildProcess] allows for result inspection if desired

# File lib/jackal/utils/process.rb, line 41
def process(identifier, *command)
  _proc = nil
  if(command.size == 1)
    command = Shellwords.shellsplit(command.first)
  end
  if(block_given?)
    if(configuration[:spawn])
      _proc = clean_env!{ ChildProcess::Unix::PosixSpawnProcess.new(command) }
      scrub_env(_proc.environment)
      clean_env!{ yield _proc }
    else
      _proc = clean_env!{ ChildProcess.build(*command) }
      scrub_env(_proc.environment)
      clean_env!{ yield _proc }
    end
  else
    raise ArgumentError.new('Expecting block but no block provided!')
  end
  _proc
end

Private Instance Methods

clean_env!() { || ... } click to toggle source

Remove environment variables that are known should NOT be set

@yield execute block within scrubbed environment

# File lib/jackal/utils/process.rb, line 79
def clean_env!
  ENV.replace(@base_env.dup)
  scrub_env(ENV)
  if(defined?(Bundler))
    Bundler.with_clean_env{ yield }
  else
    yield
  end
end
scrub_env(env) click to toggle source

Scrubs configured keys from hash

@param env [Hash] hash to scrub @return [TrueClass]

# File lib/jackal/utils/process.rb, line 93
def scrub_env(env)
  [
    BLACKLISTED_ENV,
    Carnivore::Config.get(
      :jackal, :utils, :process_manager, :blacklisted_env
    )
  ].flatten.compact.each do |key|
    env.delete(key)
  end
  true
end