1 ! Copyright (C) 2009 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors timers arrays calendar combinators
4 combinators.smart continuations debugger http.client fry
5 init io.streams.string kernel locals math math.parser db
6 namespaces sequences site-watcher.db site-watcher.email ;
9 SYMBOL: site-watcher-frequency
10 5 minutes site-watcher-frequency set-global
12 SYMBOL: running-site-watcher
13 [ f running-site-watcher set-global ] "site-watcher" add-startup-hook
17 : check-sites ( seq -- )
19 [ dup url>> http-get 2drop site-good ] [ site-bad ] recover
22 : site-up-email ( site -- body )
23 last-up>> now swap time- duration>minutes 60 /mod
24 [ >integer number>string ] bi@
25 [ " hours, " append ] [ " minutes" append ] bi* append
26 "Site was down for (at least): " prepend ;
28 : site-down-email ( site -- body ) error>> ;
30 : send-report ( site -- )
32 [ dup up?>> [ site-up-email ] [ site-down-email ] if ]
33 [ [ url>> ] [ up?>> "up" "down" ? ] bi " is " glue ] tri
36 : send-reports ( seq -- )
37 [ [ send-report ] each ] unless-empty ;
42 find-sites check-sites sites-to-report send-reports ;
44 : run-site-watcher ( db -- )
45 [ running-site-watcher get ] dip '[
46 [ _ [ watch-sites ] with-db ] site-watcher-frequency get every
47 running-site-watcher set
50 : stop-site-watcher ( -- )
51 running-site-watcher get [ stop-timer ] when* ;