queue.cc 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are
  10. * met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * * Redistributions in binary form must reproduce the above
  16. * copyright notice, this list of conditions and the following disclaimer
  17. * in the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * * The names of its contributors may not be used to endorse or
  21. * promote products derived from this software without specific prior
  22. * written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  25. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  26. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  27. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  28. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  29. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  30. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  31. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  32. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  33. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  34. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. *
  36. */
  37. #include "gear_config.h"
  38. #include <iostream>
  39. #include <boost/program_options.hpp>
  40. #include "libgearman-server/common.h"
  41. #include <libgearman-server/queue.h>
  42. #include <libgearman-server/plugins/queue/base.h>
  43. #include <libgearman-server/queue.hpp>
  44. #include <libgearman-server/log.h>
  45. #include <assert.h>
  46. gearmand_error_t gearman_queue_add(gearman_server_st *server,
  47. const char *unique,
  48. size_t unique_size,
  49. const char *function_name,
  50. size_t function_name_size,
  51. const void *data,
  52. size_t data_size,
  53. gearman_job_priority_t priority,
  54. int64_t when)
  55. {
  56. assert(server->state.queue_startup == false);
  57. gearmand_error_t ret;
  58. if (server->queue_version == QUEUE_VERSION_NONE)
  59. {
  60. return GEARMAND_SUCCESS;
  61. }
  62. else if (server->queue_version == QUEUE_VERSION_FUNCTION)
  63. {
  64. assert(server->queue.functions->_add_fn);
  65. ret= (*(server->queue.functions->_add_fn))(server,
  66. (void *)server->queue.functions->_context,
  67. unique, unique_size,
  68. function_name,
  69. function_name_size,
  70. data, data_size, priority,
  71. when);
  72. }
  73. else
  74. {
  75. assert(server->queue.object);
  76. ret= server->queue.object->store(server,
  77. unique, unique_size,
  78. function_name,
  79. function_name_size,
  80. data, data_size, priority,
  81. when);
  82. }
  83. if (gearmand_success(ret))
  84. {
  85. ret= gearman_queue_flush(server);
  86. }
  87. return ret;
  88. }
  89. gearmand_error_t gearman_queue_flush(gearman_server_st *server)
  90. {
  91. if (server->queue_version != QUEUE_VERSION_NONE)
  92. {
  93. if (server->queue_version == QUEUE_VERSION_FUNCTION)
  94. {
  95. assert(server->queue.functions->_flush_fn);
  96. return (*(server->queue.functions->_flush_fn))(server, (void *)server->queue.functions->_context);
  97. }
  98. assert(server->queue.object);
  99. return server->queue.object->flush(server);
  100. }
  101. return GEARMAND_SUCCESS;
  102. }
  103. gearmand_error_t gearman_queue_done(gearman_server_st *server,
  104. const char *unique,
  105. size_t unique_size,
  106. const char *function_name,
  107. size_t function_name_size)
  108. {
  109. if (server->queue_version == QUEUE_VERSION_NONE)
  110. {
  111. return GEARMAND_SUCCESS;
  112. }
  113. else if (server->queue_version == QUEUE_VERSION_FUNCTION)
  114. {
  115. assert(server->queue.functions->_done_fn);
  116. return (*(server->queue.functions->_done_fn))(server,
  117. (void *)server->queue.functions->_context,
  118. unique, unique_size,
  119. function_name,
  120. function_name_size);
  121. }
  122. else
  123. {
  124. assert(server->queue.object);
  125. return server->queue.object->done(server,
  126. unique, unique_size,
  127. function_name,
  128. function_name_size);
  129. }
  130. }
  131. void gearman_server_save_job(gearman_server_st& server,
  132. const gearman_server_job_st* server_job)
  133. {
  134. if (server.queue_version == QUEUE_VERSION_CLASS)
  135. {
  136. assert(server.queue.object);
  137. server.queue.object->save_job(server, server_job);
  138. }
  139. }
  140. void gearman_server_set_queue(gearman_server_st& server,
  141. void *context,
  142. gearman_queue_add_fn *add,
  143. gearman_queue_flush_fn *flush,
  144. gearman_queue_done_fn *done,
  145. gearman_queue_replay_fn *replay)
  146. {
  147. delete server.queue.functions;
  148. server.queue.functions= NULL;
  149. delete server.queue.object;
  150. server.queue.object= NULL;
  151. if (add)
  152. {
  153. server.queue_version= QUEUE_VERSION_FUNCTION;
  154. server.queue.functions= new queue_st();
  155. if (server.queue.functions)
  156. {
  157. server.queue.functions->_context= context;
  158. server.queue.functions->_add_fn= add;
  159. server.queue.functions->_flush_fn= flush;
  160. server.queue.functions->_done_fn= done;
  161. server.queue.functions->_replay_fn= replay;
  162. }
  163. assert(server.queue.functions);
  164. }
  165. else
  166. {
  167. server.queue_version= QUEUE_VERSION_NONE;
  168. }
  169. }
  170. void gearman_server_set_queue(gearman_server_st& server,
  171. gearmand::queue::Context* context)
  172. {
  173. delete server.queue.functions;
  174. server.queue.functions= NULL;
  175. delete server.queue.object;
  176. server.queue.object= NULL;
  177. assert(context);
  178. {
  179. server.queue_version= QUEUE_VERSION_CLASS;
  180. server.queue.object= context;
  181. }
  182. }
  183. namespace gearmand {
  184. namespace queue {
  185. plugins::Queue::vector all_queue_modules;
  186. void add(plugins::Queue* arg)
  187. {
  188. all_queue_modules.push_back(arg);
  189. }
  190. void load_options(boost::program_options::options_description &all)
  191. {
  192. for (plugins::Queue::vector::iterator iter= all_queue_modules.begin();
  193. iter != all_queue_modules.end();
  194. ++iter)
  195. {
  196. all.add((*iter)->command_line_options());
  197. }
  198. }
  199. gearmand_error_t initialize(gearmand_st *, std::string name)
  200. {
  201. bool launched= false;
  202. if (name.empty())
  203. {
  204. return GEARMAND_SUCCESS;
  205. }
  206. std::transform(name.begin(), name.end(), name.begin(), ::tolower);
  207. for (plugins::Queue::vector::iterator iter= all_queue_modules.begin();
  208. iter != all_queue_modules.end();
  209. ++iter)
  210. {
  211. if ((*iter)->compare(name) == 0)
  212. {
  213. if (launched)
  214. {
  215. return gearmand_gerror("Attempt to initialize multiple queues", GEARMAND_UNKNOWN_OPTION);
  216. }
  217. gearmand_error_t rc;
  218. if (gearmand_failed(rc= (*iter)->initialize()))
  219. {
  220. return gearmand_log_gerror(GEARMAN_DEFAULT_LOG_PARAM, rc,
  221. "Failed to initialize %s: %s", name.c_str(), (*iter)->error_string().c_str());
  222. }
  223. launched= true;
  224. }
  225. }
  226. if (launched == false)
  227. {
  228. return gearmand_log_gerror(GEARMAN_DEFAULT_LOG_PARAM, GEARMAND_UNKNOWN_OPTION, "Unknown queue %s", name.c_str());
  229. }
  230. return GEARMAND_SUCCESS;
  231. }
  232. } // namespace queue
  233. } // namespace gearmand