An overview of different ways to schedule REST API calls with IBM Cloud offerings.

IBM Workload Scheduler on IBM Cloud was deprecated in March of 2019, and some people worried that scenarios in which that service was used—in particular, scheduling REST API calls—would no longer be covered by IBM Cloud service offerings.

Fortunately, if you need to schedule a REST API call periodically (just to see the output without any post-process), you can still use IBM Cloud Kubernetes Service, Cloud Foundry on IBM Cloud, or IBM Cloud Functions. This post will outline how to use the different offerings.

CronJob in IBM Cloud Kubernetes Service

Kubernetes allows you to define a scheduled periodic job using a pre-defined controller: <a data-entity-substitution="" data-entity-type="" data-entity-uuid="" href="https://kubernetes.io/docs/concepts/workloads/controllers/cron-jobs/" target="_blank">CronJob</a>.

CronJobs work like Cron in Unix systems and are very simple to setup. Please see the example below:

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: hello
spec:
  schedule: "*/1 * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: getFoo
            image: busybox
            args:
            - /bin/sh
            - -c
            - curl -X GET http://www.example.com/bar/foo
          restartPolicy: OnFailure

In this example, we define a REST API call to http://www.example.com/bar/foo each minute.

CronJobs in Cloud Foundry

Cloud Foundry is not generally used to run periodic jobs, so there is no native solution to schedule REST API calls. However, you could define a silly application in whatever programming language you prefer to run this periodic job.

For example, using Node.js as runtime:

const request = require(‘request’);

const doTheRequest = () => {
    request({
        uri: "http://www.example.com/bar/foo",
	 method: "GET"	
    }, function (error, response, body) {
        console.log('response', body); 
    });

setInterval(doTheRequest, 60000);

This approach for periodic jobs would be useful in some cases (e.g., tests), but it is not recommended. The application could face some issues due to the health check Cloud Foundry performs for each app.

To adopt this approach, it would be useful to disable health check during the application push phase by using the following command: 

ibmcloud cf push <APPNAME> -u process --no-route

Periodic Actions in IBM Cloud Functions

The serverless computing approach allows for an easy way to create periodic jobs. With IBM Cloud Functions (based on the open source project Apache OpenWhisk), it is possible to create Actions (stateless code snippets) configured to execute a specific task, which can be triggered/invoked periodically.

For example, you could define an Action using Node.js as runtime with the following code:

/**
  *
  * main() will be run when you invoke this action
  *
  * @param Cloud Functions actions accept a single parameter, which must be a JSON object.
  *
  * @return The output of this action, which must be a JSON object.
  *
  */
function main(params) {
    const request = require(‘request’);
    request({
        uri: "http://www.example.com/bar/foo",
	 method: "GET"	
    }, function (error, response, body) {
        console.log('response', body);
        return { message: body };
    });
}

Once the Action is created, you could transform it into a periodic job linking it to a “Periodic” type Trigger.

Summary

These are a few short examples that illustrate how to schedule REST API calls with different offerings in IBM Cloud.

The right approach depends on your knowledge and how complex the post-processing may be. As you can see, the serverless approach is the simplest one, and you can test it yourself by following the steps described here.

If you are looking for a more customized approach, try using CronJobs in the manner described here.

Categories

More from Cloud

Kubernetes version 1.28 now available in IBM Cloud Kubernetes Service

2 min read - We are excited to announce the availability of Kubernetes version 1.28 for your clusters that are running in IBM Cloud Kubernetes Service. This is our 23rd release of Kubernetes. With our Kubernetes service, you can easily upgrade your clusters without the need for deep Kubernetes knowledge. When you deploy new clusters, the default Kubernetes version remains 1.27 (soon to be 1.28); you can also choose to immediately deploy version 1.28. Learn more about deploying clusters here. Kubernetes version 1.28 In…

Temenos brings innovative payments capabilities to IBM Cloud to help banks transform

3 min read - The payments ecosystem is at an inflection point for transformation, and we believe now is the time for change. As banks look to modernize their payments journeys, Temenos Payments Hub has become the first dedicated payments solution to deliver innovative payments capabilities on the IBM Cloud for Financial Services®—an industry-specific platform designed to accelerate financial institutions' digital transformations with security at the forefront. This is the latest initiative in our long history together helping clients transform. With the Temenos Payments…

Foundational models at the edge

7 min read - Foundational models (FMs) are marking the beginning of a new era in machine learning (ML) and artificial intelligence (AI), which is leading to faster development of AI that can be adapted to a wide range of downstream tasks and fine-tuned for an array of applications.  With the increasing importance of processing data where work is being performed, serving AI models at the enterprise edge enables near-real-time predictions, while abiding by data sovereignty and privacy requirements. By combining the IBM watsonx data…

The next wave of payments modernization: Minimizing complexity to elevate customer experience

3 min read - The payments ecosystem is at an inflection point for transformation, especially as we see the rise of disruptive digital entrants who are introducing new payment methods, such as cryptocurrency and central bank digital currencies (CDBC). With more choices for customers, capturing share of wallet is becoming more competitive for traditional banks. This is just one of many examples that show how the payments space has evolved. At the same time, we are increasingly seeing regulators more closely monitor the industry’s…