about summary refs log tree commit homepage
path: root/lib/unicorn.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/unicorn.rb')
-rw-r--r--lib/unicorn.rb23
1 files changed, 10 insertions, 13 deletions
diff --git a/lib/unicorn.rb b/lib/unicorn.rb
index aac530b..49435d8 100644
--- a/lib/unicorn.rb
+++ b/lib/unicorn.rb
@@ -54,8 +54,7 @@ module Unicorn
       0 => $0.dup,
     }
 
-    Worker = Struct.new(:nr, :tempfile) unless defined?(Worker)
-    class Worker
+    class Worker < Struct.new(:nr, :tmp)
       # worker objects may be compared to just plain numbers
       def ==(other_nr)
         self.nr == other_nr
@@ -323,7 +322,7 @@ module Unicorn
             self.pid = @pid.chomp('.oldbin') if @pid
             proc_name 'master'
           else
-            worker = WORKERS.delete(pid) and worker.tempfile.close rescue nil
+            worker = WORKERS.delete(pid) and worker.tmp.close rescue nil
             logger.info "reaped #{status.inspect} " \
                         "worker=#{worker.nr rescue 'unknown'}"
           end
@@ -383,16 +382,16 @@ module Unicorn
     end
 
     # forcibly terminate all workers that haven't checked in in @timeout
-    # seconds.  The timeout is implemented using an unlinked tempfile
+    # seconds.  The timeout is implemented using an unlinked File
     # shared between the parent process and each worker.  The worker
-    # runs File#chmod to modify the ctime of the tempfile.  If the ctime
+    # runs File#chmod to modify the ctime of the File.  If the ctime
     # is stale for >@timeout seconds, then we'll kill the corresponding
     # worker.
     def murder_lazy_workers
       diff = stat = nil
       WORKERS.dup.each_pair do |pid, worker|
         stat = begin
-          worker.tempfile.stat
+          worker.tmp.stat
         rescue => e
           logger.warn "worker=#{worker.nr} PID:#{pid} stat error: #{e.inspect}"
           kill_worker(:QUIT, pid)
@@ -416,9 +415,7 @@ module Unicorn
           SIG_QUEUE << :QUIT # forcibly emulate SIGQUIT
           return
         end
-        tempfile = Tempfile.new(nil) # as short as possible to save dir space
-        tempfile.unlink # don't allow other processes to find or see it
-        worker = Worker.new(worker_nr, tempfile)
+        worker = Worker.new(worker_nr, Unicorn::Util.tmpio)
         @before_fork.call(self, worker)
         pid = fork { worker_loop(worker) }
         WORKERS[pid] = worker
@@ -466,10 +463,10 @@ module Unicorn
       proc_name "worker[#{worker.nr}]"
       START_CTX.clear
       init_self_pipe!
-      WORKERS.values.each { |other| other.tempfile.close! rescue nil }
+      WORKERS.values.each { |other| other.tmp.close rescue nil }
       WORKERS.clear
       LISTENERS.each { |sock| sock.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC) }
-      worker.tempfile.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC)
+      worker.tmp.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC)
       @after_fork.call(self, worker) # can drop perms
       @timeout /= 2.0 # halve it for select()
       build_app! unless @preload_app
@@ -489,7 +486,7 @@ module Unicorn
       master_pid = Process.ppid # slightly racy, but less memory usage
       init_worker_process(worker)
       nr = 0 # this becomes negative if we need to reopen logs
-      alive = worker.tempfile # tempfile is our lifeline to the master process
+      alive = worker.tmp # tmp is our lifeline to the master process
       ready = LISTENERS
       t = ti = 0
 
@@ -555,7 +552,7 @@ module Unicorn
       begin
         Process.kill(signal, pid)
       rescue Errno::ESRCH
-        worker = WORKERS.delete(pid) and worker.tempfile.close rescue nil
+        worker = WORKERS.delete(pid) and worker.tmp.close rescue nil
       end
     end