execute.cc 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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/assert.hpp"
  42. #include <cerrno>
  43. static inline gearman_command_t pick_command_by_priority(const gearman_job_priority_t &arg)
  44. {
  45. if (arg == GEARMAN_JOB_PRIORITY_NORMAL)
  46. return GEARMAN_COMMAND_SUBMIT_JOB;
  47. else if (arg == GEARMAN_JOB_PRIORITY_HIGH)
  48. return GEARMAN_COMMAND_SUBMIT_JOB_HIGH;
  49. return GEARMAN_COMMAND_SUBMIT_JOB_LOW;
  50. }
  51. static inline gearman_command_t pick_command_by_priority_background(const gearman_job_priority_t &arg)
  52. {
  53. if (arg == GEARMAN_JOB_PRIORITY_NORMAL)
  54. {
  55. return GEARMAN_COMMAND_SUBMIT_JOB_BG;
  56. }
  57. else if (arg == GEARMAN_JOB_PRIORITY_HIGH)
  58. {
  59. return GEARMAN_COMMAND_SUBMIT_JOB_HIGH_BG;
  60. }
  61. return GEARMAN_COMMAND_SUBMIT_JOB_LOW_BG;
  62. }
  63. gearman_task_st *gearman_execute(gearman_client_st *client_shell,
  64. const char *function_name, size_t function_length,
  65. const char *unique_str, size_t unique_length,
  66. gearman_task_attr_t *task_attr,
  67. gearman_argument_t *arguments,
  68. void *context)
  69. {
  70. if (client_shell == NULL or client_shell->impl() == NULL)
  71. {
  72. return NULL;
  73. }
  74. Client* client= client_shell->impl();
  75. gearman_argument_t null_arg= gearman_argument_make(0, 0, 0, 0);
  76. if (arguments == NULL)
  77. {
  78. arguments= &null_arg;
  79. }
  80. if (function_name == NULL or function_length == 0)
  81. {
  82. gearman_error(client->universal, GEARMAN_INVALID_ARGUMENT, "function_name was NULL");
  83. return NULL;
  84. }
  85. gearman_string_t function= { function_name, function_length };
  86. gearman_task_st *task= NULL;
  87. gearman_unique_t unique= gearman_unique_make(unique_str, unique_length);
  88. if (task_attr)
  89. {
  90. switch (task_attr->kind)
  91. {
  92. case GEARMAN_TASK_ATTR_BACKGROUND:
  93. task= add_task(*client,
  94. context,
  95. pick_command_by_priority_background(task_attr->priority),
  96. function,
  97. unique,
  98. arguments->value,
  99. time_t(0),
  100. gearman_actions_execute_defaults());
  101. break;
  102. case GEARMAN_TASK_ATTR_EPOCH:
  103. task= add_task(*client,
  104. context,
  105. GEARMAN_COMMAND_SUBMIT_JOB_EPOCH,
  106. function,
  107. unique,
  108. arguments->value,
  109. gearman_task_attr_has_epoch(task_attr),
  110. gearman_actions_execute_defaults());
  111. break;
  112. case GEARMAN_TASK_ATTR_FOREGROUND:
  113. task= add_task(*client,
  114. context,
  115. pick_command_by_priority(task_attr->priority),
  116. function,
  117. unique,
  118. arguments->value,
  119. time_t(0),
  120. gearman_actions_execute_defaults());
  121. break;
  122. }
  123. }
  124. else
  125. {
  126. task= add_task(*client,
  127. NULL,
  128. GEARMAN_COMMAND_SUBMIT_JOB,
  129. function,
  130. unique,
  131. arguments->value,
  132. time_t(0),
  133. gearman_actions_execute_defaults());
  134. }
  135. if (task == NULL)
  136. {
  137. gearman_universal_error_code(client->universal);
  138. return NULL;
  139. }
  140. task->impl()->type= GEARMAN_TASK_KIND_EXECUTE;
  141. gearman_client_run_tasks(client->shell());
  142. return task;
  143. }
  144. gearman_task_st *gearman_execute_by_partition(gearman_client_st *client_shell,
  145. const char *partition_function, const size_t partition_function_length,
  146. const char *function_name, const size_t function_name_length,
  147. const char *unique_str, const size_t unique_length,
  148. gearman_task_attr_t *task_attr,
  149. gearman_argument_t *arguments,
  150. void *context)
  151. {
  152. if (client_shell == NULL or client_shell->impl() == NULL)
  153. {
  154. errno= EINVAL;
  155. return NULL;
  156. }
  157. Client* client= client_shell->impl();
  158. if ((partition_function == NULL) or (partition_function_length == 0))
  159. {
  160. gearman_error(client->universal, GEARMAN_INVALID_ARGUMENT, "partition_function was NULL");
  161. return NULL;
  162. }
  163. if ((function_name == NULL) or (function_name_length == 0))
  164. {
  165. gearman_error(client->universal, GEARMAN_INVALID_ARGUMENT, "function_name was NULL");
  166. return NULL;
  167. }
  168. universal_reset_error(client->universal);
  169. gearman_task_st *task= NULL;
  170. gearman_string_t partition= { partition_function, partition_function_length };
  171. gearman_string_t function= { function_name, function_name_length };
  172. gearman_unique_t unique= gearman_unique_make(unique_str, unique_length);
  173. if (task_attr)
  174. {
  175. switch (task_attr->kind)
  176. {
  177. case GEARMAN_TASK_ATTR_BACKGROUND:
  178. task= add_reducer_task(client,
  179. GEARMAN_COMMAND_SUBMIT_REDUCE_JOB_BACKGROUND,
  180. task_attr->priority,
  181. partition,
  182. function,
  183. unique,
  184. arguments->value,
  185. gearman_actions_execute_defaults(),
  186. time_t(0),
  187. context);
  188. break;
  189. case GEARMAN_TASK_ATTR_EPOCH:
  190. gearman_error(client->universal, GEARMAN_INVALID_ARGUMENT, "EPOCH is not currently supported for gearman_client_execute_reduce()");
  191. return NULL;
  192. #if 0
  193. task= add_task(client,
  194. GEARMAN_COMMAND_SUBMIT_REDUCE_JOB_BACKGROUND,
  195. task_attr->priority,
  196. partition,
  197. function,
  198. unique,
  199. arguments->value,
  200. gearman_actions_execute_defaults(),
  201. gearman_work_epoch(task_attr),
  202. context);
  203. break;
  204. #endif
  205. case GEARMAN_TASK_ATTR_FOREGROUND:
  206. task= add_reducer_task(client,
  207. GEARMAN_COMMAND_SUBMIT_REDUCE_JOB,
  208. task_attr->priority,
  209. partition,
  210. function,
  211. unique,
  212. arguments->value,
  213. gearman_actions_execute_defaults(),
  214. time_t(0),
  215. context);
  216. break;
  217. }
  218. }
  219. else
  220. {
  221. task= add_reducer_task(client,
  222. GEARMAN_COMMAND_SUBMIT_REDUCE_JOB,
  223. GEARMAN_JOB_PRIORITY_NORMAL,
  224. partition,
  225. function,
  226. unique,
  227. arguments->value,
  228. gearman_actions_execute_defaults(),
  229. time_t(0),
  230. NULL);
  231. }
  232. if (task)
  233. {
  234. do {
  235. gearman_return_t rc;
  236. if (gearman_failed(rc= gearman_client_run_tasks(client->shell())))
  237. {
  238. gearman_gerror(client->universal, rc);
  239. gearman_task_free(task);
  240. return NULL;
  241. }
  242. } while (gearman_continue(gearman_task_return(task)));
  243. }
  244. return task;
  245. }