diff --git a/extra/site-watcher/db/db.factor b/extra/site-watcher/db/db.factor index 3527f57074..3798b1ae94 100644 --- a/extra/site-watcher/db/db.factor +++ b/extra/site-watcher/db/db.factor @@ -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 swap with-db ; inline - TUPLE: account account-id email ; : ( 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 dup select-tuple [ + dup t >>up? insert-tuple + ] unless ; + +: insert-account ( email -- ) insert-tuple ; + +: find-sites ( -- seq ) f select-tuples ; + +: select-account/site ( email url -- account site ) + [ select-tuple account-id>> ] + [ insert-site site-id>> ] bi* ; + +: watch-site ( email url -- ) + select-account/site insert-tuple ; + +: unwatch-site ( email url -- ) + select-account/site delete-tuples ; diff --git a/extra/site-watcher/site-watcher-tests.factor b/extra/site-watcher/site-watcher-tests.factor index 405b6cbb1e..a19c954c25 100644 --- a/extra/site-watcher/site-watcher-tests.factor +++ b/extra/site-watcher/site-watcher-tests.factor @@ -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 swap with-db ; inline + :: fake-sites ( -- seq ) [ account ensure-table diff --git a/extra/site-watcher/site-watcher.factor b/extra/site-watcher/site-watcher.factor index aba2d12231..f47c38c50f 100644 --- a/extra/site-watcher/site-watcher.factor +++ b/extra/site-watcher/site-watcher.factor @@ -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 > = [ 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 -- ) [ ] 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 dup select-tuple [ - dup t >>up? insert-tuple - ] unless ; - -: insert-account ( email -- ) insert-tuple ; - -: select-account/site ( email url -- account site ) - [ select-tuple account-id>> ] - [ insert-site site-id>> ] bi* ; - PRIVATE> -: watch-site ( email url -- ) - select-account/site insert-tuple ; - -: unwatch-site ( email url -- ) - select-account/site delete-tuples ; - : watch-sites ( -- alarm ) [ - [ - f 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 ( -- )