DEV Community

Cover image for Run scheduled tasks on AWS, GCP or Azure.
Rak
Rak

Posted on

Run scheduled tasks on AWS, GCP or Azure.

When you want to create a scheduled job in the cloud, you'd probably think it's a really quick setup right? Really, all you should be providing is the frequency, a handler function and we're up and running...

It's always more complicated than this - AWS gives you a thousand-step (slightly exaggerated) guide to getting it setup. If you're the type that loves following 100-step guides to get anything done, then by all means.

For me, the annoyance factor of having to perform a million steps to just run a bit of code is high. I'm always interested in getting to a path of automation. This is why we've built scheduling as a first-class citizen of the Nitric framework.

Write this code, deploy this code, and the scheduler will do its job. Our local dashboard experience also lets you trigger the schedule offline for greater levels of debugging and testing before you even hit the cloud.

Here's the code, this is just a quick example of how you could clean up bucket space like S3 Storage every 3 days.

import { schedule, bucket } from "@nitric/sdk";

const assets = bucket("assets").for("deleting");

// A schedule that cleans up files in a bucket every 3 days
schedule("tidy").every("3 days", async (ctx) => {
  const files = await assets.files();
  await Promise.all(files.forEach(async (file) => await file.delete()));
});

Enter fullscreen mode Exit fullscreen mode

You can run this offline or deploy it to the cloud with the Nitric CLI.

Top comments (0)