Skip to content

Instantly share code, notes, and snippets.

@jonathanpenn
Created February 17, 2009 06:58

Revisions

  1. jonathanpenn revised this gist Feb 17, 2009. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion server_restarter
    Original file line number Diff line number Diff line change
    @@ -44,7 +44,7 @@ end
    p1 = fork { exec SERVER_COMMAND }

    Signal.trap(0) { Process.kill("INT", p1) }
    Signal.trap("INT") { puts "Shutting down server_restarter"; exit }
    Signal.trap("INT") { $stderr.puts "Shutting down server_restarter"; exit }

    check_for_changes

  2. jonathanpenn revised this gist Feb 17, 2009. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion server_restarter
    Original file line number Diff line number Diff line change
    @@ -38,7 +38,7 @@ def check_for_changes
    end

    def emphasized(m)
    "\e[1;1m\e[41m \e[0m \e[1;1m\e[5m #{m} \e[0m"
    "\e[1;1m\e[41m \e[0m \e[1;1m\e[1m #{m} \e[0m"
    end

    p1 = fork { exec SERVER_COMMAND }
  3. jonathanpenn revised this gist Feb 17, 2009. 1 changed file with 3 additions and 4 deletions.
    7 changes: 3 additions & 4 deletions server_restarter
    Original file line number Diff line number Diff line change
    @@ -43,9 +43,8 @@ end

    p1 = fork { exec SERVER_COMMAND }

    Signal.trap(0) do
    Process.kill("KILL", p1)
    end
    Signal.trap(0) { Process.kill("INT", p1) }
    Signal.trap("INT") { puts "Shutting down server_restarter"; exit }

    check_for_changes

    @@ -56,7 +55,7 @@ loop do
    changes = check_for_changes
    unless changes.empty?
    $stderr.puts emphasized("Changes found...#{changes.inspect}")
    Process.kill("KILL", p1)
    Process.kill("INT", p1)
    p1 = fork { exec SERVER_COMMAND }
    end
    end
  4. jonathanpenn revised this gist Feb 17, 2009. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions server_restarter
    Original file line number Diff line number Diff line change
    @@ -16,6 +16,9 @@ POLL_DIRECTORIES = %w(app config db lib)
    POLL_TIME = 0.9 # In Seconds
    SERVER_COMMAND = "script/server"

    # For passenger:
    # SERVER_COMMAND = "touch tmp/restart.txt"

    Dir.chdir(File.dirname(__FILE__)+"/..")

    @states = {}
  5. jonathanpenn revised this gist Feb 17, 2009. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion server_restarter
    Original file line number Diff line number Diff line change
    @@ -10,7 +10,7 @@
    #
    # To install, just copy it into the scripts directory and set it to be executable.
    #
    # Jonathan Penn (http://www.wavethenavel.com
    # Jonathan Penn (http://www.wavethenavel.com)

    POLL_DIRECTORIES = %w(app config db lib)
    POLL_TIME = 0.9 # In Seconds
  6. jonathanpenn revised this gist Feb 17, 2009. 1 changed file with 12 additions and 0 deletions.
    12 changes: 12 additions & 0 deletions server_restarter
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,16 @@
    #!/usr/bin/env ruby
    #
    # usage: script/server_restarter
    #
    # Rails autoloading, while nice in theory, frequently doesn't work. Since Rails 2.3+
    # is so fast when completely reloading the server, I wrote this script to listen to the
    # given directories, and kill/restart the server when any file is changed.
    #
    # It's quick, simple, and it reliably reloads your application when changes are made.
    #
    # To install, just copy it into the scripts directory and set it to be executable.
    #
    # Jonathan Penn (http://www.wavethenavel.com

    POLL_DIRECTORIES = %w(app config db lib)
    POLL_TIME = 0.9 # In Seconds
  7. jonathanpenn revised this gist Feb 17, 2009. 1 changed file with 2 additions and 3 deletions.
    5 changes: 2 additions & 3 deletions server_restarter
    Original file line number Diff line number Diff line change
    @@ -4,8 +4,7 @@ POLL_DIRECTORIES = %w(app config db lib)
    POLL_TIME = 0.9 # In Seconds
    SERVER_COMMAND = "script/server"

    ROOT_DIR = File.expand_path(File.dirname(__FILE__)+"/..").gsub(" ", "\ ")
    Dir.chdir(ROOT_DIR)
    Dir.chdir(File.dirname(__FILE__)+"/..")

    @states = {}

    @@ -45,4 +44,4 @@ loop do
    Process.kill("KILL", p1)
    p1 = fork { exec SERVER_COMMAND }
    end
    end
    end
  8. jonathanpenn revised this gist Feb 17, 2009. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion server_restarter
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,10 @@

    POLL_DIRECTORIES = %w(app config db lib)
    POLL_TIME = 0.9 # In Seconds
    SERVER_COMMAND = 'script/server'
    SERVER_COMMAND = "script/server"

    ROOT_DIR = File.expand_path(File.dirname(__FILE__)+"/..").gsub(" ", "\ ")
    Dir.chdir(ROOT_DIR)

    @states = {}

  9. jonathanpenn renamed this gist Feb 17, 2009. 1 changed file with 11 additions and 11 deletions.
    22 changes: 11 additions & 11 deletions watch_server → server_restarter
    Original file line number Diff line number Diff line change
    @@ -1,17 +1,14 @@
    #!/usr/bin/env ruby

    # SERVER_COMMAND = 'spin'
    POLL_DIRECTORIES = %w(app config db lib)
    POLL_TIME = 0.9 # In Seconds
    SERVER_COMMAND = 'script/server'

    POLL_TIME = 1.1 # In Seconds

    @states = {}

    def check_for_changes
    changes = []
    Dir.glob("./**/*").reject do |file|
    file =~ %r{^\./(log|public|doc)}
    end.each do |file|
    Dir[*POLL_DIRECTORIES.map{|i|i+"/**/*"}].each do |file|
    unless File.symlink?(file)
    new_time = File.stat(file).mtime
    if @states[file] != new_time
    @@ -23,6 +20,10 @@ def check_for_changes
    changes
    end

    def emphasized(m)
    "\e[1;1m\e[41m \e[0m \e[1;1m\e[5m #{m} \e[0m"
    end

    p1 = fork { exec SERVER_COMMAND }

    Signal.trap(0) do
    @@ -31,15 +32,14 @@ end

    check_for_changes

    $stderr.puts "server_restarter running...watching #{@states.length} files every #{POLL_TIME} seconds."

    loop do
    sleep POLL_TIME
    changes = check_for_changes
    unless changes.empty?
    print "\n\n\nChanges found..."
    p changes
    puts "\n\n"
    $stderr.puts emphasized("Changes found...#{changes.inspect}")
    Process.kill("KILL", p1)
    p1 = fork { exec SERVER_COMMAND }
    end
    end

    end
  10. jonathanpenn created this gist Feb 17, 2009.
    45 changes: 45 additions & 0 deletions watch_server
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,45 @@
    #!/usr/bin/env ruby

    # SERVER_COMMAND = 'spin'
    SERVER_COMMAND = 'script/server'

    POLL_TIME = 1.1 # In Seconds

    @states = {}

    def check_for_changes
    changes = []
    Dir.glob("./**/*").reject do |file|
    file =~ %r{^\./(log|public|doc)}
    end.each do |file|
    unless File.symlink?(file)
    new_time = File.stat(file).mtime
    if @states[file] != new_time
    @states[file] = new_time
    changes << file
    end
    end
    end
    changes
    end

    p1 = fork { exec SERVER_COMMAND }

    Signal.trap(0) do
    Process.kill("KILL", p1)
    end

    check_for_changes

    loop do
    sleep POLL_TIME
    changes = check_for_changes
    unless changes.empty?
    print "\n\n\nChanges found..."
    p changes
    puts "\n\n"
    Process.kill("KILL", p1)
    p1 = fork { exec SERVER_COMMAND }
    end
    end