Skip to content

Instantly share code, notes, and snippets.

@justindossey
justindossey / gist:2063267
Created March 17, 2012 17:38
MongoMapper-- check that replicas are within a minute of primary
def all_replicas_in_sync?(verbose=false)
in_sync = true
# connect to the primary
mongo = Mongo::Connection.new(*MongoMapper.connection.primary)
db = mongo.db('local')
last_op=db.collection('oplog.rs').find.sort([['$natural',-1]]).limit(1).to_a[0]
db.collection('slaves').find().to_a.each do |slave|
opdiff = last_op['ts'].increment - slave['syncedTo'].increment
diff = (last_op['ts'].seconds - slave['syncedTo'].seconds)/1000.0
in_sync = false if diff > 60
@ryandotsmith
ryandotsmith / worker-pattern.md
Created April 11, 2012 20:15
The Work Pattern

The Worker Pattern

2011-05-22

Introduction

This article is the accumulation of a tutorial that was given as a training session at Red Dirt Ruby Conf (2011) and a formal talk given at Rails Conf (2011). To understand the theory behind The Worker Pattern, read over the slides and the slide notes. From there, you can follow the instructions in the Tutorial

module Resque
module Plugins
# If you want only one instance of your job queued at a time,
# extend it with this module.
#
# For example:
#
# require 'resque/plugins/zookeeper_lock'
#
# class UpdateNetworkGraph
@rick
rick / github.com.js
Created September 19, 2013 22:04
dotjs file to mute unread github notifications on a repo
$("button.mark-all-as-read").parent().prepend('<button class="box-action mute-all-unread css-truncate tooltipped upwards" original-title="mute all unread notifications"> <span class="octicon octicon-mute"></span> </button>');
$('button.mute-all-unread').click(function(event) {
// mute all unread notifications
$(this).parent().parent().parent().find('li.unread').find('.js-mute-notification button').click();
return false;
});
require 'active_support/all'
require 'active_record'
require 'logger'
ActiveRecord::Base.logger = Logger.new(STDOUT)
spec = { 'test' => {adapter: 'sqlite3', database: ':memory:'},
'test_readonly' => {adapter: 'sqlite3', database: ':memory:'},
'shared_test' => {adapter: 'sqlite3', database: ':memory:'},
'shared_test_readonly' => {adapter: 'sqlite3', database: ':memory:'}
@ryandotsmith
ryandotsmith / process-partitioning.md
Created April 13, 2012 06:40
Process Partitioning

Process Partitioning

The Problem

When working with large, high volume, low latency systems, it is often the case that processing data sequentially becomes detrimental to the system's health. If we only allow 1 process to work on our data we run into several challenges:

  • Our process may fall behind resulting in a situation which it is impossible for our process to catch up.
  • Our singleton process could crash and leave our system in a degraded state.
  • The average latency of data processing could be dramatically affected by outlying cases.
require "raindrops"
class UnicornConnectionMonitor
def initialize(app, options = {})
@app = app
@statsd = options.fetch(:statsd)
end
def call(env)
if linux? && unicorn?
require 'socket'
module EventEmitter
def _callbacks
@_callbacks ||= Hash.new { |h, k| h[k] = [] }
end
def on(type, &blk)
_callbacks[type] << blk
self
@bravoecho
bravoecho / flattener.rb
Created November 28, 2012 13:16
flatten nested hash - recursive vs non-recursive
require 'pry'
data = {
a: {
alfa: "alfa",
beta: "beta"
},
b: 2,
c: {
alfa: {
@jqr
jqr / reader.rb
Created February 24, 2012 01:53
Locking log rotation for processing
require 'fileutils'
class Reader
def self.read(filename, processing_filename)
return [] unless File.exists?(filename)
FileUtils.move(filename, processing_filename)
lines = []
File.open(processing_filename, 'r') do |f|
f.flock(File::LOCK_EX)
while !f.eof?