Skip to content

Commit

Permalink
Merge pull request #259 from City-of-Helsinki/UHF-10354-cron-failures
Browse files Browse the repository at this point in the history
UHF-10354: Check cron pod failure codes
  • Loading branch information
hyrsky authored Aug 12, 2024
2 parents d05e73c + f4fd109 commit 40642c4
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 48 deletions.
3 changes: 2 additions & 1 deletion docker/openshift/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
42 changes: 42 additions & 0 deletions docker/openshift/cron-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -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
38 changes: 0 additions & 38 deletions docker/openshift/crons/base.sh

This file was deleted.

9 changes: 9 additions & 0 deletions docker/openshift/crons/cron.sh
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions docker/openshift/crons/linked-events.sh
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
6 changes: 6 additions & 0 deletions docker/openshift/crons/migrate-tpr.sh
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
13 changes: 4 additions & 9 deletions docker/openshift/crons/pubsub.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 8 additions & 0 deletions docker/openshift/init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 40642c4

Please sign in to comment.