]> gitweb.factorcode.org Git - factor.git/commitdiff
major refactoring of site-watcher again
authorDoug Coleman <doug.coleman@gmail.com>
Wed, 18 Mar 2009 23:05:05 +0000 (18:05 -0500)
committerDoug Coleman <doug.coleman@gmail.com>
Wed, 18 Mar 2009 23:05:05 +0000 (18:05 -0500)
extra/site-watcher/db/db.factor
extra/site-watcher/site-watcher-tests.factor
extra/site-watcher/site-watcher.factor

index 3527f57074632ba8ad13ae75dfd127b577f77dd2..3798b1ae94951b20c95840ec9f1cdec248caab08 100644 (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 ;
index 405b6cbb1edf2e4ec898b4c8acfa29f5a22f2460..a19c954c25a4f9338b9d1b1ad8a0450304f8d49c 100644 (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
index aba2d122314e34d42e2d3b43d5bb31a0af7fe2ab..f47c38c50f984d20ebe64e61037fb9913d6bb069 100644 (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 ( -- )