Class: Unicorn::Worker

Inherits:
Object
  • Object
show all
Defined in:
lib/unicorn/worker.rb

Overview

This class and its members can be considered a stable interface and will not change in a backwards-incompatible fashion between releases of Unicorn. Knowledge of this class is generally not not needed for most users of Unicorn.

Some users may want to access it in the before_fork/after_fork hooks. See the Unicorn::Configurator RDoc for examples.

Constant Summary collapse

PER_DROP =
Raindrops::PAGE_SIZE / Raindrops::SIZE
DROPS =
[]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(nr) ⇒ Worker

Returns a new instance of Worker.



19
20
21
22
23
24
25
26
# File 'lib/unicorn/worker.rb', line 19

def initialize(nr)
  drop_index = nr / PER_DROP
  @raindrop = DROPS[drop_index] ||= Raindrops.new(PER_DROP)
  @offset = nr % PER_DROP
  @raindrop[@offset] = 0
  @nr = nr
  @tmp = @switched = false
end

Instance Attribute Details

#nrObject

:stopdoc:



13
14
15
# File 'lib/unicorn/worker.rb', line 13

def nr
  @nr
end

#switchedObject

:stopdoc:



13
14
15
# File 'lib/unicorn/worker.rb', line 13

def switched
  @switched
end

#tmpObject

only exists for compatibility



44
45
46
47
48
49
50
# File 'lib/unicorn/worker.rb', line 44

def tmp # :nodoc:
  @tmp ||= begin
    tmp = Unicorn::TmpIO.new
    tmp.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC)
    tmp
  end
end

Instance Method Details

#==(other_nr) ⇒ Object

worker objects may be compared to just plain Integers



29
30
31
# File 'lib/unicorn/worker.rb', line 29

def ==(other_nr) # :nodoc:
  @nr == other_nr
end

#closeObject

:nodoc:



52
53
54
# File 'lib/unicorn/worker.rb', line 52

def close # :nodoc:
  @tmp.close if @tmp
end

#tickObject

called in the master process



39
40
41
# File 'lib/unicorn/worker.rb', line 39

def tick # :nodoc:
  @raindrop[@offset]
end

#tick=(value) ⇒ Object

called in the worker process



34
35
36
# File 'lib/unicorn/worker.rb', line 34

def tick=(value) # :nodoc:
  @raindrop[@offset] = value
end

#user(user, group = nil) ⇒ Object

In most cases, you should be using the Unicorn::Configurator#user directive instead. This method should only be used if you need fine-grained control of exactly when you want to change permissions in your after_fork hooks.

Changes the worker process to the specified user and group This is only intended to be called from within the worker process from the after_fork hook. This should be called in the after_fork hook after any privileged functions need to be run (e.g. to set per-worker CPU affinity, niceness, etc)

Any and all errors raised within this method will be propagated directly back to the caller (usually the after_fork hook. These errors commonly include ArgumentError for specifying an invalid user/group and Errno::EPERM for insufficient privileges



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/unicorn/worker.rb', line 73

def user(user, group = nil)
  # we do not protect the caller, checking Process.euid == 0 is
  # insufficient because modern systems have fine-grained
  # capabilities.  Let the caller handle any and all errors.
  uid = Etc.getpwnam(user).uid
  gid = Etc.getgrnam(group).gid if group
  Unicorn::Util.chown_logs(uid, gid)
  @tmp.chown(uid, gid) if @tmp
  if gid && Process.egid != gid
    Process.initgroups(user, gid)
    Process::GID.change_privilege(gid)
  end
  Process.euid != uid and Process::UID.change_privilege(uid)
  @switched = true
end