Skip to main content

Tutorial: Process background jobs in a Worker

Workers process jobs in the background, ideal for daily reports. They're also commonly used for sending notifications and other asynchronous tasks that we don't want to handle in HTTP requests.

We're going to create a report, and store the aggregate data in the DailyReport datastore.

  1. From the 404 section of the sidebar, hit the plus (+) button to create the StoreReport Worker. The Worker will already be named, and hovering over the white dot will show you the event triggered by your cron.

gettingstarted/newworker.png

  1. For the report, we're going to store our data by human readable dates (like 02-27-2020). First, we'll get the current date, and then get the pieces and re-aggregate them.

gettingstarted/humandate.png

  1. The event we received is not the exact shape we want. Let's process it using List::map to keep just the data field.

gettingstarted/dataonly.png

  1. Make a second datastore with three fields: the nicely-formatted date, the list of requests, and the count of requests for that day.

gettingstarted/seconddatastore.png

  1. To get the count, use List::length. Then we store it in the DB, using humanDate as the key.

gettingstarted/dbsetworker.png

  1. Now, our first datastore stores each incoming request, and every 24 hours our Cron runs, sending the right reports to our worker, which stores a report in our second datastore.