Class: Themigrator::Script

Inherits:
Object
  • Object
show all
Defined in:
lib/themigrator/script.rb

Instance Method Summary collapse

Constructor Details

#initialize(script, log_file) ⇒ Script

Returns a new instance of Script.



5
6
7
8
9
10
11
# File 'lib/themigrator/script.rb', line 5

def initialize(script, log_file)
  @script = script
  @log_file = File.open(log_file, "w", 0600)
  @wd_dir = File.dirname(script)
  @pid = nil
  @thread = nil
end

Instance Method Details

#exitcodeObject



37
38
39
# File 'lib/themigrator/script.rb', line 37

def exitcode
  @thread.value
end

#runObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/themigrator/script.rb', line 13

def run
  @thread = Thread.new do
	@pid = Process.spawn(@script, 
    err: @log_file, 
    out: @log_file, 
    chdir: @wd_dir)
	pid, status = Process.wait2(@pid)
	if status.signaled?
	  signal = Signal.list.find{|k,v|
 v == status.termsig
	  }
	  signal.nil? ? :unknown_signal : signal[0].downcase.to_sym
	else
	  status.exitstatus
	end
  end
  sleep 0
end

#stopObject



41
42
43
44
45
46
47
48
49
# File 'lib/themigrator/script.rb', line 41

def stop
  if running?
	send_int
	if wait(5) == nil
	  send_kill
	end
	wait
  end
end

#success?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/themigrator/script.rb', line 51

def success?
  exitcode == 0
end

#wait(seconds = nil) ⇒ Object

Waits for the process to be over



33
34
35
# File 'lib/themigrator/script.rb', line 33

def wait(seconds = nil)
  @thread.join(seconds) 
end