Merging bzr://gaz.tangent.org/libmemcached/build/ to Build branch
[awesomized/libmemcached] / libtest / blobslap_worker.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * libtest
4 *
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 3 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22
23 #include <config.h>
24 #include <libtest/common.h>
25
26 #include <libtest/blobslap_worker.h>
27
28 #include <cassert>
29 #include <cerrno>
30 #include <cstdio>
31 #include <cstdlib>
32 #include <cstring>
33 #include <iostream>
34 #include <signal.h>
35 #include <sys/types.h>
36 #include <sys/wait.h>
37 #include <unistd.h>
38
39 #include <libgearman/gearman.h>
40
41 #ifndef __INTEL_COMPILER
42 #pragma GCC diagnostic ignored "-Wold-style-cast"
43 #endif
44
45 namespace libtest {
46
47 class BlobslapWorker : public Server
48 {
49 private:
50 public:
51 BlobslapWorker(in_port_t port_arg) :
52 Server("localhost", port_arg, "benchmark/blobslap_worker", true)
53 {
54 set_pid_file();
55 }
56
57 pid_t get_pid(bool error_is_ok)
58 {
59 if (pid_file().empty())
60 {
61 Error << "pid_file was empty";
62 return -1;
63 }
64
65 Wait wait(pid_file(), 0);
66
67 if (error_is_ok and not wait.successful())
68 {
69 Error << "Pidfile was not found:" << pid_file();
70 return -1;
71 }
72
73 std::stringstream error_message;
74 pid_t ret= get_pid_from_file(pid_file(), error_message);
75
76 if (error_is_ok and is_pid_valid(ret) == false)
77 {
78 Error << error_message.str();
79 }
80
81 return ret;
82 }
83
84 bool ping()
85 {
86 if (pid_file().empty())
87 {
88 Error << "No pid file available";
89 return false;
90 }
91
92 Wait wait(pid_file(), 0);
93 if (not wait.successful())
94 {
95 Error << "Pidfile was not found:" << pid_file();
96 return false;
97 }
98
99 std::stringstream error_message;
100 pid_t local_pid= get_pid_from_file(pid_file(), error_message);
101 if (is_pid_valid(local_pid) == false)
102 {
103 Error << error_message.str();
104 return false;
105 }
106
107 // Use kill to determine is the process exist
108 if (::kill(local_pid, 0) == 0)
109 {
110 return true;
111 }
112
113 return false;
114 }
115
116 const char *name()
117 {
118 return "blobslap_worker";
119 };
120
121 bool has_port_option() const
122 {
123 return true;
124 }
125
126 bool has_log_file_option() const
127 {
128 return true;
129 }
130
131 bool is_libtool()
132 {
133 return true;
134 }
135
136 bool build(size_t argc, const char *argv[]);
137 };
138
139
140 #include <sstream>
141
142 bool BlobslapWorker::build(size_t argc, const char *argv[])
143 {
144 std::stringstream arg_buffer;
145
146 for (size_t x= 0 ; x < argc ; x++)
147 {
148 add_option(argv[x]);
149 }
150
151 return true;
152 }
153
154 Server *build_blobslap_worker(in_port_t try_port)
155 {
156 return new BlobslapWorker(try_port);
157 }
158
159 } // namespace libtest