backgroundrb-rails3 1.1
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/.autotest +17 -0
- data/ChangeLog +50 -0
- data/Gemfile +11 -0
- data/LICENSE +4 -0
- data/MIT-LICENSE +20 -0
- data/README +22 -0
- data/Rakefile +128 -0
- data/TODO.org +5 -0
- data/app/controller/backgroundrb_status_controller.rb +6 -0
- data/backgroundrb-rails3.gemspec +219 -0
- data/config/backgroundrb.yml +11 -0
- data/doc/Rakefile +5 -0
- data/doc/config.yaml +2 -0
- data/doc/content/advanced/advanced.txt +76 -0
- data/doc/content/advanced/advanced.yaml +4 -0
- data/doc/content/bugs/bugs.txt +20 -0
- data/doc/content/bugs/bugs.yaml +5 -0
- data/doc/content/community/community.txt +36 -0
- data/doc/content/community/community.yaml +5 -0
- data/doc/content/content.txt +168 -0
- data/doc/content/content.yaml +5 -0
- data/doc/content/faq/faq.txt +41 -0
- data/doc/content/faq/faq.yaml +5 -0
- data/doc/content/rails/rails.txt +182 -0
- data/doc/content/rails/rails.yaml +5 -0
- data/doc/content/scheduling/scheduling.txt +166 -0
- data/doc/content/scheduling/scheduling.yaml +5 -0
- data/doc/content/workers/workers.txt +178 -0
- data/doc/content/workers/workers.yaml +5 -0
- data/doc/layouts/default/default.erb +56 -0
- data/doc/layouts/default/default.yaml +4 -0
- data/doc/lib/default.rb +7 -0
- data/doc/output/Assets/BG-Ad-Top.png +0 -0
- data/doc/output/Assets/BG-Body.png +0 -0
- data/doc/output/Assets/BG-Feed.png +0 -0
- data/doc/output/Assets/BG-Menu-Hover.png +0 -0
- data/doc/output/Assets/BG-Menu.png +0 -0
- data/doc/output/Assets/BG-Sidebar-Bottom.png +0 -0
- data/doc/output/Assets/Button-Feed.png +0 -0
- data/doc/output/images/bg-ad-top.png +0 -0
- data/doc/output/images/bg-body.png +0 -0
- data/doc/output/images/bg-feed.gif +0 -0
- data/doc/output/images/bg-footer.jpg +0 -0
- data/doc/output/images/bg-header.jpg +0 -0
- data/doc/output/images/bg-menu-hover.png +0 -0
- data/doc/output/images/bg-menu.png +0 -0
- data/doc/output/images/bg-sidebar-bottom.gif +0 -0
- data/doc/output/images/button-feed.png +0 -0
- data/doc/output/images/icon-comment.png +0 -0
- data/doc/output/images/more_icon.gif +0 -0
- data/doc/output/style.css +299 -0
- data/doc/page_defaults.yaml +13 -0
- data/doc/tasks/default.rake +3 -0
- data/doc/templates/default/default.txt +1 -0
- data/doc/templates/default/default.yaml +4 -0
- data/examples/backgroundrb.yml +25 -0
- data/examples/foo_controller.rb +48 -0
- data/examples/god_worker.rb +7 -0
- data/examples/worker_tests/god_worker_test.rb +8 -0
- data/examples/workers/error_worker.rb +17 -0
- data/examples/workers/foo_worker.rb +38 -0
- data/examples/workers/god_worker.rb +7 -0
- data/examples/workers/model_worker.rb +13 -0
- data/examples/workers/renewal_worker.rb +11 -0
- data/examples/workers/rss_worker.rb +26 -0
- data/examples/workers/server_worker.rb +31 -0
- data/examples/workers/world_worker.rb +12 -0
- data/examples/workers/xmpp_worker.rb +7 -0
- data/init.rb +7 -0
- data/install.rb +1 -0
- data/know_issues.org +5 -0
- data/lib/backgroundrb.rb +1 -0
- data/lib/backgroundrb/bdrb_client_helper.rb +8 -0
- data/lib/backgroundrb/bdrb_cluster_connection.rb +156 -0
- data/lib/backgroundrb/bdrb_config.rb +43 -0
- data/lib/backgroundrb/bdrb_conn_error.rb +29 -0
- data/lib/backgroundrb/bdrb_connection.rb +179 -0
- data/lib/backgroundrb/bdrb_job_queue.rb +79 -0
- data/lib/backgroundrb/bdrb_result.rb +19 -0
- data/lib/backgroundrb/bdrb_start_stop.rb +146 -0
- data/lib/backgroundrb/rails_worker_proxy.rb +181 -0
- data/lib/backgroundrb/railtie.rb +48 -0
- data/lib/generators/backgroundrb/bdrb_migration/USAGE +12 -0
- data/lib/generators/backgroundrb/bdrb_migration/bdrb_migration_generator.rb +15 -0
- data/lib/generators/backgroundrb/bdrb_migration/templates/migration.rb +27 -0
- data/lib/generators/backgroundrb/worker/USAGE +16 -0
- data/lib/generators/backgroundrb/worker/templates/unit_test.rb +12 -0
- data/lib/generators/backgroundrb/worker/templates/worker.rb +7 -0
- data/lib/generators/backgroundrb/worker/worker_generator.rb +14 -0
- data/lib/tasks/backgroundrb_tasks.rake +103 -0
- data/release_notes.org +48 -0
- data/release_points.org +46 -0
- data/script/backgroundrb +52 -0
- data/script/bdrb_test_helper.rb +99 -0
- data/script/load_worker_env.rb +31 -0
- data/script/monitrc +25 -0
- data/server/backgroundrb_server.rb +12 -0
- data/server/lib/bdrb_result_storage.rb +62 -0
- data/server/lib/bdrb_server_helper.rb +24 -0
- data/server/lib/bdrb_thread_pool.rb +127 -0
- data/server/lib/cron_trigger.rb +197 -0
- data/server/lib/invalid_dump_error.rb +4 -0
- data/server/lib/log_worker.rb +25 -0
- data/server/lib/master_proxy.rb +140 -0
- data/server/lib/master_worker.rb +187 -0
- data/server/lib/meta_worker.rb +432 -0
- data/server/lib/trigger.rb +34 -0
- data/test/bdrb_client_test_helper.rb +5 -0
- data/test/bdrb_test_helper.rb +35 -0
- data/test/client/backgroundrb.yml +17 -0
- data/test/client/test_bdrb_client_helper.rb +13 -0
- data/test/client/test_bdrb_cluster_connection.rb +162 -0
- data/test/client/test_bdrb_config.rb +20 -0
- data/test/client/test_bdrb_connection.rb +29 -0
- data/test/client/test_bdrb_job_queue.rb +63 -0
- data/test/client/test_worker_proxy.rb +130 -0
- data/test/server/test_cron_trigger.rb +281 -0
- data/test/server/test_master_proxy.rb +54 -0
- data/test/server/test_master_worker.rb +157 -0
- data/test/server/test_meta_worker.rb +281 -0
- data/test/server/test_result_storage.rb +14 -0
- data/test/socket_mocker.rb +34 -0
- data/test/workers/bar_worker.rb +10 -0
- data/test/workers/foo_worker.rb +10 -0
- data/uninstall.rb +1 -0
- metadata +345 -0
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
Description:
|
|
2
|
+
Creates a migration to add the job queue table used by BackgrounDRb.
|
|
3
|
+
Pass the migration name, either CamelCased or under_scored, as an argument.
|
|
4
|
+
|
|
5
|
+
Example:
|
|
6
|
+
rails generate backgroundrb:bdrb_migration BackgroundrbJob
|
|
7
|
+
|
|
8
|
+
Assuming this is run at 09:00:15h on 12 September, 2008, this will create the
|
|
9
|
+
CreateBackgroundrbQueue migration in:
|
|
10
|
+
|
|
11
|
+
db/migrate/20080912090015_backgroundrb_create_backgroundrb_jobs.rb
|
|
12
|
+
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
require 'rails/generators/active_record'
|
|
2
|
+
|
|
3
|
+
module Backgroundrb
|
|
4
|
+
module Generators
|
|
5
|
+
class BdrbMigrationGenerator < ActiveRecord::Generators::Base
|
|
6
|
+
def self.source_root
|
|
7
|
+
@source_root ||= File.expand_path('../templates', __FILE__)
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def copy_backgroundrb_migration
|
|
11
|
+
migration_template "migration.rb", "db/migrate/backgroundrb_create_#{table_name}"
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
class BackgroundrbCreate<%= table_name.camelize %> < ActiveRecord::Migration
|
|
2
|
+
def self.up
|
|
3
|
+
create_table :<%= table_name %> do |t|
|
|
4
|
+
t.column :args, :text
|
|
5
|
+
t.column :worker_name, :string
|
|
6
|
+
t.column :worker_method, :string
|
|
7
|
+
t.column :job_key, :string
|
|
8
|
+
t.column :taken, :int
|
|
9
|
+
t.column :finished, :int
|
|
10
|
+
t.column :timeout, :int
|
|
11
|
+
t.column :priority, :int
|
|
12
|
+
t.column :submitted_at, :datetime
|
|
13
|
+
t.column :started_at, :datetime
|
|
14
|
+
t.column :finished_at, :datetime
|
|
15
|
+
t.column :archived_at, :datetime
|
|
16
|
+
t.column :tag, :string
|
|
17
|
+
t.column :submitter_info, :string
|
|
18
|
+
t.column :runner_info, :string
|
|
19
|
+
t.column :worker_key, :string
|
|
20
|
+
t.column :scheduled_at, :datetime
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def self.down
|
|
25
|
+
drop_table :<%= table_name %>
|
|
26
|
+
end
|
|
27
|
+
end
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
Description:
|
|
2
|
+
The worker generator creates stubs for a new BackgrounDRb worker.
|
|
3
|
+
|
|
4
|
+
The generator takes a worker name as its argument. The worker name may be
|
|
5
|
+
given in CamelCase or under_score and should not be suffixed with 'Worker'.
|
|
6
|
+
|
|
7
|
+
The generator creates a worker class in lib/workers and a test suite in
|
|
8
|
+
test/unit.
|
|
9
|
+
|
|
10
|
+
Example:
|
|
11
|
+
./script/generate backgroundrb:worker Tail
|
|
12
|
+
|
|
13
|
+
This will create an Tail worker:
|
|
14
|
+
Model: lib/workers/tail_worker.rb
|
|
15
|
+
Test: test/unit/tail_worker_test.rb
|
|
16
|
+
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
require File.dirname(__FILE__) + '<%= '/..' * class_nesting_depth %>/../test_helper'
|
|
2
|
+
require "#{RAILS_ROOT}/lib/workers/<%= file_name %>_worker"
|
|
3
|
+
require "#{RAILS_ROOT}/vendor/plugins/backgroundrb/lib/backgroundrb.rb"
|
|
4
|
+
require 'drb'
|
|
5
|
+
|
|
6
|
+
class <%= class_name %>WorkerTest < Test::Unit::TestCase
|
|
7
|
+
|
|
8
|
+
# Replace this with your real tests.
|
|
9
|
+
def test_truth
|
|
10
|
+
assert <%= class_name %>Worker.included_modules.include?(DRbUndumped)
|
|
11
|
+
end
|
|
12
|
+
end
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
module Backgroundrb
|
|
2
|
+
module Generators
|
|
3
|
+
class WorkerGenerator < Rails::Generators::NamedBase
|
|
4
|
+
def self.source_root
|
|
5
|
+
@source_root ||= File.expand_path('../templates', __FILE__)
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
def copy_backgroundrb_worker
|
|
9
|
+
template "worker.rb", "lib/workers/#{file_name}_worker.rb"
|
|
10
|
+
#template "unit_test.rb", "test/unit/#{file_name}_worker_test.rb"
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
namespace :backgroundrb do
|
|
2
|
+
require 'yaml'
|
|
3
|
+
desc 'Setup backgroundrb in your rails application'
|
|
4
|
+
task :setup do
|
|
5
|
+
script_dest = "#{Rails.root}/script/backgroundrb"
|
|
6
|
+
script_src = File.dirname(__FILE__) + "/../../script/backgroundrb"
|
|
7
|
+
|
|
8
|
+
FileUtils.chmod 0774, script_src
|
|
9
|
+
|
|
10
|
+
defaults = {:backgroundrb => {:ip => '0.0.0.0',:port => 11006 } }
|
|
11
|
+
|
|
12
|
+
config_dest = "#{Rails.root}/config/backgroundrb.yml"
|
|
13
|
+
|
|
14
|
+
unless File.exists?(config_dest)
|
|
15
|
+
puts "Copying backgroundrb.yml config file to #{config_dest}"
|
|
16
|
+
File.open(config_dest, 'w') { |f| f.write(YAML.dump(defaults)) }
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
unless File.exists?(script_dest)
|
|
20
|
+
puts "Copying backgroundrb script to #{script_dest}"
|
|
21
|
+
FileUtils.cp_r(script_src, script_dest)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
workers_dest = "#{Rails.root}/lib/workers"
|
|
25
|
+
unless File.exists?(workers_dest)
|
|
26
|
+
puts "Creating #{workers_dest}"
|
|
27
|
+
FileUtils.mkdir(workers_dest)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
test_helper_dest = "#{Rails.root}/test/bdrb_test_helper.rb"
|
|
31
|
+
test_helper_src = File.dirname(__FILE__) + "/../../script/bdrb_test_helper.rb"
|
|
32
|
+
unless File.exists?(test_helper_dest)
|
|
33
|
+
puts "Copying Worker Test helper file #{test_helper_dest}"
|
|
34
|
+
FileUtils.cp_r(test_helper_src,test_helper_dest)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
worker_env_loader_dest = "#{Rails.root}/script/load_worker_env.rb"
|
|
38
|
+
worker_env_loader_src = File.join(File.dirname(__FILE__),"../../","script","load_worker_env.rb")
|
|
39
|
+
unless File.exists? worker_env_loader_dest
|
|
40
|
+
puts "Copying Worker envionment loader file #{worker_env_loader_dest}"
|
|
41
|
+
FileUtils.cp_r(worker_env_loader_src,worker_env_loader_dest)
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
unless File.exists? "#{Rails.root}/tmp/pids"
|
|
45
|
+
puts "Creating tmp/pids directory"
|
|
46
|
+
Dir.mkdir "#{Rails.root}/tmp/pids"
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# Generate the migration
|
|
50
|
+
Rake::Task['backgroundrb:queue_migration'].invoke
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
desc "Drops and recreate backgroundrb queue table"
|
|
54
|
+
task :redo_queue => :queue_migration do
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
desc 'update backgroundrb config files from your rails application'
|
|
58
|
+
task :update do
|
|
59
|
+
temp_scripts = ["backgroundrb","load_worker_env.rb"].map {|x| "#{Rails.root}/script/#{x}"}
|
|
60
|
+
temp_scripts.each do |file_name|
|
|
61
|
+
if File.exists?(file_name)
|
|
62
|
+
puts "Removing #{file_name} ..."
|
|
63
|
+
FileUtils.rm(file_name,:force => true)
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
new_temp_scripts = ["backgroundrb","load_worker_env.rb"].map {|x| File.dirname(__FILE__) + "/../../script/#{x}" }
|
|
67
|
+
new_temp_scripts.each do |file_name|
|
|
68
|
+
puts "Updating file #{File.expand_path(file_name)} ..."
|
|
69
|
+
FileUtils.cp_r(file_name,"#{Rails.root}/script/")
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
desc 'Generate a migration for the backgroundrb queue table. The migration name can be ' +
|
|
74
|
+
'specified with the MIGRATION environment variable.'
|
|
75
|
+
task :queue_migration => :environment do
|
|
76
|
+
raise "Task unavailable to this database (no migration support)" unless ActiveRecord::Base.connection.supports_migrations?
|
|
77
|
+
require 'rails/generators'
|
|
78
|
+
Rails::Generators::invoke('backgroundrb:bdrb_migration', ENV['MIGRATION'] || 'BackgroundrbJob')
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
desc 'Remove backgroundrb from your rails application'
|
|
82
|
+
task :remove do
|
|
83
|
+
script_src = "#{Rails.root}/script/backgroundrb"
|
|
84
|
+
temp_scripts = ["backgroundrb","load_worker_env.rb"].map {|x| "#{Rails.root}/script/#{x}"}
|
|
85
|
+
|
|
86
|
+
if File.exists?(script_src)
|
|
87
|
+
puts "Removing #{script_src} ..."
|
|
88
|
+
FileUtils.rm(script_src, :force => true)
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
test_helper_src = "#{Rails.root}/test/bdrb_test_helper.rb"
|
|
92
|
+
if File.exists?(test_helper_src)
|
|
93
|
+
puts "Removing backgroundrb test helper.."
|
|
94
|
+
FileUtils.rm(test_helper_src,:force => true)
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
workers_dest = "#{Rails.root}/lib/workers"
|
|
98
|
+
if File.exists?(workers_dest) && Dir.entries("#{workers_dest}").size == 2
|
|
99
|
+
puts "#{workers_dest} is empty...deleting!"
|
|
100
|
+
FileUtils.rmdir(workers_dest)
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
end
|
data/release_notes.org
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
Hi,
|
|
2
|
+
|
|
3
|
+
BackgrounDRb 1.2 is being unleashed.
|
|
4
|
+
|
|
5
|
+
* New features:
|
|
6
|
+
|
|
7
|
+
** Exceptions/errors are now popped out at the earliest moment
|
|
8
|
+
in the client side itself. For example:
|
|
9
|
+
|
|
10
|
+
>> MiddleMan.worker(:foo_worker).async_bar(:args => {:age => 10})
|
|
11
|
+
BackgrounDRb::RemoteWorkerError: BackgrounDRb::RemoteWorkerError
|
|
12
|
+
|
|
13
|
+
Above exception is thrown because remote worker doesn't have method
|
|
14
|
+
"bar" defined on it.
|
|
15
|
+
|
|
16
|
+
Similarly:
|
|
17
|
+
|
|
18
|
+
>> MiddleMan.worker(:foo_worker).checksum(:args => {:age => "lolz"})
|
|
19
|
+
BackgrounDRb::RemoteWorkerError: BackgrounDRb::RemoteWorkerError
|
|
20
|
+
|
|
21
|
+
Above exception is thrown because remote worker's checksum method
|
|
22
|
+
expects an integer as an argument.
|
|
23
|
+
|
|
24
|
+
For asynchronous method calls, BackgrounDRb doesn't check if method
|
|
25
|
+
ran successfully, it only checks existence of methods on remote
|
|
26
|
+
worker. For sync method calls it checks if method ran successfully
|
|
27
|
+
or not.
|
|
28
|
+
|
|
29
|
+
** Its possible to have per worker configuration options now.
|
|
30
|
+
|
|
31
|
+
* Bug Fixes
|
|
32
|
+
|
|
33
|
+
** Much better error/exception handling. Rogue worker methods shouldn't
|
|
34
|
+
crash the worker now. All the unhandled exceptions and dispatch
|
|
35
|
+
errors can be found in debug log file.
|
|
36
|
+
|
|
37
|
+
** Fixes for postgres db with persistent job queues.
|
|
38
|
+
|
|
39
|
+
** Switched to lightweight Queue implementation for tasks enqueued
|
|
40
|
+
to thread pool.
|
|
41
|
+
|
|
42
|
+
** Fixes for database dropped connections while running tasks from
|
|
43
|
+
persistent queues.
|
|
44
|
+
|
|
45
|
+
** Fixes for newer Rails versions.
|
|
46
|
+
|
|
47
|
+
**
|
|
48
|
+
|
data/release_points.org
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
* Before publishing any release, please make sure that following things are working *
|
|
2
|
+
** Scheduling via cron is working
|
|
3
|
+
** Scheduling via unix scheduler is working
|
|
4
|
+
** creating a worker using job_key is working
|
|
5
|
+
** Creating a worker and its scheduling is working
|
|
6
|
+
** ask_work is working => check
|
|
7
|
+
** send_request is working => check
|
|
8
|
+
** all_worker_info is workign
|
|
9
|
+
** status retrieval is working => check
|
|
10
|
+
** sending of large objects around is working
|
|
11
|
+
** sending of rails AR objects is working
|
|
12
|
+
** sending of rails AR objects with plugin is working
|
|
13
|
+
** Environment is getting loaded properly through config file and command line argument.
|
|
14
|
+
** People are unable to call ActionController methods.
|
|
15
|
+
** People should be able to use multiple args while invoking methods.
|
|
16
|
+
** Check if clustering is working.
|
|
17
|
+
** Check if queue is working.
|
|
18
|
+
|
|
19
|
+
* Problem with new experimental API
|
|
20
|
+
** there is an issue if user invokes multiple tasks in thread pool directly from one of the worker
|
|
21
|
+
under current settings they are going to end up with same job key.
|
|
22
|
+
|
|
23
|
+
** Also, new_worker can't have same method invocation conventions because it accepts more parameters.
|
|
24
|
+
|
|
25
|
+
* Ideas
|
|
26
|
+
** Okay, use:
|
|
27
|
+
cache[job_key] = result
|
|
28
|
+
cache[job_key] =>
|
|
29
|
+
will return the result. worker_name, worker_key shall be taken in to account.
|
|
30
|
+
|
|
31
|
+
** Write a Rails controller that returns following information:
|
|
32
|
+
- For each BackgrounDRb server:
|
|
33
|
+
- Workers running
|
|
34
|
+
- Memory taken by them
|
|
35
|
+
- Number of threads in thread pool
|
|
36
|
+
- Number of Tasks in Queue
|
|
37
|
+
- If any worker key
|
|
38
|
+
- Perhaps a count of open file descriptors
|
|
39
|
+
- Scheduled tasks and next turn
|
|
40
|
+
- Overall memory taken by entire BackgrounDRb server.
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
|
data/script/backgroundrb
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
|
|
3
|
+
RAILS_HOME = File.expand_path(File.join(File.dirname(__FILE__),".."))
|
|
4
|
+
|
|
5
|
+
require "rubygems"
|
|
6
|
+
require "active_support"
|
|
7
|
+
require "active_record"
|
|
8
|
+
|
|
9
|
+
require RAILS_HOME + "/config/boot"
|
|
10
|
+
require "backgroundrb"
|
|
11
|
+
|
|
12
|
+
BDRB_HOME = ::BackgrounDRb::BACKGROUNDRB_ROOT
|
|
13
|
+
WORKER_ROOT = File.join(RAILS_HOME,"lib","workers")
|
|
14
|
+
WORKER_LOAD_ENV = File.join(RAILS_HOME,"script","load_worker_env")
|
|
15
|
+
|
|
16
|
+
["server","server/lib","lib","lib/backgroundrb"].each { |x| $LOAD_PATH.unshift(BDRB_HOME + "/#{x}")}
|
|
17
|
+
$LOAD_PATH.unshift(WORKER_ROOT)
|
|
18
|
+
|
|
19
|
+
require "bdrb_config"
|
|
20
|
+
|
|
21
|
+
BDRB_CONFIG = BackgrounDRb::Config.read_config("#{RAILS_HOME}/config/backgroundrb.yml")
|
|
22
|
+
PID_FILE = "#{RAILS_HOME}/tmp/pids/backgroundrb_#{BDRB_CONFIG[:backgroundrb][:port]}.pid"
|
|
23
|
+
SERVER_LOGGER = "#{RAILS_HOME}/log/backgroundrb_debug_#{BDRB_CONFIG[:backgroundrb][:port]}.log"
|
|
24
|
+
|
|
25
|
+
require "bdrb_start_stop"
|
|
26
|
+
daemon = BackgrounDRb::StartStop.new
|
|
27
|
+
|
|
28
|
+
case ARGV[0]
|
|
29
|
+
when 'start'; daemon.start
|
|
30
|
+
when 'stop'; daemon.stop
|
|
31
|
+
when 'restart'; daemon.stop;daemon.start
|
|
32
|
+
when 'status'
|
|
33
|
+
if daemon.running?
|
|
34
|
+
puts "BackgrounDRb Running"
|
|
35
|
+
exit
|
|
36
|
+
else
|
|
37
|
+
puts "BackgrounDRb Not Running"
|
|
38
|
+
exit!(daemon.status)
|
|
39
|
+
end
|
|
40
|
+
else
|
|
41
|
+
require "logger"
|
|
42
|
+
require "packet"
|
|
43
|
+
require "optparse"
|
|
44
|
+
|
|
45
|
+
BackgrounDRb::Config.parse_cmd_options ARGV
|
|
46
|
+
|
|
47
|
+
require RAILS_HOME + "/config/environment"
|
|
48
|
+
require "bdrb_job_queue"
|
|
49
|
+
require "backgroundrb_server"
|
|
50
|
+
BackgrounDRb::MasterProxy.new()
|
|
51
|
+
end
|
|
52
|
+
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
require File.join(File.dirname(__FILE__) + "/../config/environment")
|
|
2
|
+
WORKER_ROOT = RAILS_ROOT + "/lib/workers"
|
|
3
|
+
$LOAD_PATH.unshift(WORKER_ROOT)
|
|
4
|
+
|
|
5
|
+
class Object
|
|
6
|
+
def self.metaclass; class << self; self; end; end
|
|
7
|
+
|
|
8
|
+
def self.iattr_accessor *args
|
|
9
|
+
metaclass.instance_eval do
|
|
10
|
+
attr_accessor *args
|
|
11
|
+
args.each do |attr|
|
|
12
|
+
define_method("set_#{attr}") do |b_value|
|
|
13
|
+
self.send("#{attr}=",b_value)
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
args.each do |attr|
|
|
19
|
+
class_eval do
|
|
20
|
+
define_method(attr) do
|
|
21
|
+
self.class.send(attr)
|
|
22
|
+
end
|
|
23
|
+
define_method("#{attr}=") do |b_value|
|
|
24
|
+
self.class.send("#{attr}=",b_value)
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
module BackgrounDRb
|
|
32
|
+
class WorkerDummyLogger
|
|
33
|
+
%w(info debug fatal error warn).each do |x|
|
|
34
|
+
define_method(x) do |log_data|
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
class WorkData
|
|
40
|
+
attr_accessor :args,:block,:job_method,:persistent_job_id,:job_key
|
|
41
|
+
def initialize(args,job_key,job_method,persistent_job_id)
|
|
42
|
+
@args = args
|
|
43
|
+
@job_key = job_key
|
|
44
|
+
@job_method = job_method
|
|
45
|
+
@persistent_job_id = persistent_job_id
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
class ThreadPool
|
|
50
|
+
attr_accessor :size,:threads,:work_queue,:logger
|
|
51
|
+
attr_accessor :result_queue,:master
|
|
52
|
+
|
|
53
|
+
def initialize(master,size)
|
|
54
|
+
@master = master
|
|
55
|
+
@logger = logger
|
|
56
|
+
@size = size
|
|
57
|
+
@threads = []
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def defer(method_name,args = nil)
|
|
61
|
+
job_key = Thread.current[:job_key]
|
|
62
|
+
persistent_job_id = Thread.current[:persistent_job_id]
|
|
63
|
+
t = WorkData.new(args,job_key,method_name,persistent_job_id)
|
|
64
|
+
result = run_task(t)
|
|
65
|
+
result
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# run tasks popped out of queue
|
|
69
|
+
def run_task task
|
|
70
|
+
block_arity = master.method(task.job_method).arity
|
|
71
|
+
begin
|
|
72
|
+
t_data = task.args
|
|
73
|
+
result = nil
|
|
74
|
+
if block_arity != 0
|
|
75
|
+
result = master.send(task.job_method,task.args)
|
|
76
|
+
else
|
|
77
|
+
result = master.send(task.job_method)
|
|
78
|
+
end
|
|
79
|
+
return result
|
|
80
|
+
rescue
|
|
81
|
+
puts($!.to_s)
|
|
82
|
+
puts($!.backtrace.join("\n"))
|
|
83
|
+
return nil
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
end #end of class ThreadPool
|
|
87
|
+
|
|
88
|
+
class MetaWorker
|
|
89
|
+
attr_accessor :logger,:thread_pool
|
|
90
|
+
iattr_accessor :worker_name
|
|
91
|
+
iattr_accessor :no_auto_load
|
|
92
|
+
|
|
93
|
+
def initialize
|
|
94
|
+
@logger = WorkerDummyLogger.new
|
|
95
|
+
@thread_pool = ThreadPool.new(self,10)
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
|