Skip to content

Commit

Permalink
Initial BETA refactoring.
Browse files Browse the repository at this point in the history
rake tasks and script/backgroundrb should be working
  • Loading branch information
mtylty committed Dec 14, 2010
1 parent e75b7f3 commit e3d5f2e
Show file tree
Hide file tree
Showing 15 changed files with 124 additions and 104 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -15,3 +15,4 @@ coverage
pkg
tmp
Gemfile.lock
.DS_Store
4 changes: 2 additions & 2 deletions Gemfile
@@ -1,7 +1,7 @@
source "http://rubygems.org"

gem 'chronic'
gem 'packet'
gem 'chronic', '>= 0.2.3'
gem 'packet', '0.1.15'

group :development do
gem "shoulda", ">= 0"
Expand Down
4 changes: 3 additions & 1 deletion README
Expand Up @@ -15,6 +15,8 @@ Copyright (c) 2007 Hemant Kumar (gethemant [at] gmail.com )

== Usage

Please look into http://backgroundrb.rubyforge.org
Install using bundler via rubygems by adding gem 'backgroundrb-rails3' to your Gemfile

and then look into http://backgroundrb.rubyforge.org for instructions (DISCLAIMER: documentation on rubyforge is for the Rails2 version of backgroundrb)

Something else here.
5 changes: 3 additions & 2 deletions Rakefile
Expand Up @@ -113,15 +113,16 @@ end
require 'jeweler'
Jeweler::Tasks.new do |gem|
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
gem.name = "backgroundrb"
gem.homepage = "http://github.com/mtylty/backgroundrb"
gem.name = "backgroundrb-rails3"
gem.homepage = "http://github.com/mtylty/backgroundrb-rails3"
gem.license = "MIT"
gem.summary = %Q{BackgrounDRb is a Ruby job server and scheduler.}
gem.description = %Q{
BackgrounDRb is a Ruby job server and scheduler. Its main intent is to be used with Ruby on Rails applications for offloading long-running tasks.
Since a Rails application blocks while serving a request it is best to move long-running tasks off into a background process that is divorced from http request/response cycle.}
gem.email = "mtylty@gmail.com"
gem.authors = ["Matteo Latini"]
gem.version = "1.1"
# Include your dependencies below. Runtime dependencies are required when using your gem,
# and development dependencies are only needed for development (ie running rake tasks, tests, etc)
# gem.add_runtime_dependency 'jabber4r', '> 0.1'
Expand Down
24 changes: 1 addition & 23 deletions lib/backgroundrb.rb
@@ -1,23 +1 @@
# Backgroundrb
# FIXME: check if data that we are writing to the socket should end with newline
require "pathname"
require "packet"
require "ostruct"

BACKGROUNDRB_ROOT = Rails.root.realpath.to_s
require "backgroundrb/bdrb_config"
unless defined?(BDRB_CONFIG)
BDRB_CONFIG = BackgrounDRb::Config.read_config("#{BACKGROUNDRB_ROOT}/config/backgroundrb.yml")
end

require "backgroundrb/bdrb_client_helper"
require "backgroundrb/bdrb_job_queue"
require "backgroundrb/bdrb_conn_error"
require "backgroundrb/rails_worker_proxy"
require "backgroundrb/bdrb_connection"
require "backgroundrb/bdrb_cluster_connection"
require "backgroundrb/bdrb_start_stop"
require "backgroundrb/bdrb_result"
MiddleMan = BackgrounDRb::ClusterConnection.new


require "backgroundrb/railtie"
10 changes: 5 additions & 5 deletions lib/backgroundrb/bdrb_cluster_connection.rb
Expand Up @@ -14,7 +14,7 @@ def initialize
@last_polled_time = Time.now
@request_count = 0

initialize_memcache if BDRB_CONFIG[:backgroundrb][:result_storage] == 'memcache'
initialize_memcache if BackgrounDRb::BDRB_CONFIG[:backgroundrb][:result_storage] == 'memcache'
establish_connections
@round_robin = (0...@backend_connections.length).to_a
end
Expand All @@ -31,21 +31,21 @@ def initialize_memcache
:urlencode => false
}
@cache = MemCache.new(memcache_options)
@cache.servers = BDRB_CONFIG[:memcache].split(',')
@cache.servers = BackgrounDRb::BDRB_CONFIG[:memcache].split(',')
end

# initialize all backend server connections
def establish_connections
klass = Struct.new(:ip,:port)
if t_servers = BDRB_CONFIG[:client]
if t_servers = BackgrounDRb::BDRB_CONFIG[:client]
connections = t_servers.split(',')
connections.each do |conn_string|
ip = conn_string.split(':')[0]
port = conn_string.split(':')[1].to_i
@bdrb_servers << klass.new(ip,port)
end
end
@bdrb_servers << klass.new(BDRB_CONFIG[:backgroundrb][:ip],BDRB_CONFIG[:backgroundrb][:port].to_i)
@bdrb_servers << klass.new(BackgrounDRb::BDRB_CONFIG[:backgroundrb][:ip],BackgrounDRb::BDRB_CONFIG[:backgroundrb][:port].to_i)
@bdrb_servers.each_with_index do |connection_info,index|
next if @backend_connections.detect { |x| x.server_info == "#{connection_info.ip}:#{connection_info.port}" }
@backend_connections << Connection.new(connection_info.ip,connection_info.port,self)
Expand Down Expand Up @@ -89,7 +89,7 @@ def find_connection host_info

# find the local configured connection
def find_local
find_connection("#{BDRB_CONFIG[:backgroundrb][:ip]}:#{BDRB_CONFIG[:backgroundrb][:port]}")
find_connection("#{BackgrounDRb::BDRB_CONFIG[:backgroundrb][:ip]}:#{BackgrounDRb::BDRB_CONFIG[:backgroundrb][:port]}")
end

# return the worker proxy
Expand Down
48 changes: 48 additions & 0 deletions lib/backgroundrb/railtie.rb
@@ -0,0 +1,48 @@
# Backgroundrb
# FIXME: check if data that we are writing to the socket should end with newline
require "pathname"
require "packet"
require "ostruct"

require "rails"
require "backgroundrb"

require "backgroundrb/bdrb_config"
require "backgroundrb/bdrb_client_helper"
require "backgroundrb/bdrb_job_queue"
require "backgroundrb/bdrb_conn_error"
require "backgroundrb/rails_worker_proxy"
require "backgroundrb/bdrb_connection"
require "backgroundrb/bdrb_cluster_connection"
require "backgroundrb/bdrb_start_stop"
require "backgroundrb/bdrb_result"


module BackgrounDRb
class Railtie < Rails::Railtie
config.bdrb = ActiveSupport::OrderedOptions.new

unless config.bdrb.has_key? :root
config.bdrb.root = File.expand_path(File.join(__FILE__, '..', '..', '..'))
end
BackgrounDRb::BACKGROUNDRB_ROOT = config.bdrb.root

config.before_configuration do
config_file = "#{Rails.root}/config/backgroundrb.yml"

if File.exists?(config_file) && !config.bdrb.has_key?(:config)
config.bdrb.config = BackgrounDRb::Config.read_config(config_file)
end
BackgrounDRb::BDRB_CONFIG = config.bdrb.config

MiddleMan = BackgrounDRb::ClusterConnection.new if File.exists?(config_file)
end

rake_tasks do
load "tasks/backgroundrb_tasks.rake"
end

end

end

4 changes: 2 additions & 2 deletions lib/generators/backgroundrb/bdrb_migration/USAGE
Expand Up @@ -3,10 +3,10 @@ Description:
Pass the migration name, either CamelCased or under_scored, as an argument.

Example:
./script/generate backgroundrb:bdrb_migration CreateBackgroundrbQueue
rails generate backgroundrb:bdrb_migration BackgroundrbJob

Assuming this is run at 09:00:15h on 12 September, 2008, this will create the
CreateBackgroundrbQueue migration in:

db/migrate/20080912090015_create_backgroundrb_queue.rb
db/migrate/20080912090015_backgroundrb_create_backgroundrb_jobs.rb

@@ -1,27 +1,15 @@
require 'rails/generators/active_record'

module Backgroundrb
module Generators
class BdrbMigrationGenerator < Rails::Generators::NamedBase
class BdrbMigrationGenerator < ActiveRecord::Generators::Base
def self.source_root
@source_root ||= File.expand_path('../templates', __FILE__)
end

def initialize(runtime_args, runtime_options = {})
runtime_args << 'CreateBackgroundrbQueueTable' if runtime_args.empty?
super
end

def manifest
record do |m|
m.migration_template 'migration.rb', 'db/migrate',
:assigns => { :bdrb_table_name => default_bdrb_table_name }
end
def copy_backgroundrb_migration
migration_template "migration.rb", "db/migrate/backgroundrb_create_#{table_name}"
end

protected

def default_bdrb_table_name
ActiveRecord::Base.pluralize_table_names ? 'bdrb_job_queue'.pluralize : 'bdrb_job_queue'
end
end
end
end
@@ -1,6 +1,6 @@
class <%= class_name %> < ActiveRecord::Migration
class BackgroundrbCreate<%= table_name.camelize %> < ActiveRecord::Migration
def self.up
create_table :<%= bdrb_table_name %> do |t|
create_table :<%= table_name %> do |t|
t.column :args, :text
t.column :worker_name, :string
t.column :worker_method, :string
Expand All @@ -22,6 +22,6 @@ def self.up
end

def self.down
drop_table :<%= bdrb_table_name %>
drop_table :<%= table_name %>
end
end
4 changes: 2 additions & 2 deletions lib/generators/backgroundrb/worker/templates/worker.rb
@@ -1,5 +1,5 @@
class <%= class_name %>Worker < BackgrounDRb::MetaWorker
set_worker_name :<%= file_name %>_worker
class <%= file_name.camelize %>Worker < BackgrounDRb::MetaWorker
set_worker_name :<%= file_name.underscore %>_worker
def create(args = nil)
# this method is called, when worker is loaded for the first time
end
Expand Down
16 changes: 3 additions & 13 deletions lib/generators/backgroundrb/worker/worker_generator.rb
Expand Up @@ -5,19 +5,9 @@ def self.source_root
@source_root ||= File.expand_path('../templates', __FILE__)
end

def manifest
record do |m|
# Check for class naming collisions.
m.class_collisions class_path, class_name, "#{class_name}WorkerTest"

# Worker and test directories.
m.directory File.join('lib/workers', class_path)
#m.directory File.join('test/unit', class_path)

# Worker class and unit tests.
m.template 'worker.rb', File.join('lib/workers', class_path, "#{file_name}_worker.rb")
#m.template 'unit_test.rb', File.join('test/unit', class_path, "#{file_name}_worker_test.rb")
end
def copy_backgroundrb_worker
template "worker.rb", "lib/workers/#{file_name}_worker.rb"
#template "unit_test.rb", "test/unit/#{file_name}_worker_test.rb"
end
end
end
Expand Down
40 changes: 22 additions & 18 deletions tasks/backgroundrb_tasks.rake → lib/tasks/backgroundrb_tasks.rake
Expand Up @@ -2,14 +2,14 @@ namespace :backgroundrb do
require 'yaml'
desc 'Setup backgroundrb in your rails application'
task :setup do
script_dest = "#{RAILS_ROOT}/script/backgroundrb"
script_src = File.dirname(__FILE__) + "/../script/backgroundrb"
script_dest = "#{Rails.root}/script/backgroundrb"
script_src = File.dirname(__FILE__) + "/../../script/backgroundrb"

FileUtils.chmod 0774, script_src

defaults = {:backgroundrb => {:ip => '0.0.0.0',:port => 11006 } }

config_dest = "#{RAILS_ROOT}/config/backgroundrb.yml"
config_dest = "#{Rails.root}/config/backgroundrb.yml"

unless File.exists?(config_dest)
puts "Copying backgroundrb.yml config file to #{config_dest}"
Expand All @@ -21,26 +21,31 @@ namespace :backgroundrb do
FileUtils.cp_r(script_src, script_dest)
end

workers_dest = "#{RAILS_ROOT}/lib/workers"
workers_dest = "#{Rails.root}/lib/workers"
unless File.exists?(workers_dest)
puts "Creating #{workers_dest}"
FileUtils.mkdir(workers_dest)
end

test_helper_dest = "#{RAILS_ROOT}/test/bdrb_test_helper.rb"
test_helper_src = File.dirname(__FILE__) + "/../script/bdrb_test_helper.rb"
test_helper_dest = "#{Rails.root}/test/bdrb_test_helper.rb"
test_helper_src = File.dirname(__FILE__) + "/../../script/bdrb_test_helper.rb"
unless File.exists?(test_helper_dest)
puts "Copying Worker Test helper file #{test_helper_dest}"
FileUtils.cp_r(test_helper_src,test_helper_dest)
end

worker_env_loader_dest = "#{RAILS_ROOT}/script/load_worker_env.rb"
worker_env_loader_src = File.join(File.dirname(__FILE__),"..","script","load_worker_env.rb")
worker_env_loader_dest = "#{Rails.root}/script/load_worker_env.rb"
worker_env_loader_src = File.join(File.dirname(__FILE__),"../../","script","load_worker_env.rb")
unless File.exists? worker_env_loader_dest
puts "Copying Worker envionment loader file #{worker_env_loader_dest}"
FileUtils.cp_r(worker_env_loader_src,worker_env_loader_dest)
end

unless File.exists? "#{Rails.root}/tmp/pids"
puts "Creating tmp/pids directory"
Dir.mkdir "#{Rails.root}/tmp/pids"
end

# Generate the migration
Rake::Task['backgroundrb:queue_migration'].invoke
end
Expand All @@ -51,46 +56,45 @@ namespace :backgroundrb do

desc 'update backgroundrb config files from your rails application'
task :update do
temp_scripts = ["backgroundrb","load_worker_env.rb"].map {|x| "#{RAILS_ROOT}/script/#{x}"}
temp_scripts = ["backgroundrb","load_worker_env.rb"].map {|x| "#{Rails.root}/script/#{x}"}
temp_scripts.each do |file_name|
if File.exists?(file_name)
puts "Removing #{file_name} ..."
FileUtils.rm(file_name,:force => true)
end
end
new_temp_scripts = ["backgroundrb","load_worker_env.rb"].map {|x| File.dirname(__FILE__) + "/../script/#{x}" }
new_temp_scripts = ["backgroundrb","load_worker_env.rb"].map {|x| File.dirname(__FILE__) + "/../../script/#{x}" }
new_temp_scripts.each do |file_name|
puts "Updating file #{File.expand_path(file_name)} ..."
FileUtils.cp_r(file_name,"#{RAILS_ROOT}/script/")
FileUtils.cp_r(file_name,"#{Rails.root}/script/")
end
end

desc 'Generate a migration for the backgroundrb queue table. The migration name can be ' +
'specified with the MIGRATION environment variable.'
task :queue_migration => :environment do
raise "Task unavailable to this database (no migration support)" unless ActiveRecord::Base.connection.supports_migrations?
require 'rails_generator'
require 'rails_generator/scripts/generate'
Rails::Generator::Scripts::Generate.new.run(['bdrb_migration', ENV['MIGRATION'] || 'CreateBackgroundrbQueueTable'])
require 'rails/generators'
Rails::Generators::invoke('backgroundrb:bdrb_migration', ENV['MIGRATION'] || 'BackgroundrbJob')
end

desc 'Remove backgroundrb from your rails application'
task :remove do
script_src = "#{RAILS_ROOT}/script/backgroundrb"
temp_scripts = ["backgroundrb","load_worker_env.rb"].map {|x| "#{RAILS_ROOT}/script/#{x}"}
script_src = "#{Rails.root}/script/backgroundrb"
temp_scripts = ["backgroundrb","load_worker_env.rb"].map {|x| "#{Rails.root}/script/#{x}"}

if File.exists?(script_src)
puts "Removing #{script_src} ..."
FileUtils.rm(script_src, :force => true)
end

test_helper_src = "#{RAILS_ROOT}/test/bdrb_test_helper.rb"
test_helper_src = "#{Rails.root}/test/bdrb_test_helper.rb"
if File.exists?(test_helper_src)
puts "Removing backgroundrb test helper.."
FileUtils.rm(test_helper_src,:force => true)
end

workers_dest = "#{RAILS_ROOT}/lib/workers"
workers_dest = "#{Rails.root}/lib/workers"
if File.exists?(workers_dest) && Dir.entries("#{workers_dest}").size == 2
puts "#{workers_dest} is empty...deleting!"
FileUtils.rmdir(workers_dest)
Expand Down

0 comments on commit e3d5f2e

Please sign in to comment.