Class: Cowtech::RubyOnRails::Scheduler
- Inherits:
-
Object
- Object
- Cowtech::RubyOnRails::Scheduler
- Defined in:
- lib/cowtech/scheduler.rb
Instance Attribute Summary collapse
-
#application ⇒ Object
Returns the value of attribute application.
-
#definitions ⇒ Object
Returns the value of attribute definitions.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#pid ⇒ Object
Returns the value of attribute pid.
-
#scheduler ⇒ Object
Returns the value of attribute scheduler.
Class Method Summary collapse
Instance Method Summary collapse
- #execute ⇒ Object
- #execute_inline_task(label, name) ⇒ Object
- #execute_rake_task(label, name, args) ⇒ Object
- #handle_phusion_passenger ⇒ Object
- #handle_plain ⇒ Object
-
#initialize(application_class, log_file, pid_file, block = nil, &definitions) ⇒ Scheduler
constructor
A new instance of Scheduler.
- #log(message, options = {}) ⇒ Object
- #log_string(message, options = {}) ⇒ Object
- #method_missing(method, *args, &block) ⇒ Object
Constructor Details
#initialize(application_class, log_file, pid_file, block = nil, &definitions) ⇒ Scheduler
Returns a new instance of Scheduler.
26 27 28 29 30 31 32 33 |
# File 'lib/cowtech/scheduler.rb', line 26 def initialize(application_class, log_file, pid_file, block = nil, &definitions) @application = application_class @logger = Logger.new(log_file) @pid = pid_file.to_s @definitions = block || definitions @scheduler = Rufus::Scheduler.start_new @rake_loaded = false end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
117 118 119 |
# File 'lib/cowtech/scheduler.rb', line 117 def method_missing(method, *args, &block) self.scheduler.send(method, *args, &block) end |
Instance Attribute Details
#application ⇒ Object
Returns the value of attribute application.
10 11 12 |
# File 'lib/cowtech/scheduler.rb', line 10 def application @application end |
#definitions ⇒ Object
Returns the value of attribute definitions.
13 14 15 |
# File 'lib/cowtech/scheduler.rb', line 13 def definitions @definitions end |
#logger ⇒ Object
Returns the value of attribute logger.
11 12 13 |
# File 'lib/cowtech/scheduler.rb', line 11 def logger @logger end |
#pid ⇒ Object
Returns the value of attribute pid.
12 13 14 |
# File 'lib/cowtech/scheduler.rb', line 12 def pid @pid end |
#scheduler ⇒ Object
Returns the value of attribute scheduler.
14 15 16 |
# File 'lib/cowtech/scheduler.rb', line 14 def scheduler @scheduler end |
Class Method Details
.log(msg) ⇒ Object
20 21 22 23 24 |
# File 'lib/cowtech/scheduler.rb', line 20 def self.log(msg) puts msg #@@class_logger ||= Logger.new(Rails.root.to_s + "/log/scheduler.log") #@@class_logger.debug(msg) end |
.start(application_class, log_file, pid_file, &definitions) ⇒ Object
16 17 18 |
# File 'lib/cowtech/scheduler.rb', line 16 def self.start(application_class, log_file, pid_file, &definitions) self.new(application_class, log_file, pid_file, definitions).execute end |
Instance Method Details
#execute ⇒ Object
78 79 80 81 |
# File 'lib/cowtech/scheduler.rb', line 78 def execute self.log("Scheduler started.", {prefix: "MAIN"}) self.handle_plain end |
#execute_inline_task(label, name) ⇒ Object
68 69 70 71 72 73 74 75 76 |
# File 'lib/cowtech/scheduler.rb', line 68 def execute_inline_task(label, name) begin self.log(label + " ...", {prefix: ["INLINE", "START", name]}) yield if block_given? self.log("Inline task ended.", {prefix: ["INLINE", "END", name]}) rescue Exception => e self.log("Inline task failed with exception: [#{e.class.to_s}] #{e.to_s}.", {prefix: ["RAKE", "ERROR", name]}) end end |
#execute_rake_task(label, name, args) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/cowtech/scheduler.rb', line 51 def execute_rake_task(label, name, args) begin args = args.symbolize_keys args_string = args.present? ? " with arguments #{args.to_json}" : "" task = Rake::Task[name] values = task.arg_names.collect { |a| args[a.to_sym] } self.log(label + args_string + " ...", {prefix: ["RAKE", "START", name]}) task.reenable task.invoke(*values) self.log("Rake task ended.", {prefix: ["RAKE", "END", name]}) rescue Exception => e self.log("Rake task failed with exception: [#{e.class.to_s}] #{e.to_s}.", {prefix: ["RAKE", "ERROR", name]}) end end |
#handle_phusion_passenger ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/cowtech/scheduler.rb', line 83 def handle_phusion_passenger if defined?(PhusionPassenger) then File.delete(@pid) if FileTest.exists?(@pid) PhusionPassenger.on_event(:starting_worker_process) do |forked| if forked && !FileTest.exists?(@pid) then self.log("Starting process with PID #{$$}", {prefix: ["WORKER", "START"]}) File.open(@pid, "w") { |f| f.write($$) } self.handle_plain end end PhusionPassenger.on_event(:stopping_worker_process) do if FileTest.exists?(@pid) then if File.open(@pid, "r") { |f| pid = f.read.to_i} == $$ then self.log("Stopped process with PID #{$$}", {prefix: ["WORKER", "STOP"]}) File.delete(@pid) end end end else self.handle_plain end end |
#handle_plain ⇒ Object
108 109 110 111 112 113 114 115 |
# File 'lib/cowtech/scheduler.rb', line 108 def handle_plain if !@rake_loaded then @application.load_tasks @rake_loaded = true end @definitions.call(self) end |
#log(message, options = {}) ⇒ Object
45 46 47 48 49 |
# File 'lib/cowtech/scheduler.rb', line 45 def log(, = {}) msg = self.log_string(, ) type = [:type] || :info ([:logger] || @logger).send(type, msg) end |
#log_string(message, options = {}) ⇒ Object
35 36 37 38 39 40 41 42 43 |
# File 'lib/cowtech/scheduler.rb', line 35 def log_string(, = {}) rv = "" rv += "[#{Time.now.strftime("%Y-%m-%d %T")}] " if ![:no_time] rv += [:prefix].ensure_array.collect { |p| "[" + p.center(6, " ") + "]" }.join(" ") + " " if [:prefix].present? rv += rv end |