Scheduler

Schedulers are designed to run a particular service at a scheduled time, without triggering manually. We can have multiple schedulers for an application. It will consider the GMT format only.

How CronJob Works

The Python script reads the mdms-read-cronjob JSON from the MDMS service using a token from the CRONJOB user.

Identify the APIs set up in this MDMS using the provided script argument. The script uses the config from the MDMS to call the corresponding API.

NalJalSeva Schedulers

A total of 7 schedulers are available in the NalJalSeva:

_schedulerTodaysCollection: This scheduler will run daily to send the day collection amount to the collection employee.

_jobscheduler/true: This is to send the notification to the ULB employee when the bulk demand auto-generation is failed.

_schedulermarkexpensebill: This scheduler is used to mark the expense as paid for the paid expenses once every fortnight.

_schedulernewexpenditure: This is used to send the notification once every fortnight regarding the no of expenditures created.

_schedulermonthsummary: This is to send the monthly summary details to the ULB employee. _schedulerpendingcollection: This is to send the total pending amount details to the respective ULB employee user once every fortnight.

_jobscheduler/false: This is used to generate the bulk demand automatically once every month.

We have 7 different schedulers in NalJalSeva, running in 4 different time slots. We need to set them all to run the same Python scripts with different arguments, which you can find in the file under command -> args.

Configure the scheduler's runtime under the cron -> schedule option.

Example of failedBulkDemand scheduler:

  1. The cron schedule is set to "30 3 5 * *", which triggers the job at 3:30 AM GMT on the 5th day of every month. Converting this to IST, the job runs at 9:00 AM IST on the 5th day of each month.

  2. The command failedbulkdemand is used to instruct the Python script to invoke only the API configured as "failedbulkdemand" in the mdms-read-cronjob.mdms JSON file.

Configuration Details

Infraops Configuration Changes

PriorNote: In DevOps, the app name and schedule change based on the cron job file and set time. Arguments are set according to the job name in MDMS configuration.

labels: App: monthly-cronjob Group: mdms-read-cronjob <!-- Stays the same as we use the same Python script

cron: schedule: 30 3 4 * * // This depends on the time we need to run the scheduler

image: repository: api-cronjob tag: v1

command:

  • python3

  • cronJobAPIConfig.py

args:

  • monthly // This is the job name which differs from the requirement based on the scheduler type.

env:

name: JOB_NAME

valueFrom:

fieldRef:

fieldPath: metadata.name

resources: |

requests: {}

The remaining fields will be the same for all the schedulers.

Monthly: This will run and send the notification to the ULB employee or consumer on the 4th of every month morning at 9 am as per the scheduled time.

Fortnightevening: This scheduler will run on the 1st and 15th of every month evening at 6 pm to send the respective notification to the Consumer.

Failedbulkdemand: When the bulk demand generation is failed this scheduler will run and share the message to ULB employees to generate demand manually.

Dailyevening: This scheduler will run daily and send notifications to the collection operator daily.

Check the links below

MDMS object details and configuration:

{

"jobName": "monthly", // This will change based on the job name

"active": "true", // when the "active" param is set to true, the scheduler runs automatically. The scheduler does not run when set to false.

"method": "POST",

"url":http://echallan-services.mgramseva:8080/echallan-services/eChallan/v1/_schedulermonthsummary", // This is the respective service URL to call that service as per the scheduler.

"payload": {

"RequestInfo": "{DEFAULT_REQUESTINFO}" // this is common in all the schedulers used to send the request info.

},

"header": {

"Content-Type": "application/json" // This is a common property for all schedulers.

}

}

Here is the configuration for all the schedulers: Click here to see.

{

      "jobName": "monthly",

      "active": "true",

      "method": "POST",

      "url": "http://ws-calculator.mgramseva:8080/ws-calculator/waterCalculator/_jobscheduler/false",

      "payload": {

        "RequestInfo": "{DEFAULT_REQUESTINFO}"

      },

      "header": {

        "Content-Type": "application/json"

      }

    },

    {

      "jobName": "fortnightevening",

      "active": "true",

      "method": "POST",

      "url": "http://ws-services.mgramseva:8080/ws-services/wc/_schedulerpendingcollection",

      "payload": {

        "RequestInfo": "{DEFAULT_REQUESTINFO}"

      },

      "header": {

        "Content-Type": "application/json"

      }

    },

    {

      "jobName": "monthly",

      "active": "true",

      "method": "POST",

      "url": "http://echallan-services.mgramseva:8080/echallan-services/eChallan/v1/_schedulermonthsummary",

      "payload": {

        "RequestInfo": "{DEFAULT_REQUESTINFO}"

      },

      "header": {

        "Content-Type": "application/json"

      }

    },

    {

      "jobName": "fortnightevening",

      "active": "true",

      "method": "POST",

      "url": "http://echallan-services.mgramseva:8080/echallan-services/eChallan/v1/_schedulernewexpenditure",

      "payload": {

        "RequestInfo": "{DEFAULT_REQUESTINFO}"

      },

      "header": {

        "Content-Type": "application/json"

      }

    },

    {

      "jobName": "fortnightevening",

      "active": "true",

      "method": "POST",

      "url": "http://echallan-services.mgramseva:8080/echallan-services/eChallan/v1/_schedulermarkexpensebill",

      "payload": {

        "RequestInfo": "{DEFAULT_REQUESTINFO}"

      },

      "header": {

        "Content-Type": "application/json"

      }

    },

    {

      "jobName": "failedbulkdemand",

      "active": "true",

      "method": "POST",

      "url": "http://ws-calculator.mgramseva:8080/ws-calculator/waterCalculator/_jobscheduler/true",

      "payload": {

        "RequestInfo": "{DEFAULT_REQUESTINFO}"

      },

      "header": {

        "Content-Type": "application/json"

      }

    },

    {

      "jobName": "dailyevening",

      "active": "true",

      "method": "POST",

      "url": "http://ws-services.mgramseva:8080/ws-services/wc/_schedulerTodaysCollection",

      "payload": {

        "RequestInfo": "{DEFAULT_REQUESTINFO}"

      },

      "header": {

        "Content-Type": "application/json"

      }

    }

Create User

Need to create a user with CRONJOB as name and type as SYSTEM and ROLE as SYSTEM AND EMPLOYEE here is the sample curl to create the user.

curl --location --request POST 'http://localhost:8090/user/users/_createnovalidate'

--header 'Content-Type: application/json'

--data-raw '{
  "RequestInfo": {
    "api_id": "1",
    "ver": "1",
    "ts": null,
    "action": "create",
    "did": "",
    "key": "",
    "msg_id": "",
    "requester_id": "",
    "userInfo": {
      "userName": "XXYY",
      "name": "NAME",
      "gender": "male",
      "mobileNumber": "XXYY",
      "active": true,
      "type": "EMPLOYEE",
      "tenantId": "pb",
      "password": "eGov@123",
      "roles": [
        {
          "code": "SUPERUSER",
          "tenantId": "pb"
        }
      ]
    }
  },
  "User": {
    "userName": "CRONJOB",
    "name": "CRONJOB",
    "gender": "male",
    "mobileNumber": "XXXXXX",
    "active": true,
    "type": "SYSTEM",
    "tenantId": "pb",
    "password": "eGov@123",
    "roles": [
      {
        "code": "SYSTEM",
        "tenantId": "pb"
      }
    ]
  }
}'

Deployment Details

A Build ID (similar to the ID given below) is created when you build the cronjob. api-cronjob:develop-c0aa08a-2

Take the id only from this instead of a complete name like develop-c0aa08a-2. This id will be used as the id for your respective yaml files and the same will be deployed to the required environment to test the cron job.

For example:

Mdms-read-cronjob:develop-c0aa08a-2,

failedbulkdemand:develop-c0aa08a-2,

Fortnightevening:develop-c0aa08a-2,

monthly:develop-c0aa08a-2 Note: develop-c0aa08a-2 is the common build id for all the files which you are using.

Run the cronjob manually - Steps

  • Delete the existing cron jobs if they already exist with the same name.

  • kubectl delete cronjob mdms-read-cronjob -n mgramseva

  • Deploy these builds in QA environments, which are related to cronjob schedulers

  • mdms-read-cronjob:develop-c0aa08a-2, failedbulkdemand:develop-c0aa08a-2, fortnightevening:develop-c0aa08a-2,monthly:develop-c0aa08a-2

Test the cron job scheduler - Steps

  • kubectl get cronjob -n mgramseva -- to check the list of cron jobs

  • Create the job manually to test the messages. Below are the commands to create the jobs.

    • A message is received for the respective schedulers each time we run it.

    • We can increase the number to test again like failedbulkdemand-manually-1 next it will be failedbulkdemand-manually-2.

    • kubectl create job --from=cronjob/failedbulkdemand failedbulkdemand-manually-1 -n mgramseva

    • kubectl create job --from=cronjob/fortnightevening fortnightevening-manually-1 -n mgramseva

    • kubectl create job --from=cronjob/mdms-read-cronjob mdms-read-cronjob-manually-1 -n mgramseva

    • kubectl create job --from=cronjob/monthly monthly-manually-1 -n mgramseva

    • kubectl get job -n mgramseva -- to check the list of jobs

To check the cronjob image

kubectl describe cronjob mdms-read-cronjob -n mgramseva

To delete specific job

kubectl delete jobs mdms-read-cronjob-manually-1 -n mgramseva

Last updated

All content on this page by eGov Foundation is licensed under a Creative Commons Attribution 4.0 International License.