gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[taler-merchant] branch master updated (d178d8ab -> 37cd7259)


From: gnunet
Subject: [taler-merchant] branch master updated (d178d8ab -> 37cd7259)
Date: Tue, 24 Jan 2023 15:07:16 +0100

This is an automated email from the git hooks/post-receive script.

priscilla-huang pushed a change to branch master
in repository merchant.

    from d178d8ab use latest from prebuilt
     new f25e2b76 update
     new a99ab8c9 stash changes
     new 37cd7259 update pending webhooks

The 3 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 src/backend/taler-merchant-webhook.c       | 222 ++++++++++++++++++++++++-----
 src/backenddb/plugin_merchantdb_postgres.c |   7 +-
 src/backenddb/test_merchantdb.c            |   2 +-
 src/include/taler_merchantdb_plugin.h      |   2 +-
 4 files changed, 191 insertions(+), 42 deletions(-)

diff --git a/src/backend/taler-merchant-webhook.c 
b/src/backend/taler-merchant-webhook.c
index 7df1ad8e..6d68c303 100644
--- a/src/backend/taler-merchant-webhook.c
+++ b/src/backend/taler-merchant-webhook.c
@@ -28,6 +28,21 @@
 #include "taler_json_lib.h"
 
 
+struct Work_response
+{
+  struct Work_response *next;
+  struct Work_response *prev;
+  struct GNUNET_CURL_Job *job;
+  uint64_t webhook_serial;
+  char *body;
+  struct curl_slist *job_headers;
+};
+
+
+static struct Work_response *w_head;
+
+static struct Work_response *w_tail;
+
 /**
  * The exchange's configuration.
  */
@@ -72,6 +87,8 @@ static int test_mode;
 static void
 shutdown_task (void *cls)
 {
+  struct Work_response *w;
+
   (void) cls;
   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
               "Running shutdown\n");
@@ -80,6 +97,16 @@ shutdown_task (void *cls)
     GNUNET_SCHEDULER_cancel (task);
     task = NULL;
   }
+  while (NULL != (w = w_head))
+  {
+    GNUNET_CONTAINER_DLL_remove (w_head,
+                                 w_tail,
+                                 w);
+    GNUNET_CURL_job_cancel (w->job);
+    curl_slist_free (w->job_headers);
+    GNUNET_free (w->body);
+    GNUNET_free (w);
+  }
   db_plugin->rollback (db_plugin->cls); /* just in case */
   TALER_MERCHANTDB_plugin_unload (db_plugin);
   db_plugin = NULL;
@@ -98,27 +125,64 @@ shutdown_task (void *cls)
 
 
 /**
- * Select webhook to process.
+ * This function is used by the function pending_webhooks_cb. According to the 
response code,
+ * we delete or update the webhook.
  *
- * @param cls NULL
+ * @param cls closure
+ * @param response_code HTTP response code from server, 0 on hard error
+ * @param body http body of the response
+ * @param body_size number of bytes in @a body
  */
 static void
-select_work (void *cls);
-
-
-void
-handle_webhook_response (void *cls)
+handle_webhook_response (void *cls,
+                         long response_code,
+                         const void *body,
+                         size_t body_size)
 {
-  cls->webhook_serial;
-  // update DB status on the webhook! (delete or update webhook)
+  struct Work_response *w = cls;
+  struct GNUNET_TIME_Relative next_attempt;
 
-  task = GNUNET_SCHEDULER_add_now (&select_work,
-                                   NULL);
+  (void) body;
+  (void) body_size;
+  job = NULL;
+
+  if (2 == response_code / 100) /* any 2xx http status code is OK! */
+  {
+    db_plugib->delete_pending_webhook (db_plugin->cls,
+                                       w->webhook_serial);
+  }
+  else
+  {
+    switch (response_code)
+      {
+      case MHD_HTTP_BAD_REQUEST:
+        next_attempt = GNUNET_TIME_UNIT_FOREVER_REL; // never try again
+        break;
+      case MHD_HTTP_INTERNAL_SERVER_ERROR:
+        next_attempt = GNUNET_TIME_UNIT_MINUTES;
+        break;
+      default:
+        next_attempt = GNUNET_TIME_UNIT_HOURS;
+        break;
+      }
+    db_plugin->update_pending_webhook (db_plugin->cls,
+                                       w->webhook_serial,
+                                       GNUNET_TIME_relative_to_absolute 
(next_attempt));
+  }
+  GNUNET_CONTAINER_DLL_remove (w_head,
+                               w_tail,
+                               w);
+  GNUNET_free (w->body);
+  curl_slist_free_all (w->job_headers);
+  GNUNET_free (w);
+  if (NULL == w_head)
+    task = GNUNET_SCHEDULER_add_now (&select_work,
+                                     NULL);
 }
 
 
 /**
- * Typically called by `lookup_pending_webhooks`.
+ * Typically called by `select_work`.
  *
  * @param cls a `json_t *` JSON array to build
  * @param webhook_serial reference to the configured webhook template.
@@ -130,34 +194,105 @@ handle_webhook_response (void *cls)
  * @param body of the webhook
  */
 static void
-cb (void *cls,
-    uint64_t webhook_serial,
-    struct GNUNET_TIME_Absolute
-    next_attempt,
-    uint32_t retries,
-    const char *url,
-    const char *http_method,
-    const char *header,
-    const char *body)
+pending_webhooks_cb (void *cls,
+                     uint64_t webhook_serial,
+                     struct GNUNET_TIME_Absolute next_attempt,
+                     uint32_t retries,
+                     const char *url,
+                     const char *http_method,
+                     const char *header,
+                     const char *body)
 {
+  struct Work_response *w = GNUNET_new (struct Work_response);
   CURL *eh;
-
+  struct curl_slist *job_headers = NULL;
+  (void) retries;
+  (void) next_attempt;
+
+  GNUNET_CONTAINER_DLL_insert (w_head,
+                               w_tail,
+                               w);
+  w->webhook_serial = webhook_serial;
   eh = curl_easy_new ();
-
+  GNUNET_assert (NULL != eh);
   GNUNET_assert (CURLE_OK ==
                  curl_easy_setopt (eh,
                                    CURLOPT_CUSTOMREQUEST,
-                                   method));
+                                   http_method));
   GNUNET_assert (CURLE_OK ==
                  curl_easy_setopt (eh,
                                    CURLOPT_URL,
                                    url));
-  // run the job
-  job = GNUNET_CURL_job_add2 (ctx,
-                              eh,
-                              ofh->post_ctx.headers,
-                              &handle_webhook_response,
-                              NULL /* pass struct with webhook ID for 
update!*/);
+
+  /* conversion body data */
+  w->body = GNUNET_strdup (body);
+  GNUNET_assert (CURLE_OK ==
+                 curl_easy_setopt (eh,
+                                   CURLOPT_POSTFIELDS,
+                                   w->body));
+
+  /* conversion header to job_headers data */
+  job_headers = GNUNET_strdup (header);
+  GNUNET_assert (CURLE_OK ==
+                 curl_easy_setopt (eh,
+                                   CURLOPT_POSTFIELDS,
+                                   job_headers));
+  w->job_headers = job_headers;
+
+  w->job = GNUNET_CURL_job_add_raw (ctx,
+                                    eh,
+                                    job_headers,
+                                    &handle_webhook_response,
+                                    w);
+  if (NULL == w->job)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "Failed to start the curl job for webhook #%llu\n",
+                (unsigned long long) webhook_serial);
+    curl_slist_free_all (w->job_headers);
+    GNUNET_free (w->body);
+    GNUNET_CONTAINER_DLL_remove (w_head,
+                                 w_tail,
+                                 w);
+    GNUNET_free (w);
+    GNUNET_SCHEDULER_shutdown ();
+    return;
+  }
+}
+
+
+/**
+ * Typically called by `select_work`.
+ *
+ * @param cls a `json_t *` JSON array to build
+ * @param webhook_serial reference to the configured webhook template.
+ * @param next_attempt is the time we should make the next request to the 
webhook.
+ * @param retries how often have we tried this request to the webhook.
+ * @param url to make request to
+ * @param http_method use for the webhook
+ * @param header of the webhook
+ * @param body of the webhook
+ */
+static void
+future_webhook_cb (void *cls,
+                   uint64_t webhook_serial,
+                   struct GNUNET_TIME_Absolute next_attempt,
+                   uint32_t retries,
+                   const char *url,
+                   const char *http_method,
+                   const char *header,
+                   const char *body)
+{
+  (void) webhook_serial;
+  (void) retries;
+  (void) url;
+  (void) http_method;
+  (void) header;
+  (void) body;
+
+  task = GNUNET_SCHEDULER_add_at (next_attempt,
+                                  &select_work,
+                                  NULL);
 }
 
 
@@ -183,17 +318,34 @@ select_work (void *cls)
     return;
   }
   qs = db_plugin->lookup_pending_webhooks (db_plugin->cls,
-                                           &cb,
+                                           &pending_webhooks_cb,
                                            NULL);
   switch (qs)
   {
-  // FIXME: handle qs
-  case 1:
-  // wait for completion, then select more work.
   case 0:
-    db_plugin->lookup_future_webhook (...);
+    qs = db_plugin->lookup_future_webhook (db_plugin->cls,
+                                           &future_webhook_cb,
+                                           NULL);
+    switch (qs) {
+    case 0:
+      return;
+    case -1:
+      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                  "Failed!\n");
+      global_ret = EXIT_FAILURE;
+      GNUNET_SCHEDULER_shutdown ();
+    return;
+    }
+    return;
   case -1 / -2:
     // shutdown.
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "Failed!\n");
+    global_ret = EXIT_FAILURE;
+    GNUNET_SCHEDULER_shutdown ();
+    return;
+  default:
+    return; // wait for completion, then select more work.
   }
 }
 
diff --git a/src/backenddb/plugin_merchantdb_postgres.c 
b/src/backenddb/plugin_merchantdb_postgres.c
index 3d31b5c2..959b2253 100644
--- a/src/backenddb/plugin_merchantdb_postgres.c
+++ b/src/backenddb/plugin_merchantdb_postgres.c
@@ -7803,16 +7803,13 @@ postgres_lookup_all_webhooks (void *cls,
 static enum GNUNET_DB_QueryStatus
 postgres_update_pending_webhook (void *cls,
                                  uint64_t webhook_serial,
-                                 const struct
-                                 TALER_MERCHANTDB_PendingWebhookDetails *pwb)
-// maybe add: http status of failure?
+                                 struct GNUNET_TIME_Absolute next_attempt)
 {
   struct PostgresClosure *pg = cls;
   struct GNUNET_PQ_QueryParam params[] = {
     GNUNET_PQ_query_param_uint64 (&webhook_serial),
-    GNUNET_PQ_query_param_absolute_time (&pwb->next_attempt),
+    GNUNET_PQ_query_param_absolute_time (&next_attempt),
     GNUNET_PQ_query_param_end
-
   };
 
   check_connection (pg);
diff --git a/src/backenddb/test_merchantdb.c b/src/backenddb/test_merchantdb.c
index cbf4e4b6..69a376aa 100644
--- a/src/backenddb/test_merchantdb.c
+++ b/src/backenddb/test_merchantdb.c
@@ -7909,7 +7909,7 @@ test_update_pending_webhook (const struct InstanceData 
*instance,
   TEST_COND_RET_ON_FAIL (expected_result ==
                          plugin->update_pending_webhook (plugin->cls,
                                                          
pwebhook->webhook_serial,
-                                                         &pwebhook->pwebhook),
+                                                         
pwebhook->pwebhook.next_attempt),
                          "Update pending webhook failed\n");
   return 0;
 }
diff --git a/src/include/taler_merchantdb_plugin.h 
b/src/include/taler_merchantdb_plugin.h
index 434b82ef..d9fe0d24 100644
--- a/src/include/taler_merchantdb_plugin.h
+++ b/src/include/taler_merchantdb_plugin.h
@@ -2828,7 +2828,7 @@ struct TALER_MERCHANTDB_Plugin
   enum GNUNET_DB_QueryStatus
   (*update_pending_webhook)(void *cls,
                             uint64_t webhook_serial,
-                            const struct 
TALER_MERCHANTDB_PendingWebhookDetails *pwb);
+                            struct GNUNET_TIME_Absolute next_attempt);
                             // maybe add: http status of failure?
 
 

-- 
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]