1 ! Copyright (C) 2009 Doug Coleman.
2 ! See https://factorcode.org/license.txt for BSD license.
3 USING: accessors calendar continuations db http.client init
4 kernel math math.parser namespaces sequences site-watcher.db
5 site-watcher.email timers ;
8 SYMBOL: site-watcher-frequency
9 5 minutes site-watcher-frequency set-global
11 SYMBOL: running-site-watcher
12 STARTUP-HOOK: [ f running-site-watcher set-global ]
16 : check-sites ( seq -- )
18 [ dup url>> http-get 2drop site-good ] [ site-bad ] recover
21 : site-up-email ( site -- body )
22 last-up>> now swap time- duration>minutes 60 /mod
23 [ >integer number>string ] bi@
24 [ " hours, " append ] [ " minutes" append ] bi* append
25 "Site was down for (at least): " prepend ;
27 : site-down-email ( site -- body ) error>> ;
29 : send-report ( site -- )
31 [ dup up?>> [ site-up-email ] [ site-down-email ] if ]
32 [ [ url>> ] [ up?>> "up" "down" ? ] bi " is " glue ] tri
35 : send-reports ( seq -- )
36 [ [ send-report ] each ] unless-empty ;
41 find-sites check-sites sites-to-report send-reports ;
43 : run-site-watcher ( db -- )
44 [ running-site-watcher get ] dip '[
45 [ _ [ watch-sites ] with-db ] site-watcher-frequency get every
46 running-site-watcher set
49 : stop-site-watcher ( -- )
50 running-site-watcher get [ stop-timer ] when* ;