diff --git a/docker/openshift/Dockerfile b/docker/openshift/Dockerfile index 3d009e9..32d4a3a 100644 --- a/docker/openshift/Dockerfile +++ b/docker/openshift/Dockerfile @@ -14,7 +14,8 @@ COPY docker/openshift/init.sh / # 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 diff --git a/docker/openshift/cron-entrypoint.sh b/docker/openshift/cron-entrypoint.sh new file mode 100644 index 0000000..b9f5523 --- /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 "Cron 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 2021d03..0000000 --- a/docker/openshift/crons/base.sh +++ /dev/null @@ -1,38 +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 - -# 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 - 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/cron.sh b/docker/openshift/crons/cron.sh new file mode 100644 index 0000000..3af5b6e --- /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 4eecbdf..a698f10 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 780bcbb..637d143 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 d5d0fc1..be359b9 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 diff --git a/docker/openshift/init.sh b/docker/openshift/init.sh index 8bef138..22ccb58 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