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