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.
- 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.
- 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.
- The
event
we received is not the exact shape we want. Let's process it usingList::map
to keep just thedata
field.
- Make a second datastore with three fields: the nicely-formatted date, the list of requests, and the count of requests for that day.
- To get the count, use
List::length
. Then we store it in the DB, usinghumanDate
as the key.
- 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.