Module: Qs::Daemon::InstanceMethods

Defined in:
lib/qs/daemon.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#daemon_dataObject (readonly)

Returns the value of attribute daemon_data.



30
31
32
# File 'lib/qs/daemon.rb', line 30

def daemon_data
  @daemon_data
end

#loggerObject (readonly)

Returns the value of attribute logger.



30
31
32
# File 'lib/qs/daemon.rb', line 30

def logger
  @logger
end

#queue_redis_keysObject (readonly)

Returns the value of attribute queue_redis_keys.



31
32
33
# File 'lib/qs/daemon.rb', line 31

def queue_redis_keys
  @queue_redis_keys
end

#signals_redis_keyObject (readonly)

Returns the value of attribute signals_redis_key.



31
32
33
# File 'lib/qs/daemon.rb', line 31

def signals_redis_key
  @signals_redis_key
end

Instance Method Details

#halt(wait = false) ⇒ Object



103
104
105
106
107
108
# File 'lib/qs/daemon.rb', line 103

def halt(wait = false)
  return unless self.running?
  @state.set :halt
  wakeup_thread
  wait_for_shutdown if wait
end

#initializeObject

set the size of the client to the num workers + 1, this ensures we have 1 connection for fetching work from redis and at least 1 connection for each worker to requeue its message when hard-shutdown



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/qs/daemon.rb', line 36

def initialize
  self.class.configuration.validate!
  Qs.init
  @daemon_data = DaemonData.new(self.class.configuration.to_hash)
  @logger      = @daemon_data.logger

  @client = QsClient.new(Qs.redis_config.merge({
    :timeout => 1,
    :size    => self.daemon_data.num_workers + 1
  }))
  @queue_redis_keys = self.daemon_data.queue_redis_keys

  @signals_redis_key = "signals:#{@daemon_data.name}-" \
                       "#{Socket.gethostname}-#{::Process.pid}"

  @worker_available = WorkerAvailable.new

  @worker_pool = DatWorkerPool.new(self.daemon_data.worker_class, {
    :num_workers   => self.daemon_data.num_workers,
    :logger        => self.daemon_data.dwp_logger,
    :worker_params => self.daemon_data.worker_params.merge({
      :qs_daemon_data      => self.daemon_data,
      :qs_client           => @client,
      :qs_worker_available => @worker_available,
      :qs_logger           => @logger
    })
  })

  @thread = nil
  @state = State.new(:stop)
rescue InvalidError => exception
  exception.set_backtrace(caller)
  raise exception
end

#nameObject



71
72
73
# File 'lib/qs/daemon.rb', line 71

def name
  @daemon_data.name
end

#pid_fileObject



79
80
81
# File 'lib/qs/daemon.rb', line 79

def pid_file
  @daemon_data.pid_file
end

#process_labelObject



75
76
77
# File 'lib/qs/daemon.rb', line 75

def process_label
  @daemon_data.process_label
end

#running?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/qs/daemon.rb', line 83

def running?
  !!(@thread && @thread.alive?)
end

#startObject

ping to check that it can communicate with redis before running, this is friendlier than starting and continously erroring because it can’t dequeue



90
91
92
93
94
# File 'lib/qs/daemon.rb', line 90

def start
  @client.ping
  @state.set :run
  @thread ||= Thread.new{ work_loop }
end

#stop(wait = false) ⇒ Object



96
97
98
99
100
101
# File 'lib/qs/daemon.rb', line 96

def stop(wait = false)
  return unless self.running?
  @state.set :stop
  wakeup_thread
  wait_for_shutdown if wait
end