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
5 init io.streams.string kernel locals math math.parser
6 namespaces sequences site-watcher.db site-watcher.db.private smtp ;
9 SYMBOL: site-watcher-from
10 "factor-site-watcher@gmail.com" site-watcher-from set-global
12 SYMBOL: site-watcher-frequency
13 10 seconds site-watcher-frequency set-global
15 SYMBOL: running-site-watcher
16 [ f running-site-watcher set-global ] "site-watcher" add-init-hook
20 : check-sites ( seq -- )
22 [ dup url>> http-get 2drop site-good ] [ site-bad ] recover
25 : site-up-email ( email site -- email )
26 last-up>> now swap time- duration>minutes 60 /mod
27 [ >integer number>string ] bi@
28 [ " hours, " append ] [ " minutes" append ] bi* append
29 "Site was down for (at least): " prepend >>body ;
31 : site-down-email ( email site -- email ) error>> >>body ;
33 : send-report ( site -- )
36 [ email>> 1array >>to ]
37 [ drop site-watcher-from get "factor.site.watcher@gmail.com" or >>from ]
38 [ dup up?>> [ site-up-email ] [ site-down-email ] if ]
39 [ [ url>> ] [ up?>> "up" "down" ? ] bi " is " glue >>subject ]
42 : send-reports ( seq -- )
43 [ ] [ [ send-report ] each ] if-empty ;
48 find-sites check-sites sites-to-report send-reports ;
50 : run-site-watcher ( -- )
51 running-site-watcher get [
52 [ watch-sites ] site-watcher-frequency get every
53 running-site-watcher set-global
56 : stop-site-watcher ( -- )
57 running-site-watcher get [ cancel-alarm ] when* ;