XS Job Schedule

Introduction:

The XSJob file enables you to run a service (for example, an XS JavaScript or a SQLScript) at a scheduled interval.
In this article, we will explain how to schedule a job that triggers an XS JavaScript application.

Prerequisites:
Use-case:
In this example we will create a job that triggers an XS JavaScript application that
  • Reads the latest value of a share price from a public financial service available on the Internet.
  • Update the stock value for SAP DE in HANA table with current timestamps.
The job should run every 10th second.

Create Table:

Copy paste the below SQL script to create table.
--REPLACE <YOUR_SCHEMA> WITH YOUR SCHEMA NAME
CREATE COLUMN TABLE <YOUR_SCHEMA>.STOCK (
CREATED_AT TIMESTAMP PRIMARY KEY,
STOCK_VALUE FLOAT
)

Create XSJob to get SAP stock value and update in HANA table:

1. Create HANA XS Project as mentioned in Create Your First HANA XS Application using HANA Studio

Note: We highly recommend you that you create a package with the name “xsjob-tutorial” in Content and create the project inside it. Otherwise you will have to change the package reference in XSJS and XSJob files.
2. Create a package inside the project called “jobs” and create 3 files
  1. yahoo.xsjob // job schedule definition
  2. yahoo.xshttpdest // HTTP destination details
  3. yahoo.xsjs // Script to run on schedule



3. Open yahoo.xsjs file and paste below code.

function readStock(input) {
    var stock = input.stock;
    var dest = $.net.http.readDestination("xsjob-tutorial.jobs", "yahoo");
    var client = new $.net.http.Client();
    var req = new $.web.WebRequest($.net.http.GET, "/d/quotes.csv?f=a&s=" + stock);
    client.request(req, dest);
    var response = client.getResponse();
    var stockValue;
    if (response.body){
       stockValue = parseInt(response.body.asString(), 10);
    }
    var sql = "INSERT INTO SAP_HANA_TUTORIAL.STOCK VALUES (NOW(), ?)";
    var conn = $.db.getConnection();
    var pstmt = conn.prepareStatement(sql);
    pstmt.setDouble(1, stockValue);
    pstmt.execute();
    conn.commit();
    conn.close();
}


Note: You may have to change the package name in the line
$.net.http.readDestination("xsjob-tutorial.jobs", "yahoo");

4. Open the HTTP destination file yahoo.xshttpdest and paste below content.
host = "download.finance.yahoo.com";
port = 80; 
description = "my stock-price checker";
useSSL = false;
pathPrefix = "/d/quotes.csv?f=a";
authType = none;
useProxy = false;
proxyHost = "";
proxyPort = 0;timeout = 0;

Note: In case you are behind a proxy, then change the proxy settings. For example:
useProxy = true;
proxyHost = "proxy";
proxyPort = 8080;

5. Open XS job file and paste below content

{
   "description": "Read stock value",
   "action": "xsjob-tutorial.jobs:yahoo.xsjs::readStock",
   "schedules": [{
       "description": "Read current stock value every 10 second",
       "xscron": "* * * * * * 0:59/10",
       "parameter": {
           "stock": "SAP.DE"
       }
   }]
}

Note: You may have to change the package name in the line
"action": "xsjob-tutorial.jobs:yahoo.xsjs::readStock",

6.Save and activate all the files.
7.Open HANA Admin tool. The URL of Admin tool is: 
http://<WebServerHost>:80<SAPHANAinstance>/sap/hana/xs/admin/

8. Activate the job. Specify User name and other details and save the job.


9. Check the job logs to ensure the XS job is active and running according to the defined schedule.

You can open the table STOCK and see the data inserted into it.


Download Full Source Code:

Click here to download the full source code.

Continue reading:

2 comments: