Class: 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
- #cycle ⇒ Object
- #figure_path(file, paths) ⇒ Object
-
#initialize(app) ⇒ Reloader
constructor
A new instance of Reloader.
- #options_reload ⇒ Object
- #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
Returns a new instance of Reloader.
63 64 65 66 67 68 |
# File 'lib/ramaze/reloader.rb', line 63 def initialize(app) @app = app @files = {} @watcher = Watcher.new end |
Instance Method Details
#call(env) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/ramaze/reloader.rb', line 75 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
91 92 93 94 95 96 97 98 |
# File 'lib/ramaze/reloader.rb', line 91 def cycle before_cycle rotation{|file| @watcher.watch(file) } @watcher.changed_files{|f| safe_load(f) } after_cycle end |
#figure_path(file, paths) ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/ramaze/reloader.rb', line 123 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 ⇒ Object
70 71 72 73 |
# File 'lib/ramaze/reloader.rb', line 70 def @cooldown, @ignore, @control, @thread = OPTIONS.values_at(:cooldown, :ignore, :control, :thread) end |
#rotation ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/ramaze/reloader.rb', line 110 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
101 102 103 104 105 106 107 108 |
# File 'lib/ramaze/reloader.rb', line 101 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 |