From df8ca25a7a011317fd09e38a2ab0df6b35393f4c Mon Sep 17 00:00:00 2001 From: Santeri Hurnanen Date: Fri, 2 Aug 2024 12:10:40 +0300 Subject: [PATCH 1/6] UHF-10354: Check cron pod failure codes --- docker/openshift/crons/base.sh | 16 ++++++++++++---- docker/openshift/crons/cron.sh | 9 +++++++++ docker/openshift/crons/pubsub.sh | 13 ++++--------- 3 files changed, 25 insertions(+), 13 deletions(-) create mode 100644 docker/openshift/crons/cron.sh diff --git a/docker/openshift/crons/base.sh b/docker/openshift/crons/base.sh index 2021d034..b4783979 100644 --- a/docker/openshift/crons/base.sh +++ b/docker/openshift/crons/base.sh @@ -18,6 +18,8 @@ echo "Starting cron: $(date)" # done # @endcode +# Enable drush cron +exec "/crons/cron.sh" & # Uncomment this to enable TPR migration cron #exec "/crons/migrate-tpr.sh" & # Uncomment this to enable linked events migrations cron @@ -31,8 +33,14 @@ echo "Starting cron: $(date)" while true do - echo "Running cron: $(date +'%Y-%m-%dT%H:%M:%S%:z')\n" - drush cron - # Sleep for 10 minutes. - sleep 600 + # Rudimentary process supervisor: + # Waits for the next process to terminate. The parent + # process is killed if any subprocess exists with failure. + # OpenShift should then restart the cron pod. + wait -n + exit_code=$? + if [[ "$exit_code" -ne 0 ]]; then + output_error_message "Subprocess failed with exit code $exit_code" + exit 1 + fi done diff --git a/docker/openshift/crons/cron.sh b/docker/openshift/crons/cron.sh new file mode 100644 index 00000000..3af5b6ea --- /dev/null +++ b/docker/openshift/crons/cron.sh @@ -0,0 +1,9 @@ +#!/bin/bash + +while true +do + echo "Running cron: $(date +'%Y-%m-%dT%H:%M:%S%:z')\n" + drush cron + # Sleep for 10 minutes. + sleep 600 +done diff --git a/docker/openshift/crons/pubsub.sh b/docker/openshift/crons/pubsub.sh index d5d0fc16..be359b9f 100755 --- a/docker/openshift/crons/pubsub.sh +++ b/docker/openshift/crons/pubsub.sh @@ -2,14 +2,9 @@ echo "Running PubSub daemon: $(date +'%Y-%m-%dT%H:%M:%S%:z')" -i=0 -# Attempt to start this service five times. -until [ $i -gt 5 ] +while true do - drush helfi:azure:pubsub-listen - - if [[ "$?" -ne 0 ]]; then - ((i=i+1)) - sleep 10 - fi + # PubSub process exists with success return code after + # certain number of messages and should then be restarted. + drush helfi:azure:pubsub-listen || exit 1 done From 5a25aa4b64e639f5c8357af1690d570a0076d340 Mon Sep 17 00:00:00 2001 From: Santeri Hurnanen Date: Fri, 2 Aug 2024 15:15:24 +0300 Subject: [PATCH 2/6] Add cron entrypoint script --- docker/openshift/Dockerfile | 2 ++ docker/openshift/cron-entrypoint.sh | 41 ++++++++++++++++++++++ docker/openshift/crons/base.sh | 46 ------------------------- docker/openshift/crons/linked-events.sh | 6 ++++ docker/openshift/crons/migrate-tpr.sh | 6 ++++ docker/openshift/init.sh | 8 +++++ 6 files changed, 63 insertions(+), 46 deletions(-) create mode 100644 docker/openshift/cron-entrypoint.sh delete mode 100644 docker/openshift/crons/base.sh diff --git a/docker/openshift/Dockerfile b/docker/openshift/Dockerfile index 3d009e9b..63e2a158 100644 --- a/docker/openshift/Dockerfile +++ b/docker/openshift/Dockerfile @@ -10,6 +10,8 @@ COPY docker/openshift/entrypoints/ /entrypoints RUN chmod +x /entrypoints/* COPY docker/openshift/init.sh / +COPY docker/openshift/cron-entrypoint.sh /usr/local/bin/cron-entrypoint +RUN chmod +x /usr/local/bin/cron-entrypoint # Copy cron scripts RUN mkdir /crons diff --git a/docker/openshift/cron-entrypoint.sh b/docker/openshift/cron-entrypoint.sh new file mode 100644 index 00000000..24a700aa --- /dev/null +++ b/docker/openshift/cron-entrypoint.sh @@ -0,0 +1,41 @@ +#!/bin/bash + +source /init.sh + +echo "Starting cron: $(date)" + +# You can add any additional cron "daemons" to docker/openshift/crons/ folder. +# +# Example: +# @code +# #!/bin/bash +# while true +# do +# drush some-command +# sleep 600 +# done +# @endcode + +for cron in /crons/*.sh; do + if [ "${cron##*/}" == "base.sh" ]; then + # Skip legacy base.sh script if it exists. + continue + elif [ -r "$cron" ]; then + echo "Starting $cron" + exec "$cron" & + fi +done + +while true +do + # Rudimentary process supervisor: + # Waits for the next process to terminate. The parent + # process is killed if any subprocess exists with failure. + # OpenShift should then restart the cron pod. + wait -n + exit_code=$? + if [[ "$exit_code" -ne 0 ]]; then + output_error_message "Subprocess failed with exit code $exit_code" + exit 1 + fi +done diff --git a/docker/openshift/crons/base.sh b/docker/openshift/crons/base.sh deleted file mode 100644 index b4783979..00000000 --- a/docker/openshift/crons/base.sh +++ /dev/null @@ -1,46 +0,0 @@ -#!/bin/bash - -source /init.sh - -echo "Starting cron: $(date)" - -# You can add any additional cron "daemons" here: -# -# exec "/crons/some-command.sh" & -# -# Example cron (docker/openshift/crons/some-command.sh): -# @code -# #!/bin/bash -# while true -# do -# drush some-command -# sleep 600 -# done -# @endcode - -# Enable drush cron -exec "/crons/cron.sh" & -# Uncomment this to enable TPR migration cron -#exec "/crons/migrate-tpr.sh" & -# Uncomment this to enable linked events migrations cron -#exec "/crons/linked-events.sh" & -# Uncomment this to enable Varnish purge cron -#exec "/crons/purge-queue.sh" & -# Uncomment this to enable automatic translation updates. -# exec "/crons/update-translations.sh" & -# Uncomment this to enable content scheduler -# exec "/crons/content-scheduler.sh" & - -while true -do - # Rudimentary process supervisor: - # Waits for the next process to terminate. The parent - # process is killed if any subprocess exists with failure. - # OpenShift should then restart the cron pod. - wait -n - exit_code=$? - if [[ "$exit_code" -ne 0 ]]; then - output_error_message "Subprocess failed with exit code $exit_code" - exit 1 - fi -done diff --git a/docker/openshift/crons/linked-events.sh b/docker/openshift/crons/linked-events.sh index 4eecbdfb..a698f10a 100644 --- a/docker/openshift/crons/linked-events.sh +++ b/docker/openshift/crons/linked-events.sh @@ -1,5 +1,11 @@ #!/bin/bash +source /init.sh + +if ! is_drupal_module_enabled "helfi_react_search"; then + exit 0 +fi + while true do # Allow migrations to be run every 3 hours and reset stuck migrations every 12 hours. diff --git a/docker/openshift/crons/migrate-tpr.sh b/docker/openshift/crons/migrate-tpr.sh index 780bcbbf..637d1434 100644 --- a/docker/openshift/crons/migrate-tpr.sh +++ b/docker/openshift/crons/migrate-tpr.sh @@ -1,5 +1,11 @@ #!/bin/bash +source /init.sh + +if ! is_drupal_module_enabled "helfi_tpr"; then + exit 0 +fi + while true do # Allow migrations to be run every 3 hours and reset stuck migrations every 12 hours. diff --git a/docker/openshift/init.sh b/docker/openshift/init.sh index 8bef1380..b711d7d6 100644 --- a/docker/openshift/init.sh +++ b/docker/openshift/init.sh @@ -30,6 +30,14 @@ function deployment_in_progress { return 1 } +function is_drupal_module_enabled { + if drush pm-list --status=Enabled --filter=${1} --format=json | jq '. == []'; then + return 1 + fi + + return 0 +} + if [ ! -d "sites/default/files" ]; then output_error_message "Container start error: Public file folder does not exist. Exiting early." exit 1 From 5ff1ac57253fdc2ea8573b77ef7c9aa68039b972 Mon Sep 17 00:00:00 2001 From: Santeri Hurnanen Date: Sun, 11 Aug 2024 18:39:21 +0300 Subject: [PATCH 3/6] UHF-10354: Fix is_drupal_module_enabled command --- docker/openshift/init.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker/openshift/init.sh b/docker/openshift/init.sh index b711d7d6..22ccb583 100644 --- a/docker/openshift/init.sh +++ b/docker/openshift/init.sh @@ -31,7 +31,7 @@ function deployment_in_progress { } function is_drupal_module_enabled { - if drush pm-list --status=Enabled --filter=${1} --format=json | jq '. == []'; then + if drush pm-list --status=Enabled --filter=${1} --format=json | jq --exit-status '. == []' > /dev/null; then return 1 fi From 4af72b69803ef8d2d0f43720bf083ab49a1392ad Mon Sep 17 00:00:00 2001 From: Santeri Hurnanen Date: Mon, 12 Aug 2024 09:04:41 +0300 Subject: [PATCH 4/6] UHF-10354: Skip hooks --- docker/openshift/cron-entrypoint.sh | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/docker/openshift/cron-entrypoint.sh b/docker/openshift/cron-entrypoint.sh index 24a700aa..a7e71ff2 100644 --- a/docker/openshift/cron-entrypoint.sh +++ b/docker/openshift/cron-entrypoint.sh @@ -17,10 +17,11 @@ echo "Starting cron: $(date)" # @endcode for cron in /crons/*.sh; do - if [ "${cron##*/}" == "base.sh" ]; then - # Skip legacy base.sh script if it exists. + # Skip legacy base.sh script if it exists. + # Skip Kubernetes hooks that are stored in crons directory. + if [[ "${cron##*/}" == "base.sh" ]] || [[ "${cron##*/}" == *-hook.sh ]]; then continue - elif [ -r "$cron" ]; then + elif [[ -r "$cron" ]]; then echo "Starting $cron" exec "$cron" & fi From f667c3474099b11791cb550f8f9e2982150a4aca Mon Sep 17 00:00:00 2001 From: Santeri Hurnanen Date: Mon, 12 Aug 2024 09:21:24 +0300 Subject: [PATCH 5/6] Fix sonarcloud --- docker/openshift/Dockerfile | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/docker/openshift/Dockerfile b/docker/openshift/Dockerfile index 63e2a158..32d4a3a2 100644 --- a/docker/openshift/Dockerfile +++ b/docker/openshift/Dockerfile @@ -10,13 +10,12 @@ COPY docker/openshift/entrypoints/ /entrypoints RUN chmod +x /entrypoints/* COPY docker/openshift/init.sh / -COPY docker/openshift/cron-entrypoint.sh /usr/local/bin/cron-entrypoint -RUN chmod +x /usr/local/bin/cron-entrypoint # Copy cron scripts RUN mkdir /crons COPY docker/openshift/crons/ /crons -RUN chmod +x /crons/* +COPY docker/openshift/cron-entrypoint.sh /usr/local/bin/cron-entrypoint +RUN chmod +x /crons/* /usr/local/bin/cron-entrypoint # Copy nginx overrides. COPY docker/openshift/custom.locations /etc/nginx/conf.d/custom.locations From f4fd109884a1e3f52c3366dad7a571d12a06118e Mon Sep 17 00:00:00 2001 From: Santeri Hurnanen Date: Mon, 12 Aug 2024 12:10:24 +0300 Subject: [PATCH 6/6] Improve error message --- docker/openshift/cron-entrypoint.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker/openshift/cron-entrypoint.sh b/docker/openshift/cron-entrypoint.sh index a7e71ff2..b9f55232 100644 --- a/docker/openshift/cron-entrypoint.sh +++ b/docker/openshift/cron-entrypoint.sh @@ -36,7 +36,7 @@ do wait -n exit_code=$? if [[ "$exit_code" -ne 0 ]]; then - output_error_message "Subprocess failed with exit code $exit_code" + output_error_message "Cron subprocess failed with exit code $exit_code" exit 1 fi done