Skip to content

Instantly share code, notes, and snippets.

View unakatsuo's full-sized avatar

Masahiro Fujiwara unakatsuo

View GitHub Profile
#!/usr/bin/ruby
require 'rubygems'
require 'mq'
EM.run {
amq = MQ.new
amq.topic('evp-started')
amq.topic('evp-stopped')
Digest::SHA1.hexdigest(`/sbin/ip route get 8.8.8.8`.split("\n")[0].split.last)
@unakatsuo
unakatsuo / gist:462692
Created July 3, 2010 16:49
Rails forward action
class ApplicationController < ActionController::Base
helper :all # include all helpers, all the time
protect_from_forgery # See ActionController::RequestForgeryProtection for details
# Scrub sensitive parameters from your log
# filter_parameter_logging :password
protected
#
def forward_action(action_name)
# Ubuntu upstart file at /etc/init/mongodb.conf
start on runlevel [2345]
stop on runlevel [06]
exec /usr/local/mongodb/bin/mongod --config /usr/local/mongodb/mongod.conf
@unakatsuo
unakatsuo / gist:537584
Created August 19, 2010 10:42
fork of s3-bash
#!/bin/bash
# basic amazon s3 operations
# Licensed under the terms of the GNU GPL v2
# Copyright 2007 Victor Lowther <victor.lowther@gmail.com>
# print a message and bail
die() {
echo $*
exit 1
@unakatsuo
unakatsuo / test.rb
Created February 21, 2011 10:16
test implementation of unlimited list iterator plus concurrency.
EM.run {
wq = EM::WorkQueue.new(10)
rqueue = []
push_loop = proc {
wq.push do |sig|
rqueue << 1
EM.add_timer(rand(10) / 100.0){
sig.return
}
end if !wq.closed?
@unakatsuo
unakatsuo / gist:1026755
Created June 15, 2011 09:05
bash retry function
# Run 10 times at most.
# % retry 10 echo "xxx"
#
# Run multiple lines of command in here document.
# % retry 10 <<'_END_'
# echo 1
# echo 2
# _END_
function retry {
@unakatsuo
unakatsuo / gist:1048855
Created June 27, 2011 13:32
hup2term.sh
#!/bin/sh
# This script converts HUP signal from parent shell to TERM signal.
# Terminate the running process which can not be halted with HUP (closing at a GNU screen window).
# Usage:
# hup2term.sh /usr/sbin/nginx -g \'daemon off\;\'
trap 'kill -TERM $wpid;' 1
echo "$*"
@unakatsuo
unakatsuo / gist:1165990
Created August 23, 2011 17:52
Simple state transition check code.
class StateTrack
def initialize
@state = :initialized
end
# Expected state transition:
# :initialized -> :starting -> :running -> :shuttingdown -> :terminated
def process_event(ev, *args)
case [ev, @state]
when [:on_start, :initialized]
@unakatsuo
unakatsuo / gist:1166020
Created August 23, 2011 18:05
Rails session localization tips.
class ApplicationController < ActionController::Base
protect_from_forgery
protected
def local_session
session[params[:controller].to_s] ||= {}
end
def reset_local_session
session.delete(params[:controller].to_s)