-
Notifications
You must be signed in to change notification settings - Fork 177
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
require "puma/plugin" | ||
|
||
Puma::Plugin.create do | ||
attr_reader :puma_pid, :tailwind_pid, :log_writer | ||
|
||
def start(launcher) | ||
return unless development? | ||
|
||
@log_writer = launcher.log_writer | ||
@puma_pid = $$ | ||
@tailwind_pid = fork do | ||
Thread.new { monitor_puma } | ||
system(*Tailwindcss::Commands.watch_command) | ||
end | ||
|
||
launcher.events.on_stopped { stop_solid_queue } | ||
|
||
in_background do | ||
monitor_tailwind | ||
end | ||
end | ||
|
||
private | ||
def stop_solid_queue | ||
Process.waitpid(tailwind_pid, Process::WNOHANG) | ||
log "Stopping tailwind..." | ||
Process.kill(:INT, tailwind_pid) if tailwind_pid | ||
Process.wait(tailwind_pid) | ||
rescue Errno::ECHILD, Errno::ESRCH | ||
end | ||
|
||
def monitor_puma | ||
monitor(:puma_dead?, "Detected Puma has gone away, stopping tailwind...") | ||
end | ||
|
||
def monitor_tailwind | ||
monitor(:tailwind_dead?, "Detected tailwind has gone away, stopping Puma...") | ||
end | ||
|
||
def monitor(process_dead, message) | ||
loop do | ||
if send(process_dead) | ||
log message | ||
Process.kill(:INT, $$) | ||
break | ||
end | ||
sleep 2 | ||
end | ||
end | ||
|
||
def tailwind_dead? | ||
Process.waitpid(tailwind_pid, Process::WNOHANG) | ||
false | ||
rescue Errno::ECHILD, Errno::ESRCH | ||
true | ||
end | ||
|
||
def puma_dead? | ||
Process.ppid != puma_pid | ||
end | ||
|
||
def log(...) | ||
log_writer.log(...) | ||
end | ||
|
||
def development? | ||
(ENV["RAILS_ENV"].presence || ENV["RACK_ENV"].presence || "development") == | ||
"development" | ||
end | ||
end |