Class: Scheduler::Base
- Inherits:
-
Object
- Object
- Scheduler::Base
- Defined in:
- lib/scheduler_daemon/base.rb
Instance Attribute Summary collapse
-
#env_name ⇒ Object
readonly
Returns the value of attribute env_name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#tasks ⇒ Object
readonly
Returns the value of attribute tasks.
Instance Method Summary collapse
-
#initialize(opts = {}, command_line_args = []) ⇒ Base
constructor
:root_dir is a required option, because the scheduler likely has no idea what the original root directory was after the process was daemonized (which changes the current directory) :silent doesn’t output anything to STDOUT or logs.
- #load_rails_env ⇒ Object
- #load_tasks ⇒ Object
- #log(*args) ⇒ Object (also: #puts)
-
#register_task(task) ⇒ Object
registers a task class with the scheduler.
- #run_scheduler ⇒ Object
- #tasks_to_run ⇒ Object
-
#time ⇒ Object
time redefines itself with a faster implementation, since it gets called a lot.
Constructor Details
#initialize(opts = {}, command_line_args = []) ⇒ Base
:root_dir is a required option, because the scheduler likely has no idea what the original
root directory was after the process was daemonized (which changes the current directory)
:silent
doesn't output anything to STDOUT or logs.
:root_dir
root dir of the application.
:only
load only tasks in this list
:except
load all tasks except ones in this list
:env_name
if used without rails, you can manually set something in place of the environment name
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/scheduler_daemon/base.rb', line 34 def initialize(opts = {}, command_line_args = []) @options = ::HashWithIndifferentAccess.new(opts) @options.merge!(CommandLineArgsToHash.parse(command_line_args, :array_args => ['only', 'except'])) @options['only'] ||= [] @options['except'] ||= [] @env_name = @options['env_name'] || 'scheduler' @rufus_scheduler = nil @tasks = [] log("initialized with settings: #{@options.inspect}") if !@options['skip_init'] load_rails_env load_tasks run_scheduler end end |
Instance Attribute Details
#env_name ⇒ Object (readonly)
Returns the value of attribute env_name.
20 21 22 |
# File 'lib/scheduler_daemon/base.rb', line 20 def env_name @env_name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
20 21 22 |
# File 'lib/scheduler_daemon/base.rb', line 20 def @options end |
#tasks ⇒ Object (readonly)
Returns the value of attribute tasks.
20 21 22 |
# File 'lib/scheduler_daemon/base.rb', line 20 def tasks @tasks end |
Instance Method Details
#load_rails_env ⇒ Object
57 58 59 60 61 62 63 64 65 66 |
# File 'lib/scheduler_daemon/base.rb', line 57 def load_rails_env if File.exists?('config/environment.rb') && !@options['skip_rails'] log("loading rails environment") require File.('./config/environment') @env_name = ::Rails.env end rescue log("Error loading rails environment; #{$!.class.name}: #{$!.}") raise $! end |
#load_tasks ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/scheduler_daemon/base.rb', line 118 def load_tasks tasks_to_run.each{|f| begin unless [:only].any? && [:only].all?{|m| f !~ Regexp.new(Regexp.escape(m)) } require File.("./#{f}") filename = f.split('/').last.split('.').first log "Loading task #{filename}..." @tasks << filename.camelcase.constantize # "path/newsfeed_task.rb" => NewsfeedTask end rescue Exception => e msg = "Error loading task #{filename}: #{e.class.name}: #{e.}" log msg log e.backtrace.join("\n") Scheduler::ExceptionHandler.handle_exception(e, nil, msg) end } end |
#log(*args) ⇒ Object Also known as: puts
78 79 80 81 |
# File 'lib/scheduler_daemon/base.rb', line 78 def log(*args) return if @options[:silent] Kernel::puts(%([#{time}] #{args.join("\n")})) end |
#register_task(task) ⇒ Object
registers a task class with the scheduler
53 54 55 |
# File 'lib/scheduler_daemon/base.rb', line 53 def register_task(task) end |
#run_scheduler ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/scheduler_daemon/base.rb', line 84 def run_scheduler if defined?(::Rails) log "Starting Scheduler in #{::Rails.env}" else log "Starting Scheduler" end $daemon_scheduler = self EventMachine::run { @rufus_scheduler = Rufus::Scheduler::EmScheduler.start_new def @rufus_scheduler.handle_exception(job, exception) msg = "[#{env_name}] scheduler job #{job.job_id} (#{job. * ' '}) caught exception #{exception.inspect}" log msg log exception.backtrace.join("\n") Scheduler::ExceptionHandler.handle_exception(exception, job, ) end def @rufus_scheduler.daemon_scheduler $daemon_scheduler end # This is where the magic happens. tasks in scheduled_tasks/*.rb are loaded up. tasks.each do |task| if task.should_run_in_current_environment?(env_name) task.add_to(@rufus_scheduler) else log "[#{env_name}] #{task} not configured to run; skipping." end end } end |
#tasks_to_run ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/scheduler_daemon/base.rb', line 136 def tasks_to_run task_files = Dir[File.join(%w(scheduled_tasks *.rb))]# + File.join(%w(lib scheduled_tasks *.rb)) if [:only].any? task_files.reject!{|f| [:only].all?{|m| f !~ Regexp.new(Regexp.escape(m))}} end if [:except].any? task_files.reject!{|f| [:except].any?{|m| f =~ Regexp.new(Regexp.escape(m))}} end task_files end |
#time ⇒ Object
time redefines itself with a faster implementation, since it gets called a lot.
69 70 71 72 73 74 75 76 |
# File 'lib/scheduler_daemon/base.rb', line 69 def time if Time.respond_to?(:zone) && Time.zone self.class.send(:define_method, :time) { Time.zone.now.to_s } else self.class.send(:define_method, :time) { Time.now.to_s } end time end |