worker.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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. /**
  39. * @file
  40. * @brief Worker Declarations
  41. */
  42. #pragma once
  43. /** @addtogroup gearman_worker Worker Declarations
  44. *
  45. * This is the interface gearman workers should use.
  46. *
  47. * @ref main_page_worker "See Main Page for full details."
  48. * @{
  49. */
  50. enum gearman_worker_state_t {
  51. GEARMAN_WORKER_STATE_START,
  52. GEARMAN_WORKER_STATE_FUNCTION_SEND,
  53. GEARMAN_WORKER_STATE_CONNECT,
  54. GEARMAN_WORKER_STATE_GRAB_JOB_SEND,
  55. GEARMAN_WORKER_STATE_GRAB_JOB_RECV,
  56. GEARMAN_WORKER_STATE_PRE_SLEEP
  57. };
  58. enum gearman_worker_universal_t {
  59. GEARMAN_WORKER_WORK_UNIVERSAL_GRAB_JOB,
  60. GEARMAN_WORKER_WORK_UNIVERSAL_FUNCTION,
  61. GEARMAN_WORKER_WORK_UNIVERSAL_COMPLETE,
  62. GEARMAN_WORKER_WORK_UNIVERSAL_FAIL
  63. };
  64. /**
  65. * @ingroup gearman_worker
  66. */
  67. struct gearman_worker_st
  68. {
  69. struct {
  70. bool allocated;
  71. bool non_blocking;
  72. bool packet_init;
  73. bool change;
  74. bool grab_uniq;
  75. bool grab_all;
  76. bool timeout_return;
  77. } options;
  78. enum gearman_worker_state_t state;
  79. enum gearman_worker_universal_t work_state;
  80. uint32_t function_count;
  81. uint32_t job_count;
  82. size_t work_result_size;
  83. void *context;
  84. gearman_connection_st *con;
  85. gearman_job_st *job;
  86. gearman_job_st *job_list;
  87. struct _worker_function_st *function;
  88. struct _worker_function_st *function_list;
  89. struct _worker_function_st *work_function;
  90. void *work_result;
  91. struct gearman_universal_st universal;
  92. gearman_packet_st grab_job;
  93. gearman_packet_st pre_sleep;
  94. gearman_job_st *work_job;
  95. };
  96. #ifdef __cplusplus
  97. #define gearman_has_reducer(A) (A) ? static_cast<bool>((A)->reducer.final_fn) : false
  98. #else
  99. #define gearman_has_reducer(A) (A) ? (bool)((A)->reducer.final_fn) : false
  100. #endif
  101. #ifdef __cplusplus
  102. extern "C" {
  103. #endif
  104. /**
  105. * Initialize a worker structure. Always check the return value even if passing
  106. * in a pre-allocated structure. Some other initialization may have failed. It
  107. * is not required to memset() a structure before providing it.
  108. *
  109. * @param[in] worker Caller allocated structure, or NULL to allocate one.
  110. * @return On success, a pointer to the (possibly allocated) structure. On
  111. * failure this will be NULL.
  112. */
  113. GEARMAN_API
  114. gearman_worker_st *gearman_worker_create(gearman_worker_st *worker);
  115. /**
  116. * Clone a worker structure.
  117. *
  118. * @param[in] worker Caller allocated structure, or NULL to allocate one.
  119. * @param[in] from Structure to use as a source to clone from.
  120. * @return Same return as gearman_worker_create().
  121. */
  122. GEARMAN_API
  123. gearman_worker_st *gearman_worker_clone(gearman_worker_st *worker,
  124. const gearman_worker_st *from);
  125. /**
  126. * Free resources used by a worker structure.
  127. *
  128. * @param[in] worker Structure previously initialized with
  129. * gearman_worker_create() or gearman_worker_clone().
  130. */
  131. GEARMAN_API
  132. void gearman_worker_free(gearman_worker_st *worker);
  133. /**
  134. * See gearman_error() for details.
  135. */
  136. GEARMAN_API
  137. const char *gearman_worker_error(const gearman_worker_st *worker);
  138. /**
  139. * See gearman_errno() for details.
  140. */
  141. GEARMAN_API
  142. int gearman_worker_errno(gearman_worker_st *worker);
  143. /**
  144. * Get options for a worker structure.
  145. *
  146. * @param[in] worker Structure previously initialized with
  147. * gearman_worker_create() or gearman_worker_clone().
  148. * @return Options set for the worker structure.
  149. */
  150. GEARMAN_API
  151. gearman_worker_options_t gearman_worker_options(const gearman_worker_st *worker);
  152. /**
  153. * Set options for a worker structure.
  154. *
  155. * @param[in] worker Structure previously initialized with
  156. * gearman_worker_create() or gearman_worker_clone().
  157. * @param options Available options for worker structures.
  158. */
  159. GEARMAN_API
  160. void gearman_worker_set_options(gearman_worker_st *worker,
  161. gearman_worker_options_t options);
  162. /**
  163. * Add options for a worker structure.
  164. *
  165. * @param[in] worker Structure previously initialized with
  166. * gearman_worker_create() or gearman_worker_clone().
  167. * @param options Available options for worker structures.
  168. */
  169. GEARMAN_API
  170. void gearman_worker_add_options(gearman_worker_st *worker,
  171. gearman_worker_options_t options);
  172. /**
  173. * Remove options for a worker structure.
  174. *
  175. * @param[in] worker Structure previously initialized with
  176. * gearman_worker_create() or gearman_worker_clone().
  177. * @param options Available options for worker structures.
  178. */
  179. GEARMAN_API
  180. void gearman_worker_remove_options(gearman_worker_st *worker,
  181. gearman_worker_options_t options);
  182. /**
  183. * See gearman_universal_timeout() for details.
  184. */
  185. GEARMAN_API
  186. int gearman_worker_timeout(gearman_worker_st *worker);
  187. /**
  188. * See gearman_universal_set_timeout() for details.
  189. */
  190. GEARMAN_API
  191. void gearman_worker_set_timeout(gearman_worker_st *worker, int timeout);
  192. /**
  193. * Get the application context for a worker.
  194. *
  195. * @param[in] worker Structure previously initialized with
  196. * gearman_worker_create() or gearman_worker_clone().
  197. * @return Application context that was previously set, or NULL.
  198. */
  199. GEARMAN_API
  200. void *gearman_worker_context(const gearman_worker_st *worker);
  201. /**
  202. * Set the application context for a worker.
  203. *
  204. * @param[in] worker Structure previously initialized with
  205. * gearman_worker_create() or gearman_worker_clone().
  206. * @param[in] context Application context to set.
  207. */
  208. GEARMAN_API
  209. void gearman_worker_set_context(gearman_worker_st *worker, void *context);
  210. /**
  211. * See gearman_set_log_fn() for details.
  212. */
  213. GEARMAN_API
  214. void gearman_worker_set_log_fn(gearman_worker_st *worker,
  215. gearman_log_fn *function, void *context,
  216. gearman_verbose_t verbose);
  217. /**
  218. * See gearman_set_workload_malloc_fn() for details.
  219. */
  220. GEARMAN_API
  221. void gearman_worker_set_workload_malloc_fn(gearman_worker_st *worker,
  222. gearman_malloc_fn *function,
  223. void *context);
  224. /**
  225. * Set custom memory free function for workloads. Normally gearman uses the
  226. * standard system free to free memory used with workloads. The provided
  227. * function will be used instead.
  228. *
  229. * @param[in] gearman Structure previously initialized with gearman_universal_create() or
  230. * gearman_clone().
  231. * @param[in] function Memory free function to use instead of free().
  232. * @param[in] context Argument to pass into the callback function.
  233. */
  234. GEARMAN_API
  235. void gearman_worker_set_workload_free_fn(gearman_worker_st *worker,
  236. gearman_free_fn *function,
  237. void *context);
  238. /**
  239. * Add a job server to a worker. This goes into a list of servers that can be
  240. * used to run tasks. No socket I/O happens here, it is just added to a list.
  241. *
  242. * @param[in] worker Structure previously initialized with
  243. * gearman_worker_create() or gearman_worker_clone().
  244. * @param[in] host Hostname or IP address (IPv4 or IPv6) of the server to add.
  245. * @param[in] port Port of the server to add.
  246. * @return Standard gearman return value.
  247. */
  248. GEARMAN_API
  249. gearman_return_t gearman_worker_add_server(gearman_worker_st *worker,
  250. const char *host, in_port_t port);
  251. /**
  252. * Add a list of job servers to a worker. The format for the server list is:
  253. * SERVER[:PORT][,SERVER[:PORT]]...
  254. * Some examples are:
  255. * 10.0.0.1,10.0.0.2,10.0.0.3
  256. * localhost234,jobserver2.domain.com:7003,10.0.0.3
  257. *
  258. * @param[in] worker Structure previously initialized with
  259. * gearman_worker_create() or gearman_worker_clone().
  260. * @param[in] servers Server list described above.
  261. * @return Standard gearman return value.
  262. */
  263. GEARMAN_API
  264. gearman_return_t gearman_worker_add_servers(gearman_worker_st *worker,
  265. const char *servers);
  266. /**
  267. * Remove all servers currently associated with the worker.
  268. *
  269. * @param[in] worker Structure previously initialized with
  270. * gearman_worker_create() or gearman_worker_clone().
  271. */
  272. GEARMAN_API
  273. void gearman_worker_remove_servers(gearman_worker_st *worker);
  274. /**
  275. * When in non-blocking I/O mode, wait for activity from one of the servers.
  276. *
  277. * @param[in] worker Structure previously initialized with
  278. * gearman_worker_create() or gearman_worker_clone().
  279. * @return Standard gearman return value.
  280. */
  281. GEARMAN_API
  282. gearman_return_t gearman_worker_wait(gearman_worker_st *worker);
  283. /**
  284. * Register function with job servers with an optional timeout. The timeout
  285. * specifies how many seconds the server will wait before marking a job as
  286. * failed. If timeout is zero, there is no timeout.
  287. *
  288. * @param[in] worker Structure previously initialized with
  289. * gearman_worker_create() or gearman_worker_clone().
  290. * @param[in] function_name Function name to register.
  291. * @param[in] timeout Optional timeout (in seconds) that specifies the maximum
  292. * time a job should. This is enforced on the job server. A value of 0 means
  293. * an infinite time.
  294. * @return Standard gearman return value.
  295. */
  296. GEARMAN_API
  297. gearman_return_t gearman_worker_register(gearman_worker_st *worker,
  298. const char *function_name,
  299. uint32_t timeout);
  300. /**
  301. * Unregister function with job servers.
  302. *
  303. * @param[in] worker Structure previously initialized with
  304. * gearman_worker_create() or gearman_worker_clone().
  305. * @param[in] function_name Function name to unregister.
  306. * @return Standard gearman return value.
  307. */
  308. GEARMAN_API
  309. gearman_return_t gearman_worker_unregister(gearman_worker_st *worker,
  310. const char *function_name);
  311. /**
  312. * Unregister all functions with job servers.
  313. *
  314. * @param[in] worker Structure previously initialized with
  315. * gearman_worker_create() or gearman_worker_clone().
  316. * @return Standard gearman return value.
  317. */
  318. GEARMAN_API
  319. gearman_return_t gearman_worker_unregister_all(gearman_worker_st *worker);
  320. /**
  321. * Get a job from one of the job servers. This does not used the callback
  322. * interface below, which means results must be sent back to the job server
  323. * manually. It is also the responsibility of the caller to free the job once
  324. * it has been completed.
  325. *
  326. * @param[in] worker Structure previously initialized with
  327. * gearman_worker_create() or gearman_worker_clone().
  328. * @param[in] job Caller allocated structure, or NULL to allocate one.
  329. * @param[out] ret_ptr Standard gearman return value.
  330. * @return On success, a pointer to the (possibly allocated) structure. On
  331. * failure this will be NULL.
  332. */
  333. GEARMAN_API
  334. gearman_job_st *gearman_worker_grab_job(gearman_worker_st *worker,
  335. gearman_job_st *job,
  336. gearman_return_t *ret_ptr);
  337. /**
  338. * Free all jobs for a gearman structure.
  339. *
  340. * @param[in] worker Structure previously initialized with
  341. * gearman_worker_create() or gearman_worker_clone().
  342. */
  343. GEARMAN_API
  344. void gearman_job_free_all(gearman_worker_st *worker);
  345. /**
  346. * See if a function exists in the server. It will not return
  347. * true if the function is currently being de-allocated.
  348. * @param[in] worker gearman_worker_st that will be used.
  349. * @param[in] function_name Function name for search.
  350. * @param[in] function_length Length of function name.
  351. * @return bool
  352. */
  353. GEARMAN_API
  354. bool gearman_worker_function_exist(gearman_worker_st *worker,
  355. const char *function_name,
  356. size_t function_length);
  357. /**
  358. * Register and add callback function for worker. To remove functions that have
  359. * been added, call gearman_worker_unregister() or
  360. * gearman_worker_unregister_all().
  361. *
  362. * @param[in] worker Structure previously initialized with
  363. * gearman_worker_create() or gearman_worker_clone().
  364. * @param[in] function_name Function name to register.
  365. * @param[in] timeout Optional timeout (in seconds) that specifies the maximum
  366. * time a job should. This is enforced on the job server. A value of 0 means
  367. * an infinite time.
  368. * @param[in] function Function to run when there is a job ready.
  369. * @param[in] context Argument to pass into the callback function.
  370. * @return Standard gearman return value.
  371. */
  372. GEARMAN_API
  373. gearman_return_t gearman_worker_add_function(gearman_worker_st *worker,
  374. const char *function_name,
  375. uint32_t timeout,
  376. gearman_worker_fn *function,
  377. void *context);
  378. GEARMAN_API
  379. gearman_return_t gearman_worker_define_function(gearman_worker_st *worker,
  380. const char *function_name, const size_t function_name_length,
  381. const gearman_function_t function,
  382. const uint32_t timeout,
  383. void *context);
  384. /**
  385. * Wait for a job and call the appropriate callback function when it gets one.
  386. *
  387. * @param[in] worker Structure previously initialized with
  388. * gearman_worker_create() or gearman_worker_clone().
  389. * @return Standard gearman return value.
  390. */
  391. GEARMAN_API
  392. gearman_return_t gearman_worker_work(gearman_worker_st *worker);
  393. /**
  394. * Send data to all job servers to see if they echo it back. This is a test
  395. * function to see if job servers are responding properly.
  396. *
  397. * @param[in] worker Structure previously initialized with
  398. * gearman_worker_create() or gearman_worker_clone().
  399. * @param[in] workload The workload to ask the server to echo back.
  400. * @param[in] workload_size Size of the workload.
  401. * @return Standard gearman return value.
  402. */
  403. GEARMAN_API
  404. gearman_return_t gearman_worker_echo(gearman_worker_st *worker,
  405. const void *workload,
  406. size_t workload_size);
  407. GEARMAN_API
  408. gearman_return_t gearman_worker_set_memory_allocators(gearman_worker_st *,
  409. gearman_malloc_fn *malloc_fn,
  410. gearman_free_fn *free_fn,
  411. gearman_realloc_fn *realloc_fn,
  412. gearman_calloc_fn *calloc_fn,
  413. void *context);
  414. GEARMAN_API
  415. bool gearman_worker_set_server_option(gearman_worker_st *self, const char *option_arg, size_t option_arg_size);
  416. GEARMAN_API
  417. void gearman_worker_set_namespace(gearman_worker_st *self, const char *namespace_key, size_t namespace_key_size);
  418. /** @} */
  419. #ifdef __cplusplus
  420. }
  421. #endif