reverse_client_epoch.cc 5.7 KB

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