task.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * Gearmand client and server library.
  4. *
  5. * Copyright (C) 2011-2013 Data Differential, http://datadifferential.com/
  6. * Copyright (C) 2008 Brian Aker, Eric Day
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions are
  11. * met:
  12. *
  13. * * Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * * Redistributions in binary form must reproduce the above
  17. * copyright notice, this list of conditions and the following disclaimer
  18. * in the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * * The names of its contributors may not be used to endorse or
  22. * promote products derived from this software without specific prior
  23. * written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  26. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  27. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  28. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  29. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  30. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  31. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  32. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  33. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  34. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  35. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. */
  38. /**
  39. * @file
  40. * @brief Task Definitions
  41. */
  42. #include "gear_config.h"
  43. #include <libgearman/common.h>
  44. #include "libgearman/assert.hpp"
  45. #include <cerrno>
  46. #include <cstring>
  47. #include <memory>
  48. /*
  49. * Public Definitions
  50. */
  51. gearman_task_st *gearman_task_internal_create(Client* client, gearman_task_st *task_shell)
  52. {
  53. Task* task= new (std::nothrow) Task(client, task_shell);
  54. if (task)
  55. {
  56. return task->shell();
  57. }
  58. gearman_perror(client->universal, errno, "gearman_task_st new");
  59. gearman_task_free(task_shell);
  60. return NULL;
  61. }
  62. void gearman_task_free(Task* task)
  63. {
  64. gearman_task_free(task->shell());
  65. }
  66. void gearman_task_free(gearman_task_st *task_shell)
  67. {
  68. if (task_shell and task_shell->impl())
  69. {
  70. Task* task= task_shell->impl();
  71. assert(task_shell == task->shell());
  72. {
  73. assert(task->magic_ != TASK_ANTI_MAGIC);
  74. assert(task->magic_ == TASK_MAGIC);
  75. task->magic_= TASK_ANTI_MAGIC;
  76. if (task->client)
  77. {
  78. if (task->options.send_in_use)
  79. {
  80. gearman_packet_free(&(task->send));
  81. }
  82. if (task->type != GEARMAN_TASK_KIND_DO and task->context and task->client->task_context_free_fn)
  83. {
  84. task->client->task_context_free_fn(task_shell, static_cast<void *>(task->context));
  85. }
  86. if (task->client->task_list == task_shell)
  87. {
  88. task->client->task_list= task->next;
  89. }
  90. if (task->prev)
  91. {
  92. task->prev->impl()->next= task->next;
  93. }
  94. if (task->next)
  95. {
  96. task->next->impl()->prev= task->prev;
  97. }
  98. task->client->task_count--;
  99. // If the task we are removing is a current task, remove it from the client
  100. // structures.
  101. if (task->client->task == task_shell)
  102. {
  103. task->client->task= NULL;
  104. }
  105. task->client= NULL;
  106. }
  107. task->job_handle[0]= 0;
  108. task_shell->_impl= NULL;
  109. delete task;
  110. }
  111. }
  112. else if (task_shell)
  113. {
  114. task_shell->_impl= NULL;
  115. }
  116. }
  117. bool gearman_task_is_active(const gearman_task_st *task_shell)
  118. {
  119. assert(task_shell);
  120. assert(task_shell->impl());
  121. if (task_shell and task_shell->impl())
  122. {
  123. switch (task_shell->impl()->state)
  124. {
  125. case GEARMAN_TASK_STATE_NEW:
  126. case GEARMAN_TASK_STATE_SUBMIT:
  127. case GEARMAN_TASK_STATE_WORKLOAD:
  128. case GEARMAN_TASK_STATE_WORK:
  129. case GEARMAN_TASK_STATE_CREATED:
  130. case GEARMAN_TASK_STATE_DATA:
  131. case GEARMAN_TASK_STATE_WARNING:
  132. case GEARMAN_TASK_STATE_STATUS:
  133. return true;
  134. case GEARMAN_TASK_STATE_COMPLETE:
  135. case GEARMAN_TASK_STATE_EXCEPTION:
  136. case GEARMAN_TASK_STATE_FAIL:
  137. case GEARMAN_TASK_STATE_FINISHED:
  138. break;
  139. }
  140. }
  141. return false;
  142. }
  143. const char *gearman_task_strstate(const gearman_task_st *task_shell)
  144. {
  145. if (task_shell and task_shell->impl())
  146. {
  147. switch (task_shell->impl()->state)
  148. {
  149. case GEARMAN_TASK_STATE_NEW: return "GEARMAN_TASK_STATE_NEW";
  150. case GEARMAN_TASK_STATE_SUBMIT: return "GEARMAN_TASK_STATE_SUBMIT";
  151. case GEARMAN_TASK_STATE_WORKLOAD: return "GEARMAN_TASK_STATE_WORKLOAD";
  152. case GEARMAN_TASK_STATE_WORK: return "GEARMAN_TASK_STATE_WORK";
  153. case GEARMAN_TASK_STATE_CREATED: return "GEARMAN_TASK_STATE_CREATED";
  154. case GEARMAN_TASK_STATE_DATA: return "GEARMAN_TASK_STATE_DATA";
  155. case GEARMAN_TASK_STATE_WARNING: return "GEARMAN_TASK_STATE_WARNING";
  156. case GEARMAN_TASK_STATE_STATUS: return "GEARMAN_TASK_STATE_STATUS";
  157. case GEARMAN_TASK_STATE_COMPLETE: return "GEARMAN_TASK_STATE_COMPLETE";
  158. case GEARMAN_TASK_STATE_EXCEPTION: return "GEARMAN_TASK_STATE_EXCEPTION";
  159. case GEARMAN_TASK_STATE_FAIL: return "GEARMAN_TASK_STATE_FAIL";
  160. case GEARMAN_TASK_STATE_FINISHED: return "GEARMAN_TASK_STATE_FINISHED";
  161. }
  162. assert_msg(false, "Invalid result");
  163. return "";
  164. }
  165. return NULL;
  166. }
  167. void gearman_task_clear_fn(gearman_task_st *task_shell)
  168. {
  169. if (task_shell and task_shell->impl())
  170. {
  171. task_shell->impl()->func= gearman_actions_default();
  172. }
  173. }
  174. void *gearman_task_context(const gearman_task_st *task_shell)
  175. {
  176. if (task_shell and task_shell->impl())
  177. {
  178. return const_cast<void *>(task_shell->impl()->context);
  179. }
  180. return NULL;
  181. }
  182. void gearman_task_set_context(gearman_task_st *task_shell, void *context)
  183. {
  184. if (task_shell and task_shell->impl())
  185. {
  186. task_shell->impl()->context= context;
  187. }
  188. }
  189. const char *gearman_task_function_name(const gearman_task_st *task_shell)
  190. {
  191. if (task_shell and task_shell->impl())
  192. {
  193. return task_shell->impl()->send.arg[0];
  194. }
  195. return NULL;
  196. }
  197. const char *gearman_task_unique(const gearman_task_st *task_shell)
  198. {
  199. if (task_shell and task_shell->impl())
  200. {
  201. return task_shell->impl()->unique;
  202. }
  203. return 0;
  204. }
  205. const char *gearman_task_job_handle(const gearman_task_st *task_shell)
  206. {
  207. if (task_shell and task_shell->impl())
  208. {
  209. return task_shell->impl()->job_handle;
  210. }
  211. return 0;
  212. }
  213. bool gearman_task_is_known(const gearman_task_st *task_shell)
  214. {
  215. if (task_shell and task_shell->impl())
  216. {
  217. return task_shell->impl()->options.is_known;
  218. }
  219. return false;
  220. }
  221. bool gearman_task_is_running(const gearman_task_st *task_shell)
  222. {
  223. if (task_shell and task_shell->impl())
  224. {
  225. return task_shell->impl()->options.is_running;
  226. }
  227. return false;
  228. }
  229. uint32_t gearman_task_numerator(const gearman_task_st *task_shell)
  230. {
  231. if (task_shell and task_shell->impl())
  232. {
  233. return task_shell->impl()->numerator;
  234. }
  235. return 0;
  236. }
  237. uint32_t gearman_task_denominator(const gearman_task_st *task_shell)
  238. {
  239. if (task_shell and task_shell->impl())
  240. {
  241. return task_shell->impl()->denominator;
  242. }
  243. return 0;
  244. }
  245. void gearman_task_give_workload(gearman_task_st *task_shell, const void *workload, size_t workload_size)
  246. {
  247. if (task_shell and task_shell->impl())
  248. {
  249. gearman_packet_give_data(task_shell->impl()->send, workload, workload_size);
  250. }
  251. }
  252. size_t gearman_task_send_workload(gearman_task_st *task_shell,
  253. const void *workload, size_t workload_size,
  254. gearman_return_t *ret_ptr)
  255. {
  256. gearman_return_t unused;
  257. if (ret_ptr == NULL)
  258. {
  259. ret_ptr= &unused;
  260. }
  261. if (task_shell and task_shell->impl())
  262. {
  263. return task_shell->impl()->con->send_and_flush(workload, workload_size, ret_ptr);
  264. }
  265. *ret_ptr= GEARMAN_INVALID_ARGUMENT;
  266. return 0;
  267. }
  268. gearman_result_st *gearman_task_result(gearman_task_st *task_shell)
  269. {
  270. if (task_shell and task_shell->impl())
  271. {
  272. return task_shell->impl()->result();
  273. }
  274. return NULL;
  275. }
  276. gearman_result_st *gearman_task_mutable_result(gearman_task_st* task_shell)
  277. {
  278. if (task_shell)
  279. {
  280. Task* task= task_shell->impl();
  281. if (task)
  282. {
  283. if (task->result() == NULL)
  284. {
  285. task->create_result(0);
  286. assert(task->result());
  287. }
  288. return task->result();
  289. }
  290. }
  291. return NULL;
  292. }
  293. const void *gearman_task_data(const gearman_task_st *task_shell)
  294. {
  295. if (task_shell and task_shell->impl() and task_shell->impl()->recv and task_shell->impl()->recv->data)
  296. {
  297. return task_shell->impl()->recv->data;
  298. }
  299. return NULL;
  300. }
  301. size_t gearman_task_data_size(const gearman_task_st *task_shell)
  302. {
  303. if (task_shell and task_shell->impl())
  304. {
  305. if (task_shell->impl()->recv and task_shell->impl()->recv->data_size)
  306. {
  307. return task_shell->impl()->recv->data_size;
  308. }
  309. }
  310. return 0;
  311. }
  312. void *gearman_task_take_data(gearman_task_st *task_shell, size_t *data_size)
  313. {
  314. if (task_shell and task_shell->impl())
  315. {
  316. return gearman_packet_take_data(*(task_shell->impl())->recv, data_size);
  317. }
  318. return NULL;
  319. }
  320. size_t gearman_task_recv_data(gearman_task_st *task_shell, void *data,
  321. size_t data_size,
  322. gearman_return_t *ret_ptr)
  323. {
  324. gearman_return_t unused;
  325. if (ret_ptr == NULL)
  326. {
  327. ret_ptr= &unused;
  328. }
  329. if (task_shell and task_shell->impl())
  330. {
  331. return task_shell->impl()->con->receive_data(data, data_size, *ret_ptr);
  332. }
  333. *ret_ptr= GEARMAN_INVALID_ARGUMENT;
  334. return 0;
  335. }
  336. const char *gearman_task_error(const gearman_task_st *task_shell)
  337. {
  338. if (task_shell and task_shell->impl())
  339. {
  340. if (task_shell->impl()->error_code() == GEARMAN_UNKNOWN_STATE or
  341. task_shell->impl()->error_code() == GEARMAN_SUCCESS)
  342. {
  343. return NULL;
  344. }
  345. return gearman_strerror(task_shell->impl()->error_code());
  346. }
  347. return NULL;
  348. }
  349. gearman_return_t gearman_task_return(const gearman_task_st *task_shell)
  350. {
  351. if (task_shell and task_shell->impl())
  352. {
  353. return task_shell->impl()->error_code();
  354. }
  355. return GEARMAN_INVALID_ARGUMENT;
  356. }
  357. Task::~Task()
  358. {
  359. free_result();
  360. if (_shell)
  361. {
  362. if (_shell != &_owned_shell)
  363. {
  364. gearman_set_allocated(_shell, false);
  365. }
  366. _shell->_impl= NULL;
  367. _shell= NULL;
  368. }
  369. }
  370. void Task::result(gearman_result_st* result_)
  371. {
  372. delete _result_ptr;
  373. _result_ptr= result_;
  374. }
  375. bool Task::create_result(size_t initial_size)
  376. {
  377. assert(_result_ptr == NULL);
  378. if (_result_ptr)
  379. {
  380. _result_ptr->clear();
  381. return _result_ptr;
  382. }
  383. _result_ptr= new (std::nothrow) gearman_result_st(initial_size);
  384. return bool(_result_ptr);
  385. }
  386. bool gearman_task_has_exception(const gearman_task_st* task_shell)
  387. {
  388. if (task_shell and task_shell->impl())
  389. {
  390. if (task_shell->impl()->exception.empty() == false)
  391. {
  392. return true;
  393. }
  394. }
  395. return false;
  396. }
  397. gearman_string_t gearman_task_exception(const gearman_task_st* task_shell)
  398. {
  399. if (task_shell and task_shell->impl())
  400. {
  401. if (task_shell->impl()->exception.empty() == false)
  402. {
  403. gearman_string_t ret= { task_shell->impl()->exception.value(), task_shell->impl()->exception.size() };
  404. return ret;
  405. }
  406. }
  407. static gearman_string_t ret= {0, 0};
  408. return ret;
  409. }
  410. bool gearman_task_is_finished(const gearman_task_st *task_shell)
  411. {
  412. if (task_shell and task_shell->impl())
  413. {
  414. task_shell->impl()->is_finished();
  415. }
  416. return false;
  417. }