Module: GoldenBrindle::Hooks

Included in:
Actions::Start
Defined in:
lib/golden_brindle/hooks.rb

Instance Method Summary collapse

Instance Method Details

#after_forkObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/golden_brindle/hooks.rb', line 11

def after_fork
  lambda do |server, worker|
    defined?(ActiveRecord::Base) and
      ActiveRecord::Base.establish_connection
      # trying to change user and group
    begin
      # check if something not set in config or cli
      unless @user.nil? || @group.nil?
        uid, gid = Process.euid, Process.egid
        user, group = @user, @group
        target_uid = Etc.getpwnam(user).uid
        target_gid = Etc.getgrnam(group).gid
        worker.tmp.chown(target_uid, target_gid)
        if uid != target_uid || gid != target_gid
          Process.initgroups(user, target_gid)
          Process::GID.change_privilege(target_gid)
          Process::UID.change_privilege(target_uid)
        end
      end
    rescue => e
      if ENV['RAILS_ENV'] == 'development'
        STDERR.puts "couldn't change user, oh well"
      else
        raise e
      end
    end
  end
end

#after_reloadObject



58
59
60
61
62
# File 'lib/golden_brindle/hooks.rb', line 58

def after_reload
  lambda do
    ::FileUtils.mkdir_p(%w(cache pids sessions sockets).map! { |d| "tmp/#{d}" })
  end
end

#before_execObject



64
65
66
67
68
# File 'lib/golden_brindle/hooks.rb', line 64

def before_exec
  lambda do |server|
    ENV["BUNDLE_GEMFILE"] = "#{@cwd}/Gemfile" if @bundler
  end
end

#before_forkObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/golden_brindle/hooks.rb', line 40

def before_fork
  lambda do |server, worker|
    defined?(ActiveRecord::Base) and
      ActiveRecord::Base.connection.disconnect!
    # http://rubyenterpriseedition.com/faq.html#adapt_apps_for_cow
    if GC.respond_to?(:copy_on_write_friendly=)
        GC.copy_on_write_friendly = true
    end
    old_pid = "#{server.config[:pid]}.oldbin"
    if File.exists?(old_pid) && server.pid != old_pid
      begin
        Process.kill("QUIT", File.read(old_pid).to_i)
      rescue Errno::ENOENT, Errno::ESRCH
      end
    end
  end
end

#collect_hooksObject



4
5
6
7
8
9
# File 'lib/golden_brindle/hooks.rb', line 4

def collect_hooks
  [:after_fork, :after_reload, :before_fork, :before_exec].inject({}) do |memo, sym|
    memo[sym] = send(sym)
    memo
  end
end