run.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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. * 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. #include "gear_config.h"
  39. #include <libgearman/common.h>
  40. #include "libgearman/assert.hpp"
  41. #include <cstdio>
  42. #include <cstdlib>
  43. #include <cstring>
  44. #if __GNUC__ >= 7
  45. #pragma GCC diagnostic warning "-Wimplicit-fallthrough"
  46. #endif
  47. gearman_return_t _client_run_task(Task *task)
  48. {
  49. // This should not be possible
  50. assert_msg(task->client, "Programmer error, somehow an invalid task was specified");
  51. if (task->client == NULL)
  52. {
  53. return gearman_universal_set_error(task->client->universal, GEARMAN_INVALID_ARGUMENT, GEARMAN_AT,
  54. "Programmer error, somehow an invalid task was specified");
  55. }
  56. switch(task->state)
  57. {
  58. case GEARMAN_TASK_STATE_NEW:
  59. if (task->client->universal.has_connections() == false)
  60. {
  61. assert(task->client->universal.con_count == 0);
  62. assert(task->client->universal.con_list == NULL);
  63. task->client->new_tasks--;
  64. task->client->running_tasks--;
  65. return gearman_universal_set_error(task->client->universal, GEARMAN_NO_SERVERS, GEARMAN_AT, "no servers provided");
  66. }
  67. for (task->con= task->client->universal.con_list; task->con;
  68. task->con= task->con->next_connection())
  69. {
  70. if (task->con->send_state == GEARMAN_CON_SEND_STATE_NONE)
  71. {
  72. break;
  73. }
  74. }
  75. if (task->con == NULL)
  76. {
  77. task->client->options.no_new= true;
  78. return gearman_gerror(task->client->universal, GEARMAN_IO_WAIT);
  79. }
  80. task->client->new_tasks--;
  81. if (task->send.command != GEARMAN_COMMAND_GET_STATUS)
  82. {
  83. task->created_id= task->con->created_id_next;
  84. task->con->created_id_next++;
  85. }
  86. /* fall-thru */
  87. case GEARMAN_TASK_STATE_SUBMIT:
  88. while (1)
  89. {
  90. assert(task->con);
  91. gearman_return_t ret= task->con->send_packet(task->send, task->client->new_tasks == 0 ? true : false);
  92. if (gearman_success(ret))
  93. {
  94. break;
  95. }
  96. else if (ret == GEARMAN_IO_WAIT)
  97. {
  98. task->set_state(GEARMAN_TASK_STATE_SUBMIT);
  99. return ret;
  100. }
  101. else if (gearman_failed(ret))
  102. {
  103. /* Increment this since the job submission failed. */
  104. task->con->created_id++;
  105. if (ret == GEARMAN_COULD_NOT_CONNECT)
  106. {
  107. for (task->con= task->con->next_connection();
  108. task->con;
  109. task->con= task->con->next_connection())
  110. {
  111. if (task->con->send_state == GEARMAN_CON_SEND_STATE_NONE)
  112. {
  113. break;
  114. }
  115. }
  116. }
  117. else
  118. {
  119. task->con= NULL;
  120. }
  121. if (not task->con)
  122. {
  123. task->error_code(ret);
  124. if (ret == GEARMAN_COULD_NOT_CONNECT) // If no connection is found, we will let the user try again
  125. {
  126. task->set_state(GEARMAN_TASK_STATE_NEW);
  127. task->client->new_tasks++;
  128. }
  129. else
  130. {
  131. task->set_state(GEARMAN_TASK_STATE_FAIL);
  132. task->client->running_tasks--;
  133. }
  134. return ret;
  135. }
  136. if (task->send.command != GEARMAN_COMMAND_GET_STATUS)
  137. {
  138. task->created_id= task->con->created_id_next;
  139. task->con->created_id_next++;
  140. }
  141. }
  142. }
  143. if (task->send.data_size > 0 and not task->send.data)
  144. {
  145. if (not task->func.workload_fn)
  146. {
  147. gearman_error(task->client->universal, GEARMAN_NEED_WORKLOAD_FN,
  148. "workload size > 0, but no data pointer or workload_fn was given");
  149. return GEARMAN_NEED_WORKLOAD_FN;
  150. }
  151. case GEARMAN_TASK_STATE_WORKLOAD:
  152. gearman_return_t ret= task->func.workload_fn(task->shell());
  153. if (gearman_failed(ret))
  154. {
  155. task->set_state(GEARMAN_TASK_STATE_WORKLOAD);
  156. return ret;
  157. }
  158. }
  159. task->client->options.no_new= false;
  160. task->set_state(GEARMAN_TASK_STATE_WORK);
  161. task->con->set_events(POLLIN);
  162. return GEARMAN_SUCCESS;
  163. case GEARMAN_TASK_STATE_WORK:
  164. if (task->recv->command == GEARMAN_COMMAND_JOB_CREATED)
  165. {
  166. task->options.is_known= true;
  167. snprintf(task->job_handle, GEARMAN_JOB_HANDLE_SIZE, "%.*s",
  168. int(task->recv->arg_size[0]),
  169. static_cast<char *>(task->recv->arg[0]));
  170. case GEARMAN_TASK_STATE_CREATED:
  171. if (task->func.created_fn)
  172. {
  173. gearman_return_t ret= task->func.created_fn(task->shell());
  174. if (gearman_failed(ret))
  175. {
  176. task->set_state(GEARMAN_TASK_STATE_CREATED);
  177. return ret;
  178. }
  179. }
  180. if (task->send.command == GEARMAN_COMMAND_SUBMIT_JOB_BG ||
  181. task->send.command == GEARMAN_COMMAND_SUBMIT_JOB_HIGH_BG ||
  182. task->send.command == GEARMAN_COMMAND_SUBMIT_JOB_LOW_BG ||
  183. task->send.command == GEARMAN_COMMAND_SUBMIT_JOB_EPOCH ||
  184. task->send.command == GEARMAN_COMMAND_SUBMIT_REDUCE_JOB_BACKGROUND)
  185. {
  186. task->error_code(GEARMAN_SUCCESS);
  187. break;
  188. }
  189. }
  190. else if (task->recv->command == GEARMAN_COMMAND_WORK_DATA)
  191. {
  192. task->options.is_known= true;
  193. task->options.is_running= true;
  194. case GEARMAN_TASK_STATE_DATA:
  195. if (task->func.data_fn)
  196. {
  197. gearman_return_t ret= task->func.data_fn(task->shell());
  198. if (gearman_failed(ret))
  199. {
  200. task->set_state(GEARMAN_TASK_STATE_DATA);
  201. return ret;
  202. }
  203. }
  204. }
  205. else if (task->recv->command == GEARMAN_COMMAND_WORK_WARNING)
  206. {
  207. case GEARMAN_TASK_STATE_WARNING:
  208. if (task->func.warning_fn)
  209. {
  210. gearman_return_t ret= task->func.warning_fn(task->shell());
  211. if (gearman_failed(ret))
  212. {
  213. task->set_state(GEARMAN_TASK_STATE_WARNING);
  214. return ret;
  215. }
  216. }
  217. }
  218. else if (task->recv->command == GEARMAN_COMMAND_WORK_STATUS or
  219. task->recv->command == GEARMAN_COMMAND_STATUS_RES_UNIQUE or
  220. task->recv->command == GEARMAN_COMMAND_STATUS_RES)
  221. {
  222. uint8_t x;
  223. if (task->recv->command == GEARMAN_COMMAND_STATUS_RES)
  224. {
  225. task->error_code(GEARMAN_SUCCESS);
  226. if (atoi(static_cast<char *>(task->recv->arg[1])) == 0)
  227. {
  228. task->options.is_known= false;
  229. }
  230. else
  231. {
  232. task->options.is_known= true;
  233. }
  234. if (atoi(static_cast<char *>(task->recv->arg[2])) == 0)
  235. {
  236. task->options.is_running= false;
  237. }
  238. else
  239. {
  240. task->options.is_running= true;
  241. }
  242. x= 3;
  243. }
  244. else if (task->recv->command == GEARMAN_COMMAND_STATUS_RES_UNIQUE)
  245. {
  246. task->error_code(GEARMAN_SUCCESS);
  247. strncpy(task->unique, task->recv->arg[0], GEARMAN_MAX_UNIQUE_SIZE);
  248. task->unique[GEARMAN_MAX_UNIQUE_SIZE -1]= '\0';
  249. if (atoi(static_cast<char *>(task->recv->arg[1])) == 0)
  250. {
  251. task->options.is_known= false;
  252. }
  253. else
  254. {
  255. task->options.is_known= true;
  256. }
  257. if (atoi(static_cast<char *>(task->recv->arg[2])) == 0)
  258. {
  259. task->options.is_running= false;
  260. }
  261. else
  262. {
  263. task->options.is_running= true;
  264. }
  265. x= 3;
  266. }
  267. else
  268. {
  269. x= 1;
  270. }
  271. task->numerator= uint32_t(atoi(static_cast<char *>(task->recv->arg[x])));
  272. // denomitor
  273. {
  274. char status_buffer[11]; /* Max string size to hold a uint32_t. */
  275. snprintf(status_buffer, 11, "%.*s",
  276. int(task->recv->arg_size[x + 1]),
  277. static_cast<char *>(task->recv->arg[x + 1]));
  278. task->denominator= uint32_t(atoi(status_buffer));
  279. }
  280. // client_count
  281. if (task->recv->command == GEARMAN_COMMAND_STATUS_RES_UNIQUE)
  282. {
  283. char status_buffer[11]; /* Max string size to hold a uint32_t. */
  284. snprintf(status_buffer, 11, "%.*s",
  285. int(task->recv->arg_size[x +2]),
  286. static_cast<char *>(task->recv->arg[x +2]));
  287. task->client_count= uint32_t(atoi(status_buffer));
  288. }
  289. case GEARMAN_TASK_STATE_STATUS:
  290. if (task->func.status_fn)
  291. {
  292. gearman_return_t ret= task->func.status_fn(task->shell());
  293. if (gearman_failed(ret))
  294. {
  295. task->set_state(GEARMAN_TASK_STATE_STATUS);
  296. return ret;
  297. }
  298. }
  299. if (task->send.command == GEARMAN_COMMAND_GET_STATUS)
  300. {
  301. break;
  302. }
  303. if (task->send.command == GEARMAN_COMMAND_GET_STATUS_UNIQUE)
  304. {
  305. break;
  306. }
  307. }
  308. else if (task->recv->command == GEARMAN_COMMAND_WORK_COMPLETE)
  309. {
  310. task->options.is_known= false;
  311. task->options.is_running= false;
  312. task->error_code(GEARMAN_SUCCESS);
  313. case GEARMAN_TASK_STATE_COMPLETE:
  314. if (task->func.complete_fn)
  315. {
  316. gearman_return_t ret= task->func.complete_fn(task->shell());
  317. if (gearman_failed(ret))
  318. {
  319. task->set_state(GEARMAN_TASK_STATE_COMPLETE);
  320. return ret;
  321. }
  322. }
  323. break;
  324. }
  325. else if (task->recv->command == GEARMAN_COMMAND_WORK_EXCEPTION)
  326. {
  327. task->options.is_known= false;
  328. task->options.is_running= false;
  329. if (task->recv->argc == 1 and task->recv->data_size)
  330. {
  331. task->exception.store((const char*)(task->recv->data), task->recv->data_size);
  332. }
  333. task->free_result();
  334. task->error_code(GEARMAN_WORK_EXCEPTION);
  335. case GEARMAN_TASK_STATE_EXCEPTION:
  336. if (task->func.exception_fn)
  337. {
  338. gearman_return_t ret= task->func.exception_fn(task->shell());
  339. if (gearman_failed(ret))
  340. {
  341. task->set_state(GEARMAN_TASK_STATE_EXCEPTION);
  342. return ret;
  343. }
  344. }
  345. break;
  346. }
  347. else if (task->recv->command == GEARMAN_COMMAND_WORK_FAIL)
  348. {
  349. // If things fail we need to delete the result, and set the result_rc
  350. // correctly.
  351. task->options.is_known= false;
  352. task->options.is_running= false;
  353. task->free_result();
  354. task->error_code(GEARMAN_WORK_FAIL);
  355. case GEARMAN_TASK_STATE_FAIL:
  356. if (task->func.fail_fn)
  357. {
  358. gearman_return_t ret= task->func.fail_fn(task->shell());
  359. if (gearman_failed(ret))
  360. {
  361. task->set_state(GEARMAN_TASK_STATE_FAIL);
  362. return ret;
  363. }
  364. }
  365. break;
  366. }
  367. task->set_state(GEARMAN_TASK_STATE_WORK);
  368. return GEARMAN_SUCCESS;
  369. case GEARMAN_TASK_STATE_FINISHED:
  370. break;
  371. }
  372. if (task->state != GEARMAN_TASK_STATE_FINISHED)
  373. {
  374. task->client->running_tasks--;
  375. task->set_state(GEARMAN_TASK_STATE_FINISHED);
  376. }
  377. // @todo this should never happen... but background tasks can signal it.
  378. assert(task->error_code() != GEARMAN_UNKNOWN_STATE);
  379. if (task->client->options.free_tasks and task->type == GEARMAN_TASK_KIND_ADD_TASK)
  380. {
  381. gearman_task_free(task->shell());
  382. }
  383. return GEARMAN_SUCCESS;
  384. }