(* 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
"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";
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 =