summaryrefslogtreecommitdiff
path: root/_posts/2012-11-07-unicorn-init-script-with-restart-and-upgrade.md
blob: 8823ca6af752f9258506181167b3647c64e2af60 (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
---
layout: post
title: "Unicorn Init Script with restart and upgrade"
description: ""
category: Installation
tags: [Setup]
---
{% include JB/setup %}

##Restarting Seamlessly Unicorn Master Processes

Therefore we use a `before_fork` Proc in the unicorn configuration file.
The `before_fork` Proc object will be called by the master process before forking each worker. 
In our configuration file it kills the master process that belongs to the .oldbin PID. 

Sending the USR2 signal with an init.d script forks the unicorn master process and gives the former process an old pid. 
The process with the old pid gets a QUIT signal from the before_fork code when the new master process starts a new worker process. 
The old master and its workers are able to finish their jobs and end afterwards.


##unicorn configuration file in the application directory
can go to application directory e.g.: `/home/USER/opentox-ruby/APPLICATION/unicorn.rb`
 
<script src="https://gist.github.com/4037704.js"> </script>


##  init.d script to start|stop|restart|force-stop|upgrade the webservice
Script is `/etc/init.d/investigation` . 

<script src="https://gist.github.com/4037667.js"> </script>


## Embed it in a Debian System
* To make it work on system startup add the init.d script (start and stop) to the runlevels defined in the head of the script (Default-Start and Default-Stop) with `sudo update-rc.d APPLICATION defaults` .
* To enable logrotation for the errorlog file add `/etc/logrotate.d/investigation` to the logrotate.d directory with the content:

      /home/investigation/.opentox/log/unicorn-investigation.stderr.log {
        copytruncate
        daily
        rotate 150
        missingok
        create 640 investigation investigation
      }
* To monitor the application with monit add the configuration file: `/etc/monit/conf.d/investigation` to make monit check if the pid-file exists. If not monit starts the service.

      check process investigation with pidfile /home/investigation/.opentox/pid/unicorn-investigation.pid
        stop program = "/etc/init.d/investigation stop"
        start program = "/etc/init.d/investigation start"