Class: Explorer::Process

Inherits:
Object
  • Object
show all
Includes:
Celluloid::IO
Defined in:
lib/explorer/process.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(label, command, working_dir: ENV['PWD'], log_watcher: nil, env: {}) ⇒ Process

Log watcher should implement a ‘log’ method which accepts a label and a line of text Log watcher should also be thread-safe or an actor



12
13
14
15
16
17
18
19
20
# File 'lib/explorer/process.rb', line 12

def initialize(label, command, working_dir: ENV['PWD'], log_watcher: nil, env: {})
  @label = label
  @command = command
  @working_dir = File.expand_path(working_dir)
  @log_watcher = log_watcher
  @env = env
  @state = :stopped
  @status = nil
end

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



7
8
9
# File 'lib/explorer/process.rb', line 7

def command
  @command
end

#envObject (readonly)

Returns the value of attribute env.



8
9
10
# File 'lib/explorer/process.rb', line 8

def env
  @env
end

#labelObject (readonly)

Returns the value of attribute label.



7
8
9
# File 'lib/explorer/process.rb', line 7

def label
  @label
end

#log_watcherObject (readonly)

Returns the value of attribute log_watcher.



8
9
10
# File 'lib/explorer/process.rb', line 8

def log_watcher
  @log_watcher
end

#pgidObject (readonly)

Returns the value of attribute pgid.



8
9
10
# File 'lib/explorer/process.rb', line 8

def pgid
  @pgid
end

#pidObject (readonly)

Returns the value of attribute pid.



8
9
10
# File 'lib/explorer/process.rb', line 8

def pid
  @pid
end

#pipeObject (readonly)

Returns the value of attribute pipe.



8
9
10
# File 'lib/explorer/process.rb', line 8

def pipe
  @pipe
end

#stateObject (readonly)

Returns the value of attribute state.



7
8
9
# File 'lib/explorer/process.rb', line 7

def state
  @state
end

#statusObject (readonly)

Returns the value of attribute status.



7
8
9
# File 'lib/explorer/process.rb', line 7

def status
  @status
end

#working_dirObject (readonly)

Returns the value of attribute working_dir.



7
8
9
# File 'lib/explorer/process.rb', line 7

def working_dir
  @working_dir
end

Instance Method Details

#startObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/explorer/process.rb', line 30

def start
  return Actor.current if @state == :started

  @status = nil
  @pipe, slave = PTY.open
  slave.raw!
  @pid = spawn_process(label, command, working_dir: working_dir, pipe: slave)
  @pgid = ::Process.getpgid(@pid)
  slave.close

  @state = :started
  signal :state_changed

  wait_pid
  async.read_pipe

  return Actor.current
end

#started?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/explorer/process.rb', line 22

def started?
  @state == :started
end

#stop(sig = 'INT') ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'lib/explorer/process.rb', line 49

def stop(sig='INT')
  return Actor.current if @state == :stopped
  begin
    ::Process.kill(sig, -pgid)
    wait_on_stop
  rescue
  end
  return Actor.current
end

#stopped?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/explorer/process.rb', line 26

def stopped?
  @state == :stopped
end

#wait_on_stopObject



59
60
61
62
63
64
# File 'lib/explorer/process.rb', line 59

def wait_on_stop
  while @state != :stopped
    wait :state_changed
  end
  return Actor.current
end