Class: Mxup::GracefulStop

Inherits:
Object
  • Object
show all
Defined in:
lib/mxup/graceful_stop.rb

Overview

Sends SIGINT to every non-shell pane in a session, then waits for them to exit. Used by ‘mxup down` before the final kill-session.

Constant Summary collapse

DEFAULT_ROUND_INTERVAL =

Interval (seconds) between SIGINT rounds. Overridable for tests that don’t want to pay the 1s settle between retries.

1.0

Class Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(session, out: nil, err: nil) ⇒ GracefulStop

Returns a new instance of GracefulStop.



19
20
21
22
23
# File 'lib/mxup/graceful_stop.rb', line 19

def initialize(session, out: nil, err: nil)
  @session      = session
  @out_override = out
  @err_override = err
end

Class Attribute Details

.round_intervalObject



14
15
16
# File 'lib/mxup/graceful_stop.rb', line 14

def round_interval
  @round_interval || DEFAULT_ROUND_INTERVAL
end

Instance Method Details

#call(timeout: 30) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/mxup/graceful_stop.rb', line 33

def call(timeout: 30)
  out.puts "Stopping session #{@session}..."
  deadline = Time.now + timeout
  round    = 0

  loop do
    break unless Tmux.has_session?(@session)

    busy = busy_panes
    break if busy.empty?

    if round.positive?
      remaining = (deadline - Time.now).ceil
      plural    = busy.size == 1 ? 'process' : 'processes'
      out.puts "  waiting... #{busy.size} #{plural} still running (#{remaining}s left)"
    end

    busy.each do |pane|
      target = Tmux.pane_target(pane[:name], pane[:pane_index])
      Tmux.send_interrupt(@session, target)
    end

    if Time.now >= deadline
      names = busy.map { |p| p[:title].to_s.empty? ? p[:name] : p[:title] }.uniq
      err.puts "  timeout: #{names.join(', ')} did not exit in #{timeout}s — killing anyway"
      break
    end

    round += 1
    sleep GracefulStop.round_interval
  end
end

#errObject



29
30
31
# File 'lib/mxup/graceful_stop.rb', line 29

def err
  @err_override || $stderr
end

#outObject



25
26
27
# File 'lib/mxup/graceful_stop.rb', line 25

def out
  @out_override || $stdout
end