Skip to content

Engine server hook #347

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Use ServerProcess class
  • Loading branch information
bruno- committed Apr 29, 2024
commit 355ed6248b022f34f48f3dff0880732718b6334d
1 change: 1 addition & 0 deletions lib/tailwindcss-rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ module Tailwindcss
require_relative "tailwindcss/version"
require_relative "tailwindcss/engine"
require_relative "tailwindcss/commands"
require_relative "tailwindcss/server_process"
11 changes: 1 addition & 10 deletions lib/tailwindcss/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,7 @@ class Engine < ::Rails::Engine
end

server do
tailwind_pid = fork do
# To avoid stealing keystrokes from the debug gem's IRB console in the main process (which
# needs to be able to read from $stdin), we use `IO.open(..., 'r+')`.
IO.popen(Tailwindcss::Commands.watch_command, 'r+') do |io|
IO.copy_stream(io, $stdout)
end
end
at_exit do
Process.kill(:INT, tailwind_pid)
end
ServerProcess.start
end
end
end
99 changes: 99 additions & 0 deletions lib/tailwindcss/server_process.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
module Tailwindcss
class ServerProcess
attr_reader :server, :pid

def self.start
new.start
end

def start
@server = Server.new(self)
@pid = fork do
monitor_server
exit_hook
# Using IO.popen(command, 'r+') will avoid watch_command read from $stdin.
# If we use system(*command) instead, IRB and Debug can't read from $stdin
# correctly bacause some keystrokes will be taken by watch_command.
IO.popen(Commands.watch_command, 'r+') do |io|
IO.copy_stream(io, $stdout)
end
end
Process.detach pid

server.monitor_process
server.exit_hook
end

def stop
return if dead?

Process.kill(:INT, pid)
Process.wait(pid)
rescue Errno::ECHILD, Errno::ESRCH
end

def dead?
Process.wait(pid, Process::WNOHANG)
false
rescue Errno::ECHILD, Errno::ESRCH
true
end

private

def monitor_server
Thread.new do
loop do
if server.dead?
puts "Tailwind detected server has gone away"
exit
end
sleep 2
end
end
end

def exit_hook
at_exit do
puts "Stopping tailwind..."
server.stop
end
end

class Server
attr_reader :process, :pid

def initialize(process)
@process = process
@pid = Process.pid
end

def monitor_process
Thread.new do
loop do
if process.dead?
puts "Detected tailwind has gone away, stopping server..."
exit
end
sleep 2
end
end
end

def exit_hook
at_exit do
process.stop
end
end

def dead?
Process.ppid != pid
end

def stop
Process.kill(:INT, pid)
rescue Errno::ECHILD, Errno::ESRCH
end
end
end
end