changeset 1418:22674ac8ebe6

Proper error handling for periodic tasks
author Adam Chlipala <adam@chlipala.net>
date Sun, 23 Jan 2011 18:16:30 -0500
parents d0786ff9bb22
children 00b79f39be25
files src/c/request.c src/c/urweb.c
diffstat 2 files changed, 20 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/src/c/request.c	Sun Jan 23 17:27:51 2011 -0500
+++ b/src/c/request.c	Sun Jan 23 18:16:30 2011 -0500
@@ -96,10 +96,26 @@
     exit(1);
 
   while (1) {
+    int retries_left = MAX_RETRIES;
+
     failure_kind r;
     do {
+      uw_reset(ctx);
       r = uw_runCallback(ctx, p->pdic.callback);
-    } while (r == UNLIMITED_RETRY);
+      if (r == BOUNDED_RETRY)
+        --retries_left;
+      else if (r == UNLIMITED_RETRY)
+        p->ls->log_debug(p->ls->logger_data, "Error triggers unlimited retry in periodic: %s\n", uw_error_message(ctx));
+      else if (r == BOUNDED_RETRY)
+        p->ls->log_debug(p->ls->logger_data, "Error triggers bounded retry in periodic: %s\n", uw_error_message(ctx));
+      else if (r == FATAL)
+        p->ls->log_error(p->ls->logger_data, "Fatal error: %s\n", uw_error_message(ctx));
+      if (r == FATAL || r == BOUNDED_RETRY || r == UNLIMITED_RETRY)
+        try_rollback(ctx, 0, p->ls->logger_data, p->ls->log_error);
+    } while (r == UNLIMITED_RETRY || (r == BOUNDED_RETRY && retries_left > 0));
+
+    if (r != FATAL && r != BOUNDED_RETRY)
+      uw_commit(ctx);
 
     sleep(p->pdic.period);
   };
--- a/src/c/urweb.c	Sun Jan 23 17:27:51 2011 -0500
+++ b/src/c/urweb.c	Sun Jan 23 18:16:30 2011 -0500
@@ -3684,15 +3684,12 @@
 failure_kind uw_runCallback(uw_context ctx, void (*callback)(uw_context)) {
   int r = setjmp(ctx->jmp_buf);
 
-  if (ctx->app->db_begin(ctx))
-    uw_error(ctx, BOUNDED_RETRY, "Error running SQL BEGIN");
-
   if (r == 0) {
+    if (ctx->app->db_begin(ctx))
+      uw_error(ctx, BOUNDED_RETRY, "Error running SQL BEGIN");
+
     callback(ctx);
-    uw_commit(ctx);
   }
-  else
-    uw_rollback(ctx, 0);
 
   return r;
 }