Skip to content

Commit

Permalink
Add puma plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
npezza93 authored and flavorjones committed Jan 4, 2024
1 parent 7332768 commit cf872d6
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,13 @@ The inline version also works:
<section class="bg-[url('image.svg')]">Has the image as it's background</section>
```

## Puma plugin
We provide a Puma plugin if you want to run the Tailwind watcher together with Puma and have Puma monitor and manage it. Add
```ruby
plugin :tailwindcss if ENV.fetch("RAILS_ENV", "development") == "development"
```
to your `puma.rb` configuration.

## License

Tailwind for Rails is released under the [MIT License](https://opensource.org/licenses/MIT).
Expand Down
63 changes: 63 additions & 0 deletions lib/puma/plugin/tailwindcss.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
require "puma/plugin"

Puma::Plugin.create do
attr_reader :puma_pid, :tailwind_pid, :log_writer

def start(launcher)
@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_tailwind }

in_background do
monitor_tailwind
end
end

private
def stop_tailwind
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
end

0 comments on commit cf872d6

Please sign in to comment.