From b2f73a89e4bf363ddd1da7fde84cf4977bd4f481 Mon Sep 17 00:00:00 2001 From: Santeri Hurnanen Date: Mon, 12 Aug 2024 09:02:34 +0300 Subject: [PATCH 1/2] UHF-10354: Test new cron entrypoint --- docker/openshift/Dockerfile | 2 ++ docker/openshift/cron-entrypoint.sh | 42 +++++++++++++++++++++++++ docker/openshift/crons/cron.sh | 9 ++++++ docker/openshift/crons/linked-events.sh | 6 ++++ docker/openshift/crons/migrate-tpr.sh | 6 ++++ docker/openshift/crons/pubsub.sh | 13 +++----- docker/openshift/init.sh | 8 +++++ 7 files changed, 77 insertions(+), 9 deletions(-) create mode 100644 docker/openshift/cron-entrypoint.sh create mode 100644 docker/openshift/crons/cron.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..a7e71ff2 --- /dev/null +++ b/docker/openshift/cron-entrypoint.sh @@ -0,0 +1,42 @@ +#!/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 + # 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 + 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/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/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/crons/pubsub.sh b/docker/openshift/crons/pubsub.sh index d5d0fc16..be359b9f 100644 --- 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 diff --git a/docker/openshift/init.sh b/docker/openshift/init.sh index 8bef1380..22ccb583 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 --exit-status '. == []' > /dev/null; 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 02388f77147099ab7c21c31985fd2c669d7bc75c Mon Sep 17 00:00:00 2001 From: Santeri Hurnanen Date: Mon, 12 Aug 2024 09:20:32 +0300 Subject: [PATCH 2/2] UHF-10354: 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..0e4540ed 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 /usr/local/bin/cron-entrypoint /crons/* # Copy nginx overrides. COPY docker/openshift/custom.locations /etc/nginx/conf.d/custom.locations