Class: KueRuby
- Inherits:
-
Object
- Object
- KueRuby
- Defined in:
- lib/kue_ruby.rb,
lib/kue_ruby/version.rb
Overview
Interface with the Automattic Kue redis store
Defined Under Namespace
Classes: KueJob
Constant Summary collapse
- VERSION =
'0.3.0'
Instance Attribute Summary collapse
-
#prefix ⇒ Object
readonly
Returns the value of attribute prefix.
-
#redis ⇒ Object
readonly
Returns the value of attribute redis.
Instance Method Summary collapse
-
#create_fifo(id = 1) ⇒ Object
Create FIFO id for zset to preserve order.
-
#create_job(options = {}) ⇒ KueJob
Enqueue a job.
-
#create_job!(options = {}) ⇒ Object
Enqueue a job.
-
#initialize(options = {}) ⇒ KueRuby
constructor
Create a new client instance.
Constructor Details
#initialize(options = {}) ⇒ KueRuby
Create a new client instance
16 17 18 19 20 21 |
# File 'lib/kue_ruby.rb', line 16 def initialize( = {}) raise(ArgumentError, ':redis Redis required', caller) unless [:redis] @redis = [:redis] @prefix = [:prefix] ? [:prefix] : 'q' super() end |
Instance Attribute Details
#prefix ⇒ Object (readonly)
Returns the value of attribute prefix.
7 8 9 |
# File 'lib/kue_ruby.rb', line 7 def prefix @prefix end |
#redis ⇒ Object (readonly)
Returns the value of attribute redis.
7 8 9 |
# File 'lib/kue_ruby.rb', line 7 def redis @redis end |
Instance Method Details
#create_fifo(id = 1) ⇒ Object
Create FIFO id for zset to preserve order
28 29 30 31 32 33 34 35 36 |
# File 'lib/kue_ruby.rb', line 28 def create_fifo(id = 1) id_len = id.to_s.length.to_s len = 2 - id_len.length while len > 0 id_len = '0' + id_len len -= 1 end id_len.to_s + '|' + id.to_s end |
#create_job(options = {}) ⇒ KueJob
Enqueue a job
47 48 49 50 51 52 53 54 55 |
# File 'lib/kue_ruby.rb', line 47 def create_job( = {}) raise(ArgumentError, ':type String required', caller) unless [:type] raise(ArgumentError, ':data Hash required', caller) unless [:data] begin return create_job! rescue return nil end end |
#create_job!(options = {}) ⇒ Object
Enqueue a job
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/kue_ruby.rb', line 66 def create_job!( = {}) raise(ArgumentError, ':type String required', caller) unless [:type] raise(ArgumentError, ':data Hash required', caller) unless [:data] job = KueJob.new job.type = [:type] job.data = [:data] job.priority = [:priority] ? [:priority] : 0 job.max_attempts = [:max_attempts] ? [:max_attempts] : 1 job.state = 'inactive' job.created_at = Time.now job.backoff = { delay: 60 * 1000, type: 'exponential' } job.id = @redis.incr "#{@prefix}.ids" job.zid = create_fifo job.id @redis.sadd "#{@prefix}:job:types", job.type job.save! self @redis.zadd("#{@prefix}:jobs", job.priority, job.zid) @redis.zadd("#{@prefix}:jobs:inactive", job.priority, job.zid) @redis.zadd("#{@prefix}:jobs:#{job.type}:inactive", job.priority, job.zid) @redis.lpush("#{@prefix}:#{job.type}:jobs", 1) job end |