Integrate mono-repo(cronjobs) with Odin

(How to integrate cron-jobs in https://github.com/dream11/cronjobs with Odin)

Points to note:

  • The repo cronjobs has many cron-jobs in the form of single .js files. We don't need to treat all of them as separate applications. We'll treat them as one application and have a single artifact for the whole repo.

  • However, we'll usually only need to run a subset of them at once. Therefore, there will be multiple start scripts instead of only one start.sh as exists for other applications.

  • All jobs will be run via crontab.

Keeping the above points in mind, these are the steps to follow to integrate a cronjob in the repo with odin:

  1. There is a single build.sh file in the repo which will build the whole repo and put all the files in target/cronjobs directory. This file is as follows:

    rsync -av --exclude='./target' ./ ./target/cronjobs | grep "total"
    cd target/cronjobs && npm install && cd  ../..

    Since, this will be used for all the jobs, we need to make sure it does everything everyone would need. If the above file doesn't work for your specific case, please create an issue in the repo.

  2. In the resources directory, create a file(say echo-cron-schedule.txt) containing the schedules for each of the cronjobs to run. For example if there is a single job to run, the file will have single line for a single schedule as follows: (This is a dummy job which appends hello to a file)

    * * * * * echo "hello" >> /var/cron-logs/echo.log
  3. Create a start script with a name which can be used to identify which cron-jobs it will be used to run. This will have to code to execute to register the above cron-schedules with crontab. For example, to run the above cron, create a file echo-start.sh in the .odin/cronjobs directory with the following code:

    #!/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}
    
    envsubst < ${APP_DIR}/resources/echo-cron-schedule.txt > /tmp/crontab.txt
    crontab /tmp/crontab.txt
    
    if [[ "$DEPLOYMENT_TYPE" == "container" ]]; then
      cron  # start cron service inside containers
    fi
    
    tail -f ${APP_DIR}/resources/echo-cron-schedule.txt
  4. In the service-definition file, add the component for cron as follows: Note here that the name of the component must necessarily be cronjobs as that is used to get the artifact and there is only one artifact for the whole repo. The start_script_path is responsible for running the required jobs.

    {
      "name": "cronjobs",
      "version": "1.0.0-SNAPSHOT",
      "team": "test",
      "components": [
        {
          "name": "cronjobs",
          "type": "application",
          "version": "1.0.1-SNAPSHOT",
          "config": {
            "build_type": "node",
            "build_version": "14.15.1-buster-slim",
            "start_script_path": ".odin/echo-start.sh",
            "discovery": "none"
          }
        }
      ]
    }
  5. Deploy the service as usual.

Refer to this PR for reference.

Also, note that the start script will be more complex for actual cron-jobs. Please refer to the start.sh in this PR.

Last updated