Class: ReactEmailRails::RenderModes::Persistent::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/react_email_rails/render_modes/persistent/server.rb

Defined Under Namespace

Classes: Status

Constant Summary collapse

STDERR_LIMIT =
8 * 1024

Instance Method Summary collapse

Constructor Details

#initialize(command) ⇒ Server

Returns a new instance of Server.



8
9
10
11
12
13
14
15
# File 'lib/react_email_rails/render_modes/persistent/server.rb', line 8

def initialize(command)
  @command = command
  @mutex = Mutex.new
  @stderr_buffer = +""
  @stderr_mutex = Mutex.new
  @stdout_buffer = +""
  @requests = 0
end

Instance Method Details

#abandonObject

Release this process’s copy of an inherited child’s pipes without signalling the process itself, which is still owned by the parent that started it.



60
61
62
63
64
65
66
# File 'lib/react_email_rails/render_modes/persistent/server.rb', line 60

def abandon
  [@stdin, @stdout, @stderr].compact.each { |io| io.close unless io.closed? }
  @stdin = @stdout = @stderr = @wait_thread = @stderr_reader = nil
  @stdout_buffer.clear
rescue IOError
  nil
end

#capture(input:, timeout:, max_requests:) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/react_email_rails/render_modes/persistent/server.rb', line 17

def capture(input:, timeout:, max_requests:)
  @mutex.synchronize do
    capture_once(input:, timeout:).tap { recycle_if_needed(max_requests) }
  end
rescue Errno::EPIPE, IOError
  stop
  begin
    @mutex.synchronize do
      capture_once(input:, timeout:).tap { recycle_if_needed(max_requests) }
    end
  rescue Errno::EPIPE, IOError
    failure("render process exited before responding")
  end
end

#health_check(timeout:) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/react_email_rails/render_modes/persistent/server.rb', line 32

def health_check(timeout:)
  @mutex.synchronize { health_check_once(timeout:) }
rescue Errno::EPIPE, IOError
  stop
  begin
    @mutex.synchronize { health_check_once(timeout:) }
  rescue Errno::EPIPE, IOError
    failure("render process exited before responding")
  end
end

#stopObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/react_email_rails/render_modes/persistent/server.rb', line 43

def stop
  if @wait_thread&.alive?
    terminate_process("TERM", @wait_thread.pid)
    @wait_thread.join(1)
    terminate_process("KILL", @wait_thread.pid) if @wait_thread.alive?
  end
rescue Errno::ESRCH, Errno::EPERM
  nil
ensure
  [@stdin, @stdout, @stderr].compact.each { |io| io.close unless io.closed? }
  @stderr_reader&.kill
  @stdin = @stdout = @stderr = @wait_thread = @stderr_reader = nil
  @stdout_buffer.clear
end