Another OpenWhisk Cron Example — the Blog Nag

Raymond Camden
Feb 24, 2017 · 3 min read

Last week I blogged about my first experience working with OpenWhisk triggers and rules, specifically the Cron trigger which lets you execute actions according to a schedule. Today I’m sharing another example, which, while not as complex as the 911 scraper, I thought was kind of fun.

As a blogger, I try to keep to a certain amount of posts per month. While I a absolutely care more about quality than quantity, I still try to maintain a certain amount of content per month. I thought it would be fun to create an OpenWhisk action that would nag me if I hadn’t blogged in a few days. This turned out to be rather simple:

  • First, I get the RSS feed.
  • Then I parse the XML. There’s packages to read RSS, but there’s also xml2js which just does a basic conversion.
  • I can then check the date of the most recent article and compare it to now.
  • If it’s been too long, nag!

Let’s start with the action:

const request = require('request');
const parseString = require('xml2js').parseString;

Start with the main function which is OpenWhisk’s entry point to the function. I use the request library to open up my RSS feed and then parseString from the xml2js library. I can then get the most recent blog entry (which is the first entry in a RSS feed) and make a date object with it.

Once I have that — then it’s math. I set the constant NAG_DAY to 2, which is a bit too low if you ask me, but I had blogged on Friday so I needed a value that would trigger the alert. (For folks curious, I try to blog once every 3 days.) If we need to nag, we then simply call doNag.

The doNag function just writes an email using the Sendgrid API and fires it out. And that’s it.

So then I had to make this “live” — which beforehand would have meant provisioning a server and all that, but with the wonders of Serverless (yes, I’m half-joking here ;) I just did the following:

  • Sent the action up to OpenWhisk with the CLI (wsk action create --kind nodejs:6 rssnag rssnag.zip)
  • Made the trigger (wsk trigger create checkBlog --feed /whisk.system/alarms/alarm --param cron "* * 1 * *"). That Cron value is for once a day, and yes I had to use http://crontab-generator.org again.
  • Made the rule (wsk rule create blogNagRule checkBlog rssnag)

And that’s it. To test I used the OpenWhisk UI on IBM Bluemix and manually triggered it. And the result….

Image for post
Image for post

This post originally appeared on my blog at https://www.raymondcamden.com/2017/02/21/another-openwhisk-cron-example-the-blog-nag

Apache OpenWhisk

Apache OpenWhisk is a serverless cloud platform that…

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store