workforce 0.1.1 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.2.2
@@ -4,10 +4,9 @@ require "optparse"
4
4
  require "workforce"
5
5
 
6
6
  config = Workforce::Config.instance
7
- files_to_require = []
8
7
 
9
8
  optparser = OptionParser.new do |opts|
10
- opts.banner = "Usage: #{$0} [options] path/to/worker.rb #{Workforce::Controller::ACTIONS.join("|")}"
9
+ opts.banner = "Usage: #{$0} [options] #{Workforce::Controller::ACTIONS.join("|")} [WORKER]"
11
10
 
12
11
  opts.separator ""
13
12
  opts.separator "Options:"
@@ -16,6 +15,10 @@ optparser = OptionParser.new do |opts|
16
15
  config.load_file(config_path)
17
16
  end
18
17
 
18
+ opts.on("--workers-dir WORKERS_DIR", "Directory containing the workers definitions", "Default: #{config.workers_dir}") do |workers_dir|
19
+ config.workers_dir = workers_dir
20
+ end
21
+
19
22
  opts.on("--pid-dir PID_DIR", "Store pids on directory PID_DIR", "Default: #{config.pid_dir}") do |pid_dir|
20
23
  config.pid_dir = pid_dir
21
24
  end
@@ -23,21 +26,15 @@ optparser = OptionParser.new do |opts|
23
26
  opts.on("--log-dir LOG_DIR", "Store logs on directory LOG_DIR", "Default: #{config.log_dir}") do |log_dir|
24
27
  config.log_dir = log_dir
25
28
  end
26
-
27
- opts.on("--require FILE", "Require FILE before executing action") do |file_path|
28
- files_to_require << file_path
29
- end
30
29
  end
31
30
 
32
31
  optparser.parse!
33
- worker_path, action = ARGV
32
+ action, *args = ARGV
34
33
 
35
- unless worker_path && action
36
- puts optparser
37
- exit(1)
38
- end
34
+ $:.unshift(config.workers_dir)
39
35
 
40
- files_to_require.each { |path| require(path) }
41
- require worker_path
42
- worker_class = File.basename(worker_path, ".rb").camelcase
43
- Workforce::Controller.instance.dispatch(worker_class, action)
36
+ begin
37
+ Workforce::Controller.instance.dispatch(action, *args)
38
+ rescue ArgumentError
39
+ puts optparser
40
+ end
@@ -22,12 +22,30 @@ module Workforce
22
22
 
23
23
  def initialize
24
24
  yield self if block_given?
25
+ @before_callbacks = Hash.new { |h,k| h[k] = [] }
26
+ @after_callbacks = Hash.new { |h,k| h[k] = [] }
25
27
  end
26
28
 
27
29
  def load_file(file_path)
28
30
  instance_eval(File.read(file_path), file_path)
29
31
  end
30
32
 
33
+ def before(event, &block)
34
+ if block_given?
35
+ @before_callbacks[event] << block
36
+ else
37
+ @before_callbacks[event]
38
+ end
39
+ end
40
+
41
+ def after(event, &block)
42
+ if block_given?
43
+ @after_callbacks[event] << block
44
+ else
45
+ @after_callbacks[event]
46
+ end
47
+ end
48
+
31
49
  config_attr :pid_dir do
32
50
  "./pid"
33
51
  end
@@ -35,5 +53,9 @@ module Workforce
35
53
  config_attr :log_dir do
36
54
  "./log"
37
55
  end
56
+
57
+ config_attr :workers_dir do
58
+ "./workers"
59
+ end
38
60
  end
39
61
  end
@@ -3,16 +3,24 @@ require "workforce/manager"
3
3
 
4
4
  module Workforce
5
5
  class Controller
6
- ACTIONS = %w(run start stop restart status schedule dispose)
6
+ ACTIONS = %w(list run status start stop restart schedule dispose)
7
7
  include Singleton
8
8
 
9
- def dispatch(worker_klass, action)
9
+ def dispatch(action, *args)
10
10
  unless ACTIONS.include?(action.to_s)
11
11
  puts "Invalid action #{action}"
12
12
  exit(1)
13
13
  end
14
14
 
15
- send(action, worker_klass)
15
+ send(action, *args)
16
+ end
17
+
18
+ def list
19
+ puts "Available workers:"
20
+ Dir["#{Config.get(:workers_dir)}/*"].each do |file|
21
+ worker_name = Inflector.camelcase(File.basename(file, ".rb"))
22
+ puts " * #{worker_name}"
23
+ end
16
24
  end
17
25
 
18
26
  def run(worker_klass)
@@ -21,10 +29,20 @@ module Workforce
21
29
  exit(1)
22
30
  end
23
31
 
24
- manager = Workforce::Manager.new(get_klass(worker_klass))
25
- store_pid(worker_klass, Process.pid)
26
- manager.run
27
- remove_pid_file(worker_klass)
32
+ run_callbacks(:start) do
33
+ manager = Workforce::Manager.new(get_klass(worker_klass))
34
+ store_pid(worker_klass, Process.pid)
35
+ manager.run
36
+ remove_pid_file(worker_klass)
37
+ end
38
+ end
39
+
40
+ def status(worker_klass)
41
+ if running?(worker_klass)
42
+ puts "Running (#{running_pid(worker_klass)})"
43
+ else
44
+ puts "Not running"
45
+ end
28
46
  end
29
47
 
30
48
  def start(worker_klass)
@@ -33,25 +51,23 @@ module Workforce
33
51
  exit(1)
34
52
  end
35
53
 
36
- manager = Workforce::Manager.new(get_klass(worker_klass))
37
- store_pid(worker_klass, manager.launch)
54
+ run_callbacks(:start) do
55
+ manager = Workforce::Manager.new(get_klass(worker_klass))
56
+ store_pid(worker_klass, manager.launch)
57
+ end
38
58
  end
39
59
 
40
60
  def stop(worker_klass)
41
- Process.kill(:QUIT, running_pid(worker_klass))
42
- remove_pid_file(worker_klass)
61
+ run_callbacks(:stop) do
62
+ Process.kill(:QUIT, running_pid(worker_klass))
63
+ remove_pid_file(worker_klass)
64
+ end
43
65
  end
44
66
 
45
67
  def restart(worker_klass)
46
- stop(worker_klass) if running?(worker_klass)
47
- start(worker_klass)
48
- end
49
-
50
- def status(worker_klass)
51
- if running?(worker_klass)
52
- puts "Running (#{running_pid(worker_klass)})"
53
- else
54
- puts "Not running"
68
+ run_callbacks(:restart) do
69
+ stop(worker_klass) if running?(worker_klass)
70
+ start(worker_klass)
55
71
  end
56
72
  end
57
73
 
@@ -61,7 +77,9 @@ module Workforce
61
77
  exit(1)
62
78
  end
63
79
 
64
- Process.kill(:USR1, running_pid(worker_klass))
80
+ run_callbacks(:schedule) do
81
+ Process.kill(:USR1, running_pid(worker_klass))
82
+ end
65
83
  end
66
84
 
67
85
  def dispose(worker_klass)
@@ -70,18 +88,26 @@ module Workforce
70
88
  exit(1)
71
89
  end
72
90
 
73
- Process.kill(:USR2, running_pid(worker_klass))
91
+ run_callbacks(:dispose) do
92
+ Process.kill(:USR2, running_pid(worker_klass))
93
+ end
74
94
  end
75
95
 
76
96
  protected
77
97
  def get_klass(klass)
78
- return klass unless klass.is_a?(String) || klass.is_a?(Symbol)
98
+ require Inflector.underscore(klass)
79
99
 
80
100
  klass.to_s.split(/::/).inject(Object) do |namespace, const_name|
81
101
  namespace.const_get(const_name)
82
102
  end
83
103
  end
84
104
 
105
+ def run_callbacks(event)
106
+ Config.instance.before(:event).each { |callback| callback.call }
107
+ yield
108
+ Config.instance.after(:event).each { |callback| callback.call }
109
+ end
110
+
85
111
  def pid_file(worker_klass)
86
112
  "#{Workforce::Config.get(:pid_dir)}/#{Workforce::Inflector.underscore(worker_klass)}.pid"
87
113
  end
File without changes
@@ -0,0 +1,66 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{workforce}
8
+ s.version = "0.2.2"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Rodrigo Kochenburger"]
12
+ s.date = %q{2010-05-31}
13
+ s.default_executable = %q{workforce}
14
+ s.description = %q{Workforce is an attempt to create a framework to develop background processes and controlling the execution of these processes in a distributed environment}
15
+ s.email = %q{divoxx@gmail.com}
16
+ s.executables = ["workforce"]
17
+ s.extra_rdoc_files = [
18
+ "LICENSE",
19
+ "README.md"
20
+ ]
21
+ s.files = [
22
+ ".document",
23
+ ".gitignore",
24
+ "LICENSE",
25
+ "README.md",
26
+ "Rakefile",
27
+ "VERSION",
28
+ "bin/workforce",
29
+ "config_sample.rb",
30
+ "lib/workforce.rb",
31
+ "lib/workforce/configuration.rb",
32
+ "lib/workforce/controller.rb",
33
+ "lib/workforce/inflector.rb",
34
+ "lib/workforce/manager.rb",
35
+ "spec/spec.opts",
36
+ "spec/spec_helper.rb",
37
+ "workers/simple_daemon.rb",
38
+ "workers/sleep_daemon.rb",
39
+ "workforce.gemspec"
40
+ ]
41
+ s.homepage = %q{http://github.com/divoxx/workforce}
42
+ s.rdoc_options = ["--charset=UTF-8"]
43
+ s.require_paths = ["lib"]
44
+ s.rubygems_version = %q{1.3.6}
45
+ s.summary = %q{Framework and a distributed runtime manager to creating and running background processes}
46
+ s.test_files = [
47
+ "spec/spec_helper.rb"
48
+ ]
49
+
50
+ if s.respond_to? :specification_version then
51
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
52
+ s.specification_version = 3
53
+
54
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
55
+ s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
56
+ s.add_development_dependency(%q<yard>, [">= 0.5.5"])
57
+ else
58
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
59
+ s.add_dependency(%q<yard>, [">= 0.5.5"])
60
+ end
61
+ else
62
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
63
+ s.add_dependency(%q<yard>, [">= 0.5.5"])
64
+ end
65
+ end
66
+
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 1
8
- - 1
9
- version: 0.1.1
7
+ - 2
8
+ - 2
9
+ version: 0.2.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Rodrigo Kochenburger
@@ -68,9 +68,11 @@ files:
68
68
  - lib/workforce/controller.rb
69
69
  - lib/workforce/inflector.rb
70
70
  - lib/workforce/manager.rb
71
- - sleep_daemon.rb
72
71
  - spec/spec.opts
73
72
  - spec/spec_helper.rb
73
+ - workers/simple_daemon.rb
74
+ - workers/sleep_daemon.rb
75
+ - workforce.gemspec
74
76
  has_rdoc: true
75
77
  homepage: http://github.com/divoxx/workforce
76
78
  licenses: []