# HG changeset patch # User Adam Chlipala # Date 1295824590 18000 # Node ID 22674ac8ebe6342159fbc28b07baac8ce204dd6b # Parent d0786ff9bb22e9641f850da0f461d77598e5020c Proper error handling for periodic tasks diff -r d0786ff9bb22 -r 22674ac8ebe6 src/c/request.c --- 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); }; diff -r d0786ff9bb22 -r 22674ac8ebe6 src/c/urweb.c --- 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; }