Skip to Content
Technical Articles
Author's profile photo Andrew Lunde

SAP Cloud Platform Job Scheduler service, Recasting the Webhook.

This is a follow up blog posting to SAP Cloud Platform Job Scheduler service, I’ll get to that later. where I first introduced the sample code project mta-job-schedule.

In this blog post, I’ll focus on the use case of using the job scheduler for creating jobs that call an HTTP endpoint on a repeated(recasted) basis.  As an added bonus, I’ll use an example of calling a Jenkins style webhook endpoint to trigger a build job on a scheduled basis.

If you just need an example of how to form the proper POST request, jump to the srv/server.js file and look at the app.post and app.get “/util/trigger” routes or search for JOB_SCHED_SECRET_TOKEN on how to form and check a proper signature.

WHY DO THAT?

 

Now you may be wondering WHY would I want to build a project over and over if nobody’s making contributions to the git repository and implicitly triggering a webhook?  Well, all too often, other things change that might cause a project build to fail.  Libraries get deprecated, environments get upgraded, and security patches get raised.  Any of these can cause a previously happy build to start failing.  If this happens, at least you have a list of failing builds that you work against in order to get them back to building properly.  And if your build system is set to notify you, you’ll get nagging notifications until you fix things back to working.

We’re going to use the SAP Cloud Platform Continuous Integration and Delivery service as our target build server, but you could just your own Jenkins or Azure DevOps etc.  Because I don’t want to distribute this example with my server’s details, I’ve absctracted them out into a set of environment variable that can be either set in the mta.yaml file or with the cf set-env command after the deploy.

Check the README.md file for details, but I’ll do something like this.

cf set-env job-sched-srv JOB_SCHED_APP_URL 'https://<subdomain>-dev-job-sched-app.cfapps.<landscape>.hana.ondemand.com'
cf set-env job-sched-srv JOB_SCHED_CICD_UI 'https://subdomain.cicd.cfapps.<landscape>.hana.ondemand.com/ui/index.html'
cf set-env job-sched-srv JOB_SCHED_WEBHOOK_URL 'https://cicd-service.cfapps.<landscape>.hana.ondemand.com/v1/github_events/account/<your-account-guid>'
cf set-env job-sched-srv JOB_SCHED_SECRET_TOKEN '<your-webhook-secret-token>'
cf restage job-sched-srv

Browse to your application URL.  You should see something like this. That little icon to the right is the link to the your job scheduler dashboard.

If you click on the /sched/ link you’ll get this (I know, very boring) list of links.

For most of these links, refer to my earlier blog post, but let’s focus on the ones near the bottom of the page.

If you’ve set the JOB_SCHED_CICI_UI properly you should see this link on the right.

Since the actual calling of the URL by the scheduler is done by the scheduler process, you can’t see what’s happening in the browser.  Open up a terminal on your workstation and run the cf logs command.

cf logs job-sched-srv

Click on the date_in_1_min link.  This will schedule a job to be run in 1 minute that gets the server data.  Watch the logs and you’ll see the request get triggered.  Then after a minute you should see the following in the log output.

While you’re waiting, you can click on the get_all_jobs link, and then at the bottom of the json, there is a link to the schedules, click that.

Then click to get the particular job schedule.

If you’re fast enough, you’ll see that the job run is in the SCHEDULED state.

And then a minute later along with a bunch of log output, you should see..

Now go back and refresh the browser window with the get_run_logs url.  You should see that the runStatus is now COMPLETED and the runState is SUCCESS.

NOW TRIGGER THE WEBHOOK

 

If we click on the build_in_1_min link and you’ve set the JOB_SCHED_WEBHOOK_URL and JOB_SCHED_SECRET_TOKEN variables for your specifics, you should now see a build appear in one minute.  If you’re using this service, remember you can find the details by clicking Webhook Data.
And magically after a minute(and waiting for the UI to refresh) we see there’s a new build underway!

 

MAKE IT YOUR OWN

 

Once you’re happy that the build is getting triggered.  Open up the srv/server.js file and look at the

/sched/create_job route at line 156.  This link creates a job that runs once a day.  Make your own version of this to schedule your build once a week by altering the cron string and the action fields to meet your needs.  You can find all the detail in the cron format docs.
Rebuild and redeploy!
You can see the status of your jobs by looking at the job scheduler dashboard or by using the get_all_jobs link.  The deleteURL links at the bottom are a handy way to clean things up.
Note: Be sure to delete all jobs before you attempt to undeploy your application.

 

Let me know if you have and questions or issues by leaving me a question below or better yet, asking it on the SAP community.

-Andrew

Partners: If you have a question, click here to ask it in the SAP Community . Be sure to tag it with Partnership and leave your company name in the question so that we can better assist you.

 

Assigned tags

      2 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Yogananda Muthaiah
      Yogananda Muthaiah

      Hi Andrew,

      Its quite interesting to try it. it makes job easy and if email triggers for Job Success and fail, it will be too great for developers.

      Author's profile photo Wolfgang Mayer
      Wolfgang Mayer

      Hi Andrew,

      I've posted my question here: https://answers.sap.com/questions/13124134/schedule-background-job-for-updating-business-data.html

      Regards

      Wolfgang