reverse_client_epoch.cc 5.5 KB

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