Skip to content

Instantly share code, notes, and snippets.

@cainlevy
cainlevy / with_retries.rb
Created October 28, 2011 21:19
with_retries{} helper to easily catch and retry exceptions in Ruby
module Retriable
# This will catch any exception and retry twice (three tries total):
# with_retries { ... }
#
# This will catch any exception and retry four times (five tries total):
# with_retries(:limit => 5) { ... }
#
# This will catch a specific exception and retry once (two tries total):
# with_retries(Some::Error, :limit => 2) { ... }
#
:+1:
:-1:
:airplane:
:art:
:bear:
:beer:
:bike:
:bomb:
:book:
:bulb:
@shinzui
shinzui / Fast user switching with Devise
Created August 30, 2010 02:50 — forked from barinek/Fast user switching with Devise
Fast user switching with devise
Ever wondered how to login as another user within Devise?
Recently we had a feature request that would provide Admins with the ability
to sign is as another user. You could imagine live demonstrations or even production
support calls where you would like to be signed in as another user, yet not
have to ask for or decrypt their current password. After stewing for a bit, we found a
pretty nice solution with Devise.
Here's the Cucumber feature...
@vlado
vlado / custom_chosen.coffee
Created November 5, 2013 08:40
How to extend Chosen jQuery library example
# 1. Extend Chosen with custom method or owerwrite existing methods
class CustomChosen extends Chosen
show_search_field_default: ->
# ...
# original method updated
# ...
some_new_method: ->
# ...
@dyerc
dyerc / sidekiq
Last active February 27, 2020 10:45
Sidekiq init script
#!/bin/bash
# sidekiq Init script for Sidekiq
# chkconfig: 345 100 75
#
# Description: Starts and Stops Sidekiq message processor for Stratus application.
#
# User-specified exit parameters used in this script:
#
# Exit Code 5 - Incorrect User ID
# Exit Code 6 - Directory not found
@mitio
mitio / deploy.rb
Created August 23, 2013 15:38
Sidekiq + Capistrano + Ubuntu Upstart
# config/deploy.rb
namespace :upstart do
desc 'Generate and upload Upstard configs for daemons needed by the app'
task :update_configs, except: {no_release: true} do
upstart_config_files = File.expand_path('../upstart/*.conf.erb', __FILE__)
upstart_root = '/etc/init'
Dir[upstart_config_files].each do |upstart_config_file|
config = ERB.new(IO.read(upstart_config_file)).result(binding)
@bulljit
bulljit / transmissionUnRAR.sh
Created January 23, 2011 02:31
Transmission-Daemon Script: OnComplete unrar files and delete torrent
#!/bin/sh
DEST_DIR="/some/other/dir"
function linker() { # create simlink to downloaded files, rather than copy to final directory
if [ ! -d "$DEST_DIR/$1" ]; then mkdir "$DEST_DIR/$1"; fi
cd "$1"
for F in *; do
if [ -d "$F" ]; then linker "$1/$F"; fi
ln -s "$TR_TORRENT_DIR/$1/$F" "$DEST_DIR/$1"
@defnull
defnull / gist:1224387
Created September 17, 2011 21:22
Deploy a Bottle app on Heroku
mkdir heroku
cd heroku/
virtualenv --no-site-packages env
source env/bin/activate
pip install bottle gevent
pip freeze > requirements.txt
cat >app.py <<EOF
import bottle
import os
@bigsur0
bigsur0 / parallel_find_in_batches_start_end
Created September 23, 2011 16:53
Parallel gem and find_in_batches with start, end
num_processes = 2
batch_size = 1000
threads_per_process = 10
Parallel.map(0...num_processes, :in_processes => num_processes) do |i|·
User.connection.reconnect!
User.find_in_batches(:batch_size => batch_size,·
:start => (User.count/num_processes) * i,·
:conditions => ["id <= ?", (User.count/num_processes)*(i+1)]) do |batch|·
Parallel.map(batch, :in_threads => threads_per_process) do |record|