Unicorn::Util

Public Class Methods

chown_logs(uid, gid) click to toggle source
    # File lib/unicorn/util.rb, line 31
31:       def chown_logs(uid, gid)
32:         ObjectSpace.each_object(File) do |fp|
33:           fp.chown(uid, gid) if is_log?(fp)
34:         end
35:       end
is_log?(fp) click to toggle source
    # File lib/unicorn/util.rb, line 20
20:       def is_log?(fp)
21:         append_flags = File::WRONLY | File::APPEND
22: 
23:         ! fp.closed? &&
24:           fp.sync &&
25:           fp.path[0] == // &&
26:           (fp.fcntl(Fcntl::F_GETFL) & append_flags) == append_flags
27:         rescue IOError, Errno::EBADF
28:           false
29:       end
reopen_logs() click to toggle source

This reopens ALL logfiles in the process that have been rotated using logrotate(8) (without copytruncate) or similar tools. A File object is considered for reopening if it is:

  1) opened with the O_APPEND and O_WRONLY flags
  2) opened with an absolute path (starts with "/")
  3) the current open file handle does not match its original open path
  4) unbuffered (as far as userspace buffering goes, not O_SYNC)

Returns the number of files reopened

    # File lib/unicorn/util.rb, line 45
45:       def reopen_logs
46:         to_reopen = []
47:         nr = 0
48:         ObjectSpace.each_object(File) { |fp| is_log?(fp) and to_reopen << fp }
49: 
50:         to_reopen.each do |fp|
51:           orig_st = begin
52:             fp.stat
53:           rescue IOError, Errno::EBADF
54:             next
55:           end
56: 
57:           begin
58:             b = File.stat(fp.path)
59:             next if orig_st.ino == b.ino && orig_st.dev == b.dev
60:           rescue Errno::ENOENT
61:           end
62: 
63:           begin
64:             File.open(fp.path, 'a') { |tmpfp| fp.reopen(tmpfp) }
65:             fp.sync = true
66:             new_st = fp.stat
67: 
68:             # this should only happen in the master:
69:             if orig_st.uid != new_st.uid || orig_st.gid != new_st.gid
70:               fp.chown(orig_st.uid, orig_st.gid)
71:             end
72: 
73:             nr += 1
74:           rescue IOError, Errno::EBADF
75:             # not much we can do...
76:           end
77:         end
78: 
79:         nr
80:       end
tmpio() click to toggle source

creates and returns a new File object. The File is unlinked immediately, switched to binary mode, and userspace output buffering is disabled

    # File lib/unicorn/util.rb, line 85
85:       def tmpio
86:         fp = begin
87:           TmpIO.open("#{Dir::tmpdir}/#{rand}",
88:                      File::RDWR|File::CREAT|File::EXCL, 0600)
89:         rescue Errno::EEXIST
90:           retry
91:         end
92:         File.unlink(fp.path)
93:         fp.binmode
94:         fp.sync = true
95:         fp
96:       end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.