redisque 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/CHANGELOG.md ADDED
@@ -0,0 +1,8 @@
1
+ ## 1.0.2 (2012-11-02)
2
+ * Migrated to rspec
3
+
4
+ ## 1.0.1 (2012-05-23)
5
+ * Fixed "uninitialized constant Redisque::JSON" error
6
+
7
+ ## 1.0.0 (2012-05-17)
8
+ * Initial release
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source :rubygems
2
+ gemspec
3
+ gem 'rspec'
4
+ gem 'rspec-mocks'
data/Gemfile.lock ADDED
@@ -0,0 +1,29 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ redisque (1.0.2)
5
+ json (>= 1.7.5)
6
+ redis (>= 3.0.2)
7
+
8
+ GEM
9
+ remote: http://rubygems.org/
10
+ specs:
11
+ diff-lcs (1.1.3)
12
+ json (1.7.5)
13
+ redis (3.0.2)
14
+ rspec (2.11.0)
15
+ rspec-core (~> 2.11.0)
16
+ rspec-expectations (~> 2.11.0)
17
+ rspec-mocks (~> 2.11.0)
18
+ rspec-core (2.11.1)
19
+ rspec-expectations (2.11.3)
20
+ diff-lcs (~> 1.1.3)
21
+ rspec-mocks (2.11.3)
22
+
23
+ PLATFORMS
24
+ ruby
25
+
26
+ DEPENDENCIES
27
+ redisque!
28
+ rspec
29
+ rspec-mocks
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2012 Yuri Belogub
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,122 @@
1
+ [![Dependency Status](https://gemnasium.com/belogub/redisque.png)](https://gemnasium.com/belogub/redisque)
2
+ About
3
+ ----
4
+ Redisque is yet another lightweight and simple to use queue manager with [Redis DB](http://www.redis.io) backend.
5
+ Installation
6
+ -----
7
+ Run ``` $ sudo gem install redisque ```
8
+ How to use
9
+ -----
10
+ Redisque has only three methods that you will employ:
11
+
12
+
13
+ ``` ruby
14
+ Redisque.connect(options)
15
+ ```
16
+ with the help of this method you can customize your connection to Redis DB. `options` is a `Hash` and can have the keys: `:address, :port, :password` and `:db`.
17
+ ``` ruby
18
+ Redisque.enqueue(queue, data)
19
+ ```
20
+ makes possible to add task represented by `data` (Hash) to `queue` (String).
21
+ ``` ruby
22
+ Redisque.workoff(queue, pause = 5, &block)
23
+ ```
24
+ manages the queue: asks the `queue` for the next entry (job that is represented with JSONized Hash) and if it gets one then the `block` is executed with data received otherwise sleep for `pause` time set in seconds, this way we give a chance to another processes to do their job :)
25
+
26
+
27
+ ***Usage scenario might look like:***
28
+
29
+ in your application:
30
+
31
+ ``` ruby
32
+ require "redisque"
33
+ [
34
+ {:id => 1, :city => 'New York'},
35
+ {:id => 2, :city => 'Pekin'},
36
+ {:id => 3, :city => 'Mumbai'},
37
+ {:id => 4, :city => 'Paris'},
38
+ {:id => 5, :city => 'Madrid'}
39
+ ].each {|chunk| Redisque.enqueue('my_queue', chunk)}
40
+ ```
41
+ in your [Worker] *.rb file:
42
+
43
+ ``` ruby
44
+ require "redisque"
45
+ Redisque.connect(:password => 'secret')
46
+ Redisque.workoff('my_queue') do |data|
47
+ puts "Next city is: %s" % data["city"]
48
+ end
49
+ ```
50
+
51
+ ***Halting Redisque:***
52
+
53
+ Redisque has a key `halting` and you can use it to halt the execution, i.e. in your [Worker] *.rb file:
54
+ ``` ruby
55
+ trap('INT') do
56
+ print "1. (Q)uit now\n2. (W)ait for current task to finish\n3. (C)ontinue\n"
57
+ case gets.chomp
58
+ when 'Q'
59
+ exit
60
+ when 'W'
61
+ Redisque.halting = true
62
+ print 'Redisque instance resumes until current Job is finished'
63
+ else
64
+ end
65
+ end
66
+ trap('TERM') {exit}
67
+
68
+ require "json"
69
+ require "redisque"
70
+
71
+ ...
72
+
73
+ ```
74
+
75
+
76
+ Caution
77
+ -----
78
+ Redisque creates backup queue when it's run with the following name convention:
79
+
80
+ original `queue` name that is preceded with `asterisk symbol`
81
+
82
+ e.g. if your queue name was `my_queue` then the backup queue name would be `*my_queue` so you should keep it in mind when working with other queues that some names are already automatically reserved for backup queues
83
+
84
+ In case Worker was interrupted, backup queue data (if any) will be shifted into active queue on next Worker start, thus if task was running but didn't succeed, it's entry will be restored from backup queue. For your Worker to function properly it has to be able to redo all steps regardless the execution moment it was stopped at.
85
+
86
+ Contribute to project
87
+ -----
88
+ Please do.
89
+
90
+ If you feel that it lacks some functionality or enhancement or anything else, please feel free to:
91
+ - clone the project
92
+ - create a branch do your changes or improvements in it and
93
+ - make a pull request
94
+
95
+ to review code and merge the branch.
96
+
97
+ Thank you
98
+
99
+ License
100
+ -----
101
+ MIT License
102
+
103
+ Copyright (c) 2012 Yuri Belogub
104
+
105
+ Permission is hereby granted, free of charge, to any person obtaining
106
+ a copy of this software and associated documentation files (the
107
+ "Software"), to deal in the Software without restriction, including
108
+ without limitation the rights to use, copy, modify, merge, publish,
109
+ distribute, sublicense, and/or sell copies of the Software, and to
110
+ permit persons to whom the Software is furnished to do so, subject to
111
+ the following conditions:
112
+
113
+ The above copyright notice and this permission notice shall be
114
+ included in all copies or substantial portions of the Software.
115
+
116
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
117
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
118
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
119
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
120
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
121
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
122
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ require 'rspec/core/rake_task'
2
+
3
+ RSpec::Core::RakeTask.new('spec')
4
+
5
+ task :default => :spec
data/lib/redisque.rb ADDED
@@ -0,0 +1,73 @@
1
+ require 'redis'
2
+ require 'json'
3
+
4
+ module Redisque
5
+ extend self
6
+
7
+ attr_accessor :halting
8
+
9
+ @options = {:address => '127.0.0.1', :port => 6379, :password => '', :db => 0}
10
+
11
+ # Connect to Redis DB
12
+ # Example:
13
+ # Redisque.connect(:password => 'secret', :db => 1)
14
+ # Arguments:
15
+ # options: (Hash)
16
+ def connect(options)
17
+ @options.merge! options
18
+ @redis = Redis.new(@options)
19
+ end
20
+
21
+ # Add task to queue
22
+ # Example:
23
+ # Redisque.enqueue('tasks', {:id => 1, :email => 'my@email.com'})
24
+ # Arguments:
25
+ # queue: (String)
26
+ # data: (Hash)
27
+ def enqueue(queue, data)
28
+ redis.lpush(queue, JSON(data))
29
+ end
30
+
31
+ # Run along the queue and execute tasks
32
+ def workoff(queue, pause = 5, &block)
33
+ @backup_queue = '*' << queue
34
+ crash_check(queue)
35
+ loop do
36
+ process(queue, &block)
37
+ break if @halting
38
+ # anyway, we need to get some sleep
39
+ sleep pause
40
+ end
41
+ end
42
+
43
+ # Return Redis connection
44
+ # Arguments:
45
+ # options: (Hash)
46
+ def redis(options = @options)
47
+ @redis || connect(options)
48
+ end
49
+
50
+ # Process the queue
51
+ # Arguments:
52
+ # queue: (String)
53
+ def process(queue)
54
+ # wait for new value in `queue` for `timeout` time
55
+ # pop it and copy to bak queue named `*{queue}`
56
+ value = redis.rpoplpush(queue, @backup_queue)
57
+ # if we do have some valid value then launch the worker
58
+ if value
59
+ yield(JSON.parse(value))
60
+ # Remove task from backup queue
61
+ redis.rpop @backup_queue
62
+ end
63
+ end
64
+
65
+ # Check if backup queue has any task
66
+ # Arguments:
67
+ # queue: (String)
68
+ def crash_check(queue)
69
+ redis.lrange(@backup_queue, 0, -1).each { |unfinished| redis.lpush(queue, unfinished) }
70
+ redis.del(@backup_queue)
71
+ end
72
+
73
+ end
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+
3
+ describe Redisque do
4
+
5
+ let(:queue_data) {[
6
+ {:weather => 'is good'}.to_json,
7
+ {:sky => 'is blue'}.to_json
8
+ ]}
9
+
10
+ let(:redis) {
11
+ mock('redis', { :rpoplpush => queue_data[0], :rpop => queue_data[0] })
12
+ }
13
+
14
+ before(:each) do
15
+ Redisque.stub(:redis).and_return redis
16
+ end
17
+
18
+ it ".process" do
19
+ Redisque.process('queue') do |data|
20
+ expect(data.has_key?('weather')).to be_true
21
+ end
22
+ end
23
+
24
+ end
@@ -0,0 +1,8 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ require 'json'
4
+
5
+ require_relative '../lib/redisque'
6
+
7
+ RSpec.configure do |config|
8
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: redisque
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Yuri Belogub
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-02 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: json
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 1.7.5
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 1.7.5
30
+ - !ruby/object:Gem::Dependency
31
+ name: redis
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: 3.0.2
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: 3.0.2
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: Redis Database lightweight and simple to use queue manager
63
+ email: belogub@yahoo.com
64
+ executables: []
65
+ extensions: []
66
+ extra_rdoc_files:
67
+ - README.md
68
+ - CHANGELOG.md
69
+ - LICENSE
70
+ files:
71
+ - lib/redisque.rb
72
+ - spec/redisque_spec.rb
73
+ - spec/spec_helper.rb
74
+ - .rspec
75
+ - Gemfile
76
+ - Gemfile.lock
77
+ - Rakefile
78
+ - README.md
79
+ - CHANGELOG.md
80
+ - LICENSE
81
+ homepage: https://github.com/belogub/redisque
82
+ licenses:
83
+ - MIT
84
+ post_install_message:
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ none: false
96
+ requirements:
97
+ - - ! '>='
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ requirements: []
101
+ rubyforge_project:
102
+ rubygems_version: 1.8.24
103
+ signing_key:
104
+ specification_version: 3
105
+ summary: Redis queue manager
106
+ test_files: []