qu-redis 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/qu-redis.rb +4 -0
  2. data/lib/qu/backend/redis.rb +111 -0
  3. metadata +112 -0
data/lib/qu-redis.rb ADDED
@@ -0,0 +1,4 @@
1
+ require 'qu'
2
+ require 'qu/backend/redis'
3
+
4
+ Qu.backend = Qu::Backend::Redis.new
@@ -0,0 +1,111 @@
1
+ require 'redis-namespace'
2
+ require 'simple_uuid'
3
+
4
+ module Qu
5
+ module Backend
6
+ class Redis < Base
7
+ attr_accessor :namespace
8
+
9
+ def initialize
10
+ self.namespace = :qu
11
+ end
12
+
13
+ def connection
14
+ @connection ||= ::Redis::Namespace.new(namespace, :redis => ::Redis.connect(:url => ENV['REDISTOGO_URL']))
15
+ end
16
+ alias_method :redis, :connection
17
+
18
+ def enqueue(klass, *args)
19
+ job = Job.new(SimpleUUID::UUID.new.to_guid, klass, args)
20
+ redis.set("job:#{job.id}", encode('class' => job.klass.to_s, 'args' => job.args))
21
+ redis.rpush("queue:#{job.queue}", job.id)
22
+ redis.sadd('queues', job.queue)
23
+ job
24
+ end
25
+
26
+ def length(queue = 'default')
27
+ redis.llen("queue:#{queue}")
28
+ end
29
+
30
+ def clear(queue = nil)
31
+ queue ||= queues + ['failed']
32
+ Array(queue).each do |q|
33
+ redis.srem('queues', q)
34
+ redis.del("queue:#{q}")
35
+ end
36
+ end
37
+
38
+ def queues
39
+ Array(redis.smembers('queues'))
40
+ end
41
+
42
+ def reserve(worker, options = {:block => true})
43
+ queues = worker.queues.map {|q| "queue:#{q}" }
44
+
45
+ if options[:block]
46
+ id = redis.blpop(*queues.push(0))[1]
47
+ else
48
+ queues.detect {|queue| id = redis.lpop(queue) }
49
+ end
50
+
51
+ get(id) if id
52
+ end
53
+
54
+ def release(job)
55
+ redis.rpush("queue:#{job.queue}", job.id)
56
+ end
57
+
58
+ def failed(job, error)
59
+ redis.rpush("queue:failed", job.id)
60
+ end
61
+
62
+ def completed(job)
63
+ redis.del("job:#{job.id}")
64
+ end
65
+
66
+ def requeue(id)
67
+ if job = get(id)
68
+ redis.lrem('queue:failed', 1, id)
69
+ redis.rpush("queue:#{job.queue}", id)
70
+ job
71
+ else
72
+ false
73
+ end
74
+ end
75
+
76
+ def register_worker(worker)
77
+ redis.set("worker:#{worker.id}", encode(worker.attributes))
78
+ redis.sadd(:workers, worker.id)
79
+ end
80
+
81
+ def unregister_worker(id)
82
+ redis.del("worker:#{id}")
83
+ redis.srem('workers', id)
84
+ end
85
+
86
+ def workers
87
+ Array(redis.smembers(:workers)).map { |id| worker(id) }.compact
88
+ end
89
+
90
+ def clear_workers
91
+ while id = redis.spop(:workers)
92
+ redis.del("worker:#{id}")
93
+ end
94
+ end
95
+
96
+ private
97
+
98
+ def worker(id)
99
+ Qu::Worker.new(decode(redis.get("worker:#{id}")))
100
+ end
101
+
102
+ def get(id)
103
+ if data = redis.get("job:#{id}")
104
+ data = decode(data)
105
+ Job.new(id, data['class'], data['args'])
106
+ end
107
+ end
108
+
109
+ end
110
+ end
111
+ end
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: qu-redis
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Brandon Keepers
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-09-23 00:00:00 -04:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: redis-namespace
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: simple_uuid
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ hash: 3
44
+ segments:
45
+ - 0
46
+ version: "0"
47
+ type: :runtime
48
+ version_requirements: *id002
49
+ - !ruby/object:Gem::Dependency
50
+ name: qu
51
+ prerelease: false
52
+ requirement: &id003 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - "="
56
+ - !ruby/object:Gem::Version
57
+ hash: 27
58
+ segments:
59
+ - 0
60
+ - 1
61
+ - 0
62
+ version: 0.1.0
63
+ type: :runtime
64
+ version_requirements: *id003
65
+ description: Redis backend for qu
66
+ email:
67
+ - brandon@opensoul.org
68
+ executables: []
69
+
70
+ extensions: []
71
+
72
+ extra_rdoc_files: []
73
+
74
+ files:
75
+ - lib/qu-redis.rb
76
+ - lib/qu/backend/redis.rb
77
+ has_rdoc: true
78
+ homepage: http://github.com/bkeepers/qu
79
+ licenses: []
80
+
81
+ post_install_message:
82
+ rdoc_options: []
83
+
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ hash: 3
92
+ segments:
93
+ - 0
94
+ version: "0"
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ none: false
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ hash: 3
101
+ segments:
102
+ - 0
103
+ version: "0"
104
+ requirements: []
105
+
106
+ rubyforge_project:
107
+ rubygems_version: 1.6.1
108
+ signing_key:
109
+ specification_version: 3
110
+ summary: Redis backend for qu
111
+ test_files: []
112
+