Skip to content

Commit 355ed62

Browse files
committed
Use ServerProcess class
1 parent 534a840 commit 355ed62

File tree

3 files changed

+101
-10
lines changed

3 files changed

+101
-10
lines changed

lib/tailwindcss-rails.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ module Tailwindcss
55
require_relative "tailwindcss/version"
66
require_relative "tailwindcss/engine"
77
require_relative "tailwindcss/commands"
8+
require_relative "tailwindcss/server_process"

lib/tailwindcss/engine.rb

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,7 @@ class Engine < ::Rails::Engine
1515
end
1616

1717
server do
18-
tailwind_pid = fork do
19-
# To avoid stealing keystrokes from the debug gem's IRB console in the main process (which
20-
# needs to be able to read from $stdin), we use `IO.open(..., 'r+')`.
21-
IO.popen(Tailwindcss::Commands.watch_command, 'r+') do |io|
22-
IO.copy_stream(io, $stdout)
23-
end
24-
end
25-
at_exit do
26-
Process.kill(:INT, tailwind_pid)
27-
end
18+
ServerProcess.start
2819
end
2920
end
3021
end

lib/tailwindcss/server_process.rb

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
module Tailwindcss
2+
class ServerProcess
3+
attr_reader :server, :pid
4+
5+
def self.start
6+
new.start
7+
end
8+
9+
def start
10+
@server = Server.new(self)
11+
@pid = fork do
12+
monitor_server
13+
exit_hook
14+
# Using IO.popen(command, 'r+') will avoid watch_command read from $stdin.
15+
# If we use system(*command) instead, IRB and Debug can't read from $stdin
16+
# correctly bacause some keystrokes will be taken by watch_command.
17+
IO.popen(Commands.watch_command, 'r+') do |io|
18+
IO.copy_stream(io, $stdout)
19+
end
20+
end
21+
Process.detach pid
22+
23+
server.monitor_process
24+
server.exit_hook
25+
end
26+
27+
def stop
28+
return if dead?
29+
30+
Process.kill(:INT, pid)
31+
Process.wait(pid)
32+
rescue Errno::ECHILD, Errno::ESRCH
33+
end
34+
35+
def dead?
36+
Process.wait(pid, Process::WNOHANG)
37+
false
38+
rescue Errno::ECHILD, Errno::ESRCH
39+
true
40+
end
41+
42+
private
43+
44+
def monitor_server
45+
Thread.new do
46+
loop do
47+
if server.dead?
48+
puts "Tailwind detected server has gone away"
49+
exit
50+
end
51+
sleep 2
52+
end
53+
end
54+
end
55+
56+
def exit_hook
57+
at_exit do
58+
puts "Stopping tailwind..."
59+
server.stop
60+
end
61+
end
62+
63+
class Server
64+
attr_reader :process, :pid
65+
66+
def initialize(process)
67+
@process = process
68+
@pid = Process.pid
69+
end
70+
71+
def monitor_process
72+
Thread.new do
73+
loop do
74+
if process.dead?
75+
puts "Detected tailwind has gone away, stopping server..."
76+
exit
77+
end
78+
sleep 2
79+
end
80+
end
81+
end
82+
83+
def exit_hook
84+
at_exit do
85+
process.stop
86+
end
87+
end
88+
89+
def dead?
90+
Process.ppid != pid
91+
end
92+
93+
def stop
94+
Process.kill(:INT, pid)
95+
rescue Errno::ECHILD, Errno::ESRCH
96+
end
97+
end
98+
end
99+
end

0 commit comments

Comments
 (0)