class Breakfast::BrunchWatcher

Constants

BRUNCH_COMMAND

Attributes

log[R]
pid[RW]

Public Class Methods

new(log:) click to toggle source
# File lib/breakfast/brunch_watcher.rb, line 9
def initialize(log:)
  @log = log
end

Public Instance Methods

run() click to toggle source
# File lib/breakfast/brunch_watcher.rb, line 13
def run
  out, writer, self.pid = PTY.spawn(BRUNCH_COMMAND)
  writer.close

  Process.detach(pid)

  begin
    loop do
      output = out.readpartial(64.kilobytes).strip
      log.debug output

      output = output.gsub(/\e\[([;\d]+)?m/, "")
      case output
      when /compiled/
        broadcast(status: "success", message: output.split("info: ").last)
      when /error/
        broadcast(status: "error", message: output.split("error: ").last)
      end
    end
  rescue EOFError, Errno::EIO
    log.fatal "[BREAKFAST] Watcher died unexpectedly. Restart Rails Server"
    broadcast(
      status: "error",
      message: "Watcher died unexpectedly. Restart Rails server"
    )
  end
end
terminate() click to toggle source
# File lib/breakfast/brunch_watcher.rb, line 41
def terminate
  Process.kill("TERM", @pid)
rescue Errno::ESRCH
  # NOOP. Process exited cleanly or already terminated. Don't print
  # exception to STDOUT
end

Private Instance Methods

broadcast(status:, message:) click to toggle source
# File lib/breakfast/brunch_watcher.rb, line 50
def broadcast(status:, message:)
  ActionCable.server.broadcast(STATUS_CHANNEL, {
    status: status,
    message: message
  })
end