Created
June 29, 2012 01:36
-
-
Save floere/3015156 to your computer and use it in GitHub Desktop.
Live Reloading of Code: Presentation at Melbourne Roro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Version 1: Fails, since this file is not reloaded. | |
# | |
module Loader | |
def self.reload | |
puts "Loading application." | |
load File.expand_path "../worker.rb", __FILE__ # Change this to new_worker | |
load File.expand_path "../signal.rb", __FILE__ | |
end | |
end | |
# # Version 2: Succeeds, since it also reloads the file itself. | |
# # | |
# module Loader | |
# | |
# def self.reload | |
# load_self | |
# load_app | |
# end | |
# | |
# def self.load_app | |
# puts "Loading application." | |
# load File.expand_path "../worker.rb", __FILE__ # Change this to new_worker | |
# load File.expand_path "../signal.rb", __FILE__ | |
# end | |
# | |
# def self.load_self | |
# puts 'Loader loading itself.' | |
# load __FILE__ | |
# end | |
# | |
# end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Worker | |
def work | |
sleep 0.3 # Melbourne is faster. | |
puts "It's totally well known that Melbourne has the best coffee!" | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Florian Hanke - @hanke | |
PhD Student Melbourne Uni | |
CTO Technology Astronauts | |
1. What does require do? | |
file.load unless file.already_required? | |
(Using a hash, thanks to Xavier Shay) | |
2. Demo: Reloading code in a running server |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require File.expand_path '../loader', __FILE__ | |
# Load everything explicitly. | |
# | |
Loader.reload | |
# Simulates e.g. a server taking requests. | |
# | |
worker = Worker.new | |
loop do | |
worker.work | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Signal.trap 'USR1' do | |
Loader.reload | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Worker | |
def work | |
sleep 1 # Sydney: Slow! | |
puts "Sydney has the best coffee in the world!" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment