job_boss 0.5.5 → 0.5.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,46 +1,48 @@
1
+ require 'active_support/core_ext/string'
2
+
1
3
  module JobBoss
2
4
  class Config
3
5
  attr_accessor :application_root, :database_yaml_path, :log_path, :jobs_path, :sleep_interval, :employee_limit, :environment
4
6
 
5
7
  def parse_args(argv, options = {})
6
- @application_root = File.expand_path(options[:working_dir] || Dir.pwd)
7
- @database_yaml_path = 'config/database.yml'
8
- @log_path = 'log/job_boss.log'
9
- @jobs_path = 'app/jobs'
10
- @sleep_interval = 0.5
11
- @employee_limit = 4
12
- @environment = 'development'
8
+ @application_root = File.expand_path(ENV['JB_APPLICATION_ROOT'] || options[:working_dir] || Dir.pwd)
9
+ @database_yaml_path = ENV['JB_DATABASE_YAML_PATH'] || 'config/database.yml'
10
+ @log_path = ENV['JB_LOG_PATH'] || 'log/job_boss.log'
11
+ @jobs_path = ENV['JB_JOBS_PATH'] || 'app/jobs'
12
+ @sleep_interval = ENV['JB_SLEEP_INTERVAL'].blank? ? 0.5 : ENV['JB_SLEEP_INTERVAL'].to_f
13
+ @employee_limit = ENV['JB_EMPLOYEE_LIMIT'].blank? ? 4 : ENV['JB_EMPLOYEE_LIMIT'].to_i
14
+ @environment = ENV['JB_ENVIRONMENT'] || 'development'
13
15
 
14
16
  require 'optparse'
15
17
 
16
18
  OptionParser.new do |opts|
17
19
  opts.banner = "Usage: job_boss [start|stop|restart|run|zap] [-- <options>]"
18
20
 
19
- opts.on("-r", "--application-root PATH", "Path for the application root upon which other paths depend (defaults to .)") do |path|
21
+ opts.on("-r", "--application-root PATH", "Path for the application root upon which other paths depend (defaults to .) Environment variable: JB_APPLICATION_ROOT") do |path|
20
22
  @application_root = File.expand_path(path)
21
23
  end
22
24
 
23
- opts.on("-d", "--database-yaml PATH", "Path for database YAML (defaults to <application-root>/#{@database_yaml_path})") do |path|
25
+ opts.on("-d", "--database-yaml PATH", "Path for database YAML (defaults to <application-root>/#{@database_yaml_path}) Environment variable: JB_DATABASE_YAML_PATH") do |path|
24
26
  @database_yaml_path = path
25
27
  end
26
28
 
27
- opts.on("-l", "--log-path PATH", "Path for log file (defaults to <application-root>/#{@log_path})") do |path|
29
+ opts.on("-l", "--log-path PATH", "Path for log file (defaults to <application-root>/#{@log_path}) Environment variable: JB_LOG_PATH") do |path|
28
30
  @log_path = path
29
31
  end
30
32
 
31
- opts.on("-j", "--jobs-path PATH", "Path to folder with job classes (defaults to <application-root>/#{@jobs_path})") do |path|
33
+ opts.on("-j", "--jobs-path PATH", "Path to folder with job classes (defaults to <application-root>/#{@jobs_path}) Environment variable: JB_JOBS_PATH") do |path|
32
34
  @jobs_path = path
33
35
  end
34
36
 
35
- opts.on("-e", "--environment ENV", "Environment to use in database YAML file (defaults to '#{@environment}')") do |env|
37
+ opts.on("-e", "--environment ENV", "Environment to use in database YAML file (defaults to '#{@environment}') Environment variable: JB_ENVIRONMENT") do |env|
36
38
  @environment = env
37
39
  end
38
40
 
39
- opts.on("-s", "--sleep-interval INTERVAL", Integer, "Number of seconds for the boss to sleep between checks of the queue (default #{@sleep_interval})") do |interval|
41
+ opts.on("-s", "--sleep-interval INTERVAL", Integer, "Number of seconds for the boss to sleep between checks of the queue (default #{@sleep_interval}) Environment variable: JB_SLEEP_INTERVAL") do |interval|
40
42
  @sleep_interval = interval
41
43
  end
42
44
 
43
- opts.on("-c", "--employee-limit LIMIT", Integer, "Maximum number of employees (default 4)") do |limit|
45
+ opts.on("-c", "--employee-limit LIMIT", Integer, "Maximum number of employees (default 4) Environment variable: JB_EMPLOYEE_LIMIT") do |limit|
44
46
  @employee_limit = limit
45
47
  end
46
48
  end.parse!(argv)
data/lib/job_boss/job.rb CHANGED
@@ -12,9 +12,9 @@ module JobBoss
12
12
  scope :completed, where('completed_at IS NOT NULL')
13
13
 
14
14
  # Method used by the boss to dispatch an employee
15
- def dispatch
15
+ def dispatch(boss)
16
16
  mark_as_started
17
- Boss.logger.info "Dispatching Job ##{self.id}"
17
+ boss.logger.info "Dispatching Job ##{self.id}"
18
18
 
19
19
  pid = fork do
20
20
  ActiveRecord::Base.connection.reconnect!
@@ -29,7 +29,7 @@ module JobBoss
29
29
  self.update_attribute(:result, value)
30
30
  rescue Exception => exception
31
31
  mark_exception(exception)
32
- Boss.logger.error "Error running job ##{self.id}!"
32
+ boss.logger.error "Error running job ##{self.id}!"
33
33
  ensure
34
34
  until mark_as_completed
35
35
  sleep(1)
@@ -2,6 +2,7 @@ module JobBoss
2
2
  class Queuer
3
3
  def method_missing(method_id, *args)
4
4
  require 'active_support'
5
+ require 'job_boss/job'
5
6
 
6
7
  method_name = method_id.id2name
7
8
 
data/test/test_helper.rb CHANGED
@@ -52,7 +52,7 @@ class ActiveSupport::TestCase
52
52
  end
53
53
  end
54
54
 
55
- def start_daemon(options)
55
+ def start_daemon(options = {})
56
56
  clean_app_environment
57
57
 
58
58
  stop_daemon
@@ -1,29 +1,56 @@
1
1
  require 'test_helper'
2
2
 
3
3
  class DaemonTest < ActiveSupport::TestCase
4
- test "daemon options" do
4
+ test "Basic startup/shutdown" do
5
5
  start_daemon(:application_root => @app_root_path)
6
6
 
7
7
  wait_for_file(@log_path)
8
8
 
9
- assert File.read(@log_path).match('INFO -- : Job Boss started')
9
+ log_output = File.read(@log_path)
10
+ assert log_output.match("INFO -- : Started ActiveRecord connection in 'development' environment from database YAML: #{@db_yaml_path}")
11
+ assert log_output.match('INFO -- : Job Boss started')
12
+ assert log_output.match('INFO -- : Employee limit: 4')
13
+
10
14
 
11
15
  stop_daemon
16
+ end
17
+
18
+ test "Startup/shutdown with environment variables" do
19
+ ENV['JB_APPLICATION_ROOT'] = @app_root_path
20
+ ENV['JB_EMPLOYEE_LIMIT'] = '2'
21
+ start_daemon
12
22
 
23
+ wait_for_file(@log_path)
13
24
 
25
+ log_output = File.read(@log_path)
26
+ assert log_output.match("INFO -- : Started ActiveRecord connection in 'development' environment from database YAML: #{@db_yaml_path}")
27
+ assert log_output.match('INFO -- : Job Boss started')
28
+ assert log_output.match('INFO -- : Employee limit: 2')
29
+
30
+ stop_daemon
31
+
32
+ ENV['JB_APPLICATION_ROOT'] = nil
33
+ ENV['JB_EMPLOYEE_LIMIT'] = nil
34
+ end
35
+
36
+ test "Startup with different command line options" do
14
37
  custom_log_path = File.join('log', 'loggity.log')
15
38
  full_custom_log_path = File.join(@app_root_path, custom_log_path)
16
39
 
17
- start_daemon(:application_root => @app_root_path, :log_path => custom_log_path, :environment => 'production')
40
+ ENV['JB_EMPLOYEE_LIMIT'] = '2' # Make sure application setting overrides environment variable
41
+ start_daemon(:application_root => @app_root_path, :log_path => custom_log_path, :environment => 'production', :employee_limit => 3)
18
42
  wait_for_file(full_custom_log_path)
19
43
 
20
- assert File.read(full_custom_log_path).match('INFO -- : Job Boss started')
44
+ log_output = File.read(full_custom_log_path)
45
+ assert log_output.match("INFO -- : Started ActiveRecord connection in 'production' environment from database YAML: #{@db_yaml_path}")
46
+ assert log_output.match('INFO -- : Job Boss started')
47
+ assert log_output.match('INFO -- : Employee limit: 3')
21
48
  assert File.exist?(File.join(@app_root_path, 'db', 'production.sqlite3'))
22
49
 
23
50
  restart_daemon
24
51
 
25
52
  stop_daemon
26
53
 
27
-
54
+ ENV['JB_EMPLOYEE_LIMIT'] = nil
28
55
  end
29
56
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 5
8
- - 5
9
- version: 0.5.5
8
+ - 7
9
+ version: 0.5.7
10
10
  platform: ruby
11
11
  authors:
12
12
  - Brian Underwood
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-12-11 00:00:00 -05:00
17
+ date: 2010-12-12 00:00:00 -05:00
18
18
  default_executable: job_boss
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency