-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable custom logic to determine worker configuration
Previously, configuration could only be loaded from a YAML file, or a Hash instance. This change introduces the concept of a *configuration loader*. All of the previous configuration logic has been moved to the FileOrHashLoader class, which will be used by default. To provide your own logic, you set the `configuration_loader` class variable on Resque::Pool to an object that responds to `#call` (which can simply be a lambda/Proc). See the README for more details.
- Loading branch information
1 parent
06896d8
commit dcff985
Showing
5 changed files
with
161 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
class FileOrHashLoader | ||
def initialize(filename_or_hash=nil) | ||
case filename_or_hash | ||
when String, nil | ||
@filename = filename_or_hash | ||
when Hash | ||
@static_config = filename_or_hash.dup | ||
else | ||
raise "#{self.class} cannot be initialized with #{filename_or_hash.inspect}" | ||
end | ||
end | ||
|
||
def call(environment) | ||
load_config_from_file(environment) | ||
end | ||
|
||
private | ||
|
||
def load_config_from_file(environment) | ||
if @static_config | ||
new_config = @static_config | ||
else | ||
filename = config_filename | ||
new_config = load_config filename | ||
end | ||
apply_environment new_config, environment | ||
end | ||
|
||
def apply_environment(config, environment) | ||
environment and config[environment] and config.merge!(config[environment]) | ||
config.delete_if {|key, value| value.is_a? Hash } | ||
end | ||
|
||
def config_filename | ||
@filename || choose_config_file | ||
end | ||
|
||
def load_config(filename) | ||
return {} unless filename | ||
YAML.load(ERB.new(IO.read(filename)).result) | ||
end | ||
|
||
CONFIG_FILES = ["resque-pool.yml", "config/resque-pool.yml"] | ||
def choose_config_file | ||
if ENV["RESQUE_POOL_CONFIG"] | ||
ENV["RESQUE_POOL_CONFIG"] | ||
else | ||
CONFIG_FILES.detect { |f| File.exist?(f) } | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,16 @@ | ||
require 'rspec' | ||
$LOAD_PATH << File.expand_path("../lib", File.dirname(__FILE__)) | ||
require 'resque/pool' | ||
|
||
module PoolSpecHelpers | ||
def no_spawn(pool) | ||
pool.stub(:spawn_worker!) {} | ||
pool | ||
end | ||
|
||
def simulate_signal(pool, signal) | ||
pool.sig_queue.clear | ||
pool.sig_queue.push signal | ||
pool.handle_sig_queue! | ||
end | ||
end |