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.



26
27
28
# File 'lib/qs/daemon.rb', line 26

def daemon_data
  @daemon_data
end

#signals_redis_keyObject (readonly)

Returns the value of attribute signals_redis_key.



26
27
28
# File 'lib/qs/daemon.rb', line 26

def signals_redis_key
  @signals_redis_key
end

Instance Method Details

#halt(wait = false) ⇒ Object



119
120
121
122
123
124
# File 'lib/qs/daemon.rb', line 119

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

#initializeObject



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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/qs/daemon.rb', line 28

def initialize
  config = self.class.config
  begin
    config.validate!
  rescue InvalidError => exception
    exception.set_backtrace(caller)
    raise exception
  end
  Qs.init

  @daemon_data = DaemonData.new({
    :name             => config.name,
    :pid_file         => config.pid_file,
    :shutdown_timeout => config.shutdown_timeout,
    :worker_class     => config.worker_class,
    :worker_params    => config.worker_params,
    :num_workers      => config.num_workers,
    :error_procs      => config.error_procs,
    :logger           => config.logger,
    :queues           => config.queues,
    :verbose_logging  => config.verbose_logging,
    :routes           => config.routes
  })

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

  @thread           = nil
  @worker_available = WorkerAvailable.new
  @state            = State.new(:stop)

  # 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
  @client = QsClient.new(Qs.redis_connect_hash.merge({
    :timeout => 1,
    :size    => self.daemon_data.num_workers + 1
  }))

  @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           => self.logger
    })
  })
end

#loggerObject



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

def logger
  @daemon_data.logger
end

#nameObject



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

def name
  @daemon_data.name
end

#pid_fileObject



87
88
89
# File 'lib/qs/daemon.rb', line 87

def pid_file
  @daemon_data.pid_file
end

#process_labelObject



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

def process_label
  @daemon_data.process_label
end

#queue_redis_keysObject



95
96
97
# File 'lib/qs/daemon.rb', line 95

def queue_redis_keys
  @daemon_data.queue_redis_keys
end

#running?Boolean

Returns:

  • (Boolean)


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

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

#startObject



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

def start
  # ping to check that it can communicate with redis before running,
  # this is friendlier than starting and continously erroring because
  # it can't dequeue
  @client.ping
  @state.set :run
  @thread ||= Thread.new{ work_loop }
end

#stop(wait = false) ⇒ Object



112
113
114
115
116
117
# File 'lib/qs/daemon.rb', line 112

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