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
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:
Best regards,
Egor
Thanks for the input Egor Tokarev . Ya the scheduling services have limitations to have chaining of services.
Hi Egor Tokarev,
Thanks! for the valuable input.
We tried and it seems to work for us as well, but our concern is related to security. As we want to use this for productive environment on BTP, we are not sure if the library/corn npm package, is secure enough and regular updates will be managed by anyone? Also as this is not a sap standard, we are bit reluctant to go ahead with it for production.
What is your view/suggestion on this?
Thanks,
Rakesh
Hi Rakesh,
That's of course up to your company's policy whether to use opensource packages or create your own.
My personal recommendation is to use strict dependency version numbers to avoid conflicts with newer package versions.
Regards,
Egor
Nice blog. Keep sharing 🙂
Sure. Thanks Nandakumar S Nair .
Hi Sunoj Michael,
We are also having the same use case, but our CAP Service have XSUAA authentication. So job scheduler getting 401 unauthorized error.
I have configured job scheduler instance in mta.yaml. Like below. Not sure what are we missing here.
Thanks,
Babu
HI Babu Pasupathy ,
To add in XSUAA support, we need to have two factors to consider.
type: org.cloudfoundry.managed-service
parameters:
service: jobscheduler
service-plan: standard
service-name: jobscheduler
config:
enable-xsuaa-support: true
2. In CAP SRV get the job-scheduler related credentials from env.
Hope this helps.
Thanks
Sunoj
Hi Sunoj Michael,
Thanks for creating this blog.
It's great if you share some details about schedule a job using "job scheduling service instance" for the ABAP Restful programming applications?
I would like to know how to ACTION URL get determined for ABAP Restful programming applications?
Thanks,
Krishna.
Great, thanks for sharing.