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.1.0'
Instance Attribute Summary collapse
-
#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.
-
#initialize(options = {}) ⇒ KueRuby
constructor
Create a new client instance.
Constructor Details
#initialize(options = {}) ⇒ KueRuby
Create a new client instance
15 16 17 18 |
# File 'lib/kue_ruby.rb', line 15 def initialize( = {}) @redis = [:redis] super() end |
Instance Attribute Details
#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
25 26 27 28 29 30 31 32 33 |
# File 'lib/kue_ruby.rb', line 25 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
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/kue_ruby.rb', line 44 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 'q.ids' job.zid = create_fifo job.id @redis.sadd 'q:job:types', job.type job.save @redis @redis.zadd('q:jobs', job.priority, job.zid) @redis.zadd('q:jobs:inactive', job.priority, job.zid) @redis.zadd("q:jobs:#{job.type}:inactive", job.priority, job.zid) @redis.lpush("q:#{job.type}:jobs", 1) job end |