Skip to content

Commit 34519fc

Browse files
committedDec 15, 2011
Starman module for pinging, starting, stopping and killing
1 parent 93a2ea2 commit 34519fc

File tree

1 file changed

+161
-0
lines changed

1 file changed

+161
-0
lines changed
 

‎wre/lib/WRE/Starman.pm

+161
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
package WRE::Starman;
2+
3+
#-------------------------------------------------------------------
4+
# WRE is Copyright 2005-2011 Plain Black Corporation.
5+
#-------------------------------------------------------------------
6+
# Please read the legal notices (docs/legal.txt) and the license
7+
# (docs/license.txt) that came with this distribution before using
8+
# this software.
9+
#-------------------------------------------------------------------
10+
# http://www.plainblack.com info@plainblack.com
11+
#-------------------------------------------------------------------
12+
13+
use strict;
14+
use base 'WRE::Service';
15+
use Carp qw(croak);
16+
use Class::InsideOut qw(new);
17+
use HTTP::Request;
18+
use HTTP::Headers;
19+
use LWP::UserAgent;
20+
use WRE::Host;
21+
use Proc::ProcessTable;
22+
23+
=head1 ISA
24+
25+
WRE::Service
26+
27+
=cut
28+
29+
{ # begin inside out object
30+
31+
32+
33+
#-------------------------------------------------------------------
34+
35+
=head getName ()
36+
37+
Returns human readable name.
38+
39+
=cut
40+
41+
sub getName {
42+
return "Starman";
43+
}
44+
45+
#-------------------------------------------------------------------
46+
47+
=head killRunaways ()
48+
49+
Kills any processes that are larger than the maxMemory setting in the config file. Returns the number of processes
50+
killed.
51+
52+
=cut
53+
54+
sub killRunaways {
55+
my $self = shift;
56+
my $killed = 0;
57+
my $processTable = Proc::ProcessTable->new;
58+
my $maxMemory = $self->wreConfig->get("starman/maxMemory");
59+
foreach my $process (@{$processTable->table}) {
60+
next unless ($process->cmndline =~ /starman/);
61+
if ($process->size >= $maxMemory) {
62+
$killed += $process->kill(9);
63+
}
64+
}
65+
return $killed;
66+
}
67+
68+
69+
#-------------------------------------------------------------------
70+
71+
=head2 ping ( )
72+
73+
Returns a 1 if Modperl is running, or a 0 if it is not.
74+
75+
=cut
76+
77+
sub ping {
78+
my $self = shift;
79+
my $starman = $self->wreConfig->get("starman");
80+
my $userAgent = LWP::UserAgent->new;
81+
$userAgent->agent("wre/1.0");
82+
$userAgent->timeout($starman->{connectionTimeout});
83+
my $header = HTTP::Headers->new;
84+
my $url = "http://".$starman->{defaultHostname}.":".$starman->{port}."/";
85+
my $request = HTTP::Request->new( GET => $url, $header);
86+
my $response = $userAgent->request($request);
87+
if ($response->is_success || $response->code eq "401") {
88+
return 1;
89+
}
90+
croak "starman received error code ".$response->code." with message ".$response->error_as_HTML;
91+
return 0;
92+
}
93+
94+
#-------------------------------------------------------------------
95+
96+
=head2 start ( )
97+
98+
Returns a 1 if the start was successful, or a 0 if it was not.
99+
100+
Note: The process that runs this command must be either root or the user specified in the WRE config file.
101+
102+
=cut
103+
104+
sub start {
105+
my $self = shift;
106+
my $count = 0;
107+
my $success = 0;
108+
my $config = $self->wreConfig;
109+
$config->set("wreMonitor/starmanAdministrativelyDown", 0);
110+
my $host = WRE::Host->new(wreConfig=>$config);
111+
unless ($config->get("starman/port") > 1024 || $host->isPrivilegedUser) {
112+
croak "You are not an administrator on this machine so you cannot start services with ports 1-1024.";
113+
}
114+
my $cmd = "";
115+
#start_server --pid-file=/data/wre/var/run/starman.pid --port=8081 --status=/data/wre/var/run/starman.status starman --preload=/data/WebGUI/app.psgi
116+
$cmd = $config->getRoot("/prereqs/bin/start_server")
117+
. " --pid=" . $config->getRoot("var/run/starman.pid")
118+
. " --status=" . $config->getRoot("var/run/starman.status")
119+
. " --port=" . $config->get("starman/port")
120+
. " starman" #Beginning of the starman specific configurations
121+
. " --preload=" . $config->get("webgui/root") . "/app.psgi"
122+
;
123+
`$cmd`; # catch command line output
124+
while ($count++ < 10 && !$success) {
125+
sleep(1);
126+
eval {$success = $self->ping };
127+
}
128+
return $success;
129+
}
130+
131+
#-------------------------------------------------------------------
132+
133+
=head2 stop ( )
134+
135+
Returns a 1 if the stop was successful, or a 0 if it was not.
136+
137+
Note: The process that runs this command must be either root or the user specified in the WRE config file.
138+
139+
=cut
140+
141+
sub stop {
142+
my $self = shift;
143+
my $count = 0;
144+
my $success = 1;
145+
my $config = $self->wreConfig;
146+
$config->set("wreMonitor/starmanAdministrativelyDown", 1);
147+
my $host = WRE::Host->new(wreConfig=>$config);
148+
unless ($config->get("starman/port") > 1024 || $host->isPrivilegedUser) {
149+
croak "You are not an administrator on this machine so you cannot stop services with ports 1-1024.";
150+
}
151+
#kill "TERM", 0;
152+
while ($count++ < 10 && $success) {
153+
sleep(1);
154+
eval { $success = $self->ping };
155+
}
156+
return $success;
157+
}
158+
159+
} # end inside out object
160+
161+
1;

0 commit comments

Comments
 (0)
Please sign in to comment.