Scheduling Recurring Code Execution Through Serverless Framework

josh-holmgrem-scheduling-events

Motivation

Running code on a regular interval is a pretty common task in software development. With such common use cases as:

  • manual backup of a database

  • cleaning up files on a regular interval

  • implementing a time to live (TTL) on resources

  • polling an API on a regular interval

  • re-subscribing to expired event notifications

  • sending emails on a regular basis

The first impulse for many developers that need to accomplish such tasks is to start writing a script of a language of their choice and then scheduling execution via the commandline tool, cron. Then, once the script gains uptime requirements, web hosting, daemonization, scaling, and continuous deployment all become concerns. These supporting tasks may end up becoming more work than coding up the actual work you are trying to get done in the first place.

Fortunately, the big players in cloud computing services have solutions aimed at reducing the boiler-plate involved in running code in the cloud. These solutions are commonly referred to as Serverless. Serverless is one step beyond traditional PaaS offerings. With Severless your code takes minimal time to "spin up" for the first time, only runs when invoked, is typically cheaper than persistent servers when those servers are underutilized, autoscales, and requires less deployment environment configuration than with most PaaS setups.

As Serverless services are a relatively new avenue of cloud code execution, there is not a widely used standard for shaping your serverless project. There is, however, a few frameworks focused on abstracting away the differences between Serverless cloud providers. Using one of these frameworks helps mitigate vendor lock-in.

Setup

This post will use the Serverless Framework to deploy an AWS Lambda function that executes Node.js code on a schedule.

Make sure you install Node.js and set up AWS credentials for your development environment. Commands in this tutorial will use the npx commandline utility which was introduced with npm version 5.2.0.

Code

Feel free to download and modify the git hub project for this tutorial.

All serverless framework projects start with a simple serverless.yml file that contains basic configuration for your project.

josh-holmgrem-scheduling-events-1.png

After creating this configuration you'll want to create the entry point of your function, I named mine entrypoint.js. Note, exported scheduleFunction whose name matches the handler function name in our serverless.yml

holmgrem-sched-2 (3).png

Here is the ScheduleWorker class, with logging in every method to demonstrate that the method is called and in what order:

holmgrem-sched.3.png

If you have downloaded the github project for this tutorial you will want to run npm install to install dependencies. Otherwise, at a minimum you will need to setup the serverless npm package as a dev-dependency by running npm install serverless --save-dev. In the github project I have setup linting and testing boiler plate by way of eslint and jest. I won't go into the intricacies of these setups, but you should be able to verify that files in the repo pass linting and test requirements by executing npm run lint and npm run test. You can test running our function locally through the serverless npm package by executing npx sls invoke local -f scheduleFunction. This should produce the following output:

carbon (3).png

Now we are ready to deploy our function, which can be done with npx sls deploy, which will print something similar to:

carbon (4).png

We can see the logs of our function execution in AWS by npx sls logs -f "scheduleFunction". However, if you execute this command before the first scheduled invocation of your function (as dictated by our cron expression on our serverless.yml file) you will get the error: No existing streams for the function. This is because AWS waits for the function to invoke once before creating the CloudWatch log streams that your function logs will be available on.

Finally, once you are done with your function you can delete all AWS resources allocated for the service you specified in your serverless.yml file by executing npx sls remove

 
 
yonomi-josh-holmgrem

About the Author

Josh Holmgren is a Software Developer at Yonomi. He has 6 years experience using Node.js to create backend applications. He has a broad range of professional experience including business intelligence, product development, and applied mathematics. In his free time, Josh enjoys hiking, cooking, and going to concerts.

 
 

Learn More About Our Yonomi One API for Smart Home Apps and Services

Yonomi One is a robust 3rd party device integration and automation framework. With just one API, you can connect to over 200 smart home devices.