How to set up scheduled tasks on IBM Cloud.
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:
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 "*".
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.
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:
The Twitterbot is called from a Code Engine ping subscription which was created similar to this:
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:
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:
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.
Check out this IBM Cloud solution tutorial on serverless web app and eventing for data retrieval and analytics.