major refactoring of site-watcher again
parent
9a8e7fe3ed
commit
391d972b66
|
@ -1,16 +1,10 @@
|
||||||
! Copyright (C) 2009 Doug Coleman.
|
! Copyright (C) 2009 Doug Coleman.
|
||||||
! See http://factorcode.org/license.txt for BSD license.
|
! See http://factorcode.org/license.txt for BSD license.
|
||||||
USING: accessors continuations db db.sqlite db.tuples db.types
|
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
|
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 ;
|
TUPLE: account account-id email ;
|
||||||
|
|
||||||
: <account> ( email -- account )
|
: <account> ( email -- account )
|
||||||
|
@ -49,3 +43,45 @@ watching-site "WATCHING_SITE" {
|
||||||
{ "account-id" "ACCOUNT_ID" INTEGER +user-assigned-id+ }
|
{ "account-id" "ACCOUNT_ID" INTEGER +user-assigned-id+ }
|
||||||
{ "site-id" "SITE_ID" INTEGER +user-assigned-id+ }
|
{ "site-id" "SITE_ID" INTEGER +user-assigned-id+ }
|
||||||
} define-persistent
|
} 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 ;
|
||||||
|
|
|
@ -4,6 +4,13 @@ USING: db.tuples locals site-watcher site-watcher.db
|
||||||
site-watcher.private kernel ;
|
site-watcher.private kernel ;
|
||||||
IN: site-watcher.tests
|
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 )
|
:: fake-sites ( -- seq )
|
||||||
[
|
[
|
||||||
account ensure-table
|
account ensure-table
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
! Copyright (C) 2009 Doug Coleman.
|
! Copyright (C) 2009 Doug Coleman.
|
||||||
! See http://factorcode.org/license.txt for BSD license.
|
! See http://factorcode.org/license.txt for BSD license.
|
||||||
USING: accessors alarms arrays calendar combinators
|
USING: accessors alarms arrays calendar combinators
|
||||||
combinators.smart continuations db db.tuples debugger
|
combinators.smart continuations debugger http.client
|
||||||
http.client init io.streams.string kernel locals math
|
init io.streams.string kernel locals math math.parser
|
||||||
math.parser namespaces sequences site-watcher.db smtp ;
|
namespaces sequences site-watcher.db smtp ;
|
||||||
IN: site-watcher
|
IN: site-watcher
|
||||||
|
|
||||||
SYMBOL: site-watcher-from
|
SYMBOL: site-watcher-from
|
||||||
|
@ -17,22 +17,6 @@ SYMBOL: running-site-watcher
|
||||||
|
|
||||||
<PRIVATE
|
<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 -- )
|
: check-sites ( seq -- )
|
||||||
[
|
[
|
||||||
[ dup url>> http-get 2drop site-good ] [ site-bad ] recover
|
[ dup url>> http-get 2drop site-good ] [ site-bad ] recover
|
||||||
|
@ -44,8 +28,7 @@ SYMBOL: running-site-watcher
|
||||||
[ " hours, " append ] [ " minutes" append ] bi* append
|
[ " hours, " append ] [ " minutes" append ] bi* append
|
||||||
"Site was down for (at least): " prepend >>body ;
|
"Site was down for (at least): " prepend >>body ;
|
||||||
|
|
||||||
: site-down-email ( email site -- email )
|
: site-down-email ( email site -- email ) error>> >>body ;
|
||||||
error>> >>body ;
|
|
||||||
|
|
||||||
: send-report ( site -- )
|
: send-report ( site -- )
|
||||||
[ <email> ] dip
|
[ <email> ] dip
|
||||||
|
@ -56,40 +39,14 @@ SYMBOL: running-site-watcher
|
||||||
[ [ url>> ] [ up?>> "up" "down" ? ] bi " is " glue >>subject ]
|
[ [ url>> ] [ up?>> "up" "down" ? ] bi " is " glue >>subject ]
|
||||||
} cleave send-email ;
|
} cleave send-email ;
|
||||||
|
|
||||||
: email-accounts ( seq -- )
|
: send-reports ( seq -- )
|
||||||
[ ] [ [ send-report ] each ] if-empty ;
|
[ ] [ [ 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>
|
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 )
|
: watch-sites ( -- alarm )
|
||||||
[
|
[
|
||||||
[
|
find-sites check-sites sites-to-report send-reports
|
||||||
f <site> select-tuples check-sites report-sites
|
|
||||||
] with-sqlite-db
|
|
||||||
] site-watcher-frequency get every ;
|
] site-watcher-frequency get every ;
|
||||||
|
|
||||||
: run-site-watcher ( -- )
|
: run-site-watcher ( -- )
|
||||||
|
|
Loading…
Reference in New Issue