|
| 1 | +#!/bin/bash |
| 2 | +set -eo pipefail |
| 3 | +CONFIG_FILE='/etc/gearmand.conf' |
| 4 | + |
| 5 | +VERBOSE=${VERBOSE:-INFO} |
| 6 | +QUEUE_TYPE=${QUEUE_TYPE:-builtin} |
| 7 | + |
| 8 | +if [[ -n "$LISTEN_PORT" ]]; then |
| 9 | + export GEARMAND_PORT="$LISTEN_PORT" |
| 10 | +fi |
| 11 | +if [[ "$GEARMAND_PORT" =~ [^0-9] ]] ; then |
| 12 | + echo "WARNING: Ignoring invalid (non-numeric) value for GEARMAND_PORT: $GEARMAND_PORT" |
| 13 | + unset GEARMAND_PORT |
| 14 | +fi |
| 15 | + |
| 16 | +THREADS=${THREADS:-4} |
| 17 | +BACKLOG=${BACKLOG:-32} |
| 18 | +FILE_DESCRIPTORS=${FILE_DESCRIPTORS:-0} |
| 19 | + |
| 20 | +JOB_RETRIES=${JOB_RETRIES:-0} |
| 21 | +ROUND_ROBIN=${ROUND_ROBIN:-0} |
| 22 | +WORKER_WAKEUP=${WORKER_WAKEUP:-0} |
| 23 | + |
| 24 | +KEEPALIVE=${KEEPALIVE:-0} |
| 25 | +KEEPALIVE_IDLE=${KEEPALIVE_IDLE:-30} |
| 26 | +KEEPALIVE_INTERVAL=${KEEPALIVE_INTERVAL:-10} |
| 27 | +KEEPALIVE_COUNT=${KEEPALIVE_COUNT:-5} |
| 28 | + |
| 29 | +# usage: file_env VAR [DEFAULT] |
| 30 | +# ie: file_env 'XYZ_DB_PASSWORD' 'example' |
| 31 | +# (will allow for "$XYZ_DB_PASSWORD_FILE" to fill in the value of |
| 32 | +# "$XYZ_DB_PASSWORD" from a file, especially for Docker's secrets feature) |
| 33 | +file_env() { |
| 34 | + local var="$1" |
| 35 | + local fileVar="${var}_FILE" |
| 36 | + local def="${2:-}" |
| 37 | + if [ "${!var:-}" ] && [ "${!fileVar:-}" ]; then |
| 38 | + echo >&2 "error: both $var and $fileVar are set (but are exclusive)" |
| 39 | + exit 1 |
| 40 | + fi |
| 41 | + local val="$def" |
| 42 | + if [ "${!var:-}" ]; then |
| 43 | + val="${!var}" |
| 44 | + elif [ "${!fileVar:-}" ]; then |
| 45 | + val="$(< "${!fileVar}")" |
| 46 | + fi |
| 47 | + export "$var"="$val" |
| 48 | + unset "$fileVar" |
| 49 | +} |
| 50 | + |
| 51 | +# first arg is `-f` or `--some-option` |
| 52 | +if [ "${1#-}" != "$1" ]; then |
| 53 | + set -- gearmand "$@" |
| 54 | +fi |
| 55 | + |
| 56 | +function generate_config() { |
| 57 | + cat <<-__CONFIG_CONTENT__ > "${CONFIG_FILE}" |
| 58 | + --listen=0.0.0.0 |
| 59 | + --log-file=stderr |
| 60 | + --verbose=${VERBOSE} |
| 61 | + --queue-type=${QUEUE_TYPE} |
| 62 | + --threads=${THREADS} |
| 63 | + --backlog=${BACKLOG} |
| 64 | + --job-retries=${JOB_RETRIES} |
| 65 | + --worker-wakeup=${WORKER_WAKEUP} |
| 66 | + __CONFIG_CONTENT__ |
| 67 | + |
| 68 | + if [[ "${FILE_DESCRIPTORS}" != '0' ]]; then |
| 69 | + cat <<-__CONFIG_CONTENT__ >> "${CONFIG_FILE}" |
| 70 | + --file-descriptors=${FILE_DESCRIPTORS} |
| 71 | + __CONFIG_CONTENT__ |
| 72 | + fi |
| 73 | + |
| 74 | + if [[ "${ROUND_ROBIN}" != '0' ]]; then |
| 75 | + cat <<-__CONFIG_CONTENT__ >> "${CONFIG_FILE}" |
| 76 | + --round-robin |
| 77 | + __CONFIG_CONTENT__ |
| 78 | + fi |
| 79 | + |
| 80 | + if [[ ${KEEPALIVE} != '0' ]]; then |
| 81 | + cat <<-__CONFIG_CONTENT__ >> "${CONFIG_FILE}" |
| 82 | + --keepalive |
| 83 | + --keepalive-idle=${KEEPALIVE_IDLE} |
| 84 | + --keepalive-interval=${KEEPALIVE_INTERVAL} |
| 85 | + --keepalive-count=${KEEPALIVE_COUNT} |
| 86 | + __CONFIG_CONTENT__ |
| 87 | + fi |
| 88 | + |
| 89 | + if [[ "$QUEUE_TYPE" == 'mysql' ]]; then |
| 90 | + file_env 'MYSQL_PASSWORD' |
| 91 | + cat <<-__CONFIG_CONTENT__ >> "${CONFIG_FILE}" |
| 92 | + --mysql-host=${MYSQL_HOST:-localhost} |
| 93 | + --mysql-port=${MYSQL_PORT:-3306} |
| 94 | + --mysql-user=${MYSQL_USER:-root} |
| 95 | + --mysql-password=${MYSQL_PASSWORD} |
| 96 | + --mysql-db=${MYSQL_DB:-Gearmand} |
| 97 | + --mysql-table=${MYSQL_TABLE:-gearman_queue} |
| 98 | + __CONFIG_CONTENT__ |
| 99 | + fi |
| 100 | +} |
| 101 | + |
| 102 | +if ! [ -s "${CONFIG_FILE}" ]; then # dont genarate config if current config file is not empty |
| 103 | + generate_config |
| 104 | +fi |
| 105 | +exec "$@" |
0 commit comments