Class: BackgroundQueue::Worker::WorkerLoader
- Inherits:
-
Object
- Object
- BackgroundQueue::Worker::WorkerLoader
- Defined in:
- lib/background_queue/worker/worker_loader.rb
Overview
looks after loading and caching worker classes. worker classes sit in BackgroundQueue::Worker::Config.worker_path directory
Defined Under Namespace
Classes: WorkerEntry
Class Method Summary collapse
Instance Method Summary collapse
- #get_worker(worker_name) ⇒ Object
-
#initialize ⇒ WorkerLoader
constructor
A new instance of WorkerLoader.
- #load_class(worker_name, path) ⇒ Object
- #load_file(path) ⇒ Object
- #load_worker(worker_name) ⇒ Object
- #reload_if_updated(worker_entry) ⇒ Object
- #worker_class_name(worker_name) ⇒ Object
- #worker_path(worker_name) ⇒ Object
Constructor Details
#initialize ⇒ WorkerLoader
Returns a new instance of WorkerLoader.
6 7 8 |
# File 'lib/background_queue/worker/worker_loader.rb', line 6 def initialize @worker_entries = {} end |
Class Method Details
.get_worker(worker_name) ⇒ Object
10 11 12 13 |
# File 'lib/background_queue/worker/worker_loader.rb', line 10 def self.get_worker(worker_name) @@worker_loader ||= BackgroundQueue::Worker::WorkerLoader.new @@worker_loader.get_worker(worker_name) end |
Instance Method Details
#get_worker(worker_name) ⇒ Object
15 16 17 18 19 20 21 22 23 24 |
# File 'lib/background_queue/worker/worker_loader.rb', line 15 def get_worker(worker_name) worker_entry = get(worker_name) if worker_entry.nil? worker_entry = load_worker(worker_name) set(worker_entry) else reload_if_updated(worker_entry) end worker_entry.worker end |
#load_class(worker_name, path) ⇒ Object
38 39 40 41 42 43 44 45 |
# File 'lib/background_queue/worker/worker_loader.rb', line 38 def load_class(worker_name, path) class_name = worker_class_name(worker_name) begin eval("#{class_name}.new") rescue NameError=>e raise "#{path} did not define #{class_name}" end end |
#load_file(path) ⇒ Object
34 35 36 |
# File 'lib/background_queue/worker/worker_loader.rb', line 34 def load_file(path) load(path) end |
#load_worker(worker_name) ⇒ Object
26 27 28 29 30 31 32 |
# File 'lib/background_queue/worker/worker_loader.rb', line 26 def load_worker(worker_name) path = worker_path(worker_name) load_file(path) datestamp = File.mtime(path) worker = load_class(worker_name, path) WorkerEntry.new(worker, path, datestamp, worker_name) end |
#reload_if_updated(worker_entry) ⇒ Object
55 56 57 58 59 60 61 62 |
# File 'lib/background_queue/worker/worker_loader.rb', line 55 def reload_if_updated(worker_entry) ds = File.mtime(worker_entry.path) if ds != worker_entry.datestamp load_file(worker_entry.path) worker_entry.worker = load_class(worker_entry.name, worker_entry.path) worker_entry.datestamp = ds end end |
#worker_class_name(worker_name) ⇒ Object
47 48 49 |
# File 'lib/background_queue/worker/worker_loader.rb', line 47 def worker_class_name(worker_name) worker_name.split('_').collect!{ |w| w.capitalize }.join end |
#worker_path(worker_name) ⇒ Object
51 52 53 |
# File 'lib/background_queue/worker/worker_loader.rb', line 51 def worker_path(worker_name) File.join(BackgroundQueue::Worker::Config.worker_path, "#{worker_name}.rb") end |