@@ -7,6 +7,8 @@ SUPERVISOR_CONF=/etc/supervisor/supervisord.conf
7
7
DATA_VOLUME_MOUNTPOINT=${DATA_VOLUME_MOUNTPOINT:-/ data}
8
8
export CONFIGURED_FLAG_PATH=${CONFIGURED_FLAG_PATH:- $DATA_VOLUME_MOUNTPOINT / machine.configured}
9
9
10
+ LSN_CHECKPOINT_FILE_PATH=" ${DATA_VOLUME_MOUNTPOINT} /latest-lsn-checkpoint"
11
+
10
12
# Ref: https://gist.github.com/sj26/88e1c6584397bb7c13bd11108a579746
11
13
function retry {
12
14
# Pass 0 for unlimited retries
@@ -51,22 +53,40 @@ function enable_swap {
51
53
}
52
54
53
55
function create_lsn_checkpoint_file {
54
- if [ ! -f " ${DATA_VOLUME_MOUNTPOINT} /latest-lsn-checkpoint" ]; then
55
- echo -n " 0/0" > " ${DATA_VOLUME_MOUNTPOINT} /latest-lsn-checkpoint"
56
- chown postgres:postgres " ${DATA_VOLUME_MOUNTPOINT} /latest-lsn-checkpoint"
57
- chmod 0300 " ${DATA_VOLUME_MOUNTPOINT} /latest-lsn-checkpoint"
56
+ if [ ! -f " ${LSN_CHECKPOINT_FILE_PATH} " ]; then
57
+ echo -n " 0/0" > " ${LSN_CHECKPOINT_FILE_PATH} "
58
+ touch " ${LSN_CHECKPOINT_FILE_PATH} .previous"
59
+ chown postgres:postgres " ${LSN_CHECKPOINT_FILE_PATH} "
60
+
61
+ chmod 0300 " ${LSN_CHECKPOINT_FILE_PATH} "
58
62
fi
59
63
}
60
64
65
+ function push_lsn_checkpoint_file {
66
+ LSN_CHECKPOINT=$( cat " ${LSN_CHECKPOINT_FILE_PATH} " )
67
+ PREVIOUS_LSN_CHECKPOINT=$( cat " ${LSN_CHECKPOINT_FILE_PATH} .previous" )
68
+
69
+ if [ " ${LSN_CHECKPOINT} " == " ${PREVIOUS_LSN_CHECKPOINT} " ]; then
70
+ echo " LSN checkpoint file has not changed. Skipping push."
71
+ return
72
+ fi
73
+
74
+ if [ " ${LSN_CHECKPOINT} " == " 0/0" ]; then
75
+ echo " LSN checkpoint file is empty. Skipping push."
76
+ return
77
+ fi
78
+
79
+ /usr/bin/admin-mgr lsn-checkpoint-push || echo " Failed to push LSN checkpoint"
80
+ }
81
+
61
82
function graceful_shutdown {
62
83
echo " $( date) : Received SIGINT. Shutting down."
63
84
supervisorctl stop postgresql
64
85
65
86
# Postgres ships the latest WAL file using archive_command during shutdown, in a blocking operation
66
87
# This is to ensure that the WAL file is shipped, just in case
67
88
sleep 0.2
68
-
69
- /usr/bin/admin-mgr lsn-checkpoint-push || echo " Failed to push LSN checkpoint"
89
+ push_lsn_checkpoint_file
70
90
71
91
kill -s TERM " $( supervisorctl pid) "
72
92
}
@@ -180,6 +200,8 @@ function start_supervisor {
180
200
181
201
# Start supervisord
182
202
/usr/bin/supervisord -c $SUPERVISOR_CONF
203
+
204
+
183
205
}
184
206
185
207
# Increase max number of open connections
@@ -272,10 +294,6 @@ if [ "${PLATFORM_DEPLOYMENT:-}" ]; then
272
294
trap graceful_shutdown SIGINT
273
295
fi
274
296
275
- if [ " ${SHUTDOWN_GRACEFULLY:- } " ]; then
276
- /usr/bin/admin-mgr lsn-checkpoint-push || echo " Failed to push LSN checkpoint"
277
- exit 0
278
- fi
279
-
280
297
touch " $CONFIGURED_FLAG_PATH "
281
298
start_supervisor
299
+ push_lsn_checkpoint_file
0 commit comments