Class: Ramaze::Reloader
- Inherits:
-
Object
- Object
- Ramaze::Reloader
- Includes:
- Hooks
- Defined in:
- lib/ramaze/reloader.rb,
lib/ramaze/reloader/watch_stat.rb,
lib/ramaze/reloader/watch_inotify.rb
Overview
High performant source reloader
This class acts as Rack middleware.
It does not depend on Ramaze itself, but you might have to adjust the Reloader::Hooks module or include your own module to override the hooks. You also might have to set the Log constant.
Currently, it uses RInotify if available and falls back to using File.stat.
Please note that this will not reload files in the background, it does so only when actively called In case of Ramaze it is performing a check/reload cycle at the start of every request, but also respects a cool down time, during which nothing will be done.
After every reload the OPTIONS hash will be checked for changed options and assigned to the instance, so you may change options during the lifetime of your application.
A number of hooks will be executed during the reload cycle, see Ramaze::ReloaderHooks for more information.
Defined Under Namespace
Modules: Hooks Classes: WatchInotify, WatchStat
Constant Summary collapse
- OPTIONS =
{ # At most check every n seconds # nil/false will never trigger the reload cycle # 0 will cycle on every call :cooldown => 2, # Compiled files cannot be reloaded during runtime :ignore => /\.so$/, # Run cycle in a Thread.exclusive, by default no threads are used. :thread => false, # If you assign a block here it will be instance_evaled instead of # calling cycle. This allows you to use for example EventMachine for # well performing asynchronous cycling. :control => nil, # lambda{ cycle }, }
Instance Method Summary collapse
-
#call(env) ⇒ Object
Allows this class to be called as a Rack middleware.
-
#cycle ⇒ Object
Loops through all the files and reloads all changes files.
-
#figure_path(file, paths) ⇒ String
Tries to find a given file in an array of file paths.
-
#initialize(app) ⇒ Reloader
constructor
Creates a new instance of the class and saves the application it has to watch.
-
#options_reload ⇒ Array
Returns all the options for this class.
- #rotation ⇒ Object
-
#safe_load(file) ⇒ Object
A safe Kernel::load, issuing the hooks depending on the results.
Methods included from Hooks
#after_cycle, #after_safe_load, #after_safe_load_failed, #after_safe_load_succeed, #before_cycle, #before_safe_load
Constructor Details
#initialize(app) ⇒ Reloader
Creates a new instance of the class and saves the application it has to watch.
71 72 73 74 75 76 |
# File 'lib/ramaze/reloader.rb', line 71 def initialize(app) @app = app @files = {} @watcher = Watcher.new end |
Instance Method Details
#call(env) ⇒ Object
Allows this class to be called as a Rack middleware.
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/ramaze/reloader.rb', line 97 def call(env) @watcher.call(@cooldown) do if @control instance_eval(&@control) elsif @thread Thread.exclusive{ cycle } else cycle end end @app.call(env) end |
#cycle ⇒ Object
Loops through all the files and reloads all changes files.
119 120 121 122 123 124 125 126 |
# File 'lib/ramaze/reloader.rb', line 119 def cycle before_cycle rotation{|file| @watcher.watch(file) } @watcher.changed_files{|f| safe_load(f) } after_cycle end |
#figure_path(file, paths) ⇒ String
Tries to find a given file in an array of file paths.
166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/ramaze/reloader.rb', line 166 def figure_path(file, paths) if Pathname.new(file).absolute? return File.exist?(file) ? file : nil end paths.each do |possible_path| full_path = File.join(possible_path, file) return full_path if File.exist?(full_path) end nil end |
#options_reload ⇒ Array
Returns all the options for this class.
85 86 87 88 |
# File 'lib/ramaze/reloader.rb', line 85 def @cooldown, @ignore, @control, @thread = OPTIONS.values_at(:cooldown, :ignore, :control, :thread) end |
#rotation ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/ramaze/reloader.rb', line 144 def rotation files = [$0, __FILE__, *$LOADED_FEATURES].uniq paths = ['./', *$LOAD_PATH].uniq files.each do |file| next if file =~ @ignore if not @files.has_key?(file) and path = figure_path(file, paths) @files[file] = path yield path end end end |
#safe_load(file) ⇒ Object
A safe Kernel::load, issuing the hooks depending on the results
135 136 137 138 139 140 141 142 |
# File 'lib/ramaze/reloader.rb', line 135 def safe_load(file) before_safe_load(file) load(file) after_safe_load_succeed(file) rescue Object => ex Log.error(ex) after_safe_load_failed(file, ex) end |