client.cc 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878
  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. #include "gear_config.h"
  39. #include <libgearman/common.h>
  40. #include <libgearman/log.hpp>
  41. #include "libgearman/assert.hpp"
  42. #include "libgearman/interface/push.hpp"
  43. #include <arpa/inet.h>
  44. #include <cerrno>
  45. #include <cstdio>
  46. #include <cstdlib>
  47. #include <cstring>
  48. #include <memory>
  49. #include <netdb.h>
  50. #include <netinet/in.h>
  51. #include <sys/socket.h>
  52. /*
  53. Allocate a client structure.
  54. */
  55. static gearman_client_st *_client_allocate(gearman_client_st *client_shell, bool is_clone)
  56. {
  57. Client *client= new (std::nothrow) Client(client_shell);
  58. if (client)
  59. {
  60. if (is_clone == false)
  61. {
  62. if (getenv("GEARMAN_SERVERS"))
  63. {
  64. if (gearman_client_add_servers(client->shell(), getenv("GEARMAN_SERVERS")))
  65. {
  66. gearman_client_free(client->shell());
  67. return NULL;
  68. }
  69. }
  70. }
  71. return client->shell();
  72. }
  73. return NULL;
  74. }
  75. /**
  76. * Callback function used when parsing server lists.
  77. */
  78. static gearman_return_t _client_add_server(const char *host, in_port_t port,
  79. void *context)
  80. {
  81. return gearman_client_add_server(static_cast<gearman_client_st *>(context), host, port);
  82. }
  83. /**
  84. * Real do function.
  85. */
  86. static void *_client_do(gearman_client_st *client_shell, gearman_command_t command,
  87. const char *function_name,
  88. const char *unique,
  89. const void *workload_str, size_t workload_size,
  90. size_t *result_size, gearman_return_t *ret_ptr)
  91. {
  92. gearman_return_t unused;
  93. if (ret_ptr == NULL)
  94. {
  95. ret_ptr= &unused;
  96. }
  97. if (client_shell == NULL or client_shell->impl() == NULL)
  98. {
  99. *ret_ptr= GEARMAN_INVALID_ARGUMENT;
  100. return NULL;
  101. }
  102. Client* client= client_shell->impl();
  103. client->universal.reset_error();
  104. size_t unused_size;
  105. if (result_size == NULL)
  106. {
  107. result_size= &unused_size;
  108. }
  109. *result_size= 0;
  110. gearman_string_t function= { gearman_string_param_cstr(function_name) };
  111. gearman_unique_t local_unique= gearman_unique_make(unique, unique ? strlen(unique) : 0);
  112. gearman_string_t workload= { static_cast<const char*>(workload_str), workload_size };
  113. client->universal.options.no_new_data= true;
  114. gearman_task_st* do_task= add_task(*client, NULL, NULL, command,
  115. function,
  116. local_unique,
  117. workload,
  118. time_t(0),
  119. gearman_actions_do_default());
  120. client->universal.options.no_new_data= false;
  121. if (do_task == NULL)
  122. {
  123. *ret_ptr= client->universal.error_code();
  124. gearman_task_free(do_task);
  125. return NULL;
  126. }
  127. assert_msg(do_task->impl(), "Bad return by add_task()");
  128. do_task->impl()->type= GEARMAN_TASK_KIND_DO;
  129. gearman_return_t ret= gearman_client_run_block_tasks(client, do_task);
  130. // gearman_client_run_tasks failed
  131. assert(client->task_list); // Programmer error, we should always have the task that we used for do
  132. char *returnable= NULL;
  133. if (gearman_failed(ret))
  134. {
  135. // We only record the error if it is different then the one we saved.
  136. if (client->universal.error_code() != ret)
  137. {
  138. gearman_error(client->universal, ret, "occurred during gearman_client_run_tasks()");
  139. }
  140. *ret_ptr= ret;
  141. *result_size= 0;
  142. }
  143. else if (gearman_success(ret) and do_task->impl()->error_code() == GEARMAN_SUCCESS)
  144. {
  145. *ret_ptr= do_task->impl()->error_code();
  146. if (gearman_task_result(do_task))
  147. {
  148. if (gearman_has_allocator(client->universal))
  149. {
  150. gearman_string_t result= gearman_result_string(do_task->impl()->result());
  151. returnable= static_cast<char *>(gearman_malloc(client->universal, gearman_size(result) +1));
  152. if (returnable == NULL)
  153. {
  154. gearman_error(client->universal, GEARMAN_MEMORY_ALLOCATION_FAILURE, "custom workload_fn failed to allocate memory");
  155. *result_size= 0;
  156. }
  157. else // NULL terminate
  158. {
  159. memcpy(returnable, gearman_c_str(result), gearman_size(result));
  160. returnable[gearman_size(result)]= 0;
  161. *result_size= gearman_size(result);
  162. }
  163. }
  164. else
  165. {
  166. gearman_string_t result= gearman_result_take_string(do_task->impl()->result());
  167. *result_size= gearman_size(result);
  168. returnable= const_cast<char *>(gearman_c_str(result));
  169. }
  170. }
  171. else // NULL job
  172. {
  173. *result_size= 0;
  174. }
  175. }
  176. else // gearman_client_run_tasks() was successful, but the task was not
  177. {
  178. gearman_error(client->universal, do_task->impl()->error_code(), "occurred during gearman_client_run_tasks()");
  179. *ret_ptr= do_task->impl()->error_code();
  180. *result_size= 0;
  181. }
  182. gearman_task_free(do_task);
  183. client->new_tasks= 0;
  184. client->running_tasks= 0;
  185. return returnable;
  186. }
  187. /*
  188. Real background do function.
  189. */
  190. static gearman_return_t _client_do_background(gearman_client_st* client_shell,
  191. gearman_command_t command,
  192. gearman_string_t &function,
  193. gearman_unique_t &unique,
  194. gearman_string_t &workload,
  195. gearman_job_handle_t job_handle)
  196. {
  197. if (client_shell == NULL or client_shell->impl() == NULL)
  198. {
  199. return GEARMAN_INVALID_ARGUMENT;
  200. }
  201. Client* client= client_shell->impl();
  202. client->universal.reset_error();
  203. if (gearman_size(function) == 0)
  204. {
  205. return gearman_error(client->universal, GEARMAN_INVALID_ARGUMENT, "function argument was empty");
  206. }
  207. client->_do_handle[0]= 0; // Reset the job_handle we store in client
  208. client->universal.options.no_new_data= true;
  209. gearman_task_st* do_task= add_task(*client, NULL,
  210. client,
  211. command,
  212. function,
  213. unique,
  214. workload,
  215. time_t(0),
  216. gearman_actions_do_default());
  217. client->universal.options.no_new_data= false;
  218. if (do_task == NULL)
  219. {
  220. gearman_task_free(do_task);
  221. return client->universal.error_code();
  222. }
  223. assert(do_task);
  224. do_task->impl()->type= GEARMAN_TASK_KIND_DO;
  225. gearman_return_t ret= gearman_client_run_block_tasks(client, do_task);
  226. if (job_handle)
  227. {
  228. strncpy(job_handle, do_task->impl()->job_handle, GEARMAN_JOB_HANDLE_SIZE);
  229. }
  230. strncpy(client->_do_handle, do_task->impl()->job_handle, GEARMAN_JOB_HANDLE_SIZE);
  231. client->new_tasks= 0;
  232. client->running_tasks= 0;
  233. gearman_task_free(do_task);
  234. return ret;
  235. }
  236. /*
  237. * Public Definitions
  238. */
  239. gearman_client_st *gearman_client_create(gearman_client_st *client)
  240. {
  241. return _client_allocate(client, false);
  242. }
  243. gearman_client_st *gearman_client_clone(gearman_client_st *destination,
  244. const gearman_client_st *source)
  245. {
  246. if (source == NULL or source->impl() == NULL)
  247. {
  248. return _client_allocate(destination, false);
  249. }
  250. destination= _client_allocate(destination, true);
  251. if (destination == NULL or destination->impl() == NULL)
  252. {
  253. return NULL;
  254. }
  255. destination->impl()->options.non_blocking= source->impl()->options.non_blocking;
  256. destination->impl()->options.unbuffered_result= source->impl()->options.unbuffered_result;
  257. destination->impl()->options.no_new= source->impl()->options.no_new;
  258. destination->impl()->options.free_tasks= source->impl()->options.free_tasks;
  259. destination->impl()->options.generate_unique= source->impl()->options.generate_unique;
  260. destination->impl()->ssl(source->impl()->ssl());
  261. destination->impl()->actions= source->impl()->actions;
  262. destination->impl()->_do_handle[0]= 0;
  263. gearman_universal_clone(destination->impl()->universal, source->impl()->universal);
  264. if (gearman_failed(destination->impl()->universal.error_code()))
  265. {
  266. gearman_client_free(destination);
  267. return NULL;
  268. }
  269. return destination;
  270. }
  271. void gearman_client_free(gearman_client_st *client_shell)
  272. {
  273. if (client_shell and client_shell->impl())
  274. {
  275. gearman_client_task_free_all(client_shell);
  276. gearman_universal_free(client_shell->impl()->universal);
  277. delete client_shell->impl();
  278. }
  279. }
  280. const char *gearman_client_error(const gearman_client_st *client_shell)
  281. {
  282. if (client_shell and client_shell->impl())
  283. {
  284. return client_shell->impl()->universal.error();
  285. }
  286. return NULL;
  287. }
  288. gearman_return_t gearman_client_error_code(const gearman_client_st *client_shell)
  289. {
  290. if (client_shell and client_shell->impl())
  291. {
  292. return client_shell->impl()->universal.error_code();
  293. }
  294. return GEARMAN_INVALID_ARGUMENT;
  295. }
  296. int gearman_client_errno(const gearman_client_st *client_shell)
  297. {
  298. if (client_shell and client_shell->impl())
  299. {
  300. return client_shell->impl()->universal.last_errno();
  301. }
  302. return EINVAL;
  303. }
  304. gearman_client_options_t gearman_client_options(const gearman_client_st *client_shell)
  305. {
  306. if (client_shell and client_shell->impl())
  307. {
  308. Client* client= client_shell->impl();
  309. int32_t options;
  310. memset(&options, 0, sizeof(int32_t));
  311. if (gearman_is_allocated(client_shell))
  312. options|= int(GEARMAN_CLIENT_ALLOCATED);
  313. if (client->options.non_blocking)
  314. options|= int(GEARMAN_CLIENT_NON_BLOCKING);
  315. if (client->options.unbuffered_result)
  316. options|= int(GEARMAN_CLIENT_UNBUFFERED_RESULT);
  317. if (client->options.no_new)
  318. options|= int(GEARMAN_CLIENT_NO_NEW);
  319. if (client->options.free_tasks)
  320. options|= int(GEARMAN_CLIENT_FREE_TASKS);
  321. if (client->options.generate_unique)
  322. options|= int(GEARMAN_CLIENT_GENERATE_UNIQUE);
  323. if (client->ssl())
  324. options|= int(GEARMAN_CLIENT_SSL);
  325. return gearman_client_options_t(options);
  326. }
  327. return gearman_client_options_t(GEARMAN_WORKER_MAX);
  328. }
  329. bool gearman_client_has_option(gearman_client_st *client_shell,
  330. gearman_client_options_t option)
  331. {
  332. if (client_shell and client_shell->impl())
  333. {
  334. Client* client= client_shell->impl();
  335. switch (option)
  336. {
  337. case GEARMAN_CLIENT_ALLOCATED:
  338. return gearman_is_allocated(client_shell);
  339. case GEARMAN_CLIENT_NON_BLOCKING:
  340. return client->options.non_blocking;
  341. case GEARMAN_CLIENT_UNBUFFERED_RESULT:
  342. return client->options.unbuffered_result;
  343. case GEARMAN_CLIENT_NO_NEW:
  344. return client->options.no_new;
  345. case GEARMAN_CLIENT_FREE_TASKS:
  346. return client->options.free_tasks;
  347. case GEARMAN_CLIENT_GENERATE_UNIQUE:
  348. return client->options.generate_unique;
  349. case GEARMAN_CLIENT_EXCEPTION:
  350. return client->options.exceptions;
  351. case GEARMAN_CLIENT_SSL:
  352. return client->ssl();
  353. default:
  354. case GEARMAN_CLIENT_TASK_IN_USE:
  355. case GEARMAN_CLIENT_MAX:
  356. break; // Let these fall through to false
  357. }
  358. }
  359. return false;
  360. }
  361. void gearman_client_set_options(gearman_client_st *client_shell,
  362. gearman_client_options_t options)
  363. {
  364. if (client_shell)
  365. {
  366. gearman_client_options_t usable_options[]= {
  367. GEARMAN_CLIENT_NON_BLOCKING,
  368. GEARMAN_CLIENT_UNBUFFERED_RESULT,
  369. GEARMAN_CLIENT_FREE_TASKS,
  370. GEARMAN_CLIENT_GENERATE_UNIQUE,
  371. GEARMAN_CLIENT_EXCEPTION,
  372. GEARMAN_CLIENT_SSL,
  373. GEARMAN_CLIENT_MAX
  374. };
  375. for (gearman_client_options_t* ptr= usable_options; *ptr != GEARMAN_CLIENT_MAX ; ptr++)
  376. {
  377. if (options & *ptr)
  378. {
  379. gearman_client_add_options(client_shell, *ptr);
  380. }
  381. else
  382. {
  383. gearman_client_remove_options(client_shell, *ptr);
  384. }
  385. }
  386. }
  387. }
  388. void gearman_client_add_options(gearman_client_st *client_shell,
  389. gearman_client_options_t options)
  390. {
  391. if (client_shell and client_shell->impl())
  392. {
  393. Client* client= client_shell->impl();
  394. if (options & GEARMAN_CLIENT_NON_BLOCKING)
  395. {
  396. gearman_universal_add_options(client->universal, GEARMAN_UNIVERSAL_NON_BLOCKING);
  397. client->options.non_blocking= true;
  398. }
  399. if (options & GEARMAN_CLIENT_UNBUFFERED_RESULT)
  400. {
  401. client->options.unbuffered_result= true;
  402. }
  403. if (options & GEARMAN_CLIENT_FREE_TASKS)
  404. {
  405. client->options.free_tasks= true;
  406. }
  407. if (options & GEARMAN_CLIENT_GENERATE_UNIQUE)
  408. {
  409. client->options.generate_unique= true;
  410. }
  411. if (options & GEARMAN_CLIENT_EXCEPTION)
  412. {
  413. client->options.exceptions= gearman_client_set_server_option(client_shell, gearman_literal_param("exceptions"));
  414. }
  415. if (options & GEARMAN_CLIENT_SSL)
  416. {
  417. client->ssl(true);
  418. }
  419. }
  420. }
  421. void gearman_client_remove_options(gearman_client_st *client_shell,
  422. gearman_client_options_t options)
  423. {
  424. if (client_shell and client_shell->impl())
  425. {
  426. Client* client= client_shell->impl();
  427. if (options & GEARMAN_CLIENT_NON_BLOCKING)
  428. {
  429. gearman_universal_remove_options(client->universal, GEARMAN_UNIVERSAL_NON_BLOCKING);
  430. client->options.non_blocking= false;
  431. }
  432. if (options & GEARMAN_CLIENT_UNBUFFERED_RESULT)
  433. {
  434. client->options.unbuffered_result= false;
  435. }
  436. if (options & GEARMAN_CLIENT_FREE_TASKS)
  437. {
  438. client->options.free_tasks= false;
  439. }
  440. if (options & GEARMAN_CLIENT_GENERATE_UNIQUE)
  441. {
  442. client->options.generate_unique= false;
  443. }
  444. }
  445. }
  446. int gearman_client_timeout(gearman_client_st *client_shell)
  447. {
  448. if (client_shell and client_shell->impl())
  449. {
  450. return gearman_universal_timeout(client_shell->impl()->universal);
  451. }
  452. return -1;
  453. }
  454. void gearman_client_set_timeout(gearman_client_st *client_shell, int timeout)
  455. {
  456. if (client_shell and client_shell->impl())
  457. {
  458. gearman_universal_set_timeout(client_shell->impl()->universal, timeout);
  459. }
  460. }
  461. void gearman_client_set_ssl(gearman_client_st *client_shell, bool ssl,
  462. const char *ca_file, const char *certificate, const char *key_file)
  463. {
  464. if (client_shell && client_shell->impl())
  465. {
  466. gearman_universal_set_ssl(client_shell->impl()->universal, ssl, ca_file, certificate, key_file);
  467. }
  468. }
  469. void *gearman_client_context(const gearman_client_st *client_shell)
  470. {
  471. if (client_shell and client_shell->impl())
  472. {
  473. return const_cast<void *>(client_shell->impl()->context);
  474. }
  475. return NULL;
  476. }
  477. void gearman_client_set_context(gearman_client_st *client_shell, void *context)
  478. {
  479. if (client_shell and client_shell->impl())
  480. {
  481. client_shell->impl()->context= context;
  482. }
  483. }
  484. void gearman_client_set_log_fn(gearman_client_st *client_shell,
  485. gearman_log_fn *function, void *context,
  486. gearman_verbose_t verbose)
  487. {
  488. if (client_shell and client_shell->impl())
  489. {
  490. gearman_set_log_fn(client_shell->impl()->universal, function, context, verbose);
  491. }
  492. }
  493. void gearman_client_set_workload_malloc_fn(gearman_client_st *client_shell,
  494. gearman_malloc_fn *function,
  495. void *context)
  496. {
  497. if (client_shell and client_shell->impl())
  498. {
  499. gearman_set_workload_malloc_fn(client_shell->impl()->universal, function, context);
  500. }
  501. }
  502. void gearman_client_set_workload_free_fn(gearman_client_st *client_shell, gearman_free_fn *function, void *context)
  503. {
  504. if (client_shell and client_shell->impl())
  505. {
  506. gearman_set_workload_free_fn(client_shell->impl()->universal, function, context);
  507. }
  508. }
  509. gearman_return_t gearman_client_add_server(gearman_client_st *client_shell,
  510. const char *host, in_port_t port)
  511. {
  512. if (client_shell and client_shell->impl())
  513. {
  514. Client* client= client_shell->impl();
  515. if (gearman_connection_create(client->universal, host, port) == NULL)
  516. {
  517. assert(client->error_code() != GEARMAN_SUCCESS);
  518. return client->error_code();
  519. }
  520. assert(client->universal.has_connections());
  521. return GEARMAN_SUCCESS;
  522. }
  523. return GEARMAN_INVALID_ARGUMENT;
  524. }
  525. gearman_return_t Client::add_server(const char *host, const char* service_)
  526. {
  527. if (gearman_connection_create(universal, host, service_) == NULL)
  528. {
  529. assert(error_code() != GEARMAN_SUCCESS);
  530. return error_code();
  531. }
  532. return GEARMAN_SUCCESS;
  533. }
  534. gearman_return_t gearman_client_add_servers(gearman_client_st *client_shell,
  535. const char *servers)
  536. {
  537. return gearman_parse_servers(servers, _client_add_server, client_shell);
  538. }
  539. void gearman_client_remove_servers(gearman_client_st *client_shell)
  540. {
  541. if (client_shell and client_shell->impl())
  542. {
  543. gearman_free_all_cons(client_shell->impl()->universal);
  544. }
  545. }
  546. gearman_return_t gearman_client_wait(gearman_client_st *client_shell)
  547. {
  548. if (client_shell and client_shell->impl())
  549. {
  550. return gearman_wait(client_shell->impl()->universal);
  551. }
  552. return GEARMAN_INVALID_ARGUMENT;
  553. }
  554. gearman_return_t gearman_client_cancel_job(gearman_client_st *client_shell,
  555. gearman_job_handle_t job_handle)
  556. {
  557. if (client_shell and client_shell->impl())
  558. {
  559. client_shell->impl()->universal.reset_error();
  560. return cancel_job(client_shell->impl()->universal, job_handle);
  561. }
  562. return GEARMAN_INVALID_ARGUMENT;
  563. }
  564. void *gearman_client_do(gearman_client_st *client_shell,
  565. const char *function,
  566. const char *unique,
  567. const void *workload,
  568. size_t workload_size, size_t *result_size,
  569. gearman_return_t *ret_ptr)
  570. {
  571. return _client_do(client_shell, GEARMAN_COMMAND_SUBMIT_JOB,
  572. function,
  573. unique,
  574. workload, workload_size,
  575. result_size, ret_ptr);
  576. }
  577. void *gearman_client_do_high(gearman_client_st *client_shell,
  578. const char *function,
  579. const char *unique,
  580. const void *workload, size_t workload_size,
  581. size_t *result_size, gearman_return_t *ret_ptr)
  582. {
  583. return _client_do(client_shell, GEARMAN_COMMAND_SUBMIT_JOB_HIGH,
  584. function,
  585. unique,
  586. workload, workload_size,
  587. result_size, ret_ptr);
  588. }
  589. void *gearman_client_do_low(gearman_client_st *client_shell,
  590. const char *function,
  591. const char *unique,
  592. const void *workload, size_t workload_size,
  593. size_t *result_size, gearman_return_t *ret_ptr)
  594. {
  595. return _client_do(client_shell, GEARMAN_COMMAND_SUBMIT_JOB_LOW,
  596. function,
  597. unique,
  598. workload, workload_size,
  599. result_size, ret_ptr);
  600. }
  601. size_t gearman_client_count_tasks(gearman_client_st *client_shell)
  602. {
  603. if (client_shell == NULL or client_shell->impl() == NULL)
  604. {
  605. return 0;
  606. }
  607. size_t count= 1;
  608. gearman_task_st *search= client_shell->impl()->task_list;
  609. while ((search= search->impl()->next))
  610. {
  611. count++;
  612. }
  613. return count;
  614. }
  615. bool gearman_client_has_active_tasks(gearman_client_st *client_shell)
  616. {
  617. if (client_shell and client_shell->impl())
  618. {
  619. Client* client= client_shell->impl();
  620. if (client->task_list and client->task_list->impl())
  621. {
  622. gearman_task_st* search= client->task_list;
  623. do
  624. {
  625. if (gearman_task_is_active(search))
  626. {
  627. return true;
  628. }
  629. } while ((search= search->impl()->next));
  630. }
  631. }
  632. return false;
  633. }
  634. const char *gearman_client_do_job_handle(gearman_client_st *self)
  635. {
  636. if (self)
  637. {
  638. return self->impl()->_do_handle;
  639. }
  640. errno= EINVAL;
  641. return NULL;
  642. }
  643. void gearman_client_do_status(gearman_client_st *, uint32_t *numerator, uint32_t *denominator)
  644. {
  645. if (numerator)
  646. {
  647. *numerator= 0;
  648. }
  649. if (denominator)
  650. {
  651. *denominator= 0;
  652. }
  653. }
  654. gearman_return_t gearman_client_do_background(gearman_client_st *client_shell,
  655. const char *function_name,
  656. const char *unique,
  657. const void *workload_str,
  658. size_t workload_size,
  659. gearman_job_handle_t job_handle)
  660. {
  661. gearman_string_t function= { gearman_string_param_cstr(function_name) };
  662. gearman_unique_t local_unique= gearman_unique_make(unique, unique ? strlen(unique) : 0);
  663. gearman_string_t workload= { static_cast<const char*>(workload_str), workload_size };
  664. return _client_do_background(client_shell, GEARMAN_COMMAND_SUBMIT_JOB_BG,
  665. function,
  666. local_unique,
  667. workload,
  668. job_handle);
  669. }
  670. gearman_return_t gearman_client_do_high_background(gearman_client_st *client_shell,
  671. const char *function_name,
  672. const char *unique,
  673. const void *workload_str,
  674. size_t workload_size,
  675. gearman_job_handle_t job_handle)
  676. {
  677. gearman_string_t function= { gearman_string_param_cstr(function_name) };
  678. gearman_unique_t local_unique= gearman_unique_make(unique, unique ? strlen(unique) : 0);
  679. gearman_string_t workload= { static_cast<const char*>(workload_str), workload_size };
  680. return _client_do_background(client_shell, GEARMAN_COMMAND_SUBMIT_JOB_HIGH_BG,
  681. function,
  682. local_unique,
  683. workload,
  684. job_handle);
  685. }
  686. gearman_return_t gearman_client_do_low_background(gearman_client_st *client_shell,
  687. const char *function_name,
  688. const char *unique,
  689. const void *workload_str,
  690. size_t workload_size,
  691. gearman_job_handle_t job_handle)
  692. {
  693. gearman_string_t function= { gearman_string_param_cstr(function_name) };
  694. gearman_unique_t local_unique= gearman_unique_make(unique, unique ? strlen(unique) : 0);
  695. gearman_string_t workload= { static_cast<const char*>(workload_str), workload_size };
  696. return _client_do_background(client_shell, GEARMAN_COMMAND_SUBMIT_JOB_LOW_BG,
  697. function,
  698. local_unique,
  699. workload,
  700. job_handle);
  701. }
  702. gearman_status_t gearman_client_unique_status(gearman_client_st *client_shell,
  703. const char *unique, size_t unique_length)
  704. {
  705. (void)unique_length;
  706. gearman_status_t status;
  707. gearman_init(status);
  708. if (client_shell == NULL or client_shell->impl() == NULL)
  709. {
  710. gearman_status_set_return(status, GEARMAN_INVALID_ARGUMENT);
  711. return status;
  712. }
  713. Client* client= client_shell->impl();
  714. client->universal.reset_error();
  715. gearman_return_t ret;
  716. gearman_task_st* do_task= gearman_client_add_task_status_by_unique(client_shell,
  717. NULL,
  718. unique, &ret);
  719. if (do_task == NULL)
  720. {
  721. gearman_status_set_return(status, client->universal.error_code());
  722. return status;
  723. }
  724. if (gearman_failed(ret))
  725. {
  726. gearman_status_set_return(status, ret);
  727. return status;
  728. }
  729. Task* task= do_task->impl();
  730. task->type= GEARMAN_TASK_KIND_DO;
  731. gearman_task_clear_fn(do_task);
  732. ret= gearman_client_run_block_tasks(client, do_task);
  733. // @note we don't know if our task was run or not, we just know something
  734. // happened.
  735. if (gearman_success(ret))
  736. {
  737. gearman_status_set(status,
  738. task->options.is_known,
  739. task->options.is_running,
  740. task->numerator,
  741. task->denominator,
  742. task->client_count);
  743. if (gearman_status_is_known(status) == false and gearman_status_is_running(status) == false)
  744. {
  745. if (task->options.is_running)
  746. {
  747. ret= GEARMAN_IN_PROGRESS;
  748. }
  749. else if (task->options.is_known)
  750. {
  751. ret= GEARMAN_JOB_EXISTS;
  752. }
  753. }
  754. }
  755. gearman_task_free(do_task);
  756. gearman_status_set_return(status, ret);
  757. return status;
  758. }
  759. gearman_return_t gearman_client_job_status(gearman_client_st *client_shell,
  760. const gearman_job_handle_t job_handle,
  761. bool *is_known, bool *is_running,
  762. uint32_t *numerator,
  763. uint32_t *denominator)
  764. {
  765. gearman_return_t ret;
  766. if (client_shell == NULL or client_shell->impl() == NULL)
  767. {
  768. return GEARMAN_INVALID_ARGUMENT;
  769. }
  770. Client* client= client_shell->impl();
  771. client->universal.reset_error();
  772. gearman_task_st *do_task= gearman_client_add_task_status(client_shell, NULL, client,
  773. job_handle, &ret);
  774. if (gearman_failed(ret))
  775. {
  776. gearman_task_free(do_task);
  777. return ret;
  778. }
  779. assert_msg(do_task, "Programming error, a NULL return happend from gearman_client_add_task_status() without an error");
  780. do_task->impl()->type= GEARMAN_TASK_KIND_DO;
  781. gearman_task_clear_fn(do_task);
  782. ret= gearman_client_run_block_tasks(client, do_task);
  783. // @note we don't know if our task was run or not, we just know something
  784. // happened.
  785. if (gearman_success(ret))
  786. {
  787. if (is_known)
  788. {
  789. *is_known= do_task->impl()->options.is_known;
  790. }
  791. if (is_running)
  792. {
  793. *is_running= do_task->impl()->options.is_running;
  794. }
  795. if (numerator)
  796. {
  797. *numerator= do_task->impl()->numerator;
  798. }
  799. if (denominator)
  800. {
  801. *denominator= do_task->impl()->denominator;
  802. }
  803. if (!is_known and !is_running)
  804. {
  805. if (do_task->impl()->options.is_running)
  806. {
  807. ret= GEARMAN_IN_PROGRESS;
  808. }
  809. else if (do_task->impl()->options.is_known)
  810. {
  811. ret= GEARMAN_JOB_EXISTS;
  812. }
  813. }
  814. }
  815. else
  816. {
  817. if (is_known)
  818. {
  819. *is_known= false;
  820. }
  821. if (is_running)
  822. {
  823. *is_running= false;
  824. }
  825. if (numerator)
  826. {
  827. *numerator= 0;
  828. }
  829. if (denominator)
  830. {
  831. *denominator= 0;
  832. }
  833. }
  834. gearman_task_free(do_task);
  835. return ret;
  836. }
  837. gearman_return_t gearman_client_echo(gearman_client_st *client,
  838. const void *workload,
  839. size_t workload_size)
  840. {
  841. if (client == NULL or client->impl() == NULL)
  842. {
  843. return GEARMAN_INVALID_ARGUMENT;
  844. }
  845. return gearman_echo(client->impl()->universal, workload, workload_size);
  846. }
  847. void gearman_client_task_free_all(gearman_client_st *client_shell)
  848. {
  849. if (client_shell and client_shell->impl() and client_shell->impl()->task_list)
  850. {
  851. Client* client= client_shell->impl();
  852. while (client->task_list)
  853. {
  854. assert(client == client->task_list->impl()->client);
  855. gearman_task_free(client->task_list);
  856. }
  857. }
  858. }
  859. void gearman_client_set_task_context_free_fn(gearman_client_st *client,
  860. gearman_task_context_free_fn *function)
  861. {
  862. if (client and client->impl())
  863. {
  864. client->impl()->task_context_free_fn= function;
  865. }
  866. }
  867. gearman_return_t gearman_client_set_memory_allocators(gearman_client_st *client,
  868. gearman_malloc_fn *malloc_fn,
  869. gearman_free_fn *free_fn,
  870. gearman_realloc_fn *realloc_fn,
  871. gearman_calloc_fn *calloc_fn,
  872. void *context)
  873. {
  874. if (client and client->impl())
  875. {
  876. return gearman_set_memory_allocator(client->impl()->universal.allocator, malloc_fn, free_fn, realloc_fn, calloc_fn, context);
  877. }
  878. return GEARMAN_INVALID_ARGUMENT;
  879. }
  880. gearman_task_st *gearman_client_add_task(gearman_client_st *client,
  881. gearman_task_st *task,
  882. void *context,
  883. const char *function,
  884. const char *unique,
  885. const void *workload, size_t workload_size,
  886. gearman_return_t *ret_ptr)
  887. {
  888. gearman_return_t unused;
  889. if (ret_ptr == NULL)
  890. {
  891. ret_ptr= &unused;
  892. }
  893. if (client and client->impl())
  894. {
  895. return add_task_ptr(*(client->impl()), task,
  896. context, GEARMAN_COMMAND_SUBMIT_JOB,
  897. function,
  898. unique,
  899. workload, workload_size,
  900. time_t(0),
  901. *ret_ptr,
  902. client->impl()->actions);
  903. }
  904. *ret_ptr= GEARMAN_INVALID_ARGUMENT;
  905. return NULL;
  906. }
  907. gearman_task_st *gearman_client_add_task_high(gearman_client_st *client,
  908. gearman_task_st *task,
  909. void *context,
  910. const char *function,
  911. const char *unique,
  912. const void *workload, size_t workload_size,
  913. gearman_return_t *ret_ptr)
  914. {
  915. gearman_return_t unused;
  916. if (ret_ptr == NULL)
  917. {
  918. ret_ptr= &unused;
  919. }
  920. if (client and client->impl())
  921. {
  922. return add_task_ptr(*(client->impl()), task, context,
  923. GEARMAN_COMMAND_SUBMIT_JOB_HIGH,
  924. function,
  925. unique,
  926. workload, workload_size,
  927. time_t(0),
  928. *ret_ptr,
  929. client->impl()->actions);
  930. }
  931. *ret_ptr= GEARMAN_INVALID_ARGUMENT;
  932. return NULL;
  933. }
  934. gearman_task_st *gearman_client_add_task_low(gearman_client_st *client,
  935. gearman_task_st *task,
  936. void *context,
  937. const char *function,
  938. const char *unique,
  939. const void *workload, size_t workload_size,
  940. gearman_return_t *ret_ptr)
  941. {
  942. gearman_return_t unused;
  943. if (ret_ptr == NULL)
  944. {
  945. ret_ptr= &unused;
  946. }
  947. if (client and client->impl())
  948. {
  949. return add_task_ptr(*(client->impl()), task, context, GEARMAN_COMMAND_SUBMIT_JOB_LOW,
  950. function,
  951. unique,
  952. workload, workload_size,
  953. time_t(0),
  954. *ret_ptr,
  955. client->impl()->actions);
  956. }
  957. *ret_ptr= GEARMAN_INVALID_ARGUMENT;
  958. return NULL;
  959. }
  960. gearman_task_st *gearman_client_add_task_background(gearman_client_st *client,
  961. gearman_task_st *task,
  962. void *context,
  963. const char *function,
  964. const char *unique,
  965. const void *workload, size_t workload_size,
  966. gearman_return_t *ret_ptr)
  967. {
  968. gearman_return_t unused;
  969. if (ret_ptr == NULL)
  970. {
  971. ret_ptr= &unused;
  972. }
  973. if (client == NULL or client->impl() == NULL)
  974. {
  975. *ret_ptr= GEARMAN_INVALID_ARGUMENT;
  976. return NULL;
  977. }
  978. return add_task_ptr(*(client->impl()), task, context, GEARMAN_COMMAND_SUBMIT_JOB_BG,
  979. function,
  980. unique,
  981. workload, workload_size,
  982. time_t(0),
  983. *ret_ptr,
  984. client->impl()->actions);
  985. }
  986. gearman_task_st *
  987. gearman_client_add_task_high_background(gearman_client_st *client,
  988. gearman_task_st *task,
  989. void *context,
  990. const char *function,
  991. const char *unique,
  992. const void *workload, size_t workload_size,
  993. gearman_return_t *ret_ptr)
  994. {
  995. gearman_return_t unused;
  996. if (ret_ptr == NULL)
  997. {
  998. ret_ptr= &unused;
  999. }
  1000. if (client == NULL or client->impl() == NULL)
  1001. {
  1002. *ret_ptr= GEARMAN_INVALID_ARGUMENT;
  1003. return NULL;
  1004. }
  1005. return add_task_ptr(*(client->impl()), task, context,
  1006. GEARMAN_COMMAND_SUBMIT_JOB_HIGH_BG,
  1007. function,
  1008. unique,
  1009. workload, workload_size,
  1010. time_t(0),
  1011. *ret_ptr,
  1012. client->impl()->actions);
  1013. }
  1014. gearman_task_st* gearman_client_add_task_low_background(gearman_client_st *client,
  1015. gearman_task_st *task,
  1016. void *context,
  1017. const char *function,
  1018. const char *unique,
  1019. const void *workload, size_t workload_size,
  1020. gearman_return_t *ret_ptr)
  1021. {
  1022. gearman_return_t unused;
  1023. if (ret_ptr == NULL)
  1024. {
  1025. ret_ptr= &unused;
  1026. }
  1027. if (client == NULL or client->impl() == NULL)
  1028. {
  1029. *ret_ptr= GEARMAN_INVALID_ARGUMENT;
  1030. return NULL;
  1031. }
  1032. return add_task_ptr(*(client->impl()), task, context,
  1033. GEARMAN_COMMAND_SUBMIT_JOB_LOW_BG,
  1034. function,
  1035. unique,
  1036. workload, workload_size,
  1037. time_t(0),
  1038. *ret_ptr,
  1039. client->impl()->actions);
  1040. }
  1041. gearman_task_st *gearman_client_add_task_status(gearman_client_st *client_shell,
  1042. gearman_task_st *task_shell,
  1043. void *context,
  1044. const gearman_job_handle_t job_handle,
  1045. gearman_return_t *ret_ptr)
  1046. {
  1047. const void *args[1];
  1048. size_t args_size[1];
  1049. gearman_return_t unused;
  1050. if (ret_ptr == NULL)
  1051. {
  1052. ret_ptr= &unused;
  1053. }
  1054. if (client_shell == NULL or client_shell->impl() == NULL)
  1055. {
  1056. *ret_ptr= GEARMAN_INVALID_ARGUMENT;
  1057. return NULL;
  1058. }
  1059. Client* client= client_shell->impl();
  1060. if ((task_shell= gearman_task_internal_create(client, task_shell)) == NULL)
  1061. {
  1062. *ret_ptr= gearman_client_error_code(client_shell);
  1063. return NULL;
  1064. }
  1065. Task* task= task_shell->impl();
  1066. task->context= context;
  1067. snprintf(task->job_handle, GEARMAN_JOB_HANDLE_SIZE, "%s", job_handle);
  1068. args[0]= job_handle;
  1069. args_size[0]= strlen(job_handle);
  1070. gearman_return_t rc= gearman_packet_create_args(client->universal, task->send,
  1071. GEARMAN_MAGIC_REQUEST,
  1072. GEARMAN_COMMAND_GET_STATUS,
  1073. args, args_size, 1);
  1074. if (gearman_success(rc))
  1075. {
  1076. client->new_tasks++;
  1077. client->running_tasks++;
  1078. task->options.send_in_use= true;
  1079. }
  1080. *ret_ptr= rc;
  1081. return task_shell;
  1082. }
  1083. gearman_task_st *gearman_client_add_task_status_by_unique(gearman_client_st *client_shell,
  1084. gearman_task_st *task_shell,
  1085. const char *unique_handle,
  1086. gearman_return_t *ret_ptr)
  1087. {
  1088. const void *args[1];
  1089. size_t args_size[1];
  1090. gearman_return_t unused;
  1091. if (ret_ptr == NULL)
  1092. {
  1093. ret_ptr= &unused;
  1094. }
  1095. if (client_shell == NULL or client_shell->impl() == NULL)
  1096. {
  1097. *ret_ptr= GEARMAN_INVALID_ARGUMENT;
  1098. return NULL;
  1099. }
  1100. Client* client= client_shell->impl();
  1101. if (unique_handle == NULL)
  1102. {
  1103. *ret_ptr= GEARMAN_INVALID_ARGUMENT;
  1104. return NULL;
  1105. }
  1106. size_t unique_length= strlen(unique_handle);
  1107. if (unique_length > GEARMAN_MAX_UNIQUE_SIZE)
  1108. {
  1109. *ret_ptr= GEARMAN_INVALID_ARGUMENT;
  1110. return NULL;
  1111. }
  1112. if ((task_shell= gearman_task_internal_create(client, task_shell)) == NULL)
  1113. {
  1114. *ret_ptr= gearman_client_error_code(client_shell);
  1115. return NULL;
  1116. }
  1117. Task* task= task_shell->impl();
  1118. task->unique_length= unique_length;
  1119. memcpy(task->unique, unique_handle, unique_length);
  1120. task->unique[task->unique_length]= 0;
  1121. args[0]= task->unique;
  1122. args_size[0]= task->unique_length;
  1123. gearman_return_t rc= gearman_packet_create_args(client->universal, task->send,
  1124. GEARMAN_MAGIC_REQUEST,
  1125. GEARMAN_COMMAND_GET_STATUS_UNIQUE,
  1126. args, args_size, 1);
  1127. if (gearman_success(rc))
  1128. {
  1129. client->new_tasks++;
  1130. client->running_tasks++;
  1131. task->options.send_in_use= true;
  1132. }
  1133. *ret_ptr= rc;
  1134. return task_shell;
  1135. }
  1136. void gearman_client_set_workload_fn(gearman_client_st *client,
  1137. gearman_workload_fn *function)
  1138. {
  1139. if (client and client->impl())
  1140. {
  1141. client->impl()->actions.workload_fn= function;
  1142. }
  1143. }
  1144. void gearman_client_set_created_fn(gearman_client_st *client,
  1145. gearman_created_fn *function)
  1146. {
  1147. if (client and client->impl())
  1148. {
  1149. client->impl()->actions.created_fn= function;
  1150. }
  1151. }
  1152. void gearman_client_set_data_fn(gearman_client_st *client,
  1153. gearman_data_fn *function)
  1154. {
  1155. if (client and client->impl())
  1156. {
  1157. client->impl()->actions.data_fn= function;
  1158. }
  1159. }
  1160. void gearman_client_set_warning_fn(gearman_client_st *client,
  1161. gearman_warning_fn *function)
  1162. {
  1163. if (client and client->impl())
  1164. {
  1165. client->impl()->actions.warning_fn= function;
  1166. }
  1167. }
  1168. void gearman_client_set_status_fn(gearman_client_st *client,
  1169. gearman_universal_status_fn *function)
  1170. {
  1171. if (client and client->impl())
  1172. {
  1173. client->impl()->actions.status_fn= function;
  1174. }
  1175. }
  1176. void gearman_client_set_complete_fn(gearman_client_st *client,
  1177. gearman_complete_fn *function)
  1178. {
  1179. if (client and client->impl())
  1180. {
  1181. client->impl()->actions.complete_fn= function;
  1182. }
  1183. }
  1184. void gearman_client_set_exception_fn(gearman_client_st *client,
  1185. gearman_exception_fn *function)
  1186. {
  1187. if (client and client->impl())
  1188. {
  1189. client->impl()->actions.exception_fn= function;
  1190. }
  1191. }
  1192. void gearman_client_set_fail_fn(gearman_client_st* client,
  1193. gearman_fail_fn *function)
  1194. {
  1195. if (client and client->impl())
  1196. {
  1197. client->impl()->actions.fail_fn= function;
  1198. }
  1199. }
  1200. void gearman_client_clear_fn(gearman_client_st* client)
  1201. {
  1202. if (client and client->impl())
  1203. {
  1204. client->impl()->actions= gearman_actions_default();
  1205. }
  1206. }
  1207. static inline gearman_return_t _client_run_tasks(gearman_client_st *client_shell, gearman_task_st* exit_task)
  1208. {
  1209. gearman_return_t ret= GEARMAN_MAX_RETURN;
  1210. Client* client= client_shell->impl();
  1211. switch(client->state)
  1212. {
  1213. case GEARMAN_CLIENT_STATE_IDLE:
  1214. while (1)
  1215. {
  1216. /* Start any new tasks. */
  1217. if (client->new_tasks > 0 && ! (client->options.no_new))
  1218. {
  1219. for (client->task= client->task_list; client->task;
  1220. client->task= client->task->impl()->next)
  1221. {
  1222. if (client->task->impl()->state != GEARMAN_TASK_STATE_NEW)
  1223. {
  1224. continue;
  1225. }
  1226. case GEARMAN_CLIENT_STATE_NEW:
  1227. if (client->task == NULL)
  1228. {
  1229. client->state= GEARMAN_CLIENT_STATE_IDLE;
  1230. break;
  1231. }
  1232. gearman_return_t local_ret= _client_run_task(client->task->impl());
  1233. if (gearman_failed(local_ret) and local_ret != GEARMAN_IO_WAIT)
  1234. {
  1235. client->state= GEARMAN_CLIENT_STATE_NEW;
  1236. return local_ret;
  1237. }
  1238. }
  1239. if (client->new_tasks == 0)
  1240. {
  1241. client->universal.flush();
  1242. }
  1243. }
  1244. /* See if there are any connections ready for I/O. */
  1245. while ((client->con= gearman_ready(client->universal)))
  1246. {
  1247. if (client->con->is_revents(POLLOUT | POLLERR | POLLHUP | POLLNVAL))
  1248. {
  1249. /* Socket is ready for writing, continue submitting jobs. */
  1250. for (client->task= client->task_list; client->task;
  1251. client->task= client->task->impl()->next)
  1252. {
  1253. if (client->task->impl()->con != client->con or
  1254. (client->task->impl()->state != GEARMAN_TASK_STATE_SUBMIT and
  1255. client->task->impl()->state != GEARMAN_TASK_STATE_WORKLOAD))
  1256. {
  1257. continue;
  1258. }
  1259. case GEARMAN_CLIENT_STATE_SUBMIT:
  1260. if (client->task == NULL)
  1261. {
  1262. client->state= GEARMAN_CLIENT_STATE_IDLE;
  1263. break;
  1264. }
  1265. gearman_return_t local_ret= _client_run_task(client->task->impl());
  1266. if (local_ret == GEARMAN_COULD_NOT_CONNECT)
  1267. {
  1268. client->state= GEARMAN_CLIENT_STATE_IDLE;
  1269. return local_ret;
  1270. }
  1271. else if (gearman_failed(local_ret) and local_ret != GEARMAN_IO_WAIT)
  1272. {
  1273. client->state= GEARMAN_CLIENT_STATE_SUBMIT;
  1274. return local_ret;
  1275. }
  1276. }
  1277. /* Connection errors are fatal. */
  1278. if (client->con->is_revents(POLLERR | POLLHUP | POLLNVAL))
  1279. {
  1280. gearman_error(client->universal, GEARMAN_LOST_CONNECTION, "detected lost connection in _client_run_tasks()");
  1281. client->con->close_socket();
  1282. client->state= GEARMAN_CLIENT_STATE_IDLE;
  1283. return GEARMAN_LOST_CONNECTION;
  1284. }
  1285. }
  1286. if ((client->con->is_revents(POLLIN)) == false)
  1287. {
  1288. continue;
  1289. }
  1290. /* Socket is ready for reading. */
  1291. while (1)
  1292. {
  1293. /* Read packet on connection and find which task it belongs to. */
  1294. if (client->options.unbuffered_result)
  1295. {
  1296. /* If client is handling the data read, make sure it's complete. */
  1297. if (client->con->recv_state == GEARMAN_CON_RECV_STATE_READ_DATA)
  1298. {
  1299. for (client->task= client->task_list; client->task;
  1300. client->task= client->task->impl()->next)
  1301. {
  1302. if (client->task->impl()->con == client->con &&
  1303. (client->task->impl()->state == GEARMAN_TASK_STATE_DATA or
  1304. client->task->impl()->state == GEARMAN_TASK_STATE_COMPLETE))
  1305. {
  1306. break;
  1307. }
  1308. }
  1309. /*
  1310. Someone has set GEARMAN_CLIENT_UNBUFFERED_RESULT but hasn't setup the client to fetch data correctly.
  1311. Fatal error :(
  1312. */
  1313. return gearman_universal_set_error(client->universal, GEARMAN_INVALID_ARGUMENT, GEARMAN_AT,
  1314. "client created with GEARMAN_CLIENT_UNBUFFERED_RESULT, but was not setup to use it. %s", __func__);
  1315. }
  1316. else
  1317. {
  1318. /* Read the next packet, without buffering the data part. */
  1319. client->task= NULL;
  1320. (void)client->con->receiving(client->con->_packet, ret, false);
  1321. }
  1322. }
  1323. else
  1324. {
  1325. /* Read the next packet, buffering the data part. */
  1326. client->task= NULL;
  1327. (void)client->con->receiving(client->con->_packet, ret, true);
  1328. }
  1329. if (client->task == NULL)
  1330. {
  1331. assert(ret != GEARMAN_MAX_RETURN);
  1332. /* Check the return of the gearman_connection_recv() calls above. */
  1333. if (gearman_failed(ret))
  1334. {
  1335. if (ret == GEARMAN_IO_WAIT)
  1336. {
  1337. break;
  1338. }
  1339. client->state= GEARMAN_CLIENT_STATE_IDLE;
  1340. return ret;
  1341. }
  1342. client->con->options.packet_in_use= true;
  1343. /* We have a packet, see which task it belongs to. */
  1344. for (client->task= client->task_list; client->task;
  1345. client->task= client->task->impl()->next)
  1346. {
  1347. if (client->task->impl()->con != client->con)
  1348. {
  1349. continue;
  1350. }
  1351. if (client->con->_packet.command == GEARMAN_COMMAND_JOB_CREATED)
  1352. {
  1353. if (client->task->impl()->created_id != client->con->created_id)
  1354. {
  1355. continue;
  1356. }
  1357. /* New job created, drop through below and notify task. */
  1358. client->con->created_id++;
  1359. }
  1360. else if (client->con->_packet.command == GEARMAN_COMMAND_ERROR)
  1361. {
  1362. gearman_return_t maybe_server_error= string2return_code(static_cast<char *>(client->con->_packet.arg[0]), int(client->con->_packet.arg_size[0]));
  1363. if (maybe_server_error == GEARMAN_MAX_RETURN)
  1364. {
  1365. maybe_server_error= GEARMAN_SERVER_ERROR;
  1366. }
  1367. gearman_universal_set_error(client->universal, maybe_server_error, GEARMAN_AT,
  1368. "%s:%.*s",
  1369. static_cast<char *>(client->con->_packet.arg[0]),
  1370. int(client->con->_packet.arg_size[1]),
  1371. static_cast<char *>(client->con->_packet.arg[1]));
  1372. /*
  1373. Packet cleanup copied from "Clean up the packet" below, and must
  1374. remain in sync with its reference.
  1375. */
  1376. gearman_packet_free(&(client->con->_packet));
  1377. client->con->options.packet_in_use= false;
  1378. /* This step copied from _client_run_tasks() above: */
  1379. /* Increment this value because new job created then failed. */
  1380. client->con->created_id++;
  1381. return maybe_server_error;
  1382. }
  1383. else if (client->con->_packet.command == GEARMAN_COMMAND_STATUS_RES_UNIQUE and
  1384. (strncmp(gearman_task_unique(client->task),
  1385. static_cast<char *>(client->con->_packet.arg[0]),
  1386. client->con->_packet.arg_size[0]) == 0))
  1387. { }
  1388. else if (strncmp(client->task->impl()->job_handle,
  1389. static_cast<char *>(client->con->_packet.arg[0]),
  1390. client->con->_packet.arg_size[0]) ||
  1391. (client->con->_packet.failed() == false &&
  1392. strlen(client->task->impl()->job_handle) != client->con->_packet.arg_size[0] - 1) ||
  1393. (client->con->_packet.failed() &&
  1394. strlen(client->task->impl()->job_handle) != client->con->_packet.arg_size[0]))
  1395. {
  1396. continue;
  1397. }
  1398. /* Else, we have a matching result packet of some kind. */
  1399. break;
  1400. }
  1401. if (client->task == NULL)
  1402. {
  1403. /* The client has stopped waiting for the response, ignore it. */
  1404. client->con->free_private_packet();
  1405. continue;
  1406. }
  1407. client->task->impl()->recv= &(client->con->_packet);
  1408. }
  1409. case GEARMAN_CLIENT_STATE_PACKET:
  1410. /* Let task process job created or result packet. */
  1411. gearman_return_t local_ret= _client_run_task(client->task->impl());
  1412. if (local_ret == GEARMAN_IO_WAIT)
  1413. {
  1414. break;
  1415. }
  1416. if (gearman_failed(local_ret))
  1417. {
  1418. client->state= GEARMAN_CLIENT_STATE_PACKET;
  1419. return local_ret;
  1420. }
  1421. /* Clean up the packet. */
  1422. client->con->free_private_packet();
  1423. /* If exit task is set and matched, exit */
  1424. if (exit_task)
  1425. {
  1426. if (exit_task->impl()->error_code() != GEARMAN_UNKNOWN_STATE)
  1427. {
  1428. client->state= GEARMAN_CLIENT_STATE_IDLE;
  1429. return GEARMAN_SUCCESS;
  1430. }
  1431. }
  1432. /* If all tasks are done, return. */
  1433. if (client->running_tasks == 0)
  1434. {
  1435. client->state= GEARMAN_CLIENT_STATE_IDLE;
  1436. return GEARMAN_SUCCESS;
  1437. }
  1438. }
  1439. }
  1440. /* If all tasks are done, return. */
  1441. if (client->running_tasks == 0)
  1442. {
  1443. break;
  1444. }
  1445. if (client->new_tasks > 0 and ! (client->options.no_new))
  1446. {
  1447. continue;
  1448. }
  1449. if (client->options.non_blocking)
  1450. {
  1451. /* Let the caller wait for activity. */
  1452. client->state= GEARMAN_CLIENT_STATE_IDLE;
  1453. return gearman_gerror(client->universal, GEARMAN_IO_WAIT);
  1454. }
  1455. /* Wait for activity on one of the connections. */
  1456. gearman_return_t local_ret= gearman_wait(client->universal);
  1457. if (gearman_failed(local_ret) and local_ret != GEARMAN_IO_WAIT)
  1458. {
  1459. client->state= GEARMAN_CLIENT_STATE_IDLE;
  1460. return local_ret;
  1461. }
  1462. }
  1463. break;
  1464. }
  1465. client->state= GEARMAN_CLIENT_STATE_IDLE;
  1466. return GEARMAN_SUCCESS;
  1467. }
  1468. gearman_return_t gearman_client_run_tasks(gearman_client_st *client_shell)
  1469. {
  1470. if (client_shell and client_shell->impl())
  1471. {
  1472. Client* client= client_shell->impl();
  1473. if (client->task_list == NULL) // We are immediately successful if all tasks are completed
  1474. {
  1475. return GEARMAN_SUCCESS;
  1476. }
  1477. gearman_return_t rc;
  1478. {
  1479. PUSH_NON_BLOCKING(client->universal);
  1480. rc= _client_run_tasks(client_shell, NULL);
  1481. }
  1482. if (rc == GEARMAN_COULD_NOT_CONNECT)
  1483. {
  1484. client->universal.reset();
  1485. }
  1486. return rc;
  1487. }
  1488. return GEARMAN_INVALID_ARGUMENT;
  1489. }
  1490. gearman_return_t gearman_client_run_block_tasks(Client* client, gearman_task_st* exit_task)
  1491. {
  1492. if (client->task_list == NULL) // We are immediately successful if all tasks are completed
  1493. {
  1494. return GEARMAN_SUCCESS;
  1495. }
  1496. gearman_return_t rc;
  1497. {
  1498. PUSH_BLOCKING(client->universal);
  1499. rc= _client_run_tasks(client->shell(), exit_task);
  1500. }
  1501. if (gearman_failed(rc))
  1502. {
  1503. if (rc == GEARMAN_COULD_NOT_CONNECT)
  1504. {
  1505. client->universal.reset();
  1506. }
  1507. if (client->universal.error_code() != rc and rc != GEARMAN_COULD_NOT_CONNECT)
  1508. {
  1509. assert(client->universal.error_code() == rc);
  1510. }
  1511. }
  1512. return rc;
  1513. }
  1514. /*
  1515. * Static Definitions
  1516. */
  1517. bool gearman_client_compare(const gearman_client_st *first_shell, const gearman_client_st *second_shell)
  1518. {
  1519. if (first_shell and second_shell)
  1520. {
  1521. Client* first= first_shell->impl();
  1522. Client* second= second_shell->impl();
  1523. if (first and second)
  1524. {
  1525. if (strcmp(first->universal.con_list->_host, second->universal.con_list->_host) == 0)
  1526. {
  1527. if (strcmp(first->universal.con_list->_service, second->universal.con_list->_service) == 0)
  1528. {
  1529. return true;
  1530. }
  1531. }
  1532. }
  1533. }
  1534. return false;
  1535. }
  1536. bool gearman_client_set_server_option(gearman_client_st *client_shell, const char *option_arg, size_t option_arg_size)
  1537. {
  1538. if (client_shell and client_shell->impl())
  1539. {
  1540. Client* client= client_shell->impl();
  1541. gearman_string_t option= { option_arg, option_arg_size };
  1542. if (gearman_success(gearman_server_option(client->universal, option)))
  1543. {
  1544. if (gearman_request_option(client->universal, option))
  1545. {
  1546. if (strncmp("exceptions", option_arg, sizeof("exceptions")) == 0)
  1547. {
  1548. client->options.exceptions= true;
  1549. }
  1550. return true;
  1551. }
  1552. }
  1553. }
  1554. return false;
  1555. }
  1556. void gearman_client_set_namespace(gearman_client_st *client_shell, const char *namespace_key, size_t namespace_key_size)
  1557. {
  1558. if (client_shell and client_shell->impl())
  1559. {
  1560. gearman_universal_set_namespace(client_shell->impl()->universal, namespace_key, namespace_key_size);
  1561. }
  1562. }
  1563. gearman_return_t gearman_client_set_identifier(gearman_client_st *client,
  1564. const char *id, size_t id_size)
  1565. {
  1566. if (client and client->impl())
  1567. {
  1568. return gearman_set_identifier(client->impl()->universal, id, id_size);
  1569. }
  1570. return GEARMAN_INVALID_ARGUMENT;
  1571. }
  1572. const char *gearman_client_namespace(gearman_client_st *self)
  1573. {
  1574. return gearman_univeral_namespace(self->impl()->universal);
  1575. }
  1576. bool gearman_client_has_tasks(const gearman_client_st *client_shell)
  1577. {
  1578. if (client_shell and client_shell->impl())
  1579. {
  1580. #ifndef NDEBUG
  1581. if (client_shell->impl()->task_list)
  1582. {
  1583. assert(client_shell->impl()->task_count);
  1584. }
  1585. else
  1586. {
  1587. assert(client_shell->impl()->task_count == 0);
  1588. }
  1589. #endif
  1590. return bool(client_shell->impl()->task_list);
  1591. }
  1592. return false;
  1593. }