Tutorial: Create a daily job with a Cron handler
Now that we're storing requests, we can use a scheduled job (a "Cron", named
after the Unix cron
utility) to create a report of all requests per day.
- Hit the plus (
+
) button (or use the omnibox) to create a new Cron. Set it to run daily, and with the nameDailyReport
.
-
To know if a request was executed today, we compare it to the current time. Let's assign the current time in seconds to a variable
timeSeconds
. The current time in seconds isDate::now
, piped intoDate::toSeconds
. (Note: to create a pipe, type|>
after theDate::now
function).Run the
Date::now
function to see a live value for your code.
- Next, we'll need to filter the reports to just today's reports. Set
allRequests
toDB::getAll Requests
. Run the function to the live value forallRequests
.
- To see only today's requests, use
List::filter
.List::filter
takes two arguments - the list to filter (in this caseallRequests
) and an anonymous function that determines if each element of the list element should be includes in the new list.List::filter
is similar toList::map
and the concept is explained in more detail here.
- We want to keep only reports that occurred today, meaning we want to compare
the report's time to
timeSeconds
that we created earlier. Let's convert the time of the request into seconds so that both times have the same units.
- Next, we calculate the difference from the current time; this tells us if it happened today (in the last 86,400 seconds).
- To see this work, it's helpful to have a recent request. If you paused in the
tutorial, re-run the
HttpClient::post
function from the REPL. Then, when you place your cursor intodayRequests
orList::filter
you'll see a list of requests from the last 24 hours.
- Finally, let's emit today's requests to a background worker, using the
emit
keyword.emit
takes two arguments, and in this case we're sendingtodayRequests
to a not-yet-created worker namedstoreReport
.
- To have our first report run, hit the "replay" button in the upper right of
the Cron. This creates a 404 in the sidebar for the Worker
storeReport
.