Launch job at a earlier step
authorJosé Bollo <jose.bollo@iot.bzh>
Tue, 4 Apr 2017 09:49:15 +0000 (11:49 +0200)
committerJosé Bollo <jose.bollo@iot.bzh>
Tue, 4 Apr 2017 09:50:00 +0000 (11:50 +0200)
Change-Id: I3fcb96e4d748e38eacc4d413a451143dd9b4a10d
Signed-off-by: José Bollo <jose.bollo@iot.bzh>
src/afb-api-so-v1.c
src/afb-api-so-v2.c
src/afb-api-so.c
src/afb-api-so.h
src/afb-apis.c
src/afb-apis.h
src/afb-xreq.c
src/afb-xreq.h
src/main.c

index 63ea3f8..fee6b2e 100644 (file)
@@ -145,7 +145,6 @@ static void call_cb(void *closure, struct afb_xreq *xreq)
        if (!verb)
                afb_xreq_fail_f(xreq, "unknown-verb", "verb %s unknown within api %s", xreq->verb, desc->binding->v1.prefix);
        else {
-               xreq->timeout = afb_api_so_timeout;
                xreq->sessionflags = (int)verb->session;
                xreq->group = desc;
                xreq->callback = verb->callback;
index 401aa5c..75e59d6 100644 (file)
@@ -143,7 +143,6 @@ static void call_cb(void *closure, struct afb_xreq *xreq)
        if (!verb)
                afb_xreq_fail_f(xreq, "unknown-verb", "verb %s unknown within api %s", xreq->verb, desc->binding->api);
        else {
-               xreq->timeout = afb_api_so_timeout;
                xreq->sessionflags = (int)verb->session;
                xreq->group = desc;
                xreq->callback = verb->callback;
index 4908ba8..a22e444 100644 (file)
 #include "afb-api-so-v2.h"
 #include "verbose.h"
 
-int afb_api_so_timeout = 15;
-
-void afb_api_so_set_timeout(int to)
-{
-       afb_api_so_timeout = to;
-}
-
 static int load_binding(const char *path, int force)
 {
        int rc;
index 59125fa..fcf66cc 100644 (file)
 
 #pragma once
 
-extern int afb_api_so_timeout;
-
-extern void afb_api_so_set_timeout(int to);
-
 extern int afb_api_so_add_binding(const char *path);
 
 extern int afb_api_so_add_directory(const char *path);
index de122b6..2f61fe9 100644 (file)
@@ -29,6 +29,7 @@
 #include "afb-context.h"
 #include "afb-hook.h"
 #include "afb-xreq.h"
+#include "jobs.h"
 
 #include <afb/afb-req-itf.h>
 
@@ -42,6 +43,16 @@ struct api_desc {
 
 static struct api_desc *apis_array = NULL;
 static int apis_count = 0;
+static int apis_timeout = 15;
+
+/**
+ * Set the API timeout
+ * @param to the timeout in seconds
+ */
+void afb_apis_set_timeout(int to)
+{
+       apis_timeout = to;
+}
 
 /**
  * Checks wether 'name' is a valid API name.
@@ -220,6 +231,31 @@ int afb_apis_start_all_services(int share_session)
        return 0;
 }
 
+
+
+
+
+
+static void do_call_async(int signum, void *arg)
+{
+       struct afb_xreq *xreq = arg;
+       const struct api_desc *a;
+
+       if (signum != 0)
+               afb_xreq_fail_f(xreq, "aborted", "signal %s(%d) caught", strsignal(signum), signum);
+       else {
+               /* search the api */
+               a = search(xreq->api);
+               if (!a)
+                       afb_xreq_fail_f(xreq, "unknown-api", "api %s not found", xreq->api);
+               else {
+                       xreq->context.api_key = a->api.closure;
+                       a->api.call(a->api.closure, xreq);
+               }
+       }
+       afb_xreq_unref(xreq);
+}
+
 /**
  * Dispatch the request 'req' with the 'context' to the
  * method of 'api' and 'verb'.
@@ -230,18 +266,18 @@ int afb_apis_start_all_services(int share_session)
  */
 void afb_apis_call(struct afb_xreq *xreq)
 {
-       const struct api_desc *a;
+       int rc;
 
        /* init hooking the request */
        // TODO req = afb_hook_req_call(req, context, api, verb);
 
-       /* search the api */
-       a = search(xreq->api);
-       if (!a)
-               afb_xreq_fail_f(xreq, "unknown-api", "api %s not found", xreq->api);
-       else {
-               xreq->context.api_key = a->api.closure;
-               a->api.call(a->api.closure, xreq);
+       afb_xreq_addref(xreq);
+       rc = jobs_queue(NULL, apis_timeout, do_call_async, xreq);
+       if (rc < 0) {
+               /* TODO: allows or not to proccess it directly as when no threading? (see above) */
+               ERROR("can't process job with threads: %m");
+               afb_xreq_fail_f(xreq, "cancelled", "not able to create a job for the task");
+               afb_xreq_unref(xreq);
        }
 }
 
index 540b644..e9834d0 100644 (file)
@@ -28,6 +28,7 @@ struct afb_api
        int (*service_start)(void *closure, int share_session, int onneed);
 };
 
+extern void afb_apis_set_timeout(int to);
 
 extern int afb_apis_is_valid_api_name(const char *name);
 
index 1a70fe7..c8fd94e 100644 (file)
@@ -30,7 +30,6 @@
 #include "afb-evt.h"
 #include "afb-msg-json.h"
 #include "afb-subcall.h"
-#include "jobs.h"
 #include "verbose.h"
 
 
@@ -320,31 +319,9 @@ static int xcheck(struct afb_xreq *xreq)
        return 1;
 }
 
-static void xreq_run_cb(int signum, void *arg)
-{
-       struct afb_xreq *xreq = arg;
-
-       if (signum == 0)
-               xreq->callback((struct afb_req){ .itf = &xreq_itf, .closure = xreq });
-       else {
-               afb_xreq_fail_f(xreq, "aborted", "signal %s(%d) caught", strsignal(signum), signum);
-               
-       }
-       afb_xreq_unref(xreq);
-}
-
 void afb_xreq_call(struct afb_xreq *xreq)
 {
-       int rc;
-       if (xcheck(xreq)) {
-               afb_xreq_addref(xreq);
-               rc = jobs_queue(xreq->group, xreq->timeout, xreq_run_cb, xreq);
-               if (rc < 0) {
-                       /* TODO: allows or not to proccess it directly as when no threading? (see above) */
-                       ERROR("can't process job with threads: %m");
-                       afb_xreq_fail_f(xreq, "cancelled", "not able to pipe a job for the task");
-                       xreq_unref_cb(xreq);
-               }
-       }
+       if (xcheck(xreq))
+               xreq->callback((struct afb_req){ .itf = &xreq_itf, .closure = xreq });
 }
 
index 3b8a590..29dcb3e 100644 (file)
@@ -65,8 +65,9 @@ extern void afb_xreq_success(struct afb_xreq *xreq, struct json_object *obj, con
 extern void afb_xreq_fail(struct afb_xreq *xreq, const char *status, const char *info);
 extern void afb_xreq_fail_f(struct afb_xreq *xreq, const char *status, const char *info, ...);
 extern void afb_xreq_success_f(struct afb_xreq *xreq, struct json_object *obj, const char *info, ...);
-extern void afb_xreq_call(struct afb_xreq *xreq);
 extern const char *afb_xreq_raw(struct afb_xreq *xreq, size_t *size);
 extern int afb_xreq_subscribe(struct afb_xreq *xreq, struct afb_event event);
 extern int afb_xreq_unsubscribe(struct afb_xreq *xreq, struct afb_event event);
 
+extern void afb_xreq_call(struct afb_xreq *xreq);
+
index 1c85411..8d1c7d1 100644 (file)
@@ -412,7 +412,7 @@ static void start()
                goto error;
        }
 
-       afb_api_so_set_timeout(config->apiTimeout);
+       afb_apis_set_timeout(config->apiTimeout);
        start_list(config->dbus_clients, afb_api_dbus_add_client, "the afb-dbus client");
        start_list(config->ws_clients, afb_api_ws_add_client, "the afb-websocket client");
        start_list(config->ldpaths, afb_api_so_add_pathset, "the binding path set");