Skip to content
This repository has been archived by the owner. It is now read-only.

Commit

Permalink
Merge pull request hirefire#14 from samoli/minimum_workers.
Browse files Browse the repository at this point in the history
Add min_workers option
  • Loading branch information
Michael van Rooijen committed May 1, 2011
2 parents 24821b7 + 1fabfc0 commit e6be82e
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -44,6 +44,7 @@ And that's it. Next time you deploy to [Heroku](http://heroku.com/) it'll automa
HireFire.configure do |config|
config.environment = nil # default in production is :heroku. default in development is :noop
config.max_workers = 5 # default is 1
config.min_workers = 0 # default is 0
config.job_worker_ratio = [
{ :jobs => 1, :workers => 1 },
{ :jobs => 15, :workers => 2 },
Expand Down
7 changes: 7 additions & 0 deletions lib/hirefire/configuration.rb
Expand Up @@ -8,6 +8,12 @@ class Configuration
#
# @return [Fixnum] default: 1
attr_accessor :max_workers

##
# Contains the min amount of workers that should always be running
#
# @return [Fixnum] default: 0
attr_accessor :min_workers

##
# Contains the job/worker ratio which determines
Expand All @@ -33,6 +39,7 @@ class Configuration
# @return [HireFire::Configuration]
def initialize
@max_workers = 1
@min_workers = 0
@job_worker_ratio = [
{ :jobs => 1, :workers => 1 },
{ :jobs => 25, :workers => 2 },
Expand Down
17 changes: 13 additions & 4 deletions lib/hirefire/environment/base.rb
Expand Up @@ -160,13 +160,13 @@ def hire
# or "updated, unless the job didn't fail"
#
# If there are workers active, but there are no more pending jobs,
# then fire all the workers
# then fire all the workers or set to the minimum_workers
#
# @return [nil]
def fire
if jobs == 0 and workers > 0
Logger.message("All queued jobs have been processed. Firing all workers.")
workers(0)
if jobs == 0 and workers > min_workers
Logger.message("All queued jobs have been processed. " + (min_workers > 0 ? "Setting workers to #{min_workers}." : "Firing all workers."))
workers(min_workers)
end
end

Expand All @@ -189,6 +189,15 @@ def log_and_hire(amount)
def max_workers
HireFire.configuration.max_workers
end

##
# Wrapper method for HireFire.configuration
# Returns the min amount of workers that should always be running
#
# @return [Fixnum] the min amount of workers that should always be running
def min_workers
HireFire.configuration.min_workers
end

##
# Wrapper method for HireFire.configuration
Expand Down
3 changes: 3 additions & 0 deletions spec/configuration_spec.rb
Expand Up @@ -9,6 +9,7 @@

configuration.environment.should == nil
configuration.max_workers.should == 1
configuration.min_workers.should == 0
configuration.job_worker_ratio.should == [
{ :jobs => 1, :workers => 1 },
{ :jobs => 25, :workers => 2 },
Expand All @@ -22,6 +23,7 @@
HireFire.configure do |config|
config.environment = :noop
config.max_workers = 10
config.min_workers = 0
config.job_worker_ratio = [
{ :jobs => 1, :workers => 1 },
{ :jobs => 15, :workers => 2 },
Expand All @@ -35,6 +37,7 @@

configuration.environment.should == :noop
configuration.max_workers.should == 10
configuration.min_workers.should == 0
configuration.job_worker_ratio.should == [
{ :jobs => 1, :workers => 1 },
{ :jobs => 15, :workers => 2 },
Expand Down
10 changes: 10 additions & 0 deletions spec/environment_spec.rb
Expand Up @@ -100,6 +100,16 @@ def jobs=(amount)
base.expects(:workers).with(0).once
base.fire
end

it 'should set the workers to minimum workers when there arent any jobs' do
base.jobs = 0
base.workers = 10
base.stubs(:min_workers).returns(2)

HireFire::Logger.expects(:message).with('All queued jobs have been processed. Setting workers to 2.')
base.expects(:workers).with(2).once
base.fire
end
end

describe '#hire' do
Expand Down

0 comments on commit e6be82e

Please sign in to comment.