]> gitweb.factorcode.org Git - factor.git/blob - extra/site-watcher/site-watcher.factor
Merge branch 'master' of git://factorcode.org/git/factor into clean-linux-x86-32
[factor.git] / extra / site-watcher / site-watcher.factor
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 ;
7 IN: site-watcher
8
9 SYMBOL: site-watcher-from
10 "factor-site-watcher@gmail.com" site-watcher-from set-global
11
12 SYMBOL: site-watcher-frequency
13 10 seconds site-watcher-frequency set-global
14  
15 SYMBOL: running-site-watcher
16 [ f running-site-watcher set-global ] "site-watcher" add-init-hook
17
18 <PRIVATE
19
20 : check-sites ( seq -- )
21     [
22         [ dup url>> http-get 2drop site-good ] [ site-bad ] recover
23     ] each ;
24
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 ;
30
31 : site-down-email ( email site -- email ) error>> >>body ;
32
33 : send-report ( site -- )
34     [ <email> ] dip
35     {
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 ]
40     } cleave send-email ;
41
42 : send-reports ( seq -- )
43     [ ] [ [ send-report ] each ] if-empty ;
44
45 PRIVATE>
46
47 : watch-sites ( -- )
48     find-sites check-sites sites-to-report send-reports ;
49
50 : run-site-watcher ( -- )
51     running-site-watcher get [ 
52         [ watch-sites ] site-watcher-frequency get every
53         running-site-watcher set-global 
54     ] unless ;
55
56 : stop-site-watcher ( -- )
57     running-site-watcher get [ cancel-alarm ] when* ;