beanstalk-worker 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,19 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ gem 'beanstalk-client'
7
+
8
+ # Add dependencies to develop your gem here.
9
+ # Include everything needed to run rake, tests, features, etc.
10
+ group :development do
11
+ gem "rspec", ">= 2.13.0"
12
+ gem "yard", "> 0.8"
13
+ gem "rdoc", "> 4.0"
14
+ gem "cucumber", ">= 0"
15
+ gem "bundler", "~> 1.3.5"
16
+ gem "jeweler", "~> 1.8.4"
17
+ gem "beanstalk-client-rspec", ">= 0"
18
+ gem (RUBY_VERSION =~ /^1\.9/ ? "simplecov" : "rcov"), ">= 0"
19
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2013 Artem Veremey
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,19 @@
1
+ = beanstalk-worker
2
+
3
+ Beanstalkd Worker base class.
4
+
5
+ == Contributing to beanstalk-worker
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
9
+ * Fork the project.
10
+ * Start a feature/bugfix branch.
11
+ * Commit and push until you are happy with your contribution.
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2013 Artem Veremey. See LICENSE.txt for
18
+ further details.
19
+
data/Rakefile ADDED
@@ -0,0 +1,56 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ require './lib/beanstalk-worker/version.rb'
6
+
7
+ begin
8
+ Bundler.setup(:default, :development)
9
+ rescue Bundler::BundlerError => e
10
+ $stderr.puts e.message
11
+ $stderr.puts "Run `bundle install` to install missing gems"
12
+ exit e.status_code
13
+ end
14
+ require 'rake'
15
+
16
+ require 'jeweler'
17
+ Jeweler::Tasks.new do |gem|
18
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
19
+ gem.name = "beanstalk-worker"
20
+ gem.version = BeanStalk::Worker::Version::STRING
21
+ gem.homepage = "http://github.com/aia/beanstalk-worker"
22
+ gem.license = "MIT"
23
+ gem.summary = %Q{Beanstalkd Worker base class}
24
+ gem.description = %Q{Beanstalkd Worker base class}
25
+ gem.email = "artem@veremey.net"
26
+ gem.authors = ["Artem Veremey"]
27
+ # dependencies defined in Gemfile
28
+ end
29
+ Jeweler::RubygemsDotOrgTasks.new
30
+
31
+ require 'rspec/core'
32
+ require 'rspec/core/rake_task'
33
+ RSpec::Core::RakeTask.new(:spec) do |spec|
34
+ spec.pattern = FileList['spec/**/*_spec.rb']
35
+ end
36
+
37
+ if RUBY_VERSION =~ /^1\.9/
38
+ desc "Code coverage detail"
39
+ task :simplecov do
40
+ ENV['COVERAGE'] = "true"
41
+ Rake::Task['spec'].execute
42
+ end
43
+ else
44
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
45
+ spec.pattern = 'spec/**/*_spec.rb'
46
+ spec.rcov = true
47
+ end
48
+ end
49
+
50
+ require 'cucumber/rake/task'
51
+ Cucumber::Rake::Task.new(:features)
52
+
53
+ task :default => :spec
54
+
55
+ require 'yard'
56
+ YARD::Rake::YardocTask.new
@@ -0,0 +1,9 @@
1
+ Feature: something something
2
+ In order to something something
3
+ A user something something
4
+ something something something
5
+
6
+ Scenario: something something
7
+ Given inspiration
8
+ When I create a sweet new gem
9
+ Then everyone should see how awesome I am
@@ -0,0 +1,13 @@
1
+ require 'bundler'
2
+ begin
3
+ Bundler.setup(:default, :development)
4
+ rescue Bundler::BundlerError => e
5
+ $stderr.puts e.message
6
+ $stderr.puts "Run `bundle install` to install missing gems"
7
+ exit e.status_code
8
+ end
9
+
10
+ $LOAD_PATH.unshift(File.dirname(__FILE__) + '/../../lib')
11
+ require 'beanstalk-worker'
12
+
13
+ require 'rspec/expectations'
@@ -0,0 +1,12 @@
1
+ module BeanStalk
2
+ class Worker
3
+ module Version
4
+ MAJOR = 0
5
+ MINOR = 0
6
+ PATCH = 1
7
+ BUILD = nil
8
+
9
+ STRING = [MAJOR, MINOR, PATCH, BUILD].compact.join('.')
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,90 @@
1
+ require 'logger'
2
+ require 'beanstalk-client'
3
+
4
+ module BeanStalk
5
+ class Worker
6
+ attr_accessor :config, :log, :beanstalk, :stats
7
+
8
+ def initialize(config = {}, logger = nil)
9
+ @config = {
10
+ 'beanstalk' => {
11
+ 'server' => "127.0.0.1",
12
+ 'port' => "11300",
13
+ 'tube' => "worker1"
14
+ }
15
+ }
16
+
17
+ @config['beanstalk'].merge!(config['beanstalk'] || {})
18
+
19
+ initialize_logger unless logger
20
+
21
+ @log = logger if logger
22
+
23
+ @log.error("Logging started")
24
+
25
+ @stats = {
26
+ 'received' => 0
27
+ }
28
+
29
+ initialize_beanstalk
30
+ end
31
+
32
+ def initialize_logger
33
+ @config['log'] = {
34
+ 'file' => $stdout,
35
+ 'level' => 'INFO'
36
+ }.merge(@config['log'] || {})
37
+
38
+ log_initialize = [@config['log']['file']]
39
+ log_initialize << @config['log']['shift_age'] if @config['log']['shift_age']
40
+ log_initialize << @config['log']['shift_size'] if @config['log']['shift_size']
41
+
42
+ begin
43
+ @log = Logger.new(*log_initialize)
44
+ @log.level = Logger.const_get(@config['log']['level'])
45
+ rescue Exception => e
46
+ @config['log'] = {
47
+ 'file' => $stdout,
48
+ 'level' => 'INFO'
49
+ }
50
+ @log = Logger.new(@config['log']['file'])
51
+ @log.level = Logger.const_get(@config['log']['level'])
52
+ @log.error("Caught a problem with log settings")
53
+ @log.error("#{e.message}")
54
+ @log.error("Setting log settings to defaults")
55
+ end
56
+ end
57
+
58
+ def initialize_beanstalk
59
+ @beanstalk = Beanstalk::Pool.new([
60
+ [@config['beanstalk']['server'], @config['beanstalk']['port']].join(":")
61
+ ])
62
+
63
+ @beanstalk.watch(@config['beanstalk']['tube'])
64
+ @beanstalk.use(@config['beanstalk']['tube'])
65
+ @beanstalk.ignore('default')
66
+ end
67
+
68
+ def start(received = -1)
69
+ while (received == -1) || (@stats['received'] < received)
70
+ begin
71
+ job = @beanstalk.reserve
72
+
73
+ @log.info("job #{job.inspect}")
74
+ @log.info("job #{job.body.inspect}")
75
+
76
+ @stats['received'] += 1
77
+
78
+ job.delete if work(job)
79
+ rescue Exception => e
80
+ @log.error("Caught exception #{e.to_s}")
81
+ exit
82
+ end
83
+ end
84
+ end
85
+
86
+ def work(job)
87
+ return true
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,4 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+
3
+ require 'beanstalk-worker/version'
4
+ require 'beanstalk-worker/worker'
@@ -0,0 +1,101 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+
3
+ require 'spec_helper'
4
+ require 'beanstalk-client-rspec'
5
+ require 'pp'
6
+
7
+ describe "BeanStalk" do
8
+ describe "Worker" do
9
+ before(:each) do
10
+ @default_config = {
11
+ 'beanstalk' => {
12
+ 'server' => "127.0.0.1",
13
+ 'port' => "11300",
14
+ 'tube' => "worker1"
15
+ }
16
+ }
17
+
18
+ @test_config = {
19
+ 'beanstalk' => {
20
+ 'server' => '10.10.10.10',
21
+ 'port' => '11111',
22
+ 'tube' => 'testqueue'
23
+ }
24
+ }
25
+
26
+ @original_stdout = $stdout
27
+ $stdout = File.new('/dev/null', 'w')
28
+
29
+ stub_const("Beanstalk::Pool", Beanstalk::MockPool)
30
+ end
31
+
32
+ it "should initialize with defaults" do
33
+ @worker = BeanStalk::Worker.new
34
+
35
+ @worker.config['beanstalk'].should eql @default_config['beanstalk']
36
+ end
37
+
38
+ it "should initialize with configuration" do
39
+ @worker = BeanStalk::Worker.new(@test_config)
40
+
41
+ @worker.config['beanstalk'].should eql @test_config['beanstalk']
42
+ end
43
+
44
+ it "should receive queued messages" do
45
+ @worker = BeanStalk::Worker.new(@test_config)
46
+
47
+ @worker.beanstalk.put("foo")
48
+
49
+ job = @worker.beanstalk.reserve
50
+
51
+ job.body.should eql "foo"
52
+ end
53
+
54
+ it "should log messages" do
55
+ logger = mock Logger
56
+ logger.should_receive(:error).with("Logging started")
57
+ @worker = BeanStalk::Worker.new(@test_config, logger)
58
+ end
59
+
60
+ it "should start" do
61
+ @worker = BeanStalk::Worker.new(@test_config)
62
+
63
+ @worker.beanstalk.put("foo")
64
+
65
+ @worker.start(1)
66
+ end
67
+
68
+ it "should start to receive several messages" do
69
+ @worker = BeanStalk::Worker.new(@test_config)
70
+
71
+ 3.times { @worker.beanstalk.put("foo") }
72
+
73
+ @worker.start(3)
74
+ @worker.stats['received'].should eql 3
75
+ end
76
+
77
+ it "should start to receive no more the specified number of messages" do
78
+ @worker = BeanStalk::Worker.new(@test_config)
79
+
80
+ 3.times { @worker.beanstalk.put("foo") }
81
+
82
+ expect {
83
+ @worker.start(4)
84
+ }.to raise_exception
85
+
86
+ @worker.stats['received'].should eql 3
87
+ end
88
+
89
+ it "should start to receive undefined number of messages" do
90
+ @worker = BeanStalk::Worker.new(@test_config)
91
+
92
+ 3.times { @worker.beanstalk.put("foo") }
93
+
94
+ expect {
95
+ @worker.start(4)
96
+ }.to raise_exception
97
+
98
+ @worker.stats['received'].should eql 3
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,32 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+
4
+ if RUBY_VERSION =~ /^1\.9/
5
+ require 'simplecov'
6
+
7
+ module SimpleCov::Configuration
8
+ def clean_filters
9
+ @filters = []
10
+ end
11
+ end
12
+
13
+ SimpleCov.configure do
14
+ clean_filters
15
+ load_adapter 'test_frameworks'
16
+ end
17
+
18
+ ENV["COVERAGE"] && SimpleCov.start do
19
+ add_filter "/.rvm/"
20
+ end
21
+ end
22
+
23
+ require 'rspec'
24
+ require 'beanstalk-worker'
25
+
26
+ # Requires supporting files with custom matchers and macros, etc,
27
+ # in ./support/ and its subdirectories.
28
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
29
+
30
+ RSpec.configure do |config|
31
+
32
+ end
metadata ADDED
@@ -0,0 +1,208 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: beanstalk-worker
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Artem Veremey
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-07-24 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: beanstalk-client
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
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: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: 2.13.0
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: 2.13.0
46
+ - !ruby/object:Gem::Dependency
47
+ name: yard
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>'
52
+ - !ruby/object:Gem::Version
53
+ version: '0.8'
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.8'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rdoc
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>'
68
+ - !ruby/object:Gem::Version
69
+ version: '4.0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>'
76
+ - !ruby/object:Gem::Version
77
+ version: '4.0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: cucumber
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: bundler
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ~>
100
+ - !ruby/object:Gem::Version
101
+ version: 1.3.5
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ~>
108
+ - !ruby/object:Gem::Version
109
+ version: 1.3.5
110
+ - !ruby/object:Gem::Dependency
111
+ name: jeweler
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ~>
116
+ - !ruby/object:Gem::Version
117
+ version: 1.8.4
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ~>
124
+ - !ruby/object:Gem::Version
125
+ version: 1.8.4
126
+ - !ruby/object:Gem::Dependency
127
+ name: beanstalk-client-rspec
128
+ requirement: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ! '>='
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ type: :development
135
+ prerelease: false
136
+ version_requirements: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ! '>='
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ - !ruby/object:Gem::Dependency
143
+ name: simplecov
144
+ requirement: !ruby/object:Gem::Requirement
145
+ none: false
146
+ requirements:
147
+ - - ! '>='
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
150
+ type: :development
151
+ prerelease: false
152
+ version_requirements: !ruby/object:Gem::Requirement
153
+ none: false
154
+ requirements:
155
+ - - ! '>='
156
+ - !ruby/object:Gem::Version
157
+ version: '0'
158
+ description: Beanstalkd Worker base class
159
+ email: artem@veremey.net
160
+ executables: []
161
+ extensions: []
162
+ extra_rdoc_files:
163
+ - LICENSE.txt
164
+ - README.md
165
+ files:
166
+ - .document
167
+ - .rspec
168
+ - Gemfile
169
+ - LICENSE.txt
170
+ - README.md
171
+ - Rakefile
172
+ - features/beanstalk-worker.feature
173
+ - features/step_definitions/beanstalk-worker_steps.rb
174
+ - features/support/env.rb
175
+ - lib/beanstalk-worker.rb
176
+ - lib/beanstalk-worker/version.rb
177
+ - lib/beanstalk-worker/worker.rb
178
+ - spec/beanstalk-worker_spec.rb
179
+ - spec/spec_helper.rb
180
+ homepage: http://github.com/aia/beanstalk-worker
181
+ licenses:
182
+ - MIT
183
+ post_install_message:
184
+ rdoc_options: []
185
+ require_paths:
186
+ - lib
187
+ required_ruby_version: !ruby/object:Gem::Requirement
188
+ none: false
189
+ requirements:
190
+ - - ! '>='
191
+ - !ruby/object:Gem::Version
192
+ version: '0'
193
+ segments:
194
+ - 0
195
+ hash: -623351978344314384
196
+ required_rubygems_version: !ruby/object:Gem::Requirement
197
+ none: false
198
+ requirements:
199
+ - - ! '>='
200
+ - !ruby/object:Gem::Version
201
+ version: '0'
202
+ requirements: []
203
+ rubyforge_project:
204
+ rubygems_version: 1.8.25
205
+ signing_key:
206
+ specification_version: 3
207
+ summary: Beanstalkd Worker base class
208
+ test_files: []