reverse_worker.cc 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * Gearmand client and server library.
  4. *
  5. * Copyright (C) 2011 Data Differential, http://datadifferential.com/
  6. * Copyright (C) 2008 Brian Aker, Eric Day
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions are
  11. * met:
  12. *
  13. * * Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * * Redistributions in binary form must reproduce the above
  17. * copyright notice, this list of conditions and the following disclaimer
  18. * in the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * * The names of its contributors may not be used to endorse or
  22. * promote products derived from this software without specific prior
  23. * written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  26. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  27. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  28. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  29. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  30. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  31. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  32. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  33. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  34. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  35. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. */
  38. #include <config.h>
  39. #include <cerrno>
  40. #include <cstdio>
  41. #include <cstdlib>
  42. #include <cstring>
  43. #include <iostream>
  44. #include <signal.h>
  45. #include <libgearman/gearman.h>
  46. #include <boost/program_options.hpp>
  47. struct reverse_worker_options_t
  48. {
  49. bool chunk;
  50. bool status;
  51. bool unique;
  52. reverse_worker_options_t():
  53. chunk(false),
  54. status(false),
  55. unique(false)
  56. { }
  57. };
  58. #ifndef __INTEL_COMPILER
  59. #pragma GCC diagnostic ignored "-Wold-style-cast"
  60. #endif
  61. static void *reverse(gearman_job_st *job, void *context,
  62. size_t *result_size, gearman_return_t *ret_ptr);
  63. int main(int args, char *argv[])
  64. {
  65. uint32_t count;
  66. reverse_worker_options_t options;
  67. int timeout;
  68. in_port_t port;
  69. std::string host;
  70. boost::program_options::options_description desc("Options");
  71. desc.add_options()
  72. ("help", "Options related to the program.")
  73. ("host,h", boost::program_options::value<std::string>(&host)->default_value("localhost"),"Connect to the host")
  74. ("port,p", boost::program_options::value<in_port_t>(&port)->default_value(GEARMAN_DEFAULT_TCP_PORT), "Port number use for connection")
  75. ("count,c", boost::program_options::value<uint32_t>(&count)->default_value(0), "Number of jobs to run before exiting")
  76. ("timeout,u", boost::program_options::value<int>(&timeout)->default_value(-1), "Timeout in milliseconds")
  77. ("chunk,d", boost::program_options::bool_switch(&options.chunk)->default_value(false), "Send result back in data chunks")
  78. ("status,s", boost::program_options::bool_switch(&options.status)->default_value(false), "Send status updates and sleep while running job")
  79. ("unique,u", boost::program_options::bool_switch(&options.unique)->default_value(false), "When grabbing jobs, grab the uniqie id")
  80. ;
  81. boost::program_options::variables_map vm;
  82. try
  83. {
  84. boost::program_options::store(boost::program_options::parse_command_line(args, argv, desc), vm);
  85. boost::program_options::notify(vm);
  86. }
  87. catch(std::exception &e)
  88. {
  89. std::cout << e.what() << std::endl;
  90. return EXIT_FAILURE;
  91. }
  92. if (vm.count("help"))
  93. {
  94. std::cout << desc << std::endl;
  95. return EXIT_SUCCESS;
  96. }
  97. if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
  98. {
  99. std::cerr << "signal:" << strerror(errno) << std::endl;
  100. return EXIT_FAILURE;
  101. }
  102. gearman_worker_st worker;
  103. if (gearman_worker_create(&worker) == NULL)
  104. {
  105. std::cerr << "Memory allocation failure on worker creation." << std::endl;
  106. return EXIT_FAILURE;
  107. }
  108. if (options.unique)
  109. gearman_worker_add_options(&worker, GEARMAN_WORKER_GRAB_UNIQ);
  110. if (timeout >= 0)
  111. gearman_worker_set_timeout(&worker, timeout);
  112. gearman_return_t ret;
  113. ret= gearman_worker_add_server(&worker, host.c_str(), port);
  114. if (ret != GEARMAN_SUCCESS)
  115. {
  116. std::cerr << gearman_worker_error(&worker) << std::endl;
  117. return EXIT_FAILURE;
  118. }
  119. ret= gearman_worker_add_function(&worker, "reverse", 0, reverse, &options);
  120. if (ret != GEARMAN_SUCCESS)
  121. {
  122. std::cerr << gearman_worker_error(&worker) << std::endl;
  123. return EXIT_FAILURE;
  124. }
  125. while (1)
  126. {
  127. ret= gearman_worker_work(&worker);
  128. if (ret != GEARMAN_SUCCESS)
  129. {
  130. std::cerr << gearman_worker_error(&worker) << std::endl;
  131. break;
  132. }
  133. if (count > 0)
  134. {
  135. count--;
  136. if (count == 0)
  137. break;
  138. }
  139. }
  140. gearman_worker_free(&worker);
  141. return EXIT_SUCCESS;
  142. }
  143. static void *reverse(gearman_job_st *job, void *context,
  144. size_t *result_size, gearman_return_t *ret_ptr)
  145. {
  146. reverse_worker_options_t options= *((reverse_worker_options_t *)context);
  147. const char *workload;
  148. workload= (const char *)gearman_job_workload(job);
  149. *result_size= gearman_job_workload_size(job);
  150. char *result;
  151. result= (char *)malloc(*result_size);
  152. if (result == NULL)
  153. {
  154. perror("malloc");
  155. *ret_ptr= GEARMAN_WORK_FAIL;
  156. return NULL;
  157. }
  158. size_t x;
  159. size_t y;
  160. for (y= 0, x= *result_size; x; x--, y++)
  161. {
  162. result[y]= ((uint8_t *)workload)[x - 1];
  163. if (options.chunk)
  164. {
  165. *ret_ptr= gearman_job_send_data(job, &(result[y]), 1);
  166. if (*ret_ptr != GEARMAN_SUCCESS)
  167. {
  168. free(result);
  169. return NULL;
  170. }
  171. }
  172. if (options.status)
  173. {
  174. *ret_ptr= gearman_job_send_status(job, (uint32_t)y,
  175. (uint32_t)*result_size);
  176. if (*ret_ptr != GEARMAN_SUCCESS)
  177. {
  178. free(result);
  179. return NULL;
  180. }
  181. sleep(1);
  182. }
  183. }
  184. std::cout << "Job=" << gearman_job_handle(job);
  185. if (options.unique)
  186. {
  187. std::cout << "Unique=" << gearman_job_unique(job);
  188. }
  189. std::cout << " Workload=";
  190. std::cout.write(workload, *result_size);
  191. std::cout << " Result=";
  192. std::cout.write(result, *result_size);
  193. std::cout << std::endl;
  194. *ret_ptr= GEARMAN_SUCCESS;
  195. if (options.chunk)
  196. {
  197. *result_size= 0;
  198. return NULL;
  199. }
  200. return result;
  201. }