Skip to content
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

Add How to stop Puma on Heroku using plugins to the example directory #960

Merged
merged 1 commit into from Apr 15, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
46 changes: 46 additions & 0 deletions examples/plugins/redis_stop_puma.rb
@@ -0,0 +1,46 @@
require 'puma/plugin'
require 'redis'

# How to stop Puma on Heroku
# - You can't use normal methods because the dyno is not accessible
# - There's no file system, no way to send signals
# but ...
# - You can use Redis or Memcache; any network distributed key-value
# store

# 1. Add this plugin to your 'lib' directory
# 2. In the `puma.rb` config file add the following lines
# === Plugins ===
# require './lib/puma/plugins/redis_stop_puma'
# plugin 'redis_stop_puma'
# 3. Now, when you set the redis key "puma::restart::web.1", your web.1 dyno
# will restart
# 4. Sniffing the Heroku logs for R14 errors is application (and configuration)
# specific. I use the Logentries service, watch for the pattern and the call
# a webhook back into my app to set the Redis key. YMMV

# You can test this locally by setting the DYNO environment variable when
# when starting puma, e.g. `DYNO=pants.1 puma`

Puma::Plugin.create do
def start(launcher)

hostname = ENV['DYNO']
return unless hostname

redis = Redis.new(url: ENV.fetch('REDIS_URL', nil))
return unless redis.ping == 'PONG'

in_background do
while true
sleep 2
if message = redis.get("puma::restart::#{hostname}")
redis.del("puma::restart::#{hostname}")
$stderr.puts message
launcher.stop
break
end
end
end
end
end