Skip to content

Instantly share code, notes, and snippets.

@kplawver
kplawver / beanstalkd_plugin.rb
Created December 7, 2010 14:57
A Scout plugin for monitoring a Beanstalkd. It remembers the current ready and buried jobs and does a crude measure of jobs/second.
class BeanstalkdPlugin < Scout::Plugin
needs 'beanstalk-client'
OPTIONS=<<-EOS
connection_string:
default: localhost:11300
name: Connection String
notes: The host and port to connect to.
EOS
@kplawver
kplawver / cloud_front.rb
Created June 21, 2010 21:52
Simple example for streaming private content from CloudFront
require 'base64'
require 'openssl'
require 'sha1'
# This will generate URLs for streaming private content from CloudFront...
# I followed these instructions for setting up the bucket and CloudFront:
## http://support.rightscale.com/index.php?title=12-Guides/01-RightScale_Dashboard_User_Guide/02-Clouds/01-EC2/11-CloudFront/Serving_Private_Content
# I have a global config hash for my private key for S3:
# The access_key is the CloudFront key id you set up.
@kplawver
kplawver / loadr.rb
Created January 12, 2010 21:16
Helps simplify and automate apache benchmark runs.
## Loader
# Does a pre-configured set of apache benchmark on an array of URLs passed in.
# Yes, it's silly, but it saved me a lot of time today.
#
# Usage: Loadr.test(['http://cnn.com','http://lwvr.net'],"news_and_me")
# - Created a log file for each URL, then fills it with the results of its ab runs.
# - Only works on OS's where ab is in the path.
class Loadr
@@run_sets = [
@kplawver
kplawver / easy_created_at_named_scope.rb
Created December 15, 2009 18:44
Add this to your ActiveRecord models for an easy "find me everything that happened on this day" named scope
# Add this to your ActiveRecord models for an easy "find me everything that happened on this day" named scope:
named_scope :by_date, lambda {|date|
if date.is_a?(String)
date = Time.parse(date)
end
date_start = date.midnight
date_end = date_start.advance(:hours => 24)
{
:conditions => ["created_at between ? and ?",date_start,date_end]
}
@kplawver
kplawver / runner.rb
Created November 3, 2009 16:10
I needed a script/runner equivalent for Sinatra. This works pretty well so far.
#!/usr/bin/env ruby
# If you want to run it on a specific environment, set your RACK_ENV environment variable.
require 'rubygems'
require File.expand_path("../app",__FILE__)
# puts ARGV.options.inspect
if ARGV[0].length > 0 && ARGV[0].is_a?(String)
puts eval(ARGV[0])
@kplawver
kplawver / optimize_tables.rb
Created October 7, 2009 13:06
I was tired of running optimize table by hand, so I wrote something do to it for me.
# Will run optimize table on either just that class's MySQL InnDB table, or on ALL of that connection's tables.
# Works only with MySQL InnoDB tables, but could be used to run repair if you're using MyISAM.
class ActiveRecord::Base
def self.optimize_table
begin
self.connection.execute("optimize table #{self.table_name}")
rescue Exception => e
logger.error("#{self.class_name} - error optimizing #{self.table_name}: #{e}")
else
@kplawver
kplawver / json_callbacks.rb
Created September 24, 2009 13:24
Adds :callback option to to_json for Array, Hash and ActiveRecord::Base.
# Adds :callback option to to_json for Array, Hash and ActiveRecord::Base. Usage:
# user = User.find(:first)
# user.to_json(:callback => "foo")
# => foo({"user":{"name":"The Dude"}})
# There's probably a much better way to do this - would love ideas / improvements!
class Array
alias orig_to_json to_json
def to_json(*a)
callback = nil