Skip to content

Instantly share code, notes, and snippets.

View andrewvc's full-sized avatar

Andrew Cholakian andrewvc

View GitHub Profile
@andrewvc
andrewvc / retriable_redis.rb
Created November 28, 2011 21:55
retriable_redis
class RetriableRedis
# Wraps a redis connection, automatically retrying commands
# if the connection is refused
# Redis across our AWS instances is stable, but AutoSSH isn't
# perfect, this works around that for minor blips
# It'd be great if we consolidate around a single AWS acct. and get
# rid of SSH, but with EY that isn't easy
attr_accessor :connection
tion.rb:724:in `gsub': invalid byte sequence in US-ASCII (ArgumentError)
from /usr/lib/ruby/site_ruby/1.9.1/rubygems/specification.rb:724:in `to_yaml'
from /usr/lib/ruby/site_ruby/1.9.1/rubygems/builder.rb:78:in `block (2 levels) in write_package'
from /usr/lib/ruby/site_ruby/1.9.1/rubygems/package/tar_output.rb:73:in `block (3 levels) in add_gem_contents'
from /usr/lib/ruby/site_ruby/1.9.1/rubygems/package/tar_writer.rb:83:in `new'
from /usr/lib/ruby/site_ruby/1.9.1/rubygems/package/tar_output.rb:67:in `block (2 levels) in add_gem_contents'
from /usr/lib/ruby/site_ruby/1.9.1/rubygems/package/tar_output.rb:65:in `wrap'
from /usr/lib/ruby/site_ruby/1.9.1/rubygems/package/tar_output.rb:65:in `block in add_gem_contents'
from /usr/lib/ruby/site_ruby/1.9.1/rubygems/package/tar_writer.rb:113:in `add_file'
from /usr/lib/ruby/site_ruby/1.9.1/rubygems/package/tar_output.rb:63:in `add_gem_contents'
> h = Hash.new({}); h[1][1] = 'a'; h[2][2] = 'b'; puts h[1]; h
1a2b
=> {}
irb(main):013:0> h
=> {}
irb(main):014:0> h[1]
=> {1=>"a", 2=>"b"}
(defn ^{:connection (atom nil)}
redis-exec
"Executes a redis command with a default connection"
[command]
(let [connection (:connection (meta #'redis-exec))]
(compare-and-set! connection
nil
(redis-client {:host "localhost"}))
(@connection command)))
find images -name "*.jpg" | xargs -n1 -IF echo F F | sed -e 's/.jpg$/_thumb.jpg/' | xargs -n2 echo convert -geometry 200x
@andrewvc
andrewvc / thumbnailer.jpg
Created September 22, 2011 20:34
bashing
find images -name "*.jpg" | xargs -n1 -IF echo F F | sed -e 's/.jpg$/_thumb.jpg/' | xargs -n2 echo convert -geometry 200x
if (addFlagging) {
flaggings.add(flaggingUserId);
} {
flaggings.remove(flaggingUserId);
}
@andrewvc
andrewvc / clockwork.rake
Created July 28, 2011 07:56
Using the Clockwork gem from a rake task
task :clockwork => :environment do
Clockwork.every(10.seconds, "events.update_viewers") {
Resque.enqueue(ViewerCountWorker)
}
Clockwork.run
end
# A typical piece of data
> mixed_data = {"id"=>18929882, "title"=>"Lorem Ipsum", "author"=>"Sit Dolor"}
# JSON stores the data in a reasonable amount of space...
> mixed_data.to_json.length
=> 58
# But messagepack does much better
> MessagePack.pack(mixed_data).length
=> 44
# That's a pretty big difference! But its even more efficient with numbers
> numerical_array = [1223, 2190, 1980092, 8932191892, 98321982189]
@andrewvc
andrewvc / zmq_bind_buffer.rb
Created May 7, 2011 16:54
buffering with devices
require 'ffi-rzmq'
CTX = ZMQ::Context.new 1
class ZBufferedStream
attr_reader :bind_to, :buffer_uri, :ingress
def initialize(name,bind_to)
@bind_to = bind_to
@buffer_uri = "inproc://#{name}"