Class: FireflyServer

Inherits:
Object
  • Object
show all
Defined in:
lib/firefly_server.rb,
lib/firefly_server/version.rb,
lib/firefly_server/file_watcher.rb,
lib/firefly_server/configuration.rb

Defined Under Namespace

Classes: Configuration, FileWatcher

Constant Summary collapse

VERSION =
"0.1.8"

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ FireflyServer

Returns a new instance of FireflyServer.



8
9
10
11
# File 'lib/firefly_server.rb', line 8

def initialize(params = {})
  self.configuration = Configuration.new
  self.file_watcher = FileWatcher.new(configuration)
end

Instance Method Details

#configure {|configuration| ... } ⇒ Object

Yields:

  • (configuration)


13
14
15
16
# File 'lib/firefly_server.rb', line 13

def configure
  yield(configuration)
  self
end

#process_exists?(pid) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
21
22
23
# File 'lib/firefly_server.rb', line 18

def process_exists?(pid)
  Process.kill(0, pid)
  true
rescue Errno::ESRCH
  false
end

#start!Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/firefly_server.rb', line 25

def start!
  server_pid = nil
  configuration.validate!
  # stop server if it is running
  %x(#{configuration.stop_server})
  # trap signals and exit
  configuration.exit_signals.each do |signal|
    Signal.trap(signal) do
      # attempt to stop server
      if server_pid && process_exists?(server_pid)
        puts "Stopping Server: #{configuration.stop_server}"
        %x(#{configuration.stop_server})
        # give server a moment to exit
        begin
          Timeout.timeout(2) do
            Process.wait(server_pid)
          end
        rescue Timeout::Error
        end
      end
      # reset shell in case of server crash messing with prompt (common byebug problem)
      if server_pid && process_exists?(server_pid)
        %x{reset}
        puts 'Server Stop Failed: Shell was "reset" to ensure access after possible crash'
      end
      puts "\rStopping Firefly Server"
      exit 130
    end
  end
  # file_watcher loop
  file_watcher.watch! do |files|
    puts "Ignored: #{files.ignored.join(", ")}"   if files.ignored?
    puts "Modified: #{files.modified.join(", ")}" if files.modified?
    puts "Added: #{files.added.join(", ")}"       if files.added?
    puts "Removed: #{files.removed.join(", ")}"   if files.removed?
    # stop server
    if files.added? || files.removed?
      puts "Stopping Server: #{configuration.stop_server}"
      %x(#{configuration.stop_server})
    elsif files.modified? && configuration.reload_server && files.modified.map { |f| f.end_with?(".rb") }.uniq == [true]
      puts "Reloading Server: #{configuration.reload_server}"
      ENV["FIREFLY_MODIFIED_FILES"] = files.modified.join(",")
      %x(#{configuration.reload_server})
    elsif files.modified?
      puts "Stopping Server: #{configuration.stop_server}"
      %x(#{configuration.stop_server})
    end
  end
  # on_start callbacks
  configuration.on_start_callbacks.each do |on_start_callback|
    on_start_callback.call(self)
  end
  # server loop
  restart_attempt = 0
  loop do
    # delete stale PID file
    File.delete(configuration.pid_file) if File.file?(configuration.pid_file)
    # start server
    puts "Starting Server: #{configuration.start_server}"
    # new fork will have nil initial pid
    server_pid = fork
    # new fork
    if server_pid == nil
      exec(configuration.start_server)
    else
      Process.wait(server_pid)
      server_pid_status = $? # Process.wait sets $? to pid status
    end
    # normal restart
    if server_pid_status.success?
      restart_attempt = 0
    # failed restart
    else
      restart_attempt += 1
      # throttle restart attempts to prevent high CPU usage
      if restart_attempt >= configuration.restart_attempt_throttle_threshold
        puts "Throttling restart attempts by sleeping #{configuration.restart_attempt_throttle_sleep} seconds"
        sleep(configuration.restart_attempt_throttle_sleep)
      end
    end
  end
end