do.cc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * Gearmand client and server library.
  4. *
  5. * Copyright (C) 2011 Data Differential, http://datadifferential.com/
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are
  10. * met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * * Redistributions in binary form must reproduce the above
  16. * copyright notice, this list of conditions and the following disclaimer
  17. * in the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * * The names of its contributors may not be used to endorse or
  21. * promote products derived from this software without specific prior
  22. * written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  25. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  26. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  27. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  28. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  29. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  30. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  31. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  32. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  33. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  34. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. *
  36. */
  37. #include "gear_config.h"
  38. #include <libgearman/common.h>
  39. #include <libgearman/add.hpp>
  40. #include <libgearman/universal.hpp>
  41. #include <libgearman/do.hpp>
  42. #include "libgearman/assert.hpp"
  43. #include <cerrno>
  44. #include <cstring>
  45. void *client_do(gearman_client_st *client_shell, gearman_command_t command,
  46. const char *function_name,
  47. const char *unique,
  48. const void *workload_str, size_t workload_size,
  49. size_t *result_size, gearman_return_t *ret_ptr)
  50. {
  51. gearman_string_t function= { gearman_string_param_cstr(function_name) };
  52. gearman_unique_t local_unique= gearman_unique_make(unique, unique ? strlen(unique) : 0);
  53. gearman_string_t workload= { static_cast<const char*>(workload_str), workload_size };
  54. // Set to zero in case of error
  55. size_t unused_result_size;
  56. if (result_size == NULL)
  57. {
  58. result_size= &unused_result_size;
  59. }
  60. *result_size= 0;
  61. gearman_return_t unused;
  62. if (ret_ptr == NULL)
  63. {
  64. ret_ptr= &unused;
  65. }
  66. if (client_shell == NULL or client_shell->impl() == NULL)
  67. {
  68. *ret_ptr= GEARMAN_INVALID_ARGUMENT;
  69. return NULL;
  70. }
  71. Client* client= client_shell->impl();
  72. gearman_task_st* do_task= add_task(*(client), NULL, NULL, command,
  73. function,
  74. local_unique,
  75. workload,
  76. time_t(0),
  77. gearman_actions_do_default());
  78. if (do_task == NULL)
  79. {
  80. *ret_ptr= client->universal.error_code();
  81. return NULL;
  82. }
  83. gearman_return_t ret= gearman_client_run_block_tasks(client, do_task);
  84. const void *returnable= NULL;
  85. // gearman_client_run_block_tasks failed
  86. if (gearman_failed(ret))
  87. {
  88. gearman_error(client->universal, ret, "occurred during gearman_client_run_tasks()");
  89. *ret_ptr= ret;
  90. *result_size= 0;
  91. }
  92. else // Now we check the task itself
  93. {
  94. assert(ret == GEARMAN_SUCCESS); // Programmer mistake
  95. if (gearman_success(do_task->impl()->error_code()))
  96. {
  97. *ret_ptr= do_task->impl()->error_code();
  98. if (gearman_task_result(do_task))
  99. {
  100. gearman_string_t result= gearman_result_take_string(do_task->impl()->result());
  101. *result_size= gearman_size(result);
  102. returnable= gearman_c_str(result);
  103. }
  104. else // NULL SUCCESSFUL job
  105. { }
  106. }
  107. else // gearman_client_run_block_tasks() was successful, but the task was not
  108. {
  109. gearman_error(client->universal, do_task->impl()->error_code(), "occurred during gearman_client_run_tasks()");
  110. *ret_ptr= do_task->impl()->error_code();
  111. *result_size= 0;
  112. }
  113. }
  114. assert(client->task_list);
  115. gearman_task_free(do_task);
  116. client->new_tasks= 0;
  117. client->running_tasks= 0;
  118. return const_cast<void *>(returnable);
  119. }
  120. gearman_return_t client_do_background(gearman_client_st *client_shell,
  121. gearman_command_t command,
  122. gearman_string_t &function,
  123. gearman_unique_t &unique,
  124. gearman_string_t &workload,
  125. gearman_job_handle_t job_handle)
  126. {
  127. if (client_shell == NULL or client_shell->impl() == NULL)
  128. {
  129. return GEARMAN_INVALID_ARGUMENT;
  130. }
  131. Client* client= client_shell->impl();
  132. gearman_task_st* do_task= add_task(*(client), NULL,
  133. client,
  134. command,
  135. function,
  136. unique,
  137. workload,
  138. time_t(0),
  139. gearman_actions_do_default());
  140. if (do_task == NULL)
  141. {
  142. return client->universal.error_code();
  143. }
  144. gearman_task_clear_fn(do_task);
  145. gearman_return_t ret= gearman_client_run_block_tasks(client, do_task);
  146. assert(ret != GEARMAN_IO_WAIT);
  147. if (ret != GEARMAN_IO_WAIT)
  148. {
  149. if (job_handle)
  150. {
  151. strncpy(job_handle, do_task->impl()->job_handle, GEARMAN_JOB_HANDLE_SIZE);
  152. }
  153. client->new_tasks= 0;
  154. client->running_tasks= 0;
  155. }
  156. gearman_task_free(do_task);
  157. return ret;
  158. }