The daemon, tick by tick
The daemon is a process that runs for a long time. It checks the watch list on a schedule, and it writes each confirmed change as one JSON line. Each check is a normal fetch of the crawler, so robots.txt, the pace of each host and the stops apply.
npx polite-crawler watch add https://forno-esempio.example/hours --every 1hnpx polite-crawler daemon --tick 20m --jitter 2m --tick-budget 30s --tick-pages 3The figure starts the real scheduleWatches of polite-crawler on a simulated clock. Use Play, Pause and Step. Select a tick to see its checks. With reduced motion, the figure shows the whole schedule at once.
scheduleWatches on a simulated clock, with --tick 20m, --jitter 2m, --tick-budget 30s and --tick-pages 3.The schedule 06:00:00 UTC
albergo/faqeach 2 hbottega/fx-2207each 1 hbottega/olioeach 1 hbottega/olio-1leach 1 hforno/hourseach 1 hmuseo/visiteach 1 hSelect Play to start the daemon.
One tick
albergo-campione.examplenothing duebottega-campione.examplenothing dueforno-esempio.examplenothing duemuseo-esempio.examplenothing due- Checks of the tick
- 0 of
maxPages3 - The run stopped
- not yet
- Next tick
- after the run
The change lines standard output, one JSON line for each confirmed change
No change is confirmed yet. The daemon writes a line only when a second check sees the same new version.
The ticks as a table
| Tick | Start (UTC) | Checks | Stopped | Next tick |
|---|
What the figure shows
Section titled “What the figure shows”The watch list of the figure is a model, because the real list keeps its state in SQLite. The model follows the rules of watch.run, with made-up pages that send no request. The schedule, the jitter, the budget and the drain come from the real code.
- Each page is due at its time. A new entry is due at once. After a check, the entry is due again after
every(1 hour or 2 hours here). The circle is the due time, and the line is the wait for the next tick. - A tick comes at the first due time, or after
--tick. After each tick, the daemon waits until the first entry is due. It does not wait longer than--tick. - Jitter comes after the wait. The daemon adds a random time from 0 to
--jitter, so a check does not come before its time. Many daemons with the same list then do not ask a site at the same second. - Each tick has a budget. No request starts after
--tick-budget. In the second tick, the hostbottega-campione.examplerests 30 seconds after a slow page, past the budget. The entry stays due for the next tick. - Each tick has a page limit. The first tick checks
--tick-pagesentries (3), and the other entries wait for the next tick. - A change needs a second check. The first check that sees a new version marks it as suspected. The next check confirms it when it sees the same version. Only then does the daemon write a JSON line. A page in maintenance for one check is a glitch, and the next check clears it.
- A stop drains the run. At
SIGINTorSIGTERM, the run starts no new check. The checks in progress end and write their state. After--grace(10 seconds), the checks that are still in progress abort.
In the figure, the stop gives the checks in progress one hour instead of 10 seconds, because the clock of the figure is not the real clock.
The JSON line of a change
Section titled “The JSON line of a change”The daemon writes each confirmed change to the standard output, or to the file of --events. A line is a WatchEvent:
kindischanged,goneornew, andconfirmedistrue.capturenames the capture of the new version, andpreviousnames the capture of the old version.diffcounts the changed lines and words.next_atis the time when the page is due again.
--on-change starts your command for each change, with the line on its standard input. The command line gives each option, and the library API gives startDaemon and scheduleWatches.