Skip to content

Commit e681801

Browse files
author
Tobias Richter
authored
Merge pull request #16 from wcm-io-devops/feature/optimize-shutdown
Optimize shutdown
2 parents 685c9c2 + 751c77e commit e681801

File tree

8 files changed

+178
-71
lines changed

8 files changed

+178
-71
lines changed

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,29 @@ Format:
108108
Specifies from and to versions which are supported by the
109109
in-place-upgrade mechanism.
110110

111+
aem_cms_stop_timeout_seconds: 1200
112+
113+
Seconds to wait for instance to be stopped until process is killed.
114+
115+
aem_cms_systemd_unit_template: "aem.service.j2"
116+
117+
Path to the systemd unit template. Use this variable to specify a custom
118+
template.
119+
120+
aem_cms_sysvinit_service_template: "aem.init.j2"
121+
122+
Path to the sysvinit service template. Path to the systemd unit
123+
template. Use this variable to specify a custom template.
124+
125+
aem_cms_stop_sync_template: "stop-sync.sh.j2"
126+
127+
Path to the synchronous stop script template. Use this variable to
128+
specify a custom template.
129+
130+
aem_cms_stop_sync_path: "{{ aem_cms_home }}/crx-quickstart/bin/stop-sync.sh"
131+
132+
Destination path of the synchronous stop script on the instance.
133+
111134
## Dependencies
112135

113136
This role depends on the

defaults/main.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,18 @@ aem_cms_in_place_upgrade_paths:
6565
- "6.4.0"
6666
# "from_version":
6767
# - "to_version" # list of versions that an upgrade is allowed for
68+
69+
# Seconds to wait for instance to be stopped until process is killed
70+
aem_cms_stop_timeout_seconds: 1200
71+
72+
# Path to the systemd unit template
73+
aem_cms_systemd_unit_template: "aem.service.j2"
74+
75+
# Path to the sysvinit service template
76+
aem_cms_sysvinit_service_template: "aem.init.j2"
77+
78+
# Path to the synchronous stop script template
79+
aem_cms_stop_sync_template: "stop-sync.sh.j2"
80+
81+
# Destination path of the synchronous stop script on the instance
82+
aem_cms_stop_sync_path: "{{ aem_cms_home }}/crx-quickstart/bin/stop-sync.sh"

tasks/main.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,14 @@
7474
state: absent
7575
when: aem_cms_remove_download
7676

77+
- name: Create stop-sync shell file.
78+
template:
79+
src: "{{ aem_cms_stop_sync_template }}"
80+
dest: "{{ aem_cms_stop_sync_path }}"
81+
owner: "{{ aem_cms_user }}"
82+
group: "{{ aem_cms_group }}"
83+
mode: "0744"
84+
7785
- name: Setup AEM systemd unit.
7886
include_tasks: systemd.yml
7987
when: ansible_service_mgr == 'systemd'

tasks/systemd.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
- name: Create AEM systemd unit file.
22
template:
3-
src: aem.service.j2
3+
src: "{{ aem_cms_systemd_unit_template }}"
44
dest: "/etc/systemd/system/{{ aem_cms_service_name }}.service"
55
mode: "0644"
66

tasks/sysvinit.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
- name: Create AEM initd service file.
22
template:
3-
src: aem.init.j2
3+
src: "{{ aem_cms_sysvinit_service_template }}"
44
dest: "/etc/init.d/{{ aem_cms_service_name }}"
55
mode: "0755"
66

templates/aem.init.j2

Lines changed: 63 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,63 @@
1-
#!/bin/sh -e
2-
### BEGIN INIT INFO
3-
# Provides: {{ aem_cms_service_name }}
4-
# Required-Start: $local_fs $remote_fs $network $syslog $named
5-
# Required-Stop: $local_fs $remote_fs $network $syslog $named
6-
# Default-Start: 2 3 4 5
7-
# Default-Stop: 0 1 6
8-
# Short-Description: Start AEM process
9-
# Description: Init script for AEM instance {{ aem_cms_service_name }}
10-
### END INIT INFO
11-
12-
# Source function library.
13-
#. /etc/rc.d/init.d/functions
14-
. /lib/lsb/init-functions
15-
16-
SCRIPT_NAME=`basename $0`
17-
AEM_ROOT={{ aem_cms_home }}
18-
AEM_USER={{ aem_cms_user }}
19-
20-
21-
########
22-
BIN=${AEM_ROOT}/crx-quickstart/bin
23-
START=${BIN}/start
24-
STOP="${BIN}/stop --sync"
25-
STATUS="${BIN}/status"
26-
27-
aem_start() {
28-
if $0 status > /dev/null ; then
29-
log_success_msg "$SCRIPT_NAME already started"
30-
else
31-
log_daemon_msg "Starting $DESC" "$SCRIPT_NAME"
32-
su - ${AEM_USER} ${START} > /dev/null
33-
log_end_msg 0
34-
fi
35-
}
36-
37-
aem_stop() {
38-
if $0 status > /dev/null ; then
39-
log_daemon_msg "Stopping $DESC" "$SCRIPT_NAME"
40-
su - ${AEM_USER} -c "bash ${STOP}" > /dev/null
41-
log_end_msg 0
42-
else
43-
log_success_msg "$SCRIPT_NAME not started"
44-
fi
45-
}
46-
47-
case "$1" in
48-
start)
49-
aem_start
50-
;;
51-
stop)
52-
aem_stop
53-
;;
54-
status)
55-
su - ${AEM_USER} ${STATUS}
56-
;;
57-
restart)
58-
aem_stop
59-
aem_start
60-
;;
61-
*)
62-
echo "Usage: $SCRIPT_NAME {start|stop|status|restart}"
63-
exit 1
64-
;;
65-
esac
1+
#!/bin/bash
2+
### BEGIN INIT INFO
3+
# Provides: {{ aem_cms_service_name }}
4+
# Required-Start: $local_fs $remote_fs $network $syslog $named
5+
# Required-Stop: $local_fs $remote_fs $network $syslog $named
6+
# Default-Start: 2 3 4 5
7+
# Default-Stop: 0 1 6
8+
# Short-Description: Start AEM process
9+
# Description: Init script for AEM instance {{ aem_cms_service_name }}
10+
### END INIT INFO
11+
12+
# Source function library.
13+
. /lib/lsb/init-functions
14+
15+
SCRIPT_NAME=`basename $0`
16+
AEM_ROOT={{ aem_cms_home }}
17+
AEM_USER={{ aem_cms_user }}
18+
PID_PATH={{ aem_cms_home }}/crx-quickstart/conf/cq.pid
19+
STOP_TIMEOUT_SECONDS={{ aem_cms_stop_timeout_seconds }}
20+
21+
22+
########
23+
BIN=${AEM_ROOT}/crx-quickstart/bin
24+
START=${BIN}/start
25+
STOP=${BIN}/stop-sync.sh
26+
STATUS="${BIN}/status"
27+
28+
aem_start() {
29+
pidResult=$(pgrep --pidfile $PID_PATH || true);
30+
if [[ $pidResult != "" ]];
31+
then
32+
log_success_msg "$SCRIPT_NAME already started"
33+
else
34+
log_daemon_msg "Starting $SCRIPT_NAME"
35+
su - ${AEM_USER} ${START} > /dev/null
36+
log_end_msg 0
37+
fi
38+
}
39+
40+
aem_stop() {
41+
# execute sync stop script
42+
su - ${AEM_USER} -c "bash ${STOP}"
43+
}
44+
45+
case "$1" in
46+
start)
47+
aem_start
48+
;;
49+
stop)
50+
aem_stop
51+
;;
52+
status)
53+
su - ${AEM_USER} ${STATUS}
54+
;;
55+
restart)
56+
aem_stop
57+
aem_start
58+
;;
59+
*)
60+
echo "Usage: $SCRIPT_NAME {start|stop|status|restart}"
61+
exit 1
62+
;;
63+
esac

templates/aem.service.j2

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,28 @@ Requires=network.target
77

88
[Service]
99
Type=forking
10-
ExecStart={{ aem_cms_home }}/crx-quickstart/bin/start
11-
ExecStop={{ aem_cms_home }}/crx-quickstart/bin/stop --sync
1210
PIDFile={{ aem_cms_home }}/crx-quickstart/conf/cq.pid
13-
User={{ aem_cms_user }}
11+
1412
LimitNOFILE={{ aem_cms_limit_nofile }}
1513

14+
User={{ aem_cms_user }}
15+
Group={{ aem_cms_group }}
16+
17+
# Do not kill the process because sync stop script is taking care of this
18+
KillMode=none
19+
20+
ExecStart={{ aem_cms_home }}/crx-quickstart/bin/start
21+
22+
# sleep some time after start to ensure pid file is present for systemd
23+
ExecStartPost=/bin/sleep 5s
24+
ExecStop=-{{ aem_cms_stop_sync_path }}
25+
26+
# Configure the time to wait for ExecStop command
27+
{# Add 30 seconds to aem_cms_stop_timeout_seconds to avoid timeouts when process is killed #}
28+
TimeoutStopSec={{ aem_cms_stop_timeout_seconds | int + 30}}s
29+
30+
# Wait at least one second before restart
31+
RestartSec=1
32+
1633
[Install]
17-
WantedBy=multi-user.target
34+
WantedBy=multi-user.target

templates/stop-sync.sh.j2

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!/bin/bash
2+
# Source function library.
3+
. /lib/lsb/init-functions
4+
5+
SCRIPT_NAME=`basename $0`
6+
AEM_ROOT={{ aem_cms_home }}
7+
AEM_SERVICE_NAME={{ aem_cms_service_name }}
8+
PID_PATH=${AEM_ROOT}/crx-quickstart/conf/cq.pid
9+
STOP_TIMEOUT_SECONDS={{ aem_cms_stop_timeout_seconds }}
10+
11+
########
12+
BIN=${AEM_ROOT}/crx-quickstart/bin
13+
STOP=${BIN}/stop
14+
15+
# check if instance is running
16+
pidResult=$(pgrep --pidfile $PID_PATH || true);
17+
if [[ $pidResult != "" ]];
18+
then
19+
# AEM is running
20+
log_daemon_msg "Stopping $AEM_SERVICE_NAME"
21+
bash ${STOP} > /dev/null
22+
while [ "$STOP_TIMEOUT_SECONDS" != "0" ]; do
23+
sleep 1s
24+
pidResult=$(pgrep --pidfile $PID_PATH || true);
25+
if [[ $pidResult == "" ]];
26+
then
27+
break;
28+
fi
29+
30+
log_progress_msg "."
31+
let STOP_TIMEOUT_SECONDS-=1;
32+
33+
done
34+
# check if AEM process has to be terminated
35+
pidResult=$(pgrep --pidfile $PID_PATH || true);
36+
if [[ $pidResult != "" ]];
37+
then
38+
log_daemon_msg "Killed $AEM_SERVICE_NAME because shutdown timeout was reached"
39+
kill -9 $(cat $PID_PATH);
40+
fi
41+
log_end_msg 0
42+
else
43+
# AEM is not running
44+
log_success_msg "$AEM_SERVICE_NAME already stopped"
45+
log_end_msg 7
46+
fi

0 commit comments

Comments
 (0)