const JobSchedulerClient = require('@sap/jobs-client');
const xsenv = require('@sap/xsenv');
const jobSchedulerCreds = xsenv.serviceCredentials({ tag: 'jobscheduler' });
const jwt = require('../utility/jwt');
const createJob = async (req, logger) => {
try {
const subdomain = req.authInfo.getSubdomain();
const domain = `https://${subdomain}.${jobSchedulerCreds.uaa.uaadomain}`;
const token = await jwt(domain, jobSchedulerCreds);
const options = {
baseURL: `${jobSchedulerCreds.url}`,
token: token.accessToken,
};
const scheduler = new JobSchedulerClient.Scheduler(options);
// name field is where we are trying to generate a unique name for the job
const myJob = {
name: `${subdomain.split("-")[0] + new Date().getMilliseconds()}`,
description: "cron job that calls HTTP endpoint",
action: `${process.env.businessPartnerAPI}/api/v1/new/bp`,
// action: URL to the backend service which should be called periodically
active: true,
httpMethod: "GET",
schedules: [
{
cron: `* * * * * */${req.query.time} 0`,
// cron: frequency of job
description: `this schedule runs every ${req.query.time} minutes to fetch the tenant data and find new businesspartners`,
active: true,
startTime: {
date: `${new Date().toISOString().split('T')[0]} 00:00 +0000`,
format: "YYYY-MM-DD HH:mm Z",
},
},
],
};
const scJob = { job: myJob };
return new Promise((resolve, reject) => {
scheduler.createJob(scJob, (error, body) => {
if (error) {
logger.error('Error registering new job %s', error);
return reject(error);
}
return resolve(body);
});
});
} catch (schedulererr) {
logger.error(schedulererr);
throw new Error("Error While creating the job");
}
};
const getJob = async (req, logger) => {
try {
const subdomain = req.authInfo.getSubdomain();
const domain = `https://${subdomain}.${jobSchedulerCreds.uaa.uaadomain}`;
const token = await jwt(domain, jobSchedulerCreds);
const options = {
baseURL: `${jobSchedulerCreds.url}`,
token: token.accessToken,
};
const scheduler = new JobSchedulerClient.Scheduler(options);
const data = {};
return new Promise((resolve, reject) => {
scheduler.fetchAllJobs(data, (err, result) => {
if (err) {
logger.error('Error retrieving jobs: %s', err);
return reject(err);
}
// Jobs retrieved successfully
logger.info(result)
return resolve(result);
});
});
} catch (errr) {
logger.error(errr);
throw errr;
}
};
Here's how it looks from the Admin dashboard
Develop a multitenant Software as a Service application in SAP BTP using CAP
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
35 | |
25 | |
14 | |
13 | |
7 | |
7 | |
6 | |
6 | |
5 | |
5 |