beehive 0.1.1 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/MANIFEST CHANGED
@@ -14,8 +14,11 @@ lib/beehive.rb
14
14
  license.txt
15
15
  spec/.gitkeep
16
16
  spec/.rspec
17
+ spec/beehive/all.rb
17
18
  spec/beehive/client.rb
19
+ spec/beehive/setup.rb
18
20
  spec/beehive/worker.rb
19
21
  spec/helper.rb
20
22
  spec/tmp/.gitkeep
21
- task/build.rake
23
+ task/build.rake
24
+ task/test.rake
data/Rakefile CHANGED
@@ -1,13 +1,6 @@
1
1
  require File.expand_path('../lib/beehive', __FILE__)
2
- require 'rake/testtask'
3
2
 
4
3
  # Load all the tasks
5
4
  Dir.glob(File.expand_path("../task/*.rake", __FILE__)).each do |f|
6
5
  import(f)
7
6
  end
8
-
9
- # Set the tests so they can be run with $ rake test
10
- Rake::TestTask.new do |t|
11
- t.test_files = Dir.glob(File.expand_path('../spec/beehive/**/*.rb', __FILE__))
12
- t.verbose = true
13
- end
@@ -50,7 +50,7 @@ module Beehive
50
50
  #
51
51
  def get(job)
52
52
  job = "beehive.jobs.#{job}"
53
- job = @connection.rpop(job)
53
+ job = @connection.lpop(job)
54
54
 
55
55
  if !job.nil?
56
56
  return JSON.load(job)
@@ -5,5 +5,5 @@ module Beehive
5
5
  # @author Yorick Peterse
6
6
  # @since 0.1
7
7
  #
8
- Version = '0.1.1'
8
+ Version = '0.1.3'
9
9
  end
@@ -21,7 +21,8 @@ module Beehive
21
21
  :daemonize => false,
22
22
  :jobs => [],
23
23
  :wait => 5,
24
- :log_level => Logger::WARN
24
+ :log_level => Logger::WARN,
25
+ :pid => File.join(Dir.pwd, 'worker.pid')
25
26
  }
26
27
 
27
28
  # Instance of Beehive::Client, used for communicating with the Redis server
@@ -50,6 +51,7 @@ module Beehive
50
51
  # reduces CPU and network usage.
51
52
  # @option worker_options :log_level The log even to use for the :logger option, set to
52
53
  # Logger::WARN by default.
54
+ # @option worker_options :pid Path to the location of the PID file for the worker.
53
55
  #
54
56
  def initialize(redis_options = {}, worker_options = {})
55
57
  @connection = ::Beehive::Client.new(redis_options)
@@ -83,12 +85,16 @@ module Beehive
83
85
  Process.daemon(true)
84
86
  end
85
87
 
86
- @options[:logger].info("Starting main worker, PID: #{Process.pid}")
88
+ @worker_pid = Process.pid
89
+
90
+ @options[:logger].info("Starting main worker, PID: #{@worker_pid}")
91
+ write_pid
87
92
 
88
93
  loop do
89
94
  if @shutdown === true
90
95
  @options[:logger].info('The party has ended, time to shut down')
91
96
  @connection.disconnect
97
+ File.unlink(@options[:pid])
92
98
  exit
93
99
  end
94
100
 
@@ -125,6 +131,12 @@ module Beehive
125
131
  end
126
132
  end
127
133
 
134
+ # Did the PID change for some reason?
135
+ if Process.pid != @worker_pid
136
+ @worker_pid = Process.pid
137
+ write_pid
138
+ end
139
+
128
140
  # Reduces CPU load and network traffic
129
141
  sleep(@options[:wait])
130
142
  end
@@ -155,5 +167,17 @@ module Beehive
155
167
  end
156
168
  end
157
169
  end
170
+
171
+ ##
172
+ # Writes the given PID to the PID file.
173
+ #
174
+ # @author Yorick Peterse
175
+ # @since 0.1.2
176
+ #
177
+ def write_pid
178
+ File.open(@options[:pid], 'w') do |handle|
179
+ handle.write(@worker_pid)
180
+ end
181
+ end
158
182
  end # Worker
159
183
  end # Beehive
@@ -0,0 +1,3 @@
1
+ ['setup', 'client', 'worker'].each do |f|
2
+ require(File.expand_path("../#{f}", __FILE__))
3
+ end
@@ -2,16 +2,24 @@ require File.expand_path('../../helper', __FILE__)
2
2
 
3
3
  describe('Beehive::Client') do
4
4
 
5
- it('Queue a new job') do
6
- client = Beehive::Client.new
7
- client.queue('video', :title => 'hello')
5
+ before(:all) do
6
+ if ENV.key?('REDIS')
7
+ @client = Beehive::Client.new([ENV['REDIS']])
8
+ else
9
+ @client = Beehive::Client.new
10
+ end
8
11
  end
9
12
 
10
- it('Get the latest job') do
11
- client = Beehive::Client.new
12
- job = client.get('video')
13
+ it('Queue a new job') do
14
+ @client.queue('video', :title => 'hello')
15
+
16
+ job = @client.get('video')
13
17
 
14
18
  job['title'].should === 'hello'
15
19
  end
16
20
 
21
+ after(:all) do
22
+ @client = nil
23
+ end
24
+
17
25
  end
@@ -0,0 +1,31 @@
1
+ require File.expand_path('../../helper', __FILE__)
2
+
3
+ describe('Check if the environment is capable of running the tests') do
4
+
5
+ it('Check if Redis is running') do
6
+ # Dirty way of checking to see if Redis is running
7
+ output = `ps a | grep redis-server`
8
+ matches = output.match(/[0-9]+\s+redis-server/)
9
+
10
+ if matches.nil?
11
+ fail("You need to start Redis in order to run these tests")
12
+ end
13
+ end
14
+
15
+ it('Check the default Redis settings in case the previous test missed them') do
16
+ client = Redis.new
17
+
18
+ begin
19
+ client.client.connect
20
+ # Default settings didn't work, check if the ENV variable REDIS is set
21
+ rescue
22
+ if !ENV.key?('REDIS')
23
+ message = "Failed to connect to the default Redis host and the REDIS " +
24
+ "environment variable wasn't set."
25
+
26
+ fail(message)
27
+ end
28
+ end
29
+ end
30
+
31
+ end
@@ -2,7 +2,11 @@ require File.expand_path('../../helper', __FILE__)
2
2
 
3
3
  describe('Beehive::Worker') do
4
4
  it('Create a new instance of Beehive::Worker') do
5
- worker = Beehive::Worker.new
5
+ if ENV.key?('REDIS')
6
+ worker = Beehive::Worker.new([ENV['REDIS']])
7
+ else
8
+ worker = Beehive::Worker.new
9
+ end
6
10
 
7
11
  worker.options[:logger].level.should === Logger::WARN
8
12
  worker.options[:jobs].size.should === 0
@@ -23,7 +27,12 @@ describe('Beehive::Worker') do
23
27
  end
24
28
 
25
29
  # Queue the job
26
- client = Beehive::Client.new
30
+ if ENV.key?('REDIS')
31
+ client = Beehive::Client.new([ENV['REDIS']])
32
+ else
33
+ client = Beehive::Client.new
34
+ end
35
+
27
36
  client.queue('spec')
28
37
 
29
38
  pid = Process.fork do
@@ -40,4 +49,23 @@ describe('Beehive::Worker') do
40
49
  path.should === 'Hello, world!'
41
50
  end
42
51
 
52
+ it('Write the PID') do
53
+ pid_path = File.expand_path('../../tmp/worker.pid', __FILE__)
54
+
55
+ pid = Process.fork do
56
+ worker = Beehive::Worker.new({}, {:pid => pid_path})
57
+ worker.work
58
+ end
59
+
60
+ # Give the worker some time to boot up
61
+ sleep(1)
62
+
63
+ # Check if the PID is correct
64
+ pid_file = File.read(pid_path, File.size(pid_path)).strip
65
+ pid_file.should === pid.to_s
66
+
67
+ # Close the connection
68
+ Process.kill('INT', pid_file.to_i)
69
+ end
70
+
43
71
  end
@@ -1,3 +1,7 @@
1
1
  require File.expand_path('../../lib/beehive', __FILE__)
2
2
  require 'rspec'
3
3
  require 'rspec/autorun'
4
+
5
+ RSpec.configure do |c|
6
+ c.fail_fast = true
7
+ end
@@ -0,0 +1,5 @@
1
+ desc 'Runs all the tests'
2
+ task :test do
3
+ spec_path = File.expand_path('../../spec/', __FILE__)
4
+ sh("cd #{spec_path}; rspec beehive/all.rb")
5
+ end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: beehive
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.1.1
5
+ version: 0.1.3
6
6
  platform: ruby
7
7
  authors:
8
8
  - Yorick Peterse
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-05-20 00:00:00 +02:00
13
+ date: 2011-07-07 00:00:00 +02:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -82,11 +82,14 @@ files:
82
82
  - license.txt
83
83
  - spec/.gitkeep
84
84
  - spec/.rspec
85
+ - spec/beehive/all.rb
85
86
  - spec/beehive/client.rb
87
+ - spec/beehive/setup.rb
86
88
  - spec/beehive/worker.rb
87
89
  - spec/helper.rb
88
90
  - spec/tmp/.gitkeep
89
91
  - task/build.rake
92
+ - task/test.rake
90
93
  has_rdoc: yard
91
94
  homepage: https://github.com/yorickpeterse/beehive/
92
95
  licenses: []