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