Browse Source

Merge in additional tests for execute interface.

Brian Aker 14 years ago
parent
commit
781b9f7ada

+ 1 - 0
.bzrignore

@@ -96,3 +96,4 @@ unittests/unittests
 config/top.h
 bin/wait
 libhostile
+examples/reverse_client_epoch

+ 14 - 6
examples/reverse_client_bg.cc

@@ -125,14 +125,22 @@ int main(int args, char *argv[])
 
   gearman_function_t *function= gearman_function_create(&client, gearman_literal_param("reverse"));
 
-  gearman_workload_t workload= gearman_workload_make(text_to_echo.c_str(), text_to_echo.size(), NULL);
+  gearman_workload_t workload= gearman_workload_make(text_to_echo.c_str(), text_to_echo.size());
+  gearman_workload_set_background(&workload, true);
 
-  gearman_task_st *task;
-  task= gearman_client_execute(&client,
-                               function,
-                               NULL,
-                               &workload);
+  gearman_status_t status= gearman_client_execute(&client,
+                                                   function,
+                                                   NULL,
+                                                   &workload);
 
+  if (not gearman_status_is_successful(status))
+  {
+    std::cerr << "Failed to process job (" << gearman_client_error(&client) << std::endl;
+    gearman_client_free(&client);
+    return EXIT_FAILURE;
+  }
+
+  gearman_task_st *task= gearman_status_task(status);
   std::cout << "Background Job Handle=" << gearman_task_job_handle(task) << std::endl;
 
   int exit_code= EXIT_SUCCESS;

+ 7 - 7
examples/reverse_client_epoch.cc

@@ -127,19 +127,19 @@ int main(int args, char *argv[])
   gearman_function_t *function= gearman_function_create(&client, gearman_literal_param("reverse"));
 
   gearman_unique_t unique= gearman_unique_make(gearman_literal_param("epoch"));
-  gearman_workload_t workload= gearman_workload_make(text_to_echo.c_str(), text_to_echo.size(), NULL);
+  gearman_workload_t workload= gearman_workload_make(text_to_echo.c_str(), text_to_echo.size());
   gearman_workload_set_epoch(&workload, time(NULL) +epoch);
 
-  gearman_task_st *task;
-  task= gearman_client_execute(&client,
-                               function,
-                               NULL,
-                               &workload);
-  if (not task)
+  gearman_status_t status= gearman_client_execute(&client,
+                                                  function,
+                                                  NULL,
+                                                  &workload);
+  if (not gearman_status_is_successful(status))
   {
     std::cerr << gearman_client_error(&client) << std::endl;
     return EXIT_FAILURE;
   }
+  gearman_task_st *task= gearman_status_task(status);
 
   std::cout << "Background Job Handle=" << gearman_task_job_handle(task) << std::endl;
 

+ 16 - 11
libgearman/client.cc

@@ -404,19 +404,18 @@ static inline gearman_command_t pick_command_by_priority_background(const gearma
 }
 
 
-gearman_task_st *gearman_client_execute(gearman_client_st *client,
+gearman_status_t gearman_client_execute(gearman_client_st *client,
                                         const gearman_function_t *function,
                                         gearman_unique_t *unique,
                                         const gearman_workload_t *workload)
 {
-  gearman_command_t command;
-
   if (not client)
-    return NULL;
+    return GEARMAN_STATUS_FAIL;
 
   if (not function)
-    return NULL;
+    return GEARMAN_STATUS_FAIL;
 
+  gearman_command_t command;
   if (gearman_workload_epoch(workload))
   {
     command= GEARMAN_COMMAND_SUBMIT_JOB_EPOCH;
@@ -440,16 +439,22 @@ gearman_task_st *gearman_client_execute(gearman_client_st *client,
                                           gearman_workload_epoch(workload),
                                           &rc);
   if (not task)
-    return NULL;
+    return GEARMAN_STATUS_FAIL;
 
-  rc= gearman_client_run_tasks(client);
-  if (rc !=  GEARMAN_SUCCESS)
+  if (not gearman_workload_background(workload))
   {
-    gearman_error(&client->universal, rc, "gearman_client_run_tasks");
-    return NULL;
+    rc= gearman_client_run_tasks(client);
+    if (rc !=  GEARMAN_SUCCESS)
+    {
+      gearman_error(&client->universal, rc, "failure occured during gearman_client_run_tasks()");
+      gearman_task_free(task);
+
+      return GEARMAN_STATUS_FAIL;
+    }
   }
 
-  return task;
+  gearman_status_t status= { true, task };
+  return status;
 }
 
 const char *gearman_client_do_job_handle(const gearman_client_st *client)

+ 1 - 1
libgearman/client.h

@@ -491,7 +491,7 @@ void gearman_client_set_task_context_free_fn(gearman_client_st *client,
 
 // Use the job handle in task for returning all information.
 GEARMAN_API
-gearman_task_st *gearman_client_execute(gearman_client_st *client,
+gearman_status_t gearman_client_execute(gearman_client_st *client,
                                         const gearman_function_t *function,
                                         gearman_unique_t *unique,
                                         const gearman_workload_t *workload);

+ 1 - 0
libgearman/constants.h

@@ -207,6 +207,7 @@ typedef struct gearman_worker_st gearman_worker_st;
 typedef struct gearman_function_t gearman_function_t;
 typedef struct gearman_workload_t gearman_workload_t;
 typedef struct gearman_unique_t gearman_unique_t;
+typedef struct gearman_status_t gearman_status_t;
 
 /* Function types. */
 typedef gearman_return_t (gearman_workload_fn)(gearman_task_st *task);

+ 1 - 0
libgearman/gearman.h

@@ -74,6 +74,7 @@
 #include <libgearman/worker.h>
 #include <libgearman/client.h>
 #include <libgearman/function.h>
+#include <libgearman/status.h>
 
 #ifdef __cplusplus
 extern "C" {

+ 2 - 0
libgearman/include.am

@@ -23,6 +23,7 @@ nobase_include_HEADERS+= \
 			 libgearman/log.h \
 			 libgearman/packet.h \
 			 libgearman/protocol.h \
+			 libgearman/status.h \
 			 libgearman/strerror.h \
 			 libgearman/task.h \
 			 libgearman/unique.h \
@@ -60,6 +61,7 @@ libgearman_libgearman_la_SOURCES= \
 				  libgearman/function.cc \
 				  libgearman/gearman.cc \
 				  libgearman/job.cc \
+				  libgearman/status.cc \
 				  libgearman/strerror.cc \
 				  libgearman/task.cc \
 				  libgearman/unique.cc \

+ 54 - 0
libgearman/status.cc

@@ -0,0 +1,54 @@
+/*  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
+ * 
+ *  Gearmand client and server library.
+ *
+ *  Copyright (C) 2011 Data Differential, http://datadifferential.com/
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions are
+ *  met:
+ *
+ *      * Redistributions of source code must retain the above copyright
+ *  notice, this list of conditions and the following disclaimer.
+ *
+ *      * Redistributions in binary form must reproduce the above
+ *  copyright notice, this list of conditions and the following disclaimer
+ *  in the documentation and/or other materials provided with the
+ *  distribution.
+ *
+ *      * The names of its contributors may not be used to endorse or
+ *  promote products derived from this software without specific prior
+ *  written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#include <libgearman/common.h>
+
+gearman_status_t gearman_failure()
+{
+  static gearman_status_t status= { false, 0 };
+  return status;
+}
+
+bool gearman_status_is_successful(const gearman_status_t self)
+{
+  return self.successful;
+}
+
+gearman_task_st *gearman_status_task(const gearman_status_t self)
+{
+  return self.task;
+}

+ 61 - 0
libgearman/status.h

@@ -0,0 +1,61 @@
+/*  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
+ * 
+ *  Gearmand client and server library.
+ *
+ *  Copyright (C) 2011 Data Differential, http://datadifferential.com/
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions are
+ *  met:
+ *
+ *      * Redistributions of source code must retain the above copyright
+ *  notice, this list of conditions and the following disclaimer.
+ *
+ *      * Redistributions in binary form must reproduce the above
+ *  copyright notice, this list of conditions and the following disclaimer
+ *  in the documentation and/or other materials provided with the
+ *  distribution.
+ *
+ *      * The names of its contributors may not be used to endorse or
+ *  promote products derived from this software without specific prior
+ *  written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#pragma once
+
+struct gearman_status_t {
+  const bool successful;
+  gearman_task_st *task;
+};
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+GEARMAN_API
+gearman_status_t gearman_failure(void);
+#define GEARMAN_STATUS_FAIL gearman_failure();
+
+GEARMAN_API
+bool gearman_status_is_successful(const gearman_status_t);
+
+GEARMAN_API
+gearman_task_st *gearman_status_task(const gearman_status_t);
+
+#ifdef __cplusplus
+}
+#endif

Some files were not shown because too many files changed in this diff