Class: Deployed::RunController

Inherits:
ApplicationController show all
Includes:
ActionController::Live
Defined in:
app/controllers/deployed/run_controller.rb

Overview

Provides a centralized way to run all ‘kamal [command]` executions and streams to the browser

Defined Under Namespace

Classes: ConcurrentProcessRunning

Instance Method Summary collapse

Instance Method Details

#cancelObject

Endpoint to cancel currently running process



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'app/controllers/deployed/run_controller.rb', line 87

def cancel
  if process_running?
    # If a process is running, get the PID and attempt to kill it
    begin
      Process.kill('TERM', stored_pid)
      sse.write(
        "<div class='text-yellow-400'>Cancelled the process with PID: #{stored_pid}</div>",
        event: 'message'
      )
      release_process
    rescue Errno::ESRCH
      sse.write(
        "<div class='text-red-500'>Process with PID #{stored_pid} is not running.</div>",
        event: 'message'
      )
    end
  else
    sse.write(
      "<div class='text-slate-400'>No process is currently running, nothing to cancel.</div>",
      event: 'message'
    )
  end
rescue ActionController::Live::ClientDisconnected
  logger.info 'Client Disconnected'
rescue IOError
  logger.info 'IOError'
ensure
  sse.write(
    '[Deployed Rails] End transmission',
    event: 'message'
  )
  sse.close
  response.stream.close
  release_process
end

#executeObject

Endpoint to execute the kamal command



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'app/controllers/deployed/run_controller.rb', line 13

def execute
  if process_running?
    raise(ConcurrentProcessRunning)
  elsif stored_pid
    release_process
  end

  sse.write(
    "<div class='text-slate-400'>> <span class='text-slate-300 font-semibold'>kamal #{command}</span></div>",
    event: 'message'
  )

  read_io, write_io = IO.pipe

  # Fork a child process
  Deployed::CurrentExecution.child_pid = fork do
    # Redirect the standard output to the write end of the pipe
    $stdout.reopen(write_io)

    # Execute the command
    exec("kamal #{command}; echo \"[Deployed Rails] End transmission\"")
  end

  lock_process

  sse.write(
    "<div class='text-slate-400' data-child-pid=\"#{Deployed::CurrentExecution.child_pid}\"></div>",
    event: 'message'
  )

  write_io.close

  # Use a separate thread to read and stream the output
  output_thread = Thread.new do
    read_io.each_line do |line|
      output = line.strip
      output = output.gsub('49.13.91.176', '[redacted]')
      text_color_class = 'text-green-400'

      # Hackish way of dealing with error messages at the moment
      if output.include?('[31m')
        text_color_class = 'text-red-500'
        output.gsub!('[31m', '')
        output.gsub!('[0m', '')
      end

      sse.write("<div class='#{text_color_class}'>#{output}</div>", event: 'message')
    end

    # Ensure the response stream and the thread are closed properly
    sse.close
    response.stream.close
  end

  # Ensure that the thread is joined when the execution is complete
  Process.wait
  output_thread.join
rescue ConcurrentProcessRunning
  sse.write(
    "<div class='text-red-500'>Existing process running with PID: #{stored_pid}</div>",
    event: 'message'
  )
  logger.info 'Existing process running'
rescue ActionController::Live::ClientDisconnected
  logger.info 'Client Disconnected'
rescue IOError
  logger.info 'IOError'
ensure
  sse.close
  response.stream.close
  release_process
end