do.cc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 <libgearman/common.h>
  38. #include <libgearman/add.hpp>
  39. #include <libgearman/universal.hpp>
  40. #include <cassert>
  41. #include <cerrno>
  42. #include <cstring>
  43. void *client_do(gearman_client_st *client, gearman_command_t command,
  44. const char *function_name,
  45. const char *unique,
  46. const void *workload_str, size_t workload_size,
  47. size_t *result_size, gearman_return_t *ret_ptr)
  48. {
  49. gearman_task_st do_task;
  50. gearman_string_t function= { gearman_string_param_cstr(function_name) };
  51. gearman_unique_t local_unique= gearman_unique_make(unique, unique ? strlen(unique) : 0);
  52. gearman_string_t workload= { static_cast<const char*>(workload_str), workload_size };
  53. gearman_return_t unused;
  54. if (not ret_ptr)
  55. ret_ptr= &unused;
  56. if (not client)
  57. {
  58. *ret_ptr= GEARMAN_ERRNO;
  59. errno= EINVAL;
  60. return NULL;
  61. }
  62. gearman_task_st *do_task_ptr= add_task(client, &do_task, NULL, command,
  63. function,
  64. local_unique,
  65. workload,
  66. time_t(0),
  67. gearman_actions_do_default());
  68. if (not do_task_ptr)
  69. {
  70. *ret_ptr= gearman_universal_error_code(client->universal);
  71. return NULL;
  72. }
  73. gearman_return_t ret= gearman_client_run_block_tasks(client);
  74. const void *returnable= NULL;
  75. *result_size= 0;
  76. // gearman_client_run_block_tasks failed
  77. if (gearman_failed(ret))
  78. {
  79. gearman_error(client->universal, ret, "occured during gearman_client_run_tasks()");
  80. *ret_ptr= ret;
  81. *result_size= 0;
  82. }
  83. else // Now we check the task itself
  84. {
  85. assert(ret == GEARMAN_SUCCESS); // Programmer mistake
  86. if (gearman_success(do_task_ptr->result_rc))
  87. {
  88. *ret_ptr= do_task_ptr->result_rc;
  89. if (do_task_ptr->result_ptr)
  90. {
  91. gearman_string_t result= gearman_result_take_string(do_task_ptr->result_ptr);
  92. *result_size= gearman_size(result);
  93. returnable= gearman_c_str(result);
  94. }
  95. else // NULL SUCCESSFUL job
  96. { }
  97. }
  98. else // gearman_client_run_block_tasks() was successful, but the task was not
  99. {
  100. gearman_error(client->universal, do_task_ptr->result_rc, "occured during gearman_client_run_tasks()");
  101. *ret_ptr= do_task_ptr->result_rc;
  102. *result_size= 0;
  103. }
  104. }
  105. assert(client->task_list);
  106. gearman_task_free(&do_task);
  107. client->new_tasks= 0;
  108. client->running_tasks= 0;
  109. return const_cast<void *>(returnable);
  110. }
  111. gearman_return_t client_do_background(gearman_client_st *client,
  112. gearman_command_t command,
  113. gearman_string_t &function,
  114. gearman_unique_t &unique,
  115. gearman_string_t &workload,
  116. char *job_handle)
  117. {
  118. gearman_task_st do_task;
  119. gearman_task_st *do_task_ptr= add_task(client, &do_task,
  120. client,
  121. command,
  122. function,
  123. unique,
  124. workload,
  125. time_t(0),
  126. gearman_actions_do_default());
  127. if (not do_task_ptr)
  128. {
  129. return gearman_universal_error_code(client->universal);
  130. }
  131. gearman_task_clear_fn(do_task_ptr);
  132. gearman_return_t ret= gearman_client_run_block_tasks(client);
  133. assert(ret != GEARMAN_IO_WAIT);
  134. if (ret != GEARMAN_IO_WAIT)
  135. {
  136. if (job_handle)
  137. {
  138. strncpy(job_handle, do_task.job_handle, GEARMAN_JOB_HANDLE_SIZE);
  139. }
  140. client->new_tasks= 0;
  141. client->running_tasks= 0;
  142. }
  143. gearman_task_free(&do_task);
  144. return ret;
  145. }