major refactoring of site-watcher again

db4
Doug Coleman 2009-03-18 18:05:05 -05:00
parent 9a8e7fe3ed
commit 391d972b66
3 changed files with 57 additions and 57 deletions

View File

@ -1,16 +1,10 @@
! Copyright (C) 2009 Doug Coleman.
! See http://factorcode.org/license.txt for BSD license.
USING: accessors continuations db db.sqlite db.tuples db.types
io.directories io.files.temp kernel ;
io.directories io.files.temp kernel io.streams.string calendar
debugger combinators.smart sequences ;
IN: site-watcher.db
: site-watcher-path ( -- path ) "site-watcher.db" temp-file ; inline
[ site-watcher-path delete-file ] ignore-errors
: with-sqlite-db ( quot -- )
site-watcher-path <sqlite-db> swap with-db ; inline
TUPLE: account account-id email ;
: <account> ( email -- account )
@ -49,3 +43,45 @@ watching-site "WATCHING_SITE" {
{ "account-id" "ACCOUNT_ID" INTEGER +user-assigned-id+ }
{ "site-id" "SITE_ID" INTEGER +user-assigned-id+ }
} define-persistent
: set-notify-site-watchers ( site new-up? -- site )
[ over up?>> = [ t >>changed? ] unless ] keep >>up? ;
: site-good ( site -- )
t set-notify-site-watchers
now >>last-up
f >>error
f >>last-error
update-tuple ;
: site-bad ( site error -- )
[ error. ] with-string-writer >>error
f set-notify-site-watchers
now >>last-error
update-tuple ;
TUPLE: reporting-site email url up? changed? last-up? error last-error ;
: sites-to-report ( -- seq )
"select account.email, site.url, site.up, site.changed, site.last_up, site.error, site.last_error from account, site, watching_site where account.account_id = watching_site.account_id and site.site_id = watching_site.site_id and site.changed = '1'" sql-query
[ [ reporting-site boa ] input<sequence ] map
"update site set changed = 'f';" sql-command ;
: insert-site ( url -- site )
<site> dup select-tuple [
dup t >>up? insert-tuple
] unless ;
: insert-account ( email -- ) <account> insert-tuple ;
: find-sites ( -- seq ) f <site> select-tuples ;
: select-account/site ( email url -- account site )
[ <account> select-tuple account-id>> ]
[ insert-site site-id>> ] bi* ;
: watch-site ( email url -- )
select-account/site <watching-site> insert-tuple ;
: unwatch-site ( email url -- )
select-account/site <watching-site> delete-tuples ;

View File

@ -4,6 +4,13 @@ USING: db.tuples locals site-watcher site-watcher.db
site-watcher.private kernel ;
IN: site-watcher.tests
: site-watcher-path ( -- path ) "site-watcher.db" temp-file ; inline
[ site-watcher-path delete-file ] ignore-errors
: with-sqlite-db ( quot -- )
site-watcher-path <sqlite-db> swap with-db ; inline
:: fake-sites ( -- seq )
[
account ensure-table

View File

@ -1,9 +1,9 @@
! Copyright (C) 2009 Doug Coleman.
! See http://factorcode.org/license.txt for BSD license.
USING: accessors alarms arrays calendar combinators
combinators.smart continuations db db.tuples debugger
http.client init io.streams.string kernel locals math
math.parser namespaces sequences site-watcher.db smtp ;
combinators.smart continuations debugger http.client
init io.streams.string kernel locals math math.parser
namespaces sequences site-watcher.db smtp ;
IN: site-watcher
SYMBOL: site-watcher-from
@ -17,22 +17,6 @@ SYMBOL: running-site-watcher
<PRIVATE
: set-notify-site-watchers ( site new-up? -- site )
[ over up?>> = [ t >>changed? ] unless ] keep >>up? ;
: site-good ( site -- )
t set-notify-site-watchers
now >>last-up
f >>error
f >>last-error
update-tuple ;
: site-bad ( site error -- )
[ error. ] with-string-writer >>error
f set-notify-site-watchers
now >>last-error
update-tuple ;
: check-sites ( seq -- )
[
[ dup url>> http-get 2drop site-good ] [ site-bad ] recover
@ -44,8 +28,7 @@ SYMBOL: running-site-watcher
[ " hours, " append ] [ " minutes" append ] bi* append
"Site was down for (at least): " prepend >>body ;
: site-down-email ( email site -- email )
error>> >>body ;
: site-down-email ( email site -- email ) error>> >>body ;
: send-report ( site -- )
[ <email> ] dip
@ -56,40 +39,14 @@ SYMBOL: running-site-watcher
[ [ url>> ] [ up?>> "up" "down" ? ] bi " is " glue >>subject ]
} cleave send-email ;
: email-accounts ( seq -- )
: send-reports ( seq -- )
[ ] [ [ send-report ] each ] if-empty ;
TUPLE: reporting-site email url up? changed? last-up? error last-error ;
: report-sites ( -- )
"select account.email, site.url, site.up, site.changed, site.last_up, site.error, site.last_error from account, site, watching_site where account.account_id = watching_site.account_id and site.site_id = watching_site.site_id and site.changed = '1'" sql-query
[ [ reporting-site boa ] input<sequence ] map email-accounts
"update site set changed = 'f';" sql-command ;
: insert-site ( url -- site )
<site> dup select-tuple [
dup t >>up? insert-tuple
] unless ;
: insert-account ( email -- ) <account> insert-tuple ;
: select-account/site ( email url -- account site )
[ <account> select-tuple account-id>> ]
[ insert-site site-id>> ] bi* ;
PRIVATE>
: watch-site ( email url -- )
select-account/site <watching-site> insert-tuple ;
: unwatch-site ( email url -- )
select-account/site <watching-site> delete-tuples ;
: watch-sites ( -- alarm )
[
[
f <site> select-tuples check-sites report-sites
] with-sqlite-db
find-sites check-sites sites-to-report send-reports
] site-watcher-frequency get every ;
: run-site-watcher ( -- )