Deploy crontab with Odin

Follow these steps to integrate and deploy crontab with odin:

  1. Define the cron-jobs in the project. This may be one way to do it:

    1. Directory structure:

      -- project base-dir
          -- resources
              -- crontab-default.txt
              -- crontab-prod.txt
              ...
          ...
    2. Example of crontab-default.txt:

      * * * * * cd ${APP_DIR} && TEAM_SUFFIX=${TEAM_SUFFIX} VPC_SUFFIX=${VPC_SUFFIX} NODE_ENV=${ENV} DB_USERNAME=${VAULT_SERVICE_MYSQL_USERNAME} DB_PASSWORD=${VAULT_SERVICE_MYSQL_PASSWORD} /usr/bin/node ${APP_DIR}/crons/aerospike/crons/syncData.js 8
      * * * * * cd ${APP_DIR} && TEAM_SUFFIX=${TEAM_SUFFIX} VPC_SUFFIX=${VPC_SUFFIX} NODE_ENV=${ENV} DB_USERNAME=${VAULT_SERVICE_MYSQL_USERNAME} DB_PASSWORD=${VAULT_SERVICE_MYSQL_PASSWORD} /usr/bin/node ${APP_DIR}/crons/aerospike/bootstrap/app/index.js
      * * * * * cd ${APP_DIR} && TEAM_SUFFIX=${TEAM_SUFFIX} VPC_SUFFIX=${VPC_SUFFIX} NODE_ENV=${ENV} DB_USERNAME=${VAULT_SERVICE_MYSQL_USERNAME} DB_PASSWORD=${VAULT_SERVICE_MYSQL_PASSWORD} /usr/bin/node ${APP_DIR}/crons/aerospike/bootstrap/slug/index.js
      * * * * * cd ${APP_DIR} && TEAM_SUFFIX=${TEAM_SUFFIX} VPC_SUFFIX=${VPC_SUFFIX} NODE_ENV=${ENV} DB_USERNAME=${VAULT_SERVICE_MYSQL_USERNAME} DB_PASSWORD=${VAULT_SERVICE_MYSQL_PASSWORD} /usr/bin/node ${APP_DIR}/crons/aerospike/crons/syncPlayerData.js 6
      * * * * * cd ${APP_DIR} && TEAM_SUFFIX=${TEAM_SUFFIX} VPC_SUFFIX=${VPC_SUFFIX} NODE_ENV=${ENV} DB_USERNAME=${VAULT_SERVICE_MYSQL_USERNAME} DB_PASSWORD=${VAULT_SERVICE_MYSQL_PASSWORD} /usr/bin/node ${APP_DIR}/crons/aerospike/crons/syncPlayerMetaData.js 6
  2. Inside the project directory run: odin generate application-template --name SERVICE_NAME

    This will create .odin directory

  3. Inside build.sh, write the commands to package the code in target directory. For example, if the cron-job is written in node.js, the build.sh will look like

    rsync -av --exclude='./target' ./ ./target/fantasy-tour-cron | grep "total"
    
    cd target/fantasy-tour-cron && npm install && cd  ../..
  4. Inside pre-deploy.sh, write commands which need to be executed before deployment. For example, run database migrations.

  5. Inside start.sh, add the commands to register the cron-jobs with crontab. When running cron in containers do not forget to start the cron service. For example:

    (Note that this assumes crons are defined at resources/crontab-default.txt)

    #!/usr/bin/env bash
    set -e
    echo "APP_DIR: ${APP_DIR}"
    echo "ENV: ${ENV}"
    echo "VPC_SUFFIX: ${VPC_SUFFIX}"
    echo "TEAM_SUFFIX: ${TEAM_SUFFIX}"
    echo "SERVICE_NAME: ${SERVICE_NAME}"
    echo "NAMESPACE:" ${NAMESPACE}
    
    cd "${APP_DIR}" || exit
    unset `printenv | grep '_SERVICE\\|_PORT' | grep -o -E '^[^=]+'`
    #source .config
    if [[ "$ENV" == "prod" || "$ENV" == "uat" ]]; then
      CONSUL_ADDR="config-store-${ENV}.dream11.com"
      VAULT_ADDR="http://secret-store-${ENV}.dream11.com"
    else
      CONSUL_ADDR="config-store-${ENV}.d11dev.com"
      VAULT_ADDR="http://secret-store-${ENV}.d11dev.com"
    fi
       
    CONFIG_OPTS="-consul-addr=${CONSUL_ADDR} -vault-addr=${VAULT_ADDR} -prefix=d11/${NAMESPACE} -secret=secrets/data/d11/${NAMESPACE}/${SERVICE_NAME}/${ENV}/default"
    
    if [[ "$ENV" == "prod" ]]; then
      envconsul ${CONFIG_OPTS} envsubst < ${APP_DIR}/resources/crontab-prod.txt > /tmp/crontab.txt
    else
      envconsul ${CONFIG_OPTS} envsubst < ${APP_DIR}/resources/crontab-default.txt > /tmp/crontab.txt
    fi
    
    crontab /tmp/crontab.txt
    
    if [[ "$DEPLOYMENT_TYPE" == "container" ]]; then
      cron  # start cron service inside containers
    fi
    tail -f /tmp/crontab.txt
    
  6. In the service-definition file, add application component similar to any node application and in the config, set discovery as none. For example:

    (Note: the deployed application is assumed to not have a server running for healthcheck. Therefore discovery is set to none.)

    {
      "name": "fantasy-tour-cron",
      "version": "1.0.0-SNAPSHOT",
      "team": "devx",
      "components": [{
        "type": "application",
        "name": "fantasy-tour-cron",
        "version": "1.0.0-SNAPSHOT",
        "config": {
          "build_type": "node",
          "build_version": "14.15.1-buster-slim",
          "discovery": "none"
        }
      }]
    }
  7. Deploy the service.

Refer to this PR for reference:

Last updated