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 90c167a4d634164c54bbbdcf9905f009e6bcca09..e6fc33147ad053d9cbd1eba2ba6aba3718ff06b8 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) {
-               if (runtime < RESPAWN_ERROR)
+               if (runtime < in->respawn_threshold)
                        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);
-               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;
 
+       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)
@@ -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->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);
@@ -443,6 +461,14 @@ void instance_dump(struct blob_buf *b, struct service_instance *in, int verbose)
                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)) {
index 1c8c0a081cecbab7e24d0e2060d54fd3659cffa0..6e690869898b38cc50e76913864962b744cb95f4 100644 (file)
@@ -35,9 +35,9 @@ struct service_instance {
        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;