Commit 67a80d07ab1791489e4ef39b7a340613f1da520d

Authored by Rovanion Luckey
Committed by GitLab
1 parent c465636a

Updated the init script, now waits for pids

Showing 2 changed files with 67 additions and 39 deletions   Show diff stats
CHANGELOG
  1 +v 6.3.0
  2 + - Init script now waits for pids to appear after (re)starting before reporting status (Rovanion Luckey)
1 3 v 6.2.0
2 4 - Public project pages are now visible to everyone (files, issues, wik, etc.)
3 5 THIS MEANS YOUR ISSUES AND WIKI FOR PUBLIC PROJECTS ARE PUBLICLY VISIBLE AFTER THE UPGRADE
... ...
lib/support/init.d/gitlab
... ... @@ -20,8 +20,8 @@ RAILS_ENV="production"
20 20  
21 21 # Script variable names should be lower-case not to conflict with internal
22 22 # /bin/sh variables such as PATH, EDITOR or SHELL.
23   -app_root="/home/git/gitlab"
24 23 app_user="git"
  24 +app_root="/home/$app_user/gitlab"
25 25 pid_path="$app_root/tmp/pids"
26 26 socket_path="$app_root/tmp/sockets"
27 27 web_server_pid_path="$pid_path/unicorn.pid"
... ... @@ -44,6 +44,7 @@ fi
44 44  
45 45 ### Init Script functions
46 46  
  47 +## Gets the pids from the files
47 48 check_pids(){
48 49 if ! mkdir -p "$pid_path"; then
49 50 echo "Could not create the path $pid_path needed to store the pids."
... ... @@ -62,12 +63,29 @@ check_pids(){
62 63 fi
63 64 }
64 65  
  66 +## Called when we have started the two processes and are waiting for their pid files.
  67 +wait_for_pids(){
  68 + # We are sleeping a bit here mostly because sidekiq is slow at writing it's pid
  69 + i=0;
  70 + while [ ! -f $web_server_pid_path -o ! -f $sidekiq_pid_path ]; do
  71 + sleep 0.1;
  72 + i=$((i+1))
  73 + if [ $((i%10)) = 0 ]; then
  74 + echo -n "."
  75 + elif [ $((i)) = 301 ]; then
  76 + echo "Waited 30s for the processes to write their pids, something probably went wrong."
  77 + exit 1;
  78 + fi
  79 + done
  80 + echo
  81 +}
  82 +
65 83 # We use the pids in so many parts of the script it makes sense to always check them.
66 84 # Only after start() is run should the pids change. Sidekiq sets it's own pid.
67 85 check_pids
68 86  
69 87  
70   -# Checks whether the different parts of the service are already running or not.
  88 +## Checks whether the different parts of the service are already running or not.
71 89 check_status(){
72 90 check_pids
73 91 # If the web server is running kill -0 $wpid returns true, or rather 0.
... ... @@ -86,7 +104,7 @@ check_status(){
86 104 fi
87 105 }
88 106  
89   -# Check for stale pids and remove them if necessary
  107 +## Check for stale pids and remove them if necessary.
90 108 check_stale_pids(){
91 109 check_status
92 110 # If there is a pid it is something else than 0, the service is running if
... ... @@ -94,7 +112,7 @@ check_stale_pids(){
94 112 if [ "$wpid" != "0" -a "$web_status" != "0" ]; then
95 113 echo "Removing stale Unicorn web server pid. This is most likely caused by the web server crashing the last time it ran."
96 114 if ! rm "$web_server_pid_path"; then
97   - echo "Unable to remove stale pid, exiting"
  115 + echo "Unable to remove stale pid, exiting."
98 116 exit 1
99 117 fi
100 118 fi
... ... @@ -107,7 +125,7 @@ check_stale_pids(){
107 125 fi
108 126 }
109 127  
110   -# If no parts of the service is running, bail out.
  128 +## If no parts of the service is running, bail out.
111 129 exit_if_not_running(){
112 130 check_stale_pids
113 131 if [ "$web_status" != "0" -a "$sidekiq_status" != "0" ]; then
... ... @@ -116,86 +134,92 @@ exit_if_not_running(){
116 134 fi
117 135 }
118 136  
119   -# Starts Unicorn and Sidekiq.
  137 +## Starts Unicorn and Sidekiq if they're not running.
120 138 start() {
121 139 check_stale_pids
122 140  
  141 + if [ "$web_status" != "0" -a "$sidekiq_status" != "0" ]; then
  142 + echo -n "Starting both the GitLab Unicorn and Sidekiq"
  143 + elif [ "$web_status" != "0" ]; then
  144 + echo -n "Starting GitLab Sidekiq"
  145 + elif [ "$sidekiq_status" != "0" ]; then
  146 + echo -n "Starting GitLab Unicorn"
  147 + fi
  148 +
123 149 # Then check if the service is running. If it is: don't start again.
124 150 if [ "$web_status" = "0" ]; then
125 151 echo "The Unicorn web server already running with pid $wpid, not restarting."
126 152 else
127   - echo "Starting the GitLab Unicorn web server..."
128 153 # Remove old socket if it exists
129 154 rm -f "$socket_path"/gitlab.socket 2>/dev/null
130   - # Start the webserver
131   - RAILS_ENV=$RAILS_ENV script/web start
  155 + # Start the web server
  156 + RAILS_ENV=$RAILS_ENV script/web start &
132 157 fi
133 158  
134 159 # If sidekiq is already running, don't start it again.
135 160 if [ "$sidekiq_status" = "0" ]; then
136 161 echo "The Sidekiq job dispatcher is already running with pid $spid, not restarting"
137 162 else
138   - echo "Starting the GitLab Sidekiq event dispatcher..."
139   - RAILS_ENV=$RAILS_ENV script/background_jobs start
140   - # We are sleeping a bit here because sidekiq is slow at writing it's pid
141   - sleep 2
  163 + RAILS_ENV=$RAILS_ENV script/background_jobs start &
142 164 fi
143 165  
  166 + # Wait for the pids to be planted
  167 + wait_for_pids
144 168 # Finally check the status to tell wether or not GitLab is running
145   - status
  169 + print_status
146 170 }
147 171  
148   -# Asks the Unicorn and the Sidekiq if they would be so kind as to stop, if not kills them.
  172 +## Asks the Unicorn and the Sidekiq if they would be so kind as to stop, if not kills them.
149 173 stop() {
150 174 exit_if_not_running
  175 +
  176 + if [ "$web_status" = "0" -a "$sidekiq_status" = "0" ]; then
  177 + echo -n "Shutting down both Unicorn and Sidekiq"
  178 + elif [ "$web_status" = "0" ]; then
  179 + echo -n "Shutting down Sidekiq"
  180 + elif [ "$sidekiq_status" = "0" ]; then
  181 + echo -n "Shutting down Unicorn"
  182 + fi
  183 +
151 184 # If the Unicorn web server is running, tell it to stop;
152 185 if [ "$web_status" = "0" ]; then
153   - RAILS_ENV=$RAILS_ENV script/web stop
154   - echo "Stopping the GitLab Unicorn web server..."
155   - stopping=true
156   - else
157   - echo "The Unicorn web was not running, doing nothing."
  186 + RAILS_ENV=$RAILS_ENV script/web stop
158 187 fi
159 188 # And do the same thing for the Sidekiq.
160 189 if [ "$sidekiq_status" = "0" ]; then
161   - printf "Stopping Sidekiq job dispatcher."
162 190 RAILS_ENV=$RAILS_ENV script/background_jobs stop
163   - stopping=true
164   - else
165   - echo "The Sidekiq was not running, must have run out of breath."
166 191 fi
167 192  
168   -
169 193 # If something needs to be stopped, lets wait for it to stop. Never use SIGKILL in a script.
170   - while [ "$stopping" = "true" ]; do
  194 + while [ "$web_status" = "0" -o "$sidekiq_status" = "0" ]; do
171 195 sleep 1
172 196 check_status
173   - if [ "$web_status" = "0" -o "$sidekiq_status" = "0" ]; then
174   - printf "."
175   - else
  197 + printf "."
  198 + if [ "$web_status" != "0" -a "$sidekiq_status" != "0" ]; then
176 199 printf "\n"
177 200 break
178 201 fi
179 202 done
  203 +
180 204 sleep 1
181 205 # Cleaning up unused pids
182 206 rm "$web_server_pid_path" 2>/dev/null
183 207 # rm "$sidekiq_pid_path" # Sidekiq seems to be cleaning up it's own pid.
184 208  
185   - status
  209 + print_status
186 210 }
187 211  
188   -# Returns the status of GitLab and it's components
189   -status() {
  212 +## Prints the status of GitLab and it's components.
  213 +print_status() {
190 214 check_status
191 215 if [ "$web_status" != "0" -a "$sidekiq_status" != "0" ]; then
192 216 echo "GitLab is not running."
193 217 return
194 218 fi
195 219 if [ "$web_status" = "0" ]; then
196   - echo "The GitLab Unicorn webserver with pid $wpid is running."
  220 + echo "The GitLab Unicorn web server with pid $wpid is running."
197 221 else
198   - printf "The GitLab Unicorn webserver is server is \033[31mnot running\033[0m.\n"33[31mnot runningserver is \033[31mnot running\033[0m.\n"33[0m.\n"
  222 + printf "The GitLab Unicorn web server is server is \033[31mnot running\033[0m.\n"33[31mnot runningserver is \033[31mnot running\033[0m.\n"33[0m.\n"
199 223 fi
200 224 if [ "$sidekiq_status" = "0" ]; then
201 225 echo "The GitLab Sidekiq job dispatcher with pid $spid is running."
... ... @@ -207,6 +231,7 @@ status() {
207 231 fi
208 232 }
209 233  
  234 +## Tells unicorn to reload it's config and Sidekiq to restart
210 235 reload(){
211 236 exit_if_not_running
212 237 if [ "$wpid" = "0" ];then
... ... @@ -218,11 +243,12 @@ reload(){
218 243 echo "Done."
219 244 echo "Restarting GitLab Sidekiq since it isn't capable of reloading its config..."
220 245 RAILS_ENV=$RAILS_ENV script/background_jobs restart
221   - # Waiting 2 seconds for sidekiq to write it.
222   - sleep 2
223   - status
  246 +
  247 + wait_for_pids
  248 + print_status
224 249 }
225 250  
  251 +## Restarts Sidekiq and Unicorn.
226 252 restart(){
227 253 check_status
228 254 if [ "$web_status" = "0" -o "$sidekiq_status" = "0" ]; then
... ... @@ -232,7 +258,7 @@ restart(){
232 258 }
233 259  
234 260  
235   -## Finally the input handling.
  261 +### Finally the input handling.
236 262  
237 263 case "$1" in
238 264 start)
... ... @@ -248,7 +274,7 @@ case "$1" in
248 274 reload
249 275 ;;
250 276 status)
251   - status
  277 + print_status
252 278 ;;
253 279 *)
254 280 echo "Usage: service gitlab {start|stop|restart|reload|status}"
... ...