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.



137
138
139
140
# File 'lib/solid_queue/processes/runnable.rb', line 137

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

Instance Method Details

#closeObject



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

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

#completeObject

Runs in the forked process when it has finished booting



143
144
145
146
147
148
149
150
# File 'lib/solid_queue/processes/runnable.rb', line 143

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)


159
160
161
162
163
164
165
# File 'lib/solid_queue/processes/runnable.rb', line 159

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



153
154
155
# File 'lib/solid_queue/processes/runnable.rb', line 153

def start
  writer.close
end

#timed_out?Boolean

Returns:

  • (Boolean)


167
168
169
# File 'lib/solid_queue/processes/runnable.rb', line 167

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