diff src/c/driver.c @ 272:4d80d6122df1

Initializing database connection
author Adam Chlipala <adamc@hcoop.net>
date Tue, 02 Sep 2008 11:57:25 -0400
parents 71bafe66dbe1
children 9ad92047a499
line wrap: on
line diff
--- a/src/c/driver.c	Tue Sep 02 10:51:41 2008 -0400
+++ b/src/c/driver.c	Tue Sep 02 11:57:25 2008 -0400
@@ -52,8 +52,37 @@
 #define MAX_RETRIES 5
 
 static void *worker(void *data) {
-  int me = *(int *)data;
+  int me = *(int *)data, retries_left = MAX_RETRIES;;
   lw_context ctx = lw_init(1024, 1024);
+  
+  while (1) {
+    failure_kind fk = lw_begin_init(ctx);
+
+    if (fk == SUCCESS) {
+      lw_db_init(ctx);
+      printf("Database connection initialized.\n");
+      break;
+    } else if (fk == BOUNDED_RETRY) {
+      if (retries_left) {
+        printf("Initialization error triggers bounded retry: %s\n", lw_error_message(ctx));
+        --retries_left;
+      } else {
+        printf("Fatal initialization error (out of retries): %s\n", lw_error_message(ctx));
+        lw_free(ctx);
+        return NULL;
+      }
+    } else if (fk == UNLIMITED_RETRY)
+      printf("Initialization error triggers unlimited retry: %s\n", lw_error_message(ctx));
+    else if (fk == FATAL) {
+      printf("Fatal initialization error: %s\n", lw_error_message(ctx));
+      lw_free(ctx);
+      return NULL;
+    } else {
+      printf("Unknown lw_handle return code!\n");
+      lw_free(ctx);
+      return NULL;
+    }
+  }
 
   while (1) {
     char buf[lw_bufsize+1], *back = buf, *s;