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 name
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 is
Date::now, piped into
Date::toSeconds. (Note: to create a pipe, type
Date::nowfunction to see a live value for your code.
- Next, we'll need to filter the reports to just today's reports. Set
DB::getAll Requests. Run the function to the live value for
- To see only today's requests, use
List::filtertakes two arguments - the list to filter (in this case
allRequests) and an anonymous function that determines if each element of the list element should be includes in the new list.
List::filteris similar to
List::mapand 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
timeSecondsthat 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::postfunction from the REPL. Then, when you place your cursor in
List::filteryou'll see a list of requests from the last 24 hours.
- Finally, let's emit today's requests to a background worker, using the
emittakes two arguments, and in this case we're sending
todayRequeststo a not-yet-created worker named
- 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