reverse_client_bg.cc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 <cstdlib>
  40. #include <iomanip>
  41. #include <iostream>
  42. #include <string>
  43. #include <libgearman/gearman.h>
  44. #include <boost/program_options.hpp>
  45. #include "util/string.hpp"
  46. #include "gearmand/error.hpp"
  47. #ifndef __INTEL_COMPILER
  48. #pragma GCC diagnostic ignored "-Wold-style-cast"
  49. #endif
  50. int main(int args, char *argv[])
  51. {
  52. gearmand::error::init(argv[0]);
  53. bool wait_for_result= false;
  54. in_port_t port;
  55. int timeout;
  56. std::string host;
  57. std::string text_to_echo;
  58. boost::program_options::options_description desc("Options");
  59. desc.add_options()
  60. ("help", "Options related to the program.")
  61. ("verbose", "Send status to stdout")
  62. ("host,h", boost::program_options::value<std::string>(&host)->default_value("localhost"),"Connect to the host")
  63. ("port,p", boost::program_options::value<in_port_t>(&port)->default_value(GEARMAN_DEFAULT_TCP_PORT), "Port number use for connection")
  64. ("wait", "Submit status job and wait for results.")
  65. ("timeout,u", boost::program_options::value<int>(&timeout)->default_value(-1), "Timeout in milliseconds")
  66. ("text", boost::program_options::value<std::string>(&text_to_echo), "Text used for echo")
  67. ;
  68. boost::program_options::positional_options_description text_options;
  69. text_options.add("text", -1);
  70. boost::program_options::variables_map vm;
  71. try
  72. {
  73. boost::program_options::store(boost::program_options::command_line_parser(args, argv).
  74. options(desc).positional(text_options).run(), vm);
  75. boost::program_options::notify(vm);
  76. }
  77. catch(std::exception &e)
  78. {
  79. std::cout << e.what() << std::endl;
  80. return EXIT_FAILURE;
  81. }
  82. if (vm.count("help"))
  83. {
  84. std::cout << desc << std::endl;
  85. return EXIT_SUCCESS;
  86. }
  87. if (vm.count("verbose") == 0)
  88. {
  89. close(STDOUT_FILENO);
  90. }
  91. if (vm.count("wait_for_result"))
  92. {
  93. wait_for_result= true;
  94. }
  95. if (text_to_echo.empty())
  96. {
  97. while(std::cin.good())
  98. {
  99. char buffer[1024];
  100. std::cin.read(buffer, sizeof(buffer));
  101. text_to_echo.append(buffer, std::cin.gcount());
  102. }
  103. if (text_to_echo.empty())
  104. {
  105. gearmand::error::message("No text was provided for --text or via stdin");
  106. return EXIT_FAILURE;
  107. }
  108. }
  109. gearman_client_st client;
  110. if (gearman_client_create(&client) == NULL)
  111. {
  112. gearmand::error::message("Memory allocation failure on client creation");
  113. return EXIT_FAILURE;
  114. }
  115. gearman_return_t ret;
  116. ret= gearman_client_add_server(&client, host.c_str(), port);
  117. if (ret != GEARMAN_SUCCESS)
  118. {
  119. gearmand::error::message(gearman_client_error(&client));
  120. return EXIT_FAILURE;
  121. }
  122. if (timeout >= 0)
  123. {
  124. gearman_client_set_timeout(&client, timeout);
  125. }
  126. gearman_task_attr_t workload= gearman_task_attr_init_background(GEARMAN_JOB_PRIORITY_NORMAL);
  127. gearman_task_st *task;
  128. gearman_argument_t values[]= {
  129. gearman_argument_make(0, 0, text_to_echo.c_str(), text_to_echo.size()),
  130. gearman_argument_make(0, 0, 0, 0)
  131. };
  132. if ((task= gearman_execute(&client, util_literal_param("reverse"), NULL, 0, &workload, values, 0)) == NULL)
  133. {
  134. gearmand::error::message("Failed to process job", gearman_client_error(&client));
  135. gearman_client_free(&client);
  136. return EXIT_FAILURE;
  137. }
  138. int exit_code= EXIT_SUCCESS;
  139. if (wait_for_result)
  140. {
  141. std::cout << "Background Job Handle=" << gearman_task_job_handle(task) << std::endl;
  142. bool is_known;
  143. do
  144. {
  145. bool is_running;
  146. uint32_t numerator;
  147. uint32_t denominator;
  148. ret= gearman_client_job_status(&client, gearman_task_job_handle(task),
  149. &is_known, &is_running,
  150. &numerator, &denominator);
  151. if (gearman_continue(ret)) // Non-blocking event occurred, try again
  152. {
  153. continue;
  154. }
  155. else if (gearman_failed(ret))
  156. {
  157. gearmand::error::message(gearman_client_error(&client));
  158. exit_code= EXIT_FAILURE;
  159. break;
  160. }
  161. std::cout << std::boolalpha
  162. << "Known =" << is_known
  163. << ", Running=" << is_running
  164. << ", Percent Complete=" << numerator << "/" << denominator << std::endl;
  165. } while (is_known);
  166. gearman_client_free(&client);
  167. }
  168. return exit_code;
  169. }