Class: Delayed::WorkQueue::ParentProcess::Client

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/delayed/work_queue/parent_process/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

logger, #logger, #say

Constructor Details

#initialize(addrinfo, config: Settings.parent_process) ⇒ Client

Returns a new instance of Client.



11
12
13
14
# File 'lib/delayed/work_queue/parent_process/client.rb', line 11

def initialize(addrinfo, config: Settings.parent_process)
  @addrinfo = addrinfo
  @connect_timeout = config["client_connect_timeout"] || 2
end

Instance Attribute Details

#addrinfoObject (readonly)

Returns the value of attribute addrinfo.



7
8
9
# File 'lib/delayed/work_queue/parent_process/client.rb', line 7

def addrinfo
  @addrinfo
end

Instance Method Details

#closeObject



20
21
22
# File 'lib/delayed/work_queue/parent_process/client.rb', line 20

def close
  reset_connection
end

#get_and_lock_next_available(worker_name, worker_config) ⇒ Object



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
# File 'lib/delayed/work_queue/parent_process/client.rb', line 24

def get_and_lock_next_available(worker_name, worker_config)
  Marshal.dump([worker_name, worker_config], socket)

  # We're assuming there won't ever be a partial write here so we only need
  # to wait for anything to be available on the 'wire', this is a valid
  # assumption because we control the server and it's a Unix domain socket,
  # not TCP.
  if socket.eof?
    # Other end closed gracefully, so should we
    logger.debug("server closed connection")
    return reset_connection
  end

  readers, = IO.select([socket, @self_pipe[0]])

  if readers.include?(@self_pipe[0])
    # we're probably exiting so we just want to break out of the blocking read
    logger.debug("Broke out of select due to being awakened, exiting")
  else
    Marshal.load(socket).tap do |response|
      unless response.nil? || (response.is_a?(Delayed::Job) && response.locked_by == worker_name)
        raise(ProtocolError, "response is not a locked job: #{response.inspect}")
      end

      logger.debug("Received job #{response.id}")
    end
  end
rescue SystemCallError, IOError => e
  logger.error("Work queue connection lost, reestablishing on next poll. (#{e})")
  # The work queue process died. Return nil to signal the worker
  # process should sleep as if no job was found, and then retry.
  reset_connection
end

#initObject



16
17
18
# File 'lib/delayed/work_queue/parent_process/client.rb', line 16

def init
  @self_pipe ||= IO.pipe # rubocop:disable  Naming/MemoizedInstanceVariableName
end

#wake_upObject



58
59
60
# File 'lib/delayed/work_queue/parent_process/client.rb', line 58

def wake_up
  @self_pipe[1].write_nonblock(".", exception: false)
end