summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJosé Bollo <jose.bollo@iot.bzh>2017-04-04 11:49:15 +0200
committerJosé Bollo <jose.bollo@iot.bzh>2017-04-04 11:50:00 +0200
commit41618d081d650f9cc105034bfa37d3b861663db7 (patch)
tree306957c5ca876f2c6bf3ead8e4104fbffa3474b2
parent0b838a6101edc810098ee0645d3bfd966096ae07 (diff)
Launch job at a earlier step
Change-Id: I3fcb96e4d748e38eacc4d413a451143dd9b4a10d Signed-off-by: José Bollo <jose.bollo@iot.bzh>
-rw-r--r--src/afb-api-so-v1.c1
-rw-r--r--src/afb-api-so-v2.c1
-rw-r--r--src/afb-api-so.c7
-rw-r--r--src/afb-api-so.h4
-rw-r--r--src/afb-apis.c52
-rw-r--r--src/afb-apis.h1
-rw-r--r--src/afb-xreq.c27
-rw-r--r--src/afb-xreq.h3
-rw-r--r--src/main.c2
9 files changed, 50 insertions, 48 deletions
diff --git a/src/afb-api-so-v1.c b/src/afb-api-so-v1.c
index 63ea3f8a..fee6b2e1 100644
--- a/src/afb-api-so-v1.c
+++ b/src/afb-api-so-v1.c
@@ -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;
diff --git a/src/afb-api-so-v2.c b/src/afb-api-so-v2.c
index 401aa5cd..75e59d66 100644
--- a/src/afb-api-so-v2.c
+++ b/src/afb-api-so-v2.c
@@ -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;
diff --git a/src/afb-api-so.c b/src/afb-api-so.c
index 4908ba85..a22e4442 100644
--- a/src/afb-api-so.c
+++ b/src/afb-api-so.c
@@ -30,13 +30,6 @@
#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;
diff --git a/src/afb-api-so.h b/src/afb-api-so.h
index 59125fa5..fcf66ccf 100644
--- a/src/afb-api-so.h
+++ b/src/afb-api-so.h
@@ -18,10 +18,6 @@
#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);
diff --git a/src/afb-apis.c b/src/afb-apis.c
index de122b6b..2f61fe95 100644
--- a/src/afb-apis.c
+++ b/src/afb-apis.c
@@ -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);
}
}
diff --git a/src/afb-apis.h b/src/afb-apis.h
index 540b644b..e9834d0e 100644
--- a/src/afb-apis.h
+++ b/src/afb-apis.h
@@ -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);
diff --git a/src/afb-xreq.c b/src/afb-xreq.c
index 1a70fe7a..c8fd94e9 100644
--- a/src/afb-xreq.c
+++ b/src/afb-xreq.c
@@ -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 });
}
diff --git a/src/afb-xreq.h b/src/afb-xreq.h
index 3b8a590c..29dcb3e2 100644
--- a/src/afb-xreq.h
+++ b/src/afb-xreq.h
@@ -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);
+
diff --git a/src/main.c b/src/main.c
index 1c85411a..8d1c7d16 100644
--- a/src/main.c
+++ b/src/main.c
@@ -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");