Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added background data refresh worker.
- Loading branch information
Showing
8 changed files
with
240 additions
and
0 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
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,4 @@ | ||
--- | ||
:backgroundrb: | ||
:port: 11006 | ||
:ip: 0.0.0.0 |
27 changes: 27 additions & 0 deletions
27
db/migrate/20110219050817_create_backgroundrb_queue_table.rb
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,27 @@ | ||
class CreateBackgroundrbQueueTable < ActiveRecord::Migration | ||
def self.up | ||
create_table :bdrb_job_queues do |t| | ||
t.column :args, :text | ||
t.column :worker_name, :string | ||
t.column :worker_method, :string | ||
t.column :job_key, :string | ||
t.column :taken, :int | ||
t.column :finished, :int | ||
t.column :timeout, :int | ||
t.column :priority, :int | ||
t.column :submitted_at, :datetime | ||
t.column :started_at, :datetime | ||
t.column :finished_at, :datetime | ||
t.column :archived_at, :datetime | ||
t.column :tag, :string | ||
t.column :submitter_info, :string | ||
t.column :runner_info, :string | ||
t.column :worker_key, :string | ||
t.column :scheduled_at, :datetime | ||
end | ||
end | ||
|
||
def self.down | ||
drop_table :bdrb_job_queues | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
class DataWorker < BackgrounDRb::MetaWorker | ||
set_worker_name :data_worker | ||
def create(args = nil) | ||
add_periodic_timer(60) { refresh_data } | ||
end | ||
|
||
def refresh_data | ||
logger.info "Refreshing Data" | ||
begin | ||
Apps4Nsw::DataLoader.refresh_data | ||
logger.info "Refreshing Data Completed" | ||
rescue Exception => e | ||
logger.info "An Error Occured: " + e.to_s | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#!/usr/bin/env ruby | ||
|
||
RAILS_HOME = File.expand_path(File.join(File.dirname(__FILE__),"..")) | ||
|
||
require "rubygems" | ||
require "active_support" | ||
require "active_record" | ||
|
||
require RAILS_HOME + "/config/boot" | ||
require "backgroundrb" | ||
|
||
BDRB_HOME = ::BackgrounDRb::BACKGROUNDRB_ROOT | ||
WORKER_ROOT = File.join(RAILS_HOME,"lib","workers") | ||
WORKER_LOAD_ENV = File.join(RAILS_HOME,"script","load_worker_env") | ||
|
||
["server","server/lib","lib","lib/backgroundrb"].each { |x| $LOAD_PATH.unshift(BDRB_HOME + "/#{x}")} | ||
$LOAD_PATH.unshift(WORKER_ROOT) | ||
|
||
require "bdrb_config" | ||
|
||
BDRB_CONFIG = BackgrounDRb::Config.read_config("#{RAILS_HOME}/config/backgroundrb.yml") | ||
PID_FILE = "#{RAILS_HOME}/tmp/pids/backgroundrb_#{BDRB_CONFIG[:backgroundrb][:port]}.pid" | ||
SERVER_LOGGER = "#{RAILS_HOME}/log/backgroundrb_debug_#{BDRB_CONFIG[:backgroundrb][:port]}.log" | ||
|
||
require "bdrb_start_stop" | ||
daemon = BackgrounDRb::StartStop.new | ||
|
||
case ARGV[0] | ||
when 'start'; daemon.start | ||
when 'stop'; daemon.stop | ||
when 'restart'; daemon.stop;daemon.start | ||
when 'status' | ||
if daemon.running? | ||
puts "BackgrounDRb Running" | ||
exit | ||
else | ||
puts "BackgrounDRb Not Running" | ||
exit!(daemon.status) | ||
end | ||
else | ||
require "logger" | ||
require "packet" | ||
require "optparse" | ||
|
||
BackgrounDRb::Config.parse_cmd_options ARGV | ||
|
||
require RAILS_HOME + "/config/environment" | ||
require "bdrb_job_queue" | ||
require "backgroundrb_server" | ||
BackgrounDRb::MasterProxy.new() | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#!/usr/bin/env ruby | ||
|
||
RAILS_HOME = File.expand_path(File.join(File.dirname(__FILE__),"..")) | ||
|
||
require "rubygems" | ||
require "active_support" | ||
require "active_record" | ||
|
||
require "yaml" | ||
require "erb" | ||
require "logger" | ||
require "optparse" | ||
|
||
require RAILS_HOME + "/config/boot" | ||
require "backgroundrb" | ||
|
||
BDRB_HOME = ::BackgrounDRb::BACKGROUNDRB_ROOT | ||
|
||
["server","server/lib","lib","lib/backgroundrb"].each { |x| $LOAD_PATH.unshift(BDRB_HOME + "/#{x}")} | ||
|
||
$LOAD_PATH.unshift(File.join(RAILS_HOME,"lib","workers")) | ||
|
||
require "bdrb_config" | ||
|
||
BDRB_CONFIG = BackgrounDRb::Config.read_config("#{RAILS_HOME}/config/backgroundrb.yml") | ||
|
||
if !(::Packet::WorkerRunner::WORKER_OPTIONS[:worker_env] == false) | ||
require RAILS_HOME + "/config/environment" | ||
end | ||
require "backgroundrb_server" | ||
|
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,99 @@ | ||
require File.join(File.dirname(__FILE__) + "/../config/environment") | ||
WORKER_ROOT = RAILS_ROOT + "/lib/workers" | ||
$LOAD_PATH.unshift(WORKER_ROOT) | ||
|
||
class Object | ||
def self.metaclass; class << self; self; end; end | ||
|
||
def self.iattr_accessor *args | ||
metaclass.instance_eval do | ||
attr_accessor *args | ||
args.each do |attr| | ||
define_method("set_#{attr}") do |b_value| | ||
self.send("#{attr}=",b_value) | ||
end | ||
end | ||
end | ||
|
||
args.each do |attr| | ||
class_eval do | ||
define_method(attr) do | ||
self.class.send(attr) | ||
end | ||
define_method("#{attr}=") do |b_value| | ||
self.class.send("#{attr}=",b_value) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
module BackgrounDRb | ||
class WorkerDummyLogger | ||
%w(info debug fatal error warn).each do |x| | ||
define_method(x) do |log_data| | ||
end | ||
end | ||
end | ||
|
||
class WorkData | ||
attr_accessor :args,:block,:job_method,:persistent_job_id,:job_key | ||
def initialize(args,job_key,job_method,persistent_job_id) | ||
@args = args | ||
@job_key = job_key | ||
@job_method = job_method | ||
@persistent_job_id = persistent_job_id | ||
end | ||
end | ||
|
||
class ThreadPool | ||
attr_accessor :size,:threads,:work_queue,:logger | ||
attr_accessor :result_queue,:master | ||
|
||
def initialize(master,size) | ||
@master = master | ||
@logger = logger | ||
@size = size | ||
@threads = [] | ||
end | ||
|
||
def defer(method_name,args = nil) | ||
job_key = Thread.current[:job_key] | ||
persistent_job_id = Thread.current[:persistent_job_id] | ||
t = WorkData.new(args,job_key,method_name,persistent_job_id) | ||
result = run_task(t) | ||
result | ||
end | ||
|
||
# run tasks popped out of queue | ||
def run_task task | ||
block_arity = master.method(task.job_method).arity | ||
begin | ||
t_data = task.args | ||
result = nil | ||
if block_arity != 0 | ||
result = master.send(task.job_method,task.args) | ||
else | ||
result = master.send(task.job_method) | ||
end | ||
return result | ||
rescue | ||
puts($!.to_s) | ||
puts($!.backtrace.join("\n")) | ||
return nil | ||
end | ||
end | ||
end #end of class ThreadPool | ||
|
||
class MetaWorker | ||
attr_accessor :logger,:thread_pool | ||
iattr_accessor :worker_name | ||
iattr_accessor :no_auto_load | ||
|
||
def initialize | ||
@logger = WorkerDummyLogger.new | ||
@thread_pool = ThreadPool.new(self,10) | ||
end | ||
end | ||
end | ||
|