forked from resque/resque-scheduler
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extracting runtime env bits into separate class
- Loading branch information
1 parent
49bc40b
commit 1a5212b
Showing
4 changed files
with
130 additions
and
92 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# vim:fileencoding=utf-8 | ||
|
||
module Resque | ||
module Scheduler | ||
class Env | ||
def initialize(options) | ||
@options = options | ||
end | ||
|
||
def setup | ||
require 'resque' | ||
require 'resque/scheduler' | ||
|
||
setup_backgrounding | ||
setup_pid_file | ||
setup_scheduler_configuration | ||
end | ||
|
||
private | ||
|
||
attr_reader :options | ||
|
||
def setup_backgrounding | ||
# Need to set this here for conditional Process.daemon redirect of | ||
# stderr/stdout to /dev/null | ||
Resque::Scheduler.quiet = !!options[:quiet] | ||
|
||
if options[:background] | ||
unless Process.respond_to?('daemon') | ||
abort 'background option is set, which requires ruby >= 1.9' | ||
end | ||
|
||
Process.daemon(true, !Resque::Scheduler.quiet) | ||
Resque.redis.client.reconnect | ||
end | ||
end | ||
|
||
def setup_pid_file | ||
File.open(options[:pidfile], 'w') do |f| | ||
f.puts $PROCESS_ID | ||
end if options[:pidfile] | ||
end | ||
|
||
def setup_scheduler_configuration | ||
Resque::Scheduler.configure do |c| | ||
# These settings are somewhat redundant given the defaults present | ||
# in the attr reader methods. They are left here for clarity and | ||
# to serve as an example of how to use `.configure`. | ||
|
||
c.app_name = options[:app_name] | ||
c.dynamic = !!options[:dynamic] | ||
c.env = options[:env] | ||
c.logfile = options[:logfile] | ||
c.logformat = options[:logformat] | ||
c.poll_sleep_amount = Float(options[:poll_sleep_amount] || '5') | ||
c.verbose = !!options[:verbose] | ||
end | ||
end | ||
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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# vim:fileencoding=utf-8 | ||
require_relative 'test_helper' | ||
|
||
context 'Env' do | ||
def new_env(options = {}) | ||
Resque::Scheduler::Env.new(options) | ||
end | ||
|
||
test 'daemonizes when background is true' do | ||
Process.expects(:daemon) | ||
env = new_env(background: true) | ||
env.setup | ||
end | ||
|
||
test 'reconnects redis when background is true' do | ||
Process.stubs(:daemon) | ||
mock_redis_client = mock('redis_client') | ||
mock_redis = mock('redis') | ||
mock_redis.expects(:client).returns(mock_redis_client) | ||
mock_redis_client.expects(:reconnect) | ||
Resque.expects(:redis).returns(mock_redis) | ||
env = new_env(background: true) | ||
env.setup | ||
end | ||
|
||
test 'aborts when background is given and Process does not support daemon' do | ||
Process.stubs(:daemon) | ||
Process.expects(:respond_to?).with('daemon').returns(false) | ||
env = new_env(background: true) | ||
env.expects(:abort) | ||
env.setup | ||
end | ||
|
||
test 'writes pid to pidfile when given' do | ||
mock_pidfile = mock('pidfile') | ||
mock_pidfile.expects(:puts) | ||
File.expects(:open).with('derp.pid', 'w').yields(mock_pidfile) | ||
env = new_env(pidfile: 'derp.pid') | ||
env.setup | ||
end | ||
end |