Sinatra 0.9.2 reloading based on Rack::Reloader

279 views
Skip to first unread message

Adam Strzelecki

unread,
May 21, 2009, 2:21:37 PM5/21/09
to sina...@googlegroups.com
If you miss old Sinatra 0.9.2 reload? And not really like `shotgun`,
especially when running your Sinatra apps from TextMate, that "just
run ruby <programname.rb>" and doesn't know anything about `shotgun`,
this one is for you, catch:

You might have added:
configure :development { use Sinatra::Reloader }

But it's better if the reloader also clears the routes and all
internal stuff of Sinatra too.

------------------ CUT --------------------
# Reload scripts and reset routes on change
class Sinatra::Reloader < Rack::Reloader
def safe_load(file, mtime, stderr = $stderr)
if file == __FILE__
::Sinatra::Application.reset!
stderr.puts "#{self.class}: reseting routes"
end
super
end
end
------------------ CUT --------------------

So now you do:
configure :development { use Sinatra::Reloader }

And have all your routes, external required files reloaded once the
file changed, which is far better than old reload mechanism or shotgun
that reloads on every request.

Have fun!
--
Adam

Adam Strzelecki

unread,
May 21, 2009, 2:33:45 PM5/21/09
to sina...@googlegroups.com
Ooops.. some errata:

> You might have added:
> configure :development { use Sinatra::Reloader }

I meant you may simply use:
:development { use Rack::Reloader }
but it doesn't clear the routes of Sintra app if main application file
is reloaded.

So it is better to use `Sinatra::Reloader` from my last mail there
that does reset! for Sinatra::Application too.

If you use my Haml template cache mod this solution does NOT reload
the templates, but I'll post later on some Reloader that watches all
*.haml in views and clears the cache when necessary.

Cheers,
--
Adam

Alex Chaffee

unread,
May 21, 2009, 5:53:21 PM5/21/09
to sina...@googlegroups.com
Neat! I hadn't seen Rack::Reloader before. I wrote a script that kills
and restarts the whole application, using code stolen from Rspactor...
The Sinatra::Reloader idea seems cleaner but I can't get it to
restart if I change my app.rb file. Other files reload ok though.

My script is at http://github.com/alexch/vegas (run.rb, "rake run" and
some lib files) if you want to take a look.

---
Alex Chaffee - al...@stinky.com - http://alexch.github.com
Stalk me: http://friendfeed.com/alexch | http://twitter.com/alexch |
http://alexch.tumblr.com

Alex Chaffee

unread,
May 21, 2009, 9:45:19 PM5/21/09
to sina...@googlegroups.com
> I can't get it to restart if I change my app.rb file. Other files reload ok though.

Upgraded to rack 1.0 and it works better now.

Alex Chaffee

unread,
May 21, 2009, 9:54:28 PM5/21/09
to sina...@googlegroups.com
I think you mean this:

class Sinatra::Reloader < Rack::Reloader
def safe_load(file, mtime, stderr = $stderr)
if file == Sinatra::Application.app_file
::Sinatra::Application.reset!
stderr.puts "#{self.class}: reseting routes"
end
super
end
end

That way you can put this class in a lib directory or somewhere other
than directly inside your app file.
On Thu, May 21, 2009 at 11:33 AM, Adam Strzelecki <o...@java.pl> wrote:
>

Adam Strzelecki

unread,
May 22, 2009, 5:09:08 AM5/22/09
to sina...@googlegroups.com
> I think you mean this:
> (...)

> if file == Sinatra::Application.app_file
> ::Sinatra::Application.reset!
> (...)

> That way you can put this class in a lib directory or somewhere other
> than directly inside your app file.

Yeah, exactly, using `app_file` is much better. Thanks for a fix.

Regards,
--
Adam | nanoant.com

Marek Jelen

unread,
May 23, 2009, 8:52:33 AM5/23/09
to sinatrarb
I have a situation, can anyone help ?

Files in main direcory:

Rile reloader.rb
------------------ CUT --------------------
class Sinatra::Reloader < Rack::Reloader
def safe_load(file, mtime, stderr = $stderr)
if file == Sinatra::Application.app_file
::Sinatra::Application.reset!
stderr.puts "#{self.class}: reseting routes"
end
super
end
end
------------------ CUT --------------------

and main.rb file

------------------ CUT --------------------
require 'rubygems'
require 'sinatra'
require 'reloader'

use Rack::Session::Memcache, :key => '_xxxx_session'

configure :development do
use Sinatra::Reloader
end

load 'handlers/root.rb'
------------------ CUT --------------------

and in handlers directory I have

file root.rb
------------------ CUT --------------------
get '/' do
"AAA"
end
------------------ CUT --------------------

however when I change root.rb ... it's not reloaded ... can anyone
help, plese ?

Thank in advance

Marek

Adam Strzelecki

unread,
May 24, 2009, 2:18:14 PM5/24/09
to sina...@googlegroups.com
> and main.rb file
>
> ------------------ CUT --------------------
> (...)

> load 'handlers/root.rb'
> ------------------ CUT --------------------
>
> and in handlers directory I have
>
> file root.rb
> ------------------ CUT --------------------
> get '/' do
> "AAA"
> end
> ------------------ CUT --------------------
>
> however when I change root.rb ... it's not reloaded ... can anyone
> help, plese ?

In this case root.rb is loaded anytime and only if main.rb is
reloaded. Because you use 'load'.

Rack::Reloader (so Sinatra::Reloader) monitors files in
$LOADED_FEATURES for changes, so files that have been 'require'd not
'load'ed.

So instead of using 'load', add your application root to search path
and use 'require':

> require 'handlers/root'


Cheers,
--
Adam

Reply all
Reply to author
Forward
0 new messages