Skip to content

Instantly share code, notes, and snippets.

View dwbutler's full-sized avatar

David Butler dwbutler

View GitHub Profile
@dwbutler
dwbutler / private_protected.rb
Last active September 3, 2018 07:59
Demonstrates behavior of private and protected methods in child classes in Ruby (including some surprising behavior and pitfalls)
# Tested in Ruby 1.8.7
class ParentClass
def call_private_good
# Private methods can only be called without an explicit receiver
private_test
end
def call_private_bad
# Calling a private method with an explicit receiver (self) will fail
@dwbutler
dwbutler / heroku_slug_cleanup.rake
Created August 23, 2018 16:27
Automatically clean up Heroku slug
CLEANUP_THRESHOLD = 250 * 1_024_000 # 250 megabytes
# See https://robots.thoughtbot.com/how-to-reduce-a-large-heroku-compiled-slug-size
desc "Clean up the Heroku repo to reduce compiled slug size"
task cleanup: :set_app do
force = ENV.fetch('FORCE_CLEANUP', false)
if force || slug_size > CLEANUP_THRESHOLD
puts "Heroku compiled slug size exceeds 250 MB. Cleaning up the Heroku repo."
install_heroku_plugin('heroku-repo')
_execute heroku_command("repo:gc")
@dwbutler
dwbutler / post_mortem.md
Created May 18, 2018 05:25
Downtime Post Mortem
  • Downtime window: 4:41 PM EST - 4:55 PM EST
  • Total downtime: 14 minutes

"Bob" (backend engineer) was the developer on call during this incident. All names have been changed to protect the innocent. :)

Summary

Bad code which prevented server startup was deployed to production. It had not been manually tested in a dev environment, but had passed all unit and

@dwbutler
dwbutler / post_mortem.md
Created May 18, 2018 05:15
Postgres Downtime Post Mortem

Written by David. Much thanks to Lukasz for technical and moral support, and helping me grep the Papertrail logs.

All times are in EST.

  • Downtime window: 8:00 AM - 8:25 AM
  • Total downtime: 25 minutes

TL;DR

Heroku PGBackups was running during a database migration, and was unexpectedly holding a lock. Postgres stopped serving any queries until the backup was canceled.

@dwbutler
dwbutler / sync.rake
Last active May 8, 2018 20:54
Sync Heroku prod data to another environment
namespace :sync do
desc "Run production sync"
task run: [
:set_app,
:maint_on,
:scale_down,
:cancel_backup,
:delete_database,
:create_production_fork,
:migrate,
@dwbutler
dwbutler / 1. Running logstash
Last active April 29, 2017 06:05
LogStashLogger TCP downage error handling
$ logstash -f samples/tcp.conf
Settings: Default pipeline workers: 8
Pipeline main started
{"message":"\"2017-04-28T22:44:30.424-07:00\"","@timestamp":"2017-04-29T05:44:30.424Z","@version":"1","severity":"INFO","host":"Davids-MacBook-Pro-6.local","port":62671}
{"message":"\"2017-04-28T22:44:30.528-07:00\"","@timestamp":"2017-04-29T05:44:30.528Z","@version":"1","severity":"INFO","host":"Davids-MacBook-Pro-6.local","port":62671}
{"message":"\"2017-04-28T22:44:30.633-07:00\"","@timestamp":"2017-04-29T05:44:30.633Z","@version":"1","severity":"INFO","host":"Davids-MacBook-Pro-6.local","port":62671}
{"message":"\"2017-04-28T22:44:30.737-07:00\"","@timestamp":"2017-04-29T05:44:30.737Z","@version":"1","severity":"INFO","host":"Davids-MacBook-Pro-6.local","port":62671}
{"message":"\"2017-04-28T22:44:30.842-07:00\"","@timestamp":"2017-04-29T05:44:30.842Z","@version":"1","severity":"INFO","host":"Davids-MacBook-Pro-6.local","port":62671}
{"message":"\"2017-04-28T22:44:30.947-07:00\"","@timestamp":"2017-04-29T05:44:30.9
@dwbutler
dwbutler / constant.rb
Created January 4, 2017 15:37
Check where a constant got changed
class Config
CONSTANT = 1
def CONSTANT=(val)
fail
end
end
config = Config.new
config.CONSTANT = 2
@dwbutler
dwbutler / overwritten.rb
Last active January 4, 2017 04:52
Find where a variable is overwritten with a different value
class Config
attr_accessor :thing
def thing=(val)
raise ArgumentError if defined?(@thing)
@thing = val
end
end
config = Config.new
@dwbutler
dwbutler / db.rake
Created August 28, 2013 22:56 — forked from ches/db.rake
namespace :db do
desc 'Open a MongoDB console with connection parameters for the current Rails.env'
task :console, [:session] => :environment do |t, args|
session_name = args[:session] || :default
config = Mongoid.sessions[session_name]
username = config[:username]
password = config[:password]
database = config[:database]
@dwbutler
dwbutler / Cost of defined? in JRuby.rb
Last active December 29, 2015 19:18
Cost of defined? in JRuby
require 'benchmark'
Benchmark.bm {|x| x.report { 1_000.times { defined?(::UNF::Normalizer) } } }
user system total real
0.300000 0.010000 0.310000 ( 0.340000)