Browse Source

Adds in a gearadmin so that you don't have to telnet to the port to execute
some commands. This refactors server to no longer share code with client.
gearmand_error_t was added for server. Boost is used in the new command line
tool (rolling your own getopt is a drag).

Brian Aker 14 years ago
parent
commit
a10ce46184
10 changed files with 212 additions and 41 deletions
  1. 1 0
      .bzrignore
  2. 1 0
      Makefile.am
  3. 147 0
      bin/gearadmin.cc
  4. 9 1
      bin/include.am
  5. 2 0
      configure.ac
  6. 43 30
      gearmand/gearmand.cc
  7. 0 1
      gearmand/include.am
  8. 2 2
      libgearman-server/conf.c
  9. 3 3
      libgearman-server/conf.h
  10. 4 4
      libgearman-server/connection.c

+ 1 - 0
.bzrignore

@@ -86,3 +86,4 @@ tests/Xugear.pid
 gearmand/gearmand_hostile
 tests/burnin_test
 tests/drizzle_test
+bin/gearadmin

+ 1 - 0
Makefile.am

@@ -29,6 +29,7 @@ include gearmand/include.am
 include libgearman-server/include.am
 include libgearman/include.am
 include util/include.am
+include libtest/include.am
 include tests/include.am
 include support/include.am
 

+ 147 - 0
bin/gearadmin.cc

@@ -0,0 +1,147 @@
+/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
+ *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
+ *
+ *  Copyright (C) 2011 Data Differential
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+#include "config.h"
+
+#include <cstdio>
+#include <cstdlib>
+#include <cstring>
+#include <iostream>
+#include <vector>
+
+#include <netdb.h>
+#include <sys/socket.h>
+
+#ifdef HAVE_ERRNO_H
+#include <errno.h>
+#endif
+#ifdef HAVE_FCNTL_H
+#include <fcntl.h>
+#endif
+#ifdef HAVE_PWD_H
+#include <pwd.h>
+#endif
+#ifdef HAVE_SIGNAL_H
+#include <signal.h>
+#endif
+#ifdef HAVE_STDIO_H
+#include <stdio.h>
+#endif
+#ifdef HAVE_STDLIB_H
+#include <stdlib.h>
+#endif
+#ifdef HAVE_STRING_H
+#include <string.h>
+#endif
+#ifdef HAVE_SYS_RESOURCE_H
+#include <sys/resource.h>
+#endif
+#ifdef HAVE_SYS_STAT_H
+#include <sys/stat.h>
+#endif
+#ifdef HAVE_SYS_TYPES_H
+#include <sys/types.h>
+#endif
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+
+#include <libgearman/protocol.h>
+#include <boost/program_options.hpp>
+
+#include "util/instance.h"
+
+using namespace gearman_util;
+
+/*
+  This is a very quickly build application, I am just tired of telneting to the port.
+*/
+
+#define STRING_WITH_LEN(X) (X), (static_cast<size_t>((sizeof(X) - 1)))
+
+
+int main(int args, char *argv[])
+{
+  boost::program_options::options_description desc("Options");
+  std::string host;
+  std::string port;
+
+  desc.add_options()
+    ("help", "Options related to the program.")
+    ("host,h", boost::program_options::value<std::string>(&host)->default_value("localhost"),"Connect to the host")
+    ("port,p", boost::program_options::value<std::string>(&port)->default_value(GEARMAN_DEFAULT_TCP_PORT_STRING), "Port number or service to use for connection")
+    ("server-version", "Fetch the version number for the server.")
+    ("server-verbose", "Fetch the verbose setting for the server.")
+    ("status", "Status for the server.")
+    ("workers", "Workers for the server.")
+    ("shutdown", "Shutdown server.")
+            ;
+
+  boost::program_options::variables_map vm;
+  try
+  {
+    boost::program_options::store(boost::program_options::parse_command_line(args, argv, desc), vm);
+    boost::program_options::notify(vm);
+  }
+  catch(std::exception &e)
+  { 
+    std::cout << e.what() << std::endl;
+    return 1;
+  }
+
+  Instance instance;
+
+  instance.set_host(host);
+  instance.set_port(port);
+
+  if (vm.count("help"))
+  {
+    std::cout << desc << std::endl;
+    return 1;
+  }
+
+  if (vm.count("shutdown"))
+  {
+    Operation operation(STRING_WITH_LEN("shutdown\n"), false);
+    instance.push(operation);
+  }
+
+  if (vm.count("status"))
+  {
+    Operation operation(STRING_WITH_LEN("status\n"), true);
+    instance.push(operation);
+  }
+
+  if (vm.count("server-version"))
+  {
+    Operation operation(STRING_WITH_LEN("version\n"), true);
+    instance.push(operation);
+  }
+
+  if (vm.count("server-verbose"))
+  {
+    Operation operation(STRING_WITH_LEN("verbose\n"), true);
+    instance.push(operation);
+  }
+
+  instance.run();
+
+  return 0;
+}

+ 9 - 1
bin/include.am

@@ -14,7 +14,15 @@
 
 
 bin_PROGRAMS+= \
-	       bin/gearman
+	       bin/gearman \
+	       bin/gearadmin
+
+bin_gearadmin_SOURCES= \
+		       bin/gearadmin.cc \
+		       util/instance.cc
+
+bin_gearadmin_LDADD= \
+		     ${BOOST_LIBS}
 
 bin_gearman_SOURCES= \
 		     bin/arguments.cc \

+ 2 - 0
configure.ac

@@ -40,6 +40,8 @@ PANDORA_HAVE_LIBPQ
 PANDORA_HAVE_LIBTOKYOCABINET
 AC_FUNC_STRERROR_R
 SOCKET_SEND_FLAGS
+PANDORA_HAVE_BOOST_TEST
+_PANDORA_SEARCH_BOOST_PROGRAM_OPTIONS
 
 
 AC_CHECK_HEADERS(errno.h fcntl.h getopt.h netinet/tcp.h pwd.h signal.h)

+ 43 - 30
gearmand/gearmand.cc

@@ -57,39 +57,52 @@
 
 #include <libgearman-server/gearmand.h>
 
-#ifdef HAVE_LIBDRIZZLE
-#include <libgearman-server/queue_libdrizzle.h>
-#endif
-
-#ifdef HAVE_LIBMEMCACHED
-#include <libgearman-server/queue_libmemcached.h>
-#endif
-
-#ifdef HAVE_LIBSQLITE3
-#include <libgearman-server/queue_libsqlite3.h>
-#endif
-
-#ifdef HAVE_LIBPQ
-#include <libgearman-server/queue_libpq.h>
-#endif
-
-#ifdef HAVE_LIBTOKYOCABINET
-#include <libgearman-server/queue_libtokyocabinet.h>
-#endif
-
-#include <libgearman-server/protocol_http.h>
+#include <libgearman-server/plugins.h>
 
 #define GEARMAND_LOG_REOPEN_TIME 60
 #define GEARMAND_LISTEN_BACKLOG 32
 
 #include "util/daemon.h"
-#include "util/error.h"
 #include "util/pidfile.h"
 
 #include <iostream>
 
 using namespace gearman_util;
 
+namespace error {
+
+inline void perror(const char *message)
+{
+  char *errmsg_ptr;
+  char errmsg[BUFSIZ];
+  errmsg[0]= 0;
+
+#ifdef STRERROR_R_CHAR_P
+  errmsg_ptr= strerror_r(errno, errmsg, sizeof(errmsg));
+#else
+  strerror_r(errno, errmsg, sizeof(errmsg));
+  errmsg_ptr= errmsg;
+#endif
+  std::cerr << "gearman: " << message << " (" << errmsg_ptr << ")" << std::endl;
+}
+
+inline void message(const char *arg)
+{
+  std::cerr << "gearmand: " << arg << std::endl;
+}
+
+inline void message(const char *arg, const char *arg2)
+{
+  std::cerr << "gearmand: " << arg << " : " << arg2 << std::endl;
+}
+
+inline void message(const std::string &arg, gearmand_error_t rc)
+{
+  std::cerr << "gearmand: " << arg << " : " << gearmand_strerror(rc) << std::endl;
+}
+
+} // namespace error
+
 struct gearmand_log_info_st
 {
   const char *file;
@@ -110,9 +123,9 @@ extern "C" {
 static bool _set_signals(void);
 }
 static void _shutdown_handler(int signal_arg);
-static void _log(const char *line, gearman_verbose_t verbose, void *context);
+static void _log(const char *line, gearmand_verbose_t verbose, void *context);
 
-static gearman_return_t queue_init(gearmand_st *_gearmand, gearman_conf_st &conf, const char *queue_type);
+static gearmand_error_t queue_init(gearmand_st *_gearmand, gearman_conf_st &conf, const char *queue_type);
 static void queue_deinit(gearmand_st *_gearmand, const char *queue_type);
 static int queue_configure(gearman_conf_st &conf);
 
@@ -312,7 +325,7 @@ int main(int argc, char *argv[])
     gearmand::daemon_is_ready(verbose_count == 0);
 
 
-  gearman_verbose_t verbose= verbose_count > static_cast<int>(GEARMAN_VERBOSE_CRAZY) ? GEARMAN_VERBOSE_CRAZY : static_cast<gearman_verbose_t>(verbose_count);
+  gearmand_verbose_t verbose= verbose_count > static_cast<int>(GEARMAND_VERBOSE_CRAZY) ? GEARMAND_VERBOSE_CRAZY : static_cast<gearmand_verbose_t>(verbose_count);
 
   Pidfile _pid_file(pid_file);
 
@@ -334,7 +347,7 @@ int main(int argc, char *argv[])
 
   if (queue_type)
   {
-    gearman_return_t rc;
+    gearmand_error_t rc;
     if ((rc= queue_init(_gearmand, conf, queue_type)) != GEARMAN_SUCCESS)
     {
       std::string error_message;
@@ -354,7 +367,7 @@ int main(int argc, char *argv[])
 
     if (not strcmp(value, "http"))
     {
-      gearman_return_t ret;
+      gearmand_error_t ret;
       ret= gearmand_protocol_http_init(_gearmand, &conf);
       if (ret != GEARMAN_SUCCESS)
         return 1;
@@ -366,7 +379,7 @@ int main(int argc, char *argv[])
     }
   }
 
-  gearman_return_t ret;
+  gearmand_error_t ret;
   ret= gearmand_run(_gearmand);
 
   if (queue_type)
@@ -498,7 +511,7 @@ static void _shutdown_handler(int signal_arg)
     gearmand_wakeup(Gearmand(), GEARMAND_WAKEUP_SHUTDOWN);
 }
 
-static void _log(const char *line, gearman_verbose_t verbose, void *context)
+static void _log(const char *line, gearmand_verbose_t verbose, void *context)
 {
   gearmand_log_info_st *log_info= static_cast<gearmand_log_info_st *>(context);
   int fd;
@@ -541,7 +554,7 @@ static void _log(const char *line, gearman_verbose_t verbose, void *context)
   }
 }
 
-static gearman_return_t queue_init(gearmand_st *_gearmand, gearman_conf_st &conf, const char *queue_type)
+static gearmand_error_t queue_init(gearmand_st *_gearmand, gearman_conf_st &conf, const char *queue_type)
 {
 #ifdef HAVE_LIBDRIZZLE
   if (not strcmp(queue_type, "libdrizzle"))

+ 0 - 1
gearmand/include.am

@@ -19,7 +19,6 @@ gearmand_gearmand_LDADD= \
 gearmand_gearmand_SOURCES= \
 			   gearmand/gearmand.cc \
 			   util/daemon.cc \
-			   util/error.cc \
 			   util/pidfile.cc
 
 gearmand-valgrind: gearmand/gearmand

+ 2 - 2
libgearman-server/conf.c

@@ -87,7 +87,7 @@ void gearman_conf_free(gearman_conf_st *conf)
     free(conf);
 }
 
-gearman_return_t gearman_conf_return(gearman_conf_st *conf)
+gearmand_error_t gearman_conf_return(gearman_conf_st *conf)
 {
   return conf->last_return;
 }
@@ -102,7 +102,7 @@ int gearman_conf_errno(gearman_conf_st *conf)
   return conf->last_errno;
 }
 
-gearman_return_t gearman_conf_parse_args(gearman_conf_st *conf, int argc,
+gearmand_error_t gearman_conf_parse_args(gearman_conf_st *conf, int argc,
                                          char *argv[])
 {
   int c;

+ 3 - 3
libgearman-server/conf.h

@@ -39,7 +39,7 @@ struct gearman_conf_st
   struct {
     bool allocated;
   } options;
-  gearman_return_t last_return;
+  gearmand_error_t last_return;
   int last_errno;
   size_t module_count;
   size_t option_count;
@@ -67,7 +67,7 @@ void gearman_conf_free(gearman_conf_st *conf);
  * Return an return code for the last library error encountered.
  */
 GEARMAN_API
-gearman_return_t gearman_conf_return(gearman_conf_st *conf);
+gearmand_error_t gearman_conf_return(gearman_conf_st *conf);
 
 /**
  * Return an error string for last library error encountered.
@@ -85,7 +85,7 @@ int gearman_conf_errno(gearman_conf_st *conf);
  * Parse command line arguments.
  */
 GEARMAN_API
-gearman_return_t gearman_conf_parse_args(gearman_conf_st *conf, int argc,
+gearmand_error_t gearman_conf_parse_args(gearman_conf_st *conf, int argc,
                                          char *argv[]);
 
 /**

+ 4 - 4
libgearman-server/connection.c

@@ -14,13 +14,13 @@
 #include "common.h"
 
 static gearman_server_con_st * _server_con_create(gearman_server_thread_st *thread, gearmand_con_st *dcon,
-                                                  gearman_return_t *ret);
+                                                  gearmand_error_t *ret);
 
 /*
  * Public definitions
  */
 
-gearman_server_con_st *gearman_server_con_add(gearman_server_thread_st *thread, gearmand_con_st *dcon, gearman_return_t *ret)
+gearman_server_con_st *gearman_server_con_add(gearman_server_thread_st *thread, gearmand_con_st *dcon, gearmand_error_t *ret)
 {
   gearman_server_con_st *con;
 
@@ -48,7 +48,7 @@ gearman_server_con_st *gearman_server_con_add(gearman_server_thread_st *thread,
 }
 
 static gearman_server_con_st * _server_con_create(gearman_server_thread_st *thread, gearmand_con_st *dcon,
-                                                  gearman_return_t *ret)
+                                                  gearmand_error_t *ret)
 {
   gearman_server_con_st *con;
 
@@ -75,7 +75,7 @@ static gearman_server_con_st * _server_con_create(gearman_server_thread_st *thre
     return NULL;
   }
 
-  gearman_connection_options_t options[] = { GEARMAN_CON_MAX };
+  gearmand_connection_options_t options[] = { GEARMAND_CON_MAX };
   gearmand_connection_init(thread->gearman, &(con->con), dcon, options);
 
   con->con.root= con;

Some files were not shown because too many files changed in this diff