Class: Faktory::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/faktory/client.rb

Constant Summary collapse

@@random_process_wid =
""

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#middlewareObject

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

#beatObject

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

#closeObject



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.message[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

#flushObject

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

#infoObject



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