Class: SolidQueue::Processes::BootGuards::ForkGuard

Inherits:
Object
  • Object
show all
Defined in:
lib/solid_queue/processes/runnable.rb

Overview

Tracks a forked process from the moment it's started until its boot callbacks finish, over a pipe that survives forking: the forked process writes to it when it's done booting, and its supervisor reads from it to decide whether the process is taking too long to boot and needs replacing.

Instance Method Summary collapse

Constructor Details

#initializeForkGuard

Returns a new instance of ForkGuard.



140
141
142
143
# File 'lib/solid_queue/processes/runnable.rb', line 140

def initialize
  @reader, @writer = IO.pipe
  @created_at = SolidQueue::Timer.monotonic_time_now
end

Instance Method Details

#closeObject



174
175
176
177
# File 'lib/solid_queue/processes/runnable.rb', line 174

def close
  reader.close unless reader.closed?
  writer.close unless writer.closed?
end

#completeObject

Runs in the forked process when it has finished booting



146
147
148
149
150
151
152
153
# File 'lib/solid_queue/processes/runnable.rb', line 146

def complete
  reader.close
  writer.write(".")
rescue Errno::EPIPE
  # The supervisor stopped waiting while this process finished booting
ensure
  writer.close
end

#completed?Boolean

A byte means boot completed; EOF means the process exited before finishing its boot, and will be replaced when it's reaped

Returns:

  • (Boolean)


162
163
164
165
166
167
168
# File 'lib/solid_queue/processes/runnable.rb', line 162

def completed?
  @completed ||= begin
    completed = reader.read_nonblock(1, exception: false) != :wait_readable
    reader.close if completed
    completed
  end
end

#startObject

Runs in the parent process right after forking



156
157
158
# File 'lib/solid_queue/processes/runnable.rb', line 156

def start
  writer.close
end

#timed_out?Boolean

Returns:

  • (Boolean)


170
171
172
# File 'lib/solid_queue/processes/runnable.rb', line 170

def timed_out?
  !completed? && SolidQueue::Timer.monotonic_time_now - created_at >= SolidQueue.fork_boot_timeout
end