Class: Expedite::Client::Exec

Inherits:
Base
  • Object
show all
Defined in:
lib/expedite/client/exec.rb

Constant Summary collapse

FORWARDED_SIGNALS =
%w(INT QUIT USR1 USR2 INFO WINCH) & Signal.list.keys

Constants inherited from Base

Base::BOOT_TIMEOUT, Base::CONNECT_TIMEOUT

Instance Attribute Summary

Attributes inherited from Base

#agent, #args, #env, #server

Instance Method Summary collapse

Methods inherited from Base

#boot_server, #connect, #connect_to_agent, #gem_env, #log, #perform, #run_command, #server_booted?, #stop_server

Constructor Details

#initialize(env: nil, agent: nil) ⇒ Exec

Returns a new instance of Exec.



10
11
12
13
14
# File 'lib/expedite/client/exec.rb', line 10

def initialize(env: nil, agent: nil)
  super

  @signal_queue  = []
end

Instance Method Details

#call(*args) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/expedite/client/exec.rb', line 16

def call(*args)
  @args = args
  begin
    connect
  rescue Errno::ENOENT, Errno::ECONNRESET, Errno::ECONNREFUSED
    cold_run
  else
    warm_run
  end
ensure
  server.close if server
end

#cold_runObject



44
45
46
47
48
# File 'lib/expedite/client/exec.rb', line 44

def cold_run
  boot_server
  connect
  run
end

#forward_signal(sig, application) ⇒ Object



110
111
112
113
114
115
116
117
# File 'lib/expedite/client/exec.rb', line 110

def forward_signal(sig, application)
  if kill(sig, application) != 0
    # If the application process is gone, then don't block the
    # signal on this process.
    trap(sig, 'DEFAULT')
    Process.kill(sig, Process.pid)
  end
end

#forward_signals(application) ⇒ Object



102
103
104
105
106
107
108
# File 'lib/expedite/client/exec.rb', line 102

def forward_signals(application)
  @signal_queue.each { |sig| kill sig, application }

  FORWARDED_SIGNALS.each do |sig|
    trap(sig) { forward_signal sig, application }
  end
end

#kill(sig, application) ⇒ Object



119
120
121
122
# File 'lib/expedite/client/exec.rb', line 119

def kill(sig, application)
  application.puts(sig)
  application.gets.to_i
end

#queue_signalsObject



84
85
86
87
88
# File 'lib/expedite/client/exec.rb', line 84

def queue_signals
  FORWARDED_SIGNALS.each do |sig|
    trap(sig) { @signal_queue << sig }
  end
end

#runObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/expedite/client/exec.rb', line 50

def run
  status = perform(*@args)

  code = if status.respond_to?(:to_i)
    status.to_i
  elsif status == true
    0
  elsif status == false
    1
  else
    126
  end
  exit code
rescue Errno::ECONNRESET
  exit 1
end

#suspend_resume_on_tstp_cont(pid) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/expedite/client/exec.rb', line 90

def suspend_resume_on_tstp_cont(pid)
  trap("TSTP") {
    log "suspended"
    Process.kill("STOP", pid.to_i)
    Process.kill("STOP", Process.pid)
  }
  trap("CONT") {
    log "resumed"
    Process.kill("CONT", pid.to_i)
  }
end

#verify_server_versionObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/expedite/client/exec.rb', line 67

def verify_server_version
  server_version = server.gets.chomp
  if server_version != env.version
    $stderr.puts "There is a version mismatch between the Expedite client " \
                   "(#{env.version}) and the server (#{server_version})."

    if server_booted?
      $stderr.puts "We already tried to reboot the server, but the mismatch is still present."
      exit 1
    else
      $stderr.puts "Restarting to resolve."
      stop_server
      cold_run
    end
  end
end

#warm_runObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/expedite/client/exec.rb', line 29

def warm_run
  run
rescue CommandNotFound
  raise
  require "expedite/command"

  if Expedite.command(args.first)
    # Command installed since Expedite started
    stop_server
    cold_run
  else
    raise
  end
end