Skip to content

Instantly share code, notes, and snippets.

@benweint
benweint / gist:4370423
Last active December 10, 2015 02:48
Ruby Logger multi-process concurrency test
#!/usr/bin/env ruby
# Ruby Logger multi-process concurrency test.
# In an attempt to answer my own question at:
# http://stackoverflow.com/questions/13908745/can-rubys-stdlib-logger-class-safely-handle-writers-from-multiple-proceses
#
# Every line in each log file should begin with 'W,', and this string should
# appear in no other places in each line. To check results:
#
# grep -l '.W,' *.log
def start_command(queue, pid)
"cd #{current_path} && RAILS_ENV=#{rails_env} QUEUE=\"#{queue}\" \
PIDFILE=#{pid} BACKGROUND=yes VERBOSE=1 NEWRELIC_DISPATCHER=resque INTERVAL=#{interval} \
#{fetch(:bundle_cmd, "bundle")} exec rake resque:work"
end
#!/usr/bin/env ruby
text = "</script></head>\n"
outpath = 'text.txt'
File.open(outpath, 'w') { |f| f.write(text) }
text_read = File.read(outpath)
puts "text == text_read => #{(text == text_read).inspect}"
puts "text.index('</head>') => #{text.index('</head>')}"
#!/usr/bin/env ruby
text = "</</h"
outpath = 'text.txt'
File.open(outpath, 'w') { |f| f.write(text) }
text_read = File.read(outpath)
puts "text == text_read => #{(text == text_read).inspect}"
puts "text.index('</h') => #{text.index('</h')}"
@benweint
benweint / gdb-stuck-ruby.txt
Created April 16, 2013 14:55
An example of how to gather C and Ruby backtraces from a stuck Ruby process using gdb.
# Here's the script I'll use to demonstrate - it just loops forever:
$ cat test.rb
#!/usr/bin/env ruby
loop do
sleep 1
end
# Now, I'll start the script in the background, and redirect stdout and stderr
@benweint
benweint / nrdebug
Last active December 16, 2015 07:39
#!/usr/bin/env ruby
# encoding: utf-8
# This file is distributed under New Relic's license terms.
# See https://github.com/newrelic/rpm/blob/master/LICENSE for complete details.
require 'tempfile'
require 'rbconfig'
def fail(msg, opts={})
#!/usr/bin/env ruby
require 'logger'
logger = Logger.new("/dev/null")
logger_thread = Thread.new do
loop do
logger.debug("hey" * 80)
sleep 0.01
#!/usr/bin/env ruby
require 'logger'
logger = Logger.new("/dev/null")
logger_thread = Thread.new do
loop do
logger.debug("hey" * 80)
sleep 0.01
@benweint
benweint / thread-bt-demo.rb
Last active December 16, 2015 23:38
Demonstration of nil backtraces in Ruby. nil backtraces show up sometimes in MRI. In JRuby-1.7.3, NullPointerExceptions show up for the backtracing thread.
#!/usr/bin/env ruby
Thread.new do
loop do
Thread.list.each do |t|
puts "Got a nil backtrace for thread #{t}" if t.backtrace.nil?
end
end
end
@benweint
benweint / redis-memory-debugging-notes.txt
Last active December 18, 2015 01:28
pdx.rb Redis presentation notes
# Redis memory dump tool
https://github.com/sripathikrishnan/redis-rdb-tools
# Generate dump csv
rdb -c memory dump.rdb >dump.csv
# Filter out unique IDs from Redis key names
time cat dump.csv | ruby -pe '$_.gsub! /:\d+:/, ":"' >keys.csv
# ... or in Perl