April 2, 2021 By Henrik Loeser 4 min read

How to set up scheduled tasks on IBM Cloud.

A few weeks ago, I stumbled over one of my private blog posts from 2015. It discusses kicking off activities using time-based, cron-like scheduling — all done in Python on Bluemix with Cloud Foundry. 

It is not just the name Bluemix which is dated (small pun). Since then, it has transformed into IBM Cloud Platform and has added serverless compute options like IBM Cloud Functions (OpenWhisk) and, more recently, IBM Cloud Code Engine. Both of them support “eventing” — event-based execution of code. Both also support Python code and many other programming languages like Node.js, Go (Golang), PHP and more. So, what does it take to set up cron-like scheduling? Not much.

In the following, I am going to provide a quick introduction into cron. Thereafter, I am taking the scenario of scheduled tweets (see screenshot below) to showcase cron-like events in Code Engine and Cloud Functions. The code is available on GitHub:

Twitterbot with scheduled tweets.

Cron

The utility cron is a time-based scheduler for tasks (jobs) on UNIX/Linux systems. Events are specified by providing a set of values for minute, hour, day of the month, month and day of the week. Values are either an explicit number, a comma-separated list of numbers or the wildcard “*”.

20 4,9,13,18 * * 1,3,5

The above would cause an event at 20 minutes past the hour at 4 am, 9 am, 1 pm and 6 pm (i.e., at 04:20, 09:20, 13:20 and 18:20). The event would only happen on Mondays (1), Wednesdays (3) and Fridays (5).

Alarms and pings

Cloud Functions features several built-in packages, and some of them provide events. One of them is the alarm package — it allows you to set up time-based triggers. A trigger can be associated with a Functions action by a rule. That way, an event can trigger multiple actions.

Code Engine has event producers, too. A so-called ping subscription serves for cron-like time events. Subscriptions are directly associated with a destination (app) and a path.

Aaaand: Action!

Knowing the concepts of cron, Cloud Functions triggers and rules and Code Engine ping subscriptions, it is time for some fun and action. I am using a simple Twitterbot which I wrote in Go (Golang) and deployed as Code Engine app (Application in the diagram below). The app tweets a message like the one shown above when invoked, with the content coming from a blog feed (RSS/Atom). The bot has some default values for the blog feed to use and how the tweet should be composed. But, it also allows customization as part of the payload when invoked:

Architecture for cron-scheduled tweets.

The Twitterbot is called from a Code Engine ping subscription which was created similar to this:

ibmcloud ce sub ping create -n tweety --destination twitterbot --path /tweet --data

'{"secret_key":"SET_YOUR_SECRET","tweet_string2":"Written in #Golang by @data_henrik and running on #IBMCloud #CodeEngine"}' --content-type 'application/json' --schedule '07 9,17 * * *'

The destination is the twitterbot at the path /tweet. The data to pass in (payload) in JSON format has a key/value pair for a shared secret to secure the API. The second key/value is tweet_string2 to customize the Twitter status message. The bot is called twice daily at 09:07 UTC and 17:07 UTC.

I also created a Cloud Functions action showing a generic “dispatcher.” The action decodes incoming parameters and reacts to them. In the sample scenario, it sets up a POST request to the Twitterbot and passes on the included payload. The payload is specified as part of the trigger definition:

ibmcloud fn trigger create cf_tweety --feed /whisk.system/alarms/alarm --param cron "22 10 * * *" 
--param trigger_payload '{"CE_TWEET": { "url":"https://twitterbot.xxxxxxxxxx.us-south.codeengine.appdomain.cloud/tweet", "payload":{"secret_key":"SET_YOUR_SECRET","tweet_string2":"Triggered by #OpenWhisk and dispatched in #Python on #IBMCloud" }}}'

The above trigger fires at 10:22 UTC daily. The trigger_payload includes the configuration for the Code Engine tweet (CE_TWEET). It is the URI to invoke with a POST request and the actual payload to pass. The latter is the secret_key and a tweet_string2 again. To associate the trigger with the dispatch action, we need a rule like this:

ibmcloud fn rule create myTweetRule cf_tweety dispatch

Conclusions

Since my early tests with Python scheduling, it has become much, much simpler to set up cron-like scheduling. IBM Cloud offers IBM Cloud Functions and IBM Cloud Code Engine as serverless compute options, each of them with eventing functionality. Both support the standard cron syntax for time events and allow you to execute your tasks directly or use them as wrapper for something different. This could be calls into webhooks or the invocation of other functions, apps or jobs to perform administrative or housekeeping tasks and more.

The code for the Twitterbot and the dispatch function is available on GitHub.

Check out this IBM Cloud solution tutorial on serverless web app and eventing for data retrieval and analytics.

If you have feedback, suggestions, or questions about this post, please reach out to me on Twitter (@data_henrik) or LinkedIn.

Was this article helpful?
YesNo

More from Cloud

How a US bank modernized its mainframe applications with IBM Consulting and Microsoft Azure

9 min read - As organizations strive to stay ahead of the curve in today's fast-paced digital landscape, mainframe application modernization has emerged as a critical component of any digital transformation strategy. In this blog, we'll discuss the example of a US bank which embarked on a journey to modernize its mainframe applications. This strategic project has helped it to transform into a more modern, flexible and agile business. In looking at the ways in which it approached the problem, you’ll gain insights into…

The power of the mainframe and cloud-native applications 

4 min read - Mainframe modernization refers to the process of transforming legacy mainframe systems, applications and infrastructure to align with modern technology and business standards. This process unlocks the power of mainframe systems, enabling organizations to use their existing investments in mainframe technology and capitalize on the benefits of modernization. By modernizing mainframe systems, organizations can improve agility, increase efficiency, reduce costs, and enhance customer experience.  Mainframe modernization empowers organizations to harness the latest technologies and tools, such as cloud computing, artificial intelligence,…

Modernize your mainframe applications with Azure

4 min read - Mainframes continue to play a vital role in many businesses' core operations. According to new research from IBM's Institute for Business Value, a significant 7 out of 10 IT executives believe that mainframe-based applications are crucial to their business and technology strategies. However, the rapid pace of digital transformation is forcing companies to modernize across their IT landscape, and as the pace of innovation continuously accelerates, organizations must react and adapt to these changes or risk being left behind. Mainframe…

IBM Newsletters

Get our newsletters and topic updates that deliver the latest thought leadership and insights on emerging trends.
Subscribe now More newsletters