If fetching the RSS from the feed fails, keep using the old, cached
authorrich <rich>
Fri, 8 Dec 2006 14:10:37 +0000 (14:10 +0000)
committerrich <rich>
Fri, 8 Dec 2006 14:10:37 +0000 (14:10 +0000)
RSS (if it was in the cache).

scripts/lib/cocanwiki_func_rss.ml

index dafeba7..b9d791c 100644 (file)
@@ -1,5 +1,5 @@
 (* An RSS feed reader function.
- * $Id: cocanwiki_func_rss.ml,v 1.2 2006/12/07 17:16:17 rich Exp $
+ * $Id: cocanwiki_func_rss.ml,v 1.3 2006/12/08 14:10:37 rich Exp $
  *)
 
 open Printf
@@ -197,15 +197,24 @@ let rss r dbh hostid arg =
          "select t, rss from rss_cache where url = $url" in
       let need_to_fetch, rss =
        match rows with
-       | [] -> true, "" (* not in cache, so have to fetch URL *)
+       | [] -> true, None (* not in cache, so have to fetch URL *)
        | (t, rss) :: _ ->
            if Calendar.compare
              (Calendar.add t refresh) (Calendar.now ()) < 0 then
-             true, ""
+             true, Some rss
            else
-             false, rss in
+             false, Some rss in
       if need_to_fetch then (
-       let rss = fetch_url url in
+        (* If the fetch fails, keep using the old cached RSS.
+        * fetch_url prints the error in the error log in this case.
+        *)
+       let rss =
+         try fetch_url url
+         with Error _ as exn ->
+           match rss with
+           | Some rss -> rss
+           | None -> raise exn in
+
        (* This lock is OK - refetches are supposed to be infrequent. *)
        PGSQL(dbh) "lock table rss_cache in share mode";
        PGSQL(dbh) "delete from rss_cache where url = $url";
@@ -213,8 +222,11 @@ let rss r dbh hostid arg =
        PGOCaml.commit dbh; (* XXX How to avoid? *)
        PGOCaml.begin_work dbh;
        rss
-      ) else
-       rss in
+      ) else (
+        match rss with
+       | Some rss -> rss
+       | None -> assert false
+      ) in
 
     (* Try to parse the RSS. *)
     let channel =