do.cc 5.5 KB

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