1 ! Copyright (C) 2009 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alarms 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.db.private
10 SYMBOL: site-watcher-from
11 "factor-site-watcher@gmail.com" site-watcher-from set-global
13 SYMBOL: site-watcher-frequency
14 5 minutes site-watcher-frequency set-global
16 SYMBOL: running-site-watcher
17 [ f running-site-watcher set-global ] "site-watcher" add-init-hook
21 : check-sites ( seq -- )
23 [ dup url>> http-get 2drop site-good ] [ site-bad ] recover
26 : site-up-email ( email site -- email )
27 last-up>> now swap time- duration>minutes 60 /mod
28 [ >integer number>string ] bi@
29 [ " hours, " append ] [ " minutes" append ] bi* append
30 "Site was down for (at least): " prepend >>body ;
32 : site-down-email ( email site -- email ) error>> >>body ;
34 : send-report ( site -- )
37 [ email>> 1array >>to ]
38 [ drop site-watcher-from get "factor.site.watcher@gmail.com" or >>from ]
39 [ dup up?>> [ site-up-email ] [ site-down-email ] if ]
40 [ [ url>> ] [ up?>> "up" "down" ? ] bi " is " glue >>subject ]
43 : send-reports ( seq -- )
44 [ ] [ [ send-report ] each ] if-empty ;
48 : watch-sites ( db -- )
49 [ find-sites check-sites sites-to-report send-reports ] with-db ;
51 : run-site-watcher ( db -- )
52 [ running-site-watcher get ] dip '[
53 [ _ watch-sites ] site-watcher-frequency get every
54 running-site-watcher set
57 : stop-site-watcher ( -- )
58 running-site-watcher get [ cancel-alarm ] when* ;