Skip to content

Instantly share code, notes, and snippets.

@softr8
softr8 / gist:2bd96fc296505616adff
Created May 9, 2014 19:46
Auto retry 3 times a resque job without any dependency
#it retries only if Timeout is present
module HookTimeoutRetry
def on_failure_retry(e, *args)
key = "AutoRetry:#{self.name}:#{Digest::MD5.hexdigest(Resque.encode(args))}"
counter = Resque.redis.incr(key)
Resque.redis.expire(key, 180)
if [Timeout::Error, Redis::TimeoutError].include?(e.class) && counter <= 3
Rails.logger.info "Performing #{self} caused an exception (#{e}). Retrying..."
Resque.enqueue self, *args
@softr8
softr8 / gist:6a650de7e505126cb082
Created July 31, 2014 20:32
Monitor puma workers
#!/bin/bash
MASTER_PID=`cat /home/deploy/app/shared/sockets/puma.state | grep pid | awk '{print $2}'`
PIDS=`ps auxwww | grep -v $MASTER_PID | grep [p]uma | awk '{print $2}'`
MAX_MEMORY=500000000
for pid in $PIDS
do
MEM_USAGE=`ls -l /proc/$pid/as | awk '{print $5}'`
if [ $MEM_USAGE -gt $MAX_MEMORY ]
then
echo "Memory $MEM_USAGE exceeded $MAX_MEMORY killing $pid"
@softr8
softr8 / gist:d1d6195105ed25facc5d
Created August 27, 2014 23:21
Fonts without CDN in a Rails app
config.action_controller.asset_host = proc do |source|
if source =~ /(ttf|ttc|otf|eot|woff|svg)$/
"https://www.domain.com"
else
"https://cdn#{Digest::MD5.hexdigest(source).to_i(16) % 4 }.domain.com"
end
end
@softr8
softr8 / responders_decorator.rb
Last active August 29, 2015 14:25
Fix spree vulnerability with old versions
#lib/spree/api/responders_decorator.rb
Spree::Api::Responders::AppResponder.class_eval do
def template
options[:default_template]
end
end
@softr8
softr8 / gist:be3a08309dadaf947012
Created September 4, 2015 18:23
Backup github repos locally
require 'github_api'
require 'git'
client = Github.new oauth_token: 'TOKEN', org: 'ORG_NAME'
BASE_DIR ||= '../repos'
i = 1
while true
repos = client.repos.list(page: i)