Skip to Content
Technical Articles

SAP BTP Cloud foundry Job Scheduling for CAP based project

Hi All,

Intro:

We might come across scenarios where we must create a CAP based application that needs to have a schedule option as well. The job had to be executed at specific intervals, like may be in every 15 seconds or so.

SAP BTP Cloud foundry have this service available in ‘Service Marketplace’ for scheduling jobs. We can just create an instance of it and bind it to our application.

This blog post explains how to create a job in Cloud foundry and schedule it for a CAP based project.

We can think of our task having two parts

A CAP Application

For my testing purpose I used the bookshop application in sap-cap-samples.

The URL for github repo: https://github.com/SAP-samples/cloud-cap-samples

In  CAP Project, you would need to have a task that needs to be running at specific intervals, for example, sending an Email Notification or doing some data processing etc.

We can use the concept of custom action/functions for the same. What I did is, I modified the cap-samples bookshop application service at cat-service.cds file and added a custom action handleNotification ( ) which can send out email notifications, but your use-case would be something different.

 

//at cat-service.cds

using { sap.capire.bookshop as my } from ‘../db/schema’;

// @impl:’cat-service.js’

service CatalogService @(path:’/browse’) {

action handleNotification ( );

}

 

The email implementation part at cat-service.js. Note that, I am using an email provider called sendgrid for sending emails.

//at cat-service.js

const sgMail = require('@sendgrid/mail')
const cds = require('@sap/cds')
const { Books } = cds.entities ('sap.capire.bookshop')

class CatalogService extends cds.ApplicationService { init(){

//Sent Email with Job
this.on ('handleNotification', async req=> {

  //to sent email    
sgMail.setApiKey(‘YOUR_API_KEY')

const msg = {
  to: '<abc@gmail.com>', // Change to your recipient
  from: 'xyz@gmail.com, // Change to your verified sender
  subject: 'Your Subject',
  html: '<strong> YOUR EMAIL CONTENT </strong>',
  attachments: [{"content": "dGVzdA==", "type": "text/plain", "filename": "attachment.txt"}]
}

 sgMail
   .send(msg)
   .then(() => {
     console.log('Email sent')
   })
   .catch((error) => {
//custom error message
      req.error(400, 'Error Sending Notification')
   })
})

module.exports = { CatalogService }

Now that the code changes are done, kindly deploy your application in cloud foundry.

 

Next, Scheduling part

Now that we are having a running bookshop backend service in cloud foundry, we can proceed with the scheduling a job.

Just search for job in ‘Service Marketplace’

 

Create an instance of it

You can provide any name as ‘Instance Name’ I went with testjobemail.

And bind to your application

Now, for the job instance you would be having the ‘View Dashboard’ option available.

You can create a job in dashboard with necessary details as shown in figure

Endpoint

https://ea5ce15etrial-dev-bookshop-srv.cfapps.eu10.hana.ondemand.com/browse/handleNotification

 

Next is to Create Schedule, its in schedule that we can provide the interval for executing the job.

You can see the logs for each run as well

 

 

Conclusion:

We implemented a job and scheduled it for a CAP Service end point.

By the way, I used the trial subaccount for scheduling which have limited features like I cannot create repeating runs. But if it’s a paid version, I would be able to.

We can optimize the Service creation and binding of application by adding the details in deployer file mta.yaml.

Hope you find the blog post useful!

Thanks

Sunoj

4 Comments
You must be Logged on to comment or reply to a post.
  • Thanks for sharing your experience. Recently we had to do the same task of scheduling a job but we've found that standard scheduling service allows only one REST service call.

    We've ended up with implementing scheduler job with cron npm package and bootstrapped it via "srv/server.js" file (anyway we had odata V2 adapter proxy) as follows:

    const cds = require("@sap/cds");
    const proxy = require("@sap/cds-odata-v2-adapter-proxy");
    cds.on("bootstrap", app => app.use(proxy()));
    
    //jobs bootstrap
    const { runJob } = require("./scheduler");
    cds.once("listening", () => runJob())
    
    module.exports = cds.server;

    Best regards,

    Egor