reverse_worker.cc 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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 "gear_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. bool verbose;
  53. reverse_worker_options_t():
  54. chunk(false),
  55. status(false),
  56. unique(false),
  57. verbose(true)
  58. { }
  59. };
  60. #ifndef __INTEL_COMPILER
  61. #pragma GCC diagnostic ignored "-Wold-style-cast"
  62. #endif
  63. static gearman_return_t reverse_worker(gearman_job_st *job, void *context)
  64. {
  65. reverse_worker_options_t options= *((reverse_worker_options_t *)context);
  66. const char *workload= (const char *)gearman_job_workload(job);
  67. const size_t workload_size= gearman_job_workload_size(job);
  68. if (options.verbose)
  69. {
  70. std::cout << "Received " << workload_size << " bytes" << std::endl;
  71. }
  72. std::vector<char> result;
  73. result.resize(workload_size);
  74. // Copy workload
  75. for (size_t y= 0, x= workload_size; x; x--, y++)
  76. {
  77. result[y]= ((uint8_t *)workload)[x - 1];
  78. }
  79. if (options.chunk) // Chunk the result set
  80. {
  81. for (size_t y= 0, x= workload_size; x; x--, y++)
  82. {
  83. if (gearman_failed(gearman_job_send_data(job, &result[y], 1)))
  84. {
  85. return GEARMAN_ERROR;
  86. }
  87. if (options.status)
  88. {
  89. // Notice that we send based on y divided by zero.
  90. if (gearman_failed(gearman_job_send_status(job, (uint32_t)y, (uint32_t)workload_size)))
  91. {
  92. return GEARMAN_ERROR;
  93. }
  94. }
  95. }
  96. }
  97. else
  98. {
  99. if (options.status)
  100. {
  101. // Notice that we send based on y divided by zero.
  102. if (gearman_failed(gearman_job_send_status(job, (uint32_t)0, (uint32_t)workload_size)))
  103. {
  104. return GEARMAN_ERROR;
  105. }
  106. }
  107. if (gearman_failed(gearman_job_send_data(job, &result[0], workload_size)))
  108. {
  109. return GEARMAN_ERROR;
  110. }
  111. if (options.status)
  112. {
  113. // Notice that we send based on y divided by zero.
  114. if (gearman_failed(gearman_job_send_status(job, (uint32_t)workload_size, (uint32_t)workload_size)))
  115. {
  116. return GEARMAN_ERROR;
  117. }
  118. }
  119. }
  120. if (options.verbose)
  121. {
  122. std::cout << "Job=" << gearman_job_handle(job);
  123. }
  124. if (options.unique and options.verbose)
  125. {
  126. std::cout << "Unique=" << gearman_job_unique(job);
  127. }
  128. if (options.verbose)
  129. {
  130. std::cout << " Reversed=";
  131. std::cout.write(workload, workload_size);
  132. std::cout << std::endl;
  133. }
  134. return GEARMAN_SUCCESS;
  135. }
  136. int main(int args, char *argv[])
  137. {
  138. uint64_t limit;
  139. reverse_worker_options_t options;
  140. int timeout;
  141. in_port_t port;
  142. std::string host;
  143. std::string identifier;
  144. boost::program_options::options_description desc("Options");
  145. desc.add_options()
  146. ("help", "Options related to the program.")
  147. ("host,h", boost::program_options::value<std::string>(&host)->default_value("localhost"),"Connect to the host")
  148. ("identifier", boost::program_options::value<std::string>(&identifier),"Assign identifier")
  149. ("port,p", boost::program_options::value<in_port_t>(&port)->default_value(GEARMAN_DEFAULT_TCP_PORT), "Port number use for connection")
  150. ("count,c", boost::program_options::value<uint64_t>(&limit)->default_value(0), "Number of jobs to run before exiting")
  151. ("timeout,u", boost::program_options::value<int>(&timeout)->default_value(-1), "Timeout in milliseconds")
  152. ("chunk,d", boost::program_options::bool_switch(&options.chunk)->default_value(false), "Send result back in data chunks")
  153. ("status,s", boost::program_options::bool_switch(&options.status)->default_value(false), "Send status updates and sleep while running job")
  154. ("unique,u", boost::program_options::bool_switch(&options.unique)->default_value(false), "When grabbing jobs, grab the uniqie id")
  155. ("verbose", boost::program_options::bool_switch(&options.verbose)->default_value(true), "Print to stdout information as job is processed.")
  156. ;
  157. boost::program_options::variables_map vm;
  158. try
  159. {
  160. boost::program_options::store(boost::program_options::parse_command_line(args, argv, desc), vm);
  161. boost::program_options::notify(vm);
  162. }
  163. catch(std::exception &e)
  164. {
  165. std::cout << e.what() << std::endl;
  166. return EXIT_FAILURE;
  167. }
  168. if (vm.count("help"))
  169. {
  170. std::cout << desc << std::endl;
  171. return EXIT_SUCCESS;
  172. }
  173. if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
  174. {
  175. std::cerr << "signal:" << strerror(errno) << std::endl;
  176. return EXIT_FAILURE;
  177. }
  178. gearman_worker_st *worker;
  179. if ((worker= gearman_worker_create(NULL)) == NULL)
  180. {
  181. std::cerr << "Memory allocation failure on worker creation." << std::endl;
  182. return EXIT_FAILURE;
  183. }
  184. if (options.unique)
  185. {
  186. gearman_worker_add_options(worker, GEARMAN_WORKER_GRAB_UNIQ);
  187. }
  188. if (timeout >= 0)
  189. {
  190. gearman_worker_set_timeout(worker, timeout);
  191. }
  192. if (gearman_failed(gearman_worker_add_server(worker, host.c_str(), port)))
  193. {
  194. std::cerr << gearman_worker_error(worker) << std::endl;
  195. return EXIT_FAILURE;
  196. }
  197. if (identifier.empty() == false)
  198. {
  199. if (gearman_failed(gearman_worker_set_identifier(worker, identifier.c_str(), identifier.size())))
  200. {
  201. std::cerr << gearman_worker_error(worker) << std::endl;
  202. return EXIT_FAILURE;
  203. }
  204. }
  205. gearman_function_t worker_fn= gearman_function_create(reverse_worker);
  206. if (gearman_failed(gearman_worker_define_function(worker,
  207. gearman_literal_param("reverse"),
  208. worker_fn,
  209. 0,
  210. &options)))
  211. {
  212. std::cerr << gearman_worker_error(worker) << std::endl;
  213. return EXIT_FAILURE;
  214. }
  215. // Add one if count is not zero
  216. if (limit != 0)
  217. {
  218. limit++;
  219. }
  220. while (--limit)
  221. {
  222. if (gearman_failed(gearman_worker_work(worker)))
  223. {
  224. std::cerr << gearman_worker_error(worker) << std::endl;
  225. break;
  226. }
  227. }
  228. gearman_worker_free(worker);
  229. return EXIT_SUCCESS;
  230. }