Class: Faktory::Client
- Inherits:
-
Object
- Object
- Faktory::Client
- Defined in:
- lib/faktory/client.rb
Constant Summary collapse
- @@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: '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.
- #push(job) ⇒ Object
Constructor Details
#initialize(url: '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.
28 29 30 31 32 |
# File 'lib/faktory/client.rb', line 28 def initialize(url: 'tcp://localhost:7419', debug: false) @debug = debug @location = uri_from_env || URI(url) open end |
Instance Attribute Details
#middleware ⇒ Object
Returns the value of attribute middleware.
19 20 21 |
# File 'lib/faktory/client.rb', line 19 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.
15 16 17 |
# File 'lib/faktory/client.rb', line 15 def self.worker! @@random_process_wid = SecureRandom.hex(8) end |
Instance Method Details
#ack(jid) ⇒ Object
66 67 68 69 70 71 |
# File 'lib/faktory/client.rb', line 66 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.
89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/faktory/client.rb', line 89 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
34 35 36 37 38 39 |
# File 'lib/faktory/client.rb', line 34 def close return unless @sock command "END" @sock.close @sock = nil end |
#fail(jid, ex) ⇒ Object
73 74 75 76 77 78 79 80 81 |
# File 'lib/faktory/client.rb', line 73 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
57 58 59 60 61 62 63 64 |
# File 'lib/faktory/client.rb', line 57 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
42 43 44 45 46 47 |
# File 'lib/faktory/client.rb', line 42 def flush transaction do command "FLUSH" ok! end end |
#info ⇒ Object
102 103 104 105 106 107 108 |
# File 'lib/faktory/client.rb', line 102 def info transaction do command("INFO") str = result JSON.parse(str) if str end end |
#push(job) ⇒ Object
49 50 51 52 53 54 55 |
# File 'lib/faktory/client.rb', line 49 def push(job) transaction do command "PUSH", JSON.generate(job) ok! job["jid"] end end |