Class: Faktory::Client
- Inherits:
-
Object
- Object
- Faktory::Client
- Defined in:
- lib/faktory/client.rb,
lib/faktory/testing.rb
Constant Summary collapse
- HASHER =
proc do |iter, pwd, salt| sha = Digest::SHA256.new hashing = pwd + salt iter.times do hashing = sha.digest(hashing) end Digest.hexencode(hashing) end
- @@random_process_wid =
""
Instance Attribute Summary collapse
-
#middleware ⇒ Object
Returns the value of attribute middleware.
Class Method Summary collapse
-
.worker! ⇒ Object
Called when booting the worker process to signal that this process will consume jobs and send BEAT.
Instance Method Summary collapse
- #ack(jid) ⇒ Object
-
#beat ⇒ Object
Sends a heartbeat to the server, in order to prove this worker process is still alive.
- #close ⇒ Object
- #fail(jid, ex) ⇒ Object
- #fetch(*queues) ⇒ Object
-
#flush ⇒ Object
Warning: this clears all job data in Faktory.
- #info ⇒ Object
-
#initialize(url: uri_from_env || 'tcp://localhost:7419', debug: false) ⇒ Client
constructor
Best practice is to rely on the localhost default for development and configure the environment variables for non-development environments.
- #open ⇒ Object
- #push(job) ⇒ Object
- #real_open ⇒ Object
- #real_push ⇒ Object
Constructor Details
#initialize(url: uri_from_env || 'tcp://localhost:7419', debug: false) ⇒ Client
Best practice is to rely on the localhost default for development and configure the environment variables for non-development environments.
FAKTORY_PROVIDER=MY_FAKTORY_URL MY_FAKTORY_URL=tcp://:[email protected]:7419
Note above, the URL can contain the password for secure installations.
38 39 40 41 42 |
# File 'lib/faktory/client.rb', line 38 def initialize(url: uri_from_env || 'tcp://localhost:7419', debug: false) @debug = debug @location = URI(url) open end |
Instance Attribute Details
#middleware ⇒ Object
Returns the value of attribute middleware.
29 30 31 |
# File 'lib/faktory/client.rb', line 29 def middleware @middleware end |
Class Method Details
.worker! ⇒ Object
Called when booting the worker process to signal that this process will consume jobs and send BEAT.
25 26 27 |
# File 'lib/faktory/client.rb', line 25 def self.worker! @@random_process_wid = SecureRandom.hex(8) end |
Instance Method Details
#ack(jid) ⇒ Object
76 77 78 79 80 81 |
# File 'lib/faktory/client.rb', line 76 def ack(jid) transaction do command("ACK", %Q[{"jid":"#{jid}"}]) ok! end end |
#beat ⇒ Object
Sends a heartbeat to the server, in order to prove this worker process is still alive.
Return a string signal to process, legal values are “quiet” or “terminate”. The quiet signal is informative: the server won’t allow this process to FETCH any more jobs anyways.
99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/faktory/client.rb', line 99 def beat transaction do command("BEAT", %Q[{"wid":"#{@@random_process_wid}"}]) str = result if str == "OK" str else hash = JSON.parse(str) hash["state"] end end end |
#close ⇒ Object
44 45 46 47 48 49 |
# File 'lib/faktory/client.rb', line 44 def close return unless @sock command "END" @sock.close @sock = nil end |
#fail(jid, ex) ⇒ Object
83 84 85 86 87 88 89 90 91 |
# File 'lib/faktory/client.rb', line 83 def fail(jid, ex) transaction do command("FAIL", JSON.dump({ message: ex.[0...1000], errtype: ex.class.name, jid: jid, backtrace: ex.backtrace})) ok! end end |
#fetch(*queues) ⇒ Object
67 68 69 70 71 72 73 74 |
# File 'lib/faktory/client.rb', line 67 def fetch(*queues) job = nil transaction do command("FETCH", *queues) job = result end JSON.parse(job) if job end |
#flush ⇒ Object
Warning: this clears all job data in Faktory
52 53 54 55 56 57 |
# File 'lib/faktory/client.rb', line 52 def flush transaction do command "FLUSH" ok! end end |
#info ⇒ Object
112 113 114 115 116 117 118 |
# File 'lib/faktory/client.rb', line 112 def info transaction do command("INFO") str = result JSON.parse(str) if str end end |
#open ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/faktory/client.rb', line 131 def open if tls? sock = TCPSocket.new(@location.hostname, @location.port) ctx = OpenSSL::SSL::SSLContext.new ctx.set_params(verify_mode: OpenSSL::SSL::VERIFY_PEER) ctx.ssl_version = :TLSv1_2 @sock = OpenSSL::SSL::SSLSocket.new(sock, ctx).tap do |socket| socket.sync_close = true socket.connect end else @sock = TCPSocket.new(@location.hostname, @location.port) @sock.setsockopt(Socket::SOL_SOCKET, Socket::SO_KEEPALIVE, true) end payload = { "wid": @@random_process_wid, "hostname": Socket.gethostname, "pid": $$, "labels": ["ruby-#{RUBY_VERSION}"], "v": 2, } hi = result if hi =~ /\AHI (.*)/ hash = JSON.parse($1) ver = hash["v"].to_i if ver > 2 puts "Warning: Faktory server protocol #{ver} in use, this worker doesn't speak that version." puts "We recommend you upgrade this gem with `bundle up faktory_worker_ruby`." end salt = hash["s"] if salt pwd = @location.password if !pwd raise ArgumentError, "Server requires password, but none has been configured" end iter = (hash["i"] || 1).to_i raise ArgumentError, "Invalid hashing" if iter < 1 payload["pwdhash"] = HASHER.(iter, pwd, salt) end end command("HELLO", JSON.dump(payload)) ok! end |
#push(job) ⇒ Object
59 60 61 62 63 64 65 |
# File 'lib/faktory/client.rb', line 59 def push(job) transaction do command "PUSH", JSON.generate(job) ok! job["jid"] end end |
#real_open ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/faktory/testing.rb', line 79 def open if tls? sock = TCPSocket.new(@location.hostname, @location.port) ctx = OpenSSL::SSL::SSLContext.new ctx.set_params(verify_mode: OpenSSL::SSL::VERIFY_PEER) ctx.ssl_version = :TLSv1_2 @sock = OpenSSL::SSL::SSLSocket.new(sock, ctx).tap do |socket| socket.sync_close = true socket.connect end else @sock = TCPSocket.new(@location.hostname, @location.port) @sock.setsockopt(Socket::SOL_SOCKET, Socket::SO_KEEPALIVE, true) end payload = { "wid": @@random_process_wid, "hostname": Socket.gethostname, "pid": $$, "labels": ["ruby-#{RUBY_VERSION}"], "v": 2, } hi = result if hi =~ /\AHI (.*)/ hash = JSON.parse($1) ver = hash["v"].to_i if ver > 2 puts "Warning: Faktory server protocol #{ver} in use, this worker doesn't speak that version." puts "We recommend you upgrade this gem with `bundle up faktory_worker_ruby`." end salt = hash["s"] if salt pwd = @location.password if !pwd raise ArgumentError, "Server requires password, but none has been configured" end iter = (hash["i"] || 1).to_i raise ArgumentError, "Invalid hashing" if iter < 1 payload["pwdhash"] = HASHER.(iter, pwd, salt) end end command("HELLO", JSON.dump(payload)) ok! end |
#real_push ⇒ Object
78 79 80 81 82 83 84 |
# File 'lib/faktory/testing.rb', line 78 def push(job) transaction do command "PUSH", JSON.generate(job) ok! job["jid"] end end |