Skip to content

Commit cf872d6

Browse files
npezza93flavorjones
authored andcommitted
Add puma plugin
1 parent 7332768 commit cf872d6

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

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. Add
176+
```ruby
177+
plugin :tailwindcss if ENV.fetch("RAILS_ENV", "development") == "development"
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).

lib/puma/plugin/tailwindcss.rb

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

0 commit comments

Comments
 (0)