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 = nil) ⇒ 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 = nil) ⇒ Reloader
Returns a new instance of Reloader.
59 60 61 62 63 64 |
# File 'lib/ramaze/reloader.rb', line 59 def initialize(app = nil) @app = app @files = {} @watcher = Watcher.new end |
Instance Method Details
#call(env) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/ramaze/reloader.rb', line 71 def call(env) @watcher.call(@cooldown) do if @control instance_eval(&@control) elsif @thread Thread.exclusive{ cycle } else cycle end end @app.call(env) if @app end |
#cycle ⇒ Object
87 88 89 90 91 92 93 94 |
# File 'lib/ramaze/reloader.rb', line 87 def cycle before_cycle rotation{|file| @watcher.watch(file) } @watcher.changed_files{|f| safe_load(f) } after_cycle end |
#figure_path(file, paths) ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/ramaze/reloader.rb', line 119 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
66 67 68 69 |
# File 'lib/ramaze/reloader.rb', line 66 def @cooldown, @ignore, @control, @thread = OPTIONS.values_at(:cooldown, :ignore, :control, :thread) end |
#rotation ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/ramaze/reloader.rb', line 106 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
97 98 99 100 101 102 103 104 |
# File 'lib/ramaze/reloader.rb', line 97 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 |