Created
February 20, 2017 18:38
-
-
Save clok/00cafe3d13766f5979983a0d9fc05add to your computer and use it in GitHub Desktop.
Docker cron daemon pattern example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# /etc/cron.d/app-cron | |
0 0 * * * root bash -l -c '/usr/bin/rake.sh rake:task1 >> /dev/console 2>&1' | |
*/5 * * * * root bash -l -c '/usr/bin/rake.sh rake:task2 >> /dev/console 2>&1' | |
*/10 * * * * root bash -l -c '/usr/bin/rake.sh rake:task3 >> /dev/console 2>&1' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# /usr/bin/cron-env.sh | |
set -e | |
echo "[`date`] Generateing /etc/environment" | |
if [ ! -f /usr/src/app/.cron-env-prepared ]; then | |
echo "[`date`] Preparing environment ..." | |
else | |
echo "[`date`] Environment has been previously prepared." | |
exit 0 | |
fi | |
BLACKLIST=( | |
LS_COLORS | |
HOSTNAME | |
TERM | |
PWD | |
SHLVL | |
HOME | |
LESSOPEN | |
LESSCLOSE | |
_ | |
no_proxy | |
) | |
for VAR in `compgen -e`; do | |
eval unique_value=\$$VAR | |
if [[ ${BLACKLIST[*]} =~ "$VAR" ]]; then | |
echo "== Skipping blacklisted: $VAR" | |
else | |
sed_escaped_value="$(echo "$unique_value" | sed 's/"/\\"/g')" | |
echo "$VAR=\"$sed_escaped_value\"" >> /etc/environment | |
fi | |
done | |
touch /usr/src/app/.cron-env-prepared | |
echo "[`date`] Generated /etc/environment" | |
exit 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -e | |
VALUE="$1" | |
echo "Using input value: $VALUE" | |
case $VALUE in | |
cron) | |
echo "Starting cron..." | |
/usr/bin/cron-env.sh | |
cron -L 15 -f | |
;; | |
*) | |
if [ ! -f /usr/src/app/.app-prepared ]; then | |
echo "-- NOTE: Backend Environment has NOT been previously prepared. --" | |
echo "-- Backend Environment is prepared by executing the /usr/bin/prepare-app.sh script. --" | |
else | |
echo "Environment has previously been prepared." | |
fi | |
echo "Executing: $@" | |
exec "$@" | |
;; | |
esac | |
echo "Goodbye!" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# /usr/bin/prepare-app.sh | |
set -e | |
echo "Verifying app environment configured..." | |
if [ ! -f /usr/src/app/.app-prepared ]; then | |
echo "Preparing environment ..." | |
else | |
echo "Environment has been previously prepared. Skipping execution of /usr/bin/prepare-app.sh" | |
echo "Backend ready to start." | |
exit 0 | |
fi | |
REQUIRED=( | |
A_REQUIRED_ENV_VAR | |
ANOTHER_REQUIRED_ENV_VAR | |
) | |
for unique in "${REQUIRED[@]}"; do | |
eval unique_value=\$$unique | |
if [ "$unique_value" ]; then | |
echo "Required Value Set: $unique" | |
else | |
echo >&2 "Required Value '$unique' must be set" | |
echo >&2 "Goodbye!" | |
exit 1 | |
fi | |
done | |
touch /usr/src/app/.app-prepared | |
echo "Backend ready to start." | |
exit 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# /usr/bin/rake.sh | |
set -e | |
VALUE="$1" | |
/usr/bin/prepare-app.sh | while read -r line; do echo "[$VALUE] $line"; done | |
echo "[$VALUE] Executing rake task: $VALUE" | |
cd /usr/src/app && /usr/local/bin/bundle exec rails $VALUE | while read -r line; do echo "[$VALUE] $line"; done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment