Class: Jackal::Utils::Process

Inherits:
Object
  • Object
show all
Defined in:
lib/jackal/utils/process.rb

Constant Summary collapse

DEFAULT_STORAGE_DIRECTORY =

Default path for IO tmp files

'/tmp/jackal-process-manager'
BLACKLISTED_ENV =

Environment variables that should be removed from process environment

['GIT_DIR']

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ self

Create new instance

Parameters:

  • config (Smash) (defaults to: {})

    process manager configuration



26
27
28
29
30
31
32
33
# 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

Instance Attribute Details

#configurationSmash (readonly)

Returns manager configuration.

Returns:

  • (Smash)

    manager configuration



18
19
20
# File 'lib/jackal/utils/process.rb', line 18

def configuration
  @configuration
end

#storage_directoryString (readonly)

Returns storage directory path.

Returns:

  • (String)

    storage directory path



20
21
22
# File 'lib/jackal/utils/process.rb', line 20

def storage_directory
  @storage_directory
end

Instance Method Details

#create_io_tmp(*args) ⇒ IO

Temporary IO for logging

Parameters:

  • args (String)

    argument list joined for filename

Returns:

  • (IO)


66
67
68
69
70
71
72
# 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) {|| ... } ⇒ ChildProcess

Create new process

Parameters:

  • identifier (String)

    command identifier (compat argument)

  • command (String)

    command in single string or splatted array

Yield Parameters:

  • (ChildProcess)

Returns:

  • (ChildProcess)

    allows for result inspection if desired



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# 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