Scheduling website content is hard. Most content management systems (CMS) have features for publishing products, blog articles, and other types of platform-related content, but what about simply scheduling HTML? As developers sometimes we want to show a section of our website during a certain time frame.
Think holiday sales or launch events. Surprisingly this is harder to pull off then one might think. You have to set up some sort of cron that triggers some function that publishes some piece of content.
It can be tough to build! Thatās why Iām building newclick.io, a tool to help youĀ schedule promotional content on your website. Weāre starting with something simple, announcement banners, but will soon branch out to different types of components.
In this article, Iāll briefly describe some of the things that went into developing the newclick.io Scheduling API.
Architecture āļø
The first step was to figure out if this was even possible. How are developers scheduling content today? CMS platforms like Shopify and Wordpress allow users to set publish dates for things like products and blog posts, so itās definitely achievable, but certainly, they arenāt using something like a crontab, are they?
No way!
Itās 2020 we run things in the cloud these days so any type of server-side crontab would be out of the question. This included out of the box solutions such as theĀ node-scheduleĀ andĀ node-cronĀ libraries. These wouldnāt work with how I wanted to architect my servers.
From the very beginning, I wanted to use theĀ serverless frameworkĀ and deploy toĀ AWS Lambda. We didnāt want to think about spinning up EC2 instances and worrying about uptime and other server-related things.
I wanted to keep the development focused on code and less on infrastructure. I was also intrigued by the inherent costs savings you get with Lambda functions. You donāt pay for idle server time, which made sense for my fledgling startup.
āSo how am I going to pull this off?ā, I thought. Well, I canāt use a traditional server cron so I needed to look elsewhere. Thatās when I foundĀ AWS CloudWatch.
CloudWatch is used for what AWS describes as āapplication and infrastructure monitoringā, but also provides tooling around scheduling crons and other event-related processes. āThis is great!ā I thought to myself. I could use CloudWatch along with theĀ AWS JavaScript SDKĀ to create our scheduled events. Onward!
Development š
So with my new tool in hand, I was ready to start writing some code. I started by figuring out how to use the AWS SDK to interface with CloudWatch.
First I needed to create a constant that would be available throughout our entire application. I started by defining and exporting aĀ
cloudwatchEvents
Ā service.I used theĀ CloudWatchEventsĀ constructor to define a constant. It takes an AWS access key, secret key and region for itās parameters. You can pass those in using environment variables, good practice in case these end up changing later.
Now that we have ourĀ
cloudWatchEvents
Ā service ready to go, we have access to theĀ putRule
Ā function. This allows you to schedule an expression using a cron-like syntax. We also have access to theĀ putTargets
Ā function which allows you to define aĀ target, in our case a Lambda function (more on this later). For each event thatās scheduled I created a new CloudWatch Rule that runs a Lambda function for a given date and time. The expression passed into the function looks a lot like yourĀ standard cron expression.The last step is to attach our target. The target is theĀ
RuleArn
Ā returned back from ourĀ putRule
Ā call. This tells our CloudWatch event to only processes this ARN (Amazon Resource Names).And there you have it! We have a scheduled task ready to rockš¤
In follow up article are go into how and why this wasnāt enough, and why I needed to āclean upā our rules after they executed. Stay tuned!
If you enjoyed this, feel free toĀ follow me on TwitterĀ and take a look atĀ newclick.ioĀ if youāre interested in scheduling your next promotion.