summary refs log tree commit homepage
path: root/SIGNALS
blob: 48c651ef3acf0302b7ce664d63e484099c97ac31 (plain)
1
2
3
4
5
6
7
8
9
10
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
== Signal handling

In general, signals need only be sent to the master process.  However,
the signals Unicorn uses internally to communicate with the worker
processes are documented here as well.  With the exception of TTIN/TTOU,
signal handling matches the behavior of {nginx}[http://nginx.net/] so it
should be possible to easily share process management scripts between
Unicorn and nginx.

One example init script is distributed with unicorn:
http://unicorn.bogomips.org/examples/init.sh

=== Master Process

* HUP - reloads config file and gracefully restart all workers.
  If the "preload_app" directive is false (the default), then workers
  will also pick up any application code changes when restarted.  If
  "preload_app" is true, then application code changes will have no
  effect; USR2 + QUIT (see below) must be used to load newer code in
  this case.  When reloading the application, +Gem.refresh+ will
  be called so updated code for your application can pick up newly
  installed RubyGems.  It is not recommended that you uninstall
  libraries your application depends on while Unicorn is running,
  as respawned workers may enter a spawn loop when they fail to
  load an uninstalled dependency.

* INT/TERM - quick shutdown, kills all workers immediately

* QUIT - graceful shutdown, waits for workers to finish their
  current request before finishing.

* USR1 - reopen all logs owned by the master and all workers
  See Unicorn::Util.reopen_logs for what is considered a log.

* USR2 - reexecute the running binary.  A separate QUIT
  should be sent to the original process once the child is verified to
  be up and running.

* WINCH - gracefully stops workers but keep the master running.
  This will only work for daemonized processes.

* TTIN - increment the number of worker processes by one

* TTOU - decrement the number of worker processes by one

=== Worker Processes

Sending signals directly to the worker processes should not normally be
needed.  If the master process is running, any exited worker will be
automatically respawned.

* INT/TERM - Quick shutdown, immediately exit.
  Unless WINCH has been sent to the master (or the master is killed),
  the master process will respawn a worker to replace this one.

* QUIT - Gracefully exit after finishing the current request.
  Unless WINCH has been sent to the master (or the master is killed),
  the master process will respawn a worker to replace this one.

* USR1 - Reopen all logs owned by the worker process.
  See Unicorn::Util.reopen_logs for what is considered a log.
  Log files are not reopened until it is done processing
  the current request, so multiple log lines for one request
  (as done by Rails) will not be split across multiple logs.

  It is NOT recommended to send the USR1 signal directly to workers via
  "killall -USR1 unicorn" if you are using user/group-switching support
  in your workers.  You will encounter incorrect file permissions and
  workers will need to be respawned.  Sending USR1 to the master process
  first will ensure logs have the correct permissions before the master
  forwards the USR1 signal to workers.

=== Procedure to replace a running unicorn executable

You may replace a running instance of unicorn with a new one without
losing any incoming connections.  Doing so will reload all of your
application code, Unicorn config, Ruby executable, and all libraries.
The only things that will not change (due to OS limitations) are:

1. The path to the unicorn executable script.  If you want to change to
   a different installation of Ruby, you can modify the shebang
   line to point to your alternative interpreter.

The procedure is exactly like that of nginx:

1. Send USR2 to the master process

2. Check your process manager or pid files to see if a new master spawned
   successfully.  If you're using a pid file, the old process will have
   ".oldbin" appended to its path.  You should have two master instances
   of unicorn running now, both of which will have workers servicing
   requests.  Your process tree should look something like this:

     unicorn master (old)
     \_ unicorn worker[0]
     \_ unicorn worker[1]
     \_ unicorn worker[2]
     \_ unicorn worker[3]
     \_ unicorn master
        \_ unicorn worker[0]
        \_ unicorn worker[1]
        \_ unicorn worker[2]
        \_ unicorn worker[3]

3. You can now send WINCH to the old master process so only the new workers
   serve requests.  If your unicorn process is bound to an interactive
   terminal (not daemonized), you can skip this step.  Step 5 will be more
   difficult but you can also skip it if your process is not daemonized.

4. You should now ensure that everything is running correctly with the
   new workers as the old workers die off.

5. If everything seems ok, then send QUIT to the old master.  You're done!

   If something is broken, then send HUP to the old master to reload
   the config and restart its workers.  Then send QUIT to the new master
   process.