add code to configure respawn via ubus
authorJohn Crispin <blogic@openwrt.org>
Tue, 3 Sep 2013 23:50:25 +0000 (01:50 +0200)
committerJohn Crispin <blogic@openwrt.org>
Fri, 13 Sep 2013 15:35:55 +0000 (17:35 +0200)
Signed-off-by: John Crispin <blogic@openwrt.org>
instance.c
instance.h

index 90c167a..e6fc331 100644 (file)
@@ -155,14 +155,18 @@ instance_exit(struct uloop_process *p, int ret)
        } else if (in->restart) {
                instance_start(in);
        } else if (in->respawn) {
        } else if (in->restart) {
                instance_start(in);
        } else if (in->respawn) {
-               if (runtime < RESPAWN_ERROR)
+               if (runtime < in->respawn_threshold)
                        in->respawn_count++;
                else
                        in->respawn_count = 0;
                        in->respawn_count++;
                else
                        in->respawn_count = 0;
-               if (in->respawn_count > 5)
-                       DEBUG(1, "Instance %s::%s s in a crash loop %d crashes, %ld seconds since last crash\n",
+               if (in->respawn_count > in->respawn_retry) {
+                       LOG("Instance %s::%s s in a crash loop %d crashes, %ld seconds since last crash\n",
                                                                in->srv->name, in->name, in->respawn_count, runtime);
                                                                in->srv->name, in->name, in->respawn_count, runtime);
-               uloop_timeout_set(&in->timeout, 5000);
+                       in->restart = in->respawn = 0;
+                       in->halt = 1;
+               } else {
+                       uloop_timeout_set(&in->timeout, in->respawn_timeout * 1000);
+               }
        }
 }
 
        }
 }
 
@@ -318,6 +322,22 @@ instance_config_parse(struct service_instance *in)
 
        in->command = cur;
 
 
        in->command = cur;
 
+       if (tb[INSTANCE_ATTR_RESPAWN]) {
+               int i = 0;
+               uint32_t vals[3] = { 3600, 5, 5};
+
+               blobmsg_for_each_attr(cur2, tb[INSTANCE_ATTR_RESPAWN], rem) {
+                       if ((i >= 3) && (blobmsg_type(cur2) == BLOBMSG_TYPE_STRING))
+                               continue;
+                       vals[i] = atoi(blobmsg_get_string(cur2));
+                       i++;
+               }
+               in->respawn = true;
+               in->respawn_count = 0;
+               in->respawn_timeout = vals[0];
+               in->respawn_threshold = vals[1];
+               in->respawn_retry = vals[2];
+       }
        if (tb[INSTANCE_ATTR_TRIGGER]) {
                in->trigger = malloc(blob_len(tb[INSTANCE_ATTR_TRIGGER]));
                if (!in->trigger)
        if (tb[INSTANCE_ATTR_TRIGGER]) {
                in->trigger = malloc(blob_len(tb[INSTANCE_ATTR_TRIGGER]));
                if (!in->trigger)
@@ -414,8 +434,6 @@ instance_init(struct service_instance *in, struct service *s, struct blob_attr *
        in->config = config;
        in->timeout.cb = instance_timeout;
        in->proc.cb = instance_exit;
        in->config = config;
        in->timeout.cb = instance_timeout;
        in->proc.cb = instance_exit;
-       in->respawn = true;
-       in->respawn_count = 0;
 
        blobmsg_list_init(&in->netdev, struct instance_netdev, node, instance_netdev_cmp);
        blobmsg_list_init(&in->file, struct instance_file, node, instance_file_cmp);
 
        blobmsg_list_init(&in->netdev, struct instance_netdev, node, instance_netdev_cmp);
        blobmsg_list_init(&in->file, struct instance_file, node, instance_file_cmp);
@@ -443,6 +461,14 @@ void instance_dump(struct blob_buf *b, struct service_instance *in, int verbose)
                blobmsg_close_table(b, e);
        }
 
                blobmsg_close_table(b, e);
        }
 
+       if (in->respawn) {
+               void *r = blobmsg_open_table(b, "respawn");
+               blobmsg_add_u32(b, "timeout", in->respawn_timeout);
+               blobmsg_add_u32(b, "threshold", in->respawn_threshold);
+               blobmsg_add_u32(b, "retry", in->respawn_retry);
+               blobmsg_close_table(b, r);
+       }
+
        if (verbose && in->trigger)
                blobmsg_add_blob(b, in->trigger);
        if (!measure_process(in->proc.pid, &pi)) {
        if (verbose && in->trigger)
                blobmsg_add_blob(b, in->trigger);
        if (!measure_process(in->proc.pid, &pi)) {
index 1c8c0a0..6e69086 100644 (file)
@@ -35,9 +35,9 @@ struct service_instance {
        int respawn_count;
        struct timespec start;
 
        int respawn_count;
        struct timespec start;
 
-       int respawn_timeout;
-       int respawn_threshold;
-       int respawn_retry;
+       uint32_t respawn_timeout;
+       uint32_t respawn_threshold;
+       uint32_t respawn_retry;
 
        struct blob_attr *config;
        struct uloop_process proc;
 
        struct blob_attr *config;
        struct uloop_process proc;