]> gitweb.factorcode.org Git - factor.git/blob - extra/site-watcher/site-watcher.factor
factor: trim using lists
[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 calendar continuations db http.client init
4 kernel math math.parser namespaces sequences site-watcher.db
5 site-watcher.email timers ;
6 IN: site-watcher
7
8 SYMBOL: site-watcher-frequency
9 5 minutes site-watcher-frequency set-global
10
11 SYMBOL: running-site-watcher
12 [ f running-site-watcher set-global ] "site-watcher" add-startup-hook
13
14 <PRIVATE
15
16 : check-sites ( seq -- )
17     [
18         [ dup url>> http-get 2drop site-good ] [ site-bad ] recover
19     ] each ;
20
21 : site-up-email ( site -- body )
22     last-up>> now swap time- duration>minutes 60 /mod
23     [ >integer number>string ] bi@
24     [ " hours, " append ] [ " minutes" append ] bi* append
25     "Site was down for (at least): " prepend ;
26
27 : site-down-email ( site -- body ) error>> ;
28
29 : send-report ( site -- )
30     [ ]
31     [ dup up?>> [ site-up-email ] [ site-down-email ] if ]
32     [ [ url>> ] [ up?>> "up" "down" ? ] bi " is " glue ] tri
33     send-site-email ;
34
35 : send-reports ( seq -- )
36     [ [ send-report ] each ] unless-empty ;
37
38 PRIVATE>
39
40 : watch-sites ( -- )
41     find-sites check-sites sites-to-report send-reports ;
42
43 : run-site-watcher ( db -- )
44     [ running-site-watcher get ] dip '[
45         [ _ [ watch-sites ] with-db ] site-watcher-frequency get every
46         running-site-watcher set
47     ] unless ;
48
49 : stop-site-watcher ( -- )
50     running-site-watcher get [ stop-timer ] when* ;