worker.h 16 KB

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