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
15
# 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
  @self_pipe = IO.pipe
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


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

def close
  reset_connection
end

#get_and_lock_next_available(worker_name, worker_config) ⇒ Object


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

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 => ex
  logger.error("Work queue connection lost, reestablishing on next poll. (#{ex})")
  # 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

#wake_upObject


54
55
56
# File 'lib/delayed/work_queue/parent_process/client.rb', line 54

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