Skip to content

Instantly share code, notes, and snippets.

@DmytroStepaniuk
Created August 6, 2020 11:36
Show Gist options
  • Save DmytroStepaniuk/2f02cdf9ce2bc576c2ef66f603b1324b to your computer and use it in GitHub Desktop.
Save DmytroStepaniuk/2f02cdf9ce2bc576c2ef66f603b1324b to your computer and use it in GitHub Desktop.
Send info about Puma phased restart - useful to have progress info in slack
def print_worker_index_of index
worker_index = ""
(index + 1).to_s.split('').each do |number|
case number
when "1"
worker_index = worker_index.concat(":one:")
when "2"
worker_index = worker_index.concat(":two:")
when "3"
worker_index = worker_index.concat(":three:")
when "4"
worker_index = worker_index.concat(":four:")
when "5"
worker_index = worker_index.concat(":five:")
when "6"
worker_index = worker_index.concat(":six:")
when "7"
worker_index = worker_index.concat(":seven:")
when "8"
worker_index = worker_index.concat(":eight:")
when "9"
worker_index = worker_index.concat(":nine:")
when "0"
worker_index = worker_index.concat(":zero:")
end
end
return worker_index
end
#
# https://github.com/puma/puma/blob/a6dcb506f285496140bf5802414ea83801b0eb38/docs/signals.md#send-usr2
#
on_worker_shutdown do |index|
begin
require 'dotenv'
if webhook_url = ( ENV['SLACK_SPEAKER__WEBHOOK_URL_FOR_PUMA'] || Dotenv.load.fetch('SLACK_SPEAKER__WEBHOOK_URL_FOR_PUMA') )
require "uri"
require "net/http"
require "json"
data = { text: "#{ print_worker_index_of(index) } :point_right: `#{ @options[:workers] }` :dancer:" }
url = URI(webhook_url)
https = Net::HTTP.new(url.host, url.port);
https.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Accept"] = "application/json"
request["Content-Type"] = ["application/json", "text/plain"]
request.body = JSON.generate(data)
response = https.request(request)
puts "[slack api] => #{ response.read_body }"
end
rescue => e
puts "[slack api] => #{ e.message }"
end
end
#
# https://github.com/puma/puma/blob/a6dcb506f285496140bf5802414ea83801b0eb38/docs/signals.md#send-usr2
#
on_worker_boot do |index|
begin
require 'dotenv'
if webhook_url = ( ENV['SLACK_SPEAKER__WEBHOOK_URL_FOR_PUMA'] || Dotenv.load.fetch('SLACK_SPEAKER__WEBHOOK_URL_FOR_PUMA') )
require "uri"
require "net/http"
require "json"
data = { text: "#{ print_worker_index_of(index) } :point_right: `#{ @options[:workers] }` :ballot_box_with_check:" }
url = URI(webhook_url)
https = Net::HTTP.new(url.host, url.port);
https.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Accept"] = "application/json"
request["Content-Type"] = ["application/json", "text/plain"]
request.body = JSON.generate(data)
response = https.request(request)
puts "[slack api] => #{ response.read_body }"
end
rescue => e
puts "[slack api] => #{ e.message }"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment