Skip to content

Instantly share code, notes, and snippets.

@ono
ono / unicorn.conf.rb
Created August 3, 2011 15:49
Workaround to avoid "adding listener failed addr=0.0.0.0:8080 (in use)" error by unicorn on FreeBSD jail host
# If you run unicorn on FreeBSD Jail, you might experience the following error
# when you send some signals such as HUP, USR2 to master process.
# adding listener failed addr=0.0.0.0:8080 (in use)
#
# This snippet gives you a workaround without specifying IP address.
# Returns true if the host is in the Jail.
def in_jail?
`sysctl security.jail.jailed` =~ /security.jail.jailed: 1/ ? true : false
end
@ono
ono / gist:1071604
Created July 8, 2011 11:06
base_job.rb
# BaseJob class is an abstruct class for all jobs.
# The common operations such as newrelic monitoring should be written here.
# Jobs which is inherited from this class have to override instance method of perform instead of the class method to be monitored.
# Please check hello_world_job.rb as an example.
class BaseJob
# include NewRelic::Agent::Instrumentation::ControllerInstrumentation
def self.skip_mode?
$rails_config && $rails_config.app_config && $rails_config.app_config['resque_skip_queue']
end
@ono
ono / gist:1071586
Created July 8, 2011 10:55
retry_sample.rb
def perform(member_course_id, ls_db, retry_count=0)
member_course = nil
begin
member_course = MemberCourse.find(member_course_id)
rescue ActiveRecord::RecordNotFound => e
if retry_count<3
# Chances are that the job is enqueued before the transaction is
# committed. #6511
Resque.enqueue_at(30.seconds.from_now, self.class, member_course_id, ls_db, retry_count+1)
return
require 'formula'
class FileMagic <Formula
url 'ftp://ftp.astron.com//pub/file//file-5.04.tar.gz'
homepage 'http://www.darwinsys.com/file/'
md5 'accade81ff1cc774904b47c72c8aeea0'
def install
system "./configure"
system "make install"
@ono
ono / bday.irb
Created February 3, 2011 08:32
Kosuke's first program. "Dad, how many days until my birthday?"
kaz:~ kazueono$ irb
>> bday = Time.local 2011,3,19
=> Sat Mar 19 00:00:00 +0000 2011
>> bday.yday
=> 78
>> Time.now
=> Thu Feb 03 08:19:58 +0000 2011
>> Time.now.yday
=> 34
>> bday.yday - Time.now.yday
module Maybe
def wrap_block(block)
lambda do |a|
a==nil ? nil : block.call(a)
end
end
end
module MaybeNumeric
def wrap_block(block)
class TryChain
attr_reader :obj
def initialize(obj)
@obj = obj
end
def method_missing(sym,*args,&block)
@obj = @obj.respond_to?(sym) ? @obj.send(sym,*args,&block) : nil
self
A response for:
https://gist.github.com/660194
https://gist.github.com/660968
https://gist.github.com/661951
I don't discuss here whether you shouldn't change the Object class behaviour.
I will write my thoughts on an assumption which it were in ruby standard library.
1. Method name must be shorter at least
# You have to define Gemspec files for Rails 2.3 libraries. A commit bellow will help it:
# http://github.com/jpzwarte/rails/commit/55ede36d8907f481b18fcefa21d099f69ae9965b
# Load rails from github
git 'git://github.com/reallyenglish/rails.git', :branch => '2-3-re', :ref => '1760b049f43ba2ab32eb47fdc42f5638115aeb8a' do
%w(active_support active_record action_pack action_mailer active_resource).each { |lib| gem lib.sub(/_/,''), '2.3.8', :require=>lib}
gem 'rails', '2.3.8'
end
# Load rails from your local
require 'rmagick'
dir = File.dirname(__FILE__)
Dir.glob "#{dir}/**/*.jpg" do |filename|
image = Magick::Image.read(filename)[0]
# note: there is another useful method which you can use to resize and crop square
# image.resize_to_fill(200)