THE SERVERLESS SERIES — Schedule Anything With CRON

Let me show you a single command to schedule anything HTTP

Nicolas Dao
Neap For Startups

--

If you’re a seasoned coder, you must have encountered at least once a situation where a piece of work must run periodically (e.g., batch job to resize images, create a daily report at 10 pm). If you have exposed an HTTP endpoint that triggers that piece of work, then the rest of this article demonstrates how to contact that endpoint periodically using a crontab format and a neat Serverless product called Webtask. All this with a single command similar to:

wt cron create my-template 
--name your-task-name
--schedule "*/1 * * * *"
--secret target="https://yourendpoint.com/that/can/do/something"

Like many serverless solutions, the following is FREE, simple as hell, and scales very well.

SHOW ME THE CODE

Prerequisites — Install The Webtask CLI

#1. Install The Webtask CLI: npm install wt-cli -g

There is no need to create an account upfront. The next command sets you up automatically.

#2. Login To Your Account: wt init

This command opens your default browser, so you log in to your Webtask account. If you don’t have an account yet, this step creates one for you.

Create Your Scheduler With ONE Command

wt cron create https://gist.githubusercontent.com/nicolasdao/0653b5996bd23de84a7e5f946456160c/raw/5742e067dfe9db1a17989eea54ebaa532fc436d0/webtask_webhook.js --name your-cron-task --schedule "*/1 * * * *" --secret target="https://example.com/do/something"

The above command creates a webtask that is fired every minute (that’s what the */1 * * * * means. This convention is called a crontab. I’ve added more details about the crontab in the next section). You don’t have to worry about the long gist URL. This is just a javascript code template that I’ve publicly published on my GitHub account to facilitate firing HTTP endpoints. That template uses variables stored in the secrets key/value pairs of your webtask so you can configure it based on your requirements. By default, it assumes you’re trying to trigger an action using an HTTP GET method, but you can change that (more details about all available variables in the annex at the bottom of this article).

Obviously, don’t forget to replace https://example.com/do/something with your HTTP endpoint as well as renaming your scheduler name from your-cron-task to whatever is relevant to you. I’ve added more details about other options in an annex at the bottom of this article.

That’s it. You haven’t coded anything, and yet, you’re done!

A Few Words About The CRONTAB

The command above creates two serverless pieces:

  1. A Webtask based on my template.
  2. A Scheduler responsible to fire that Webtask periodically.

You can see those two pieces by login to your Webtask console at https://webtask.io/make. There, you can manually update the scheduler with a set of drop-downs instead of the crontab if you’re not familiar with it.

The crontab is a POSIX convention used to configure CRON jobs. You can find more details on using that convention at contab.guru.

Learned something? Click the 👏 to say “thanks!” and help others find this article.

WANT MORE??? Just follow me in Medium — Nicolas Dao 😺 🙌 🙏

COMING NEXT…

ANNEX — Template Variables

The example shown in step #3 uses a single secret variable called target. I’ve built my template to support more options that I store in the secret key/value store provided out-of-the-box in each webtask. The following table describes all those options.

Examples:

wt create https://... --name your-cron-task --secret timeout=500 --secret verb=POST --secret body="{\"hello\":\"world\"}" --secret headers="{\"Authorization\":\"Bearer cn389ncoiwuencr\"}"

--

--

Focused on nurturing happiness in tech. and in life. Co-founder of Neap (https://neap.co), a Tech. Consultancy based in Sydney.