gearmand.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * Data Differential YATL (i.e. libtest) library
  4. *
  5. * Copyright (C) 2012 Data Differential, http://datadifferential.com/
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are
  9. * met:
  10. *
  11. * * Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * * Redistributions in binary form must reproduce the above
  15. * copyright notice, this list of conditions and the following disclaimer
  16. * in the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * * The names of its contributors may not be used to endorse or
  20. * promote products derived from this software without specific prior
  21. * written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  24. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  25. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  26. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  27. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  28. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  29. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  30. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  31. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  32. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  33. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. */
  36. #include <config.h>
  37. #include <libtest/common.h>
  38. #include <libtest/gearmand.h>
  39. #include "util/instance.hpp"
  40. #include "util/operation.hpp"
  41. using namespace datadifferential;
  42. using namespace libtest;
  43. #include <cassert>
  44. #include <cerrno>
  45. #include <cstdio>
  46. #include <cstdlib>
  47. #include <cstring>
  48. #include <iostream>
  49. #include <signal.h>
  50. #include <sstream>
  51. #include <sys/types.h>
  52. #include <sys/wait.h>
  53. #include <unistd.h>
  54. #include <libgearman/gearman.h>
  55. #ifndef __INTEL_COMPILER
  56. #pragma GCC diagnostic ignored "-Wold-style-cast"
  57. #endif
  58. using namespace libtest;
  59. class Gearmand : public libtest::Server
  60. {
  61. private:
  62. public:
  63. Gearmand(const std::string& host_arg, in_port_t port_arg) :
  64. libtest::Server(host_arg, port_arg, GEARMAND_BINARY, true)
  65. {
  66. set_pid_file();
  67. }
  68. bool ping()
  69. {
  70. gearman_client_st *client= gearman_client_create(NULL);
  71. if (client == NULL)
  72. {
  73. Error << "Could not allocate memory for gearman_client_create()";
  74. return false;
  75. }
  76. gearman_client_set_timeout(client, 3000);
  77. if (gearman_success(gearman_client_add_server(client, hostname().c_str(), port())))
  78. {
  79. gearman_return_t rc= gearman_client_echo(client, test_literal_param("This is my echo test"));
  80. if (gearman_success(rc))
  81. {
  82. gearman_client_free(client);
  83. return true;
  84. }
  85. #if 0
  86. Error << hostname().c_str() << ":" << port() << " was " << gearman_strerror(rc) << " extended: " << gearman_client_error(client);
  87. #endif
  88. }
  89. else
  90. {
  91. Error << "gearman_client_add_server() " << gearman_client_error(client);
  92. }
  93. gearman_client_free(client);
  94. return false;;
  95. }
  96. const char *name()
  97. {
  98. return "gearmand";
  99. };
  100. void log_file_option(Application& app, const std::string& arg)
  101. {
  102. if (arg.empty() == false)
  103. {
  104. std::string buffer("--log-file=");
  105. buffer+= arg;
  106. app.add_option("--verbose=DEBUG");
  107. app.add_option(buffer);
  108. }
  109. }
  110. bool has_log_file_option() const
  111. {
  112. return true;
  113. }
  114. bool is_libtool()
  115. {
  116. return true;
  117. }
  118. bool has_syslog() const
  119. {
  120. return false; // --syslog.errmsg-enable
  121. }
  122. bool has_port_option() const
  123. {
  124. return true;
  125. }
  126. bool build(size_t argc, const char *argv[]);
  127. };
  128. bool Gearmand::build(size_t argc, const char *argv[])
  129. {
  130. if (getuid() == 0 or geteuid() == 0)
  131. {
  132. add_option("-u", "root");
  133. }
  134. add_option("--listen=localhost");
  135. for (size_t x= 0 ; x < argc ; x++)
  136. {
  137. add_option(argv[x]);
  138. }
  139. return true;
  140. }
  141. namespace libtest {
  142. libtest::Server *build_gearmand(const char *hostname, in_port_t try_port)
  143. {
  144. return new Gearmand(hostname, try_port);
  145. }
  146. }