Skip to content

Commit cd042db

Browse files
committed
Add puma plugin
1 parent 8f7c0a3 commit cd042db

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

Diff for: README.md

+7
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,13 @@ The inline version also works:
171171
<section class="bg-[url('image.svg')]">Has the image as it's background</section>
172172
```
173173

174+
## Puma plugin
175+
We provide a Puma plugin if you want to run the Tailwind watcher together with Puma and have Puma monitor and manage it. You just need to add
176+
```ruby
177+
plugin :tailwindcss
178+
```
179+
to your `puma.rb` configuration.
180+
174181
## License
175182

176183
Tailwind for Rails is released under the [MIT License](https://opensource.org/licenses/MIT).

Diff for: lib/puma/plugin/tailwindcss.rb

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
require "puma/plugin"
2+
3+
Puma::Plugin.create do
4+
attr_reader :puma_pid, :tailwind_pid, :log_writer
5+
6+
def start(launcher)
7+
return unless Rails.env.development?
8+
9+
@log_writer = launcher.log_writer
10+
@puma_pid = $$
11+
@tailwind_pid = fork do
12+
Thread.new { monitor_puma }
13+
system(*Tailwindcss::Commands.watch_command)
14+
end
15+
16+
launcher.events.on_stopped { stop_solid_queue }
17+
18+
in_background do
19+
monitor_tailwind
20+
end
21+
end
22+
23+
private
24+
def stop_solid_queue
25+
Process.waitpid(tailwind_pid, Process::WNOHANG)
26+
log "Stopping tailwind..."
27+
Process.kill(:INT, tailwind_pid) if tailwind_pid
28+
Process.wait(tailwind_pid)
29+
rescue Errno::ECHILD, Errno::ESRCH
30+
end
31+
32+
def monitor_puma
33+
monitor(:puma_dead?, "Detected Puma has gone away, stopping tailwind...")
34+
end
35+
36+
def monitor_tailwind
37+
monitor(:tailwind_dead?, "Detected tailwind has gone away, stopping Puma...")
38+
end
39+
40+
def monitor(process_dead, message)
41+
loop do
42+
if send(process_dead)
43+
log message
44+
Process.kill(:INT, $$)
45+
break
46+
end
47+
sleep 2
48+
end
49+
end
50+
51+
def tailwind_dead?
52+
Process.waitpid(tailwind_pid, Process::WNOHANG)
53+
false
54+
rescue Errno::ECHILD, Errno::ESRCH
55+
true
56+
end
57+
58+
def puma_dead?
59+
Process.ppid != puma_pid
60+
end
61+
62+
def log(...)
63+
log_writer.log(...)
64+
end
65+
end

0 commit comments

Comments
 (0)