forked from plainblack/webgui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spectre.pl
executable file
·262 lines (205 loc) · 6.49 KB
/
spectre.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#!/usr/bin/env perl
#-------------------------------------------------------------------
# WebGUI is Copyright 2001-2009 Plain Black Corporation.
#-------------------------------------------------------------------
# Please read the legal notices (docs/legal.txt) and the license
# (docs/license.txt) that came with this distribution before using
# this software.
#-------------------------------------------------------------------
# http://www.plainblack.com info@plainblack.com
#-------------------------------------------------------------------
our ($webguiRoot);
BEGIN {
$webguiRoot = "..";
unshift (@INC, $webguiRoot."/lib");
}
use Pod::Usage;
use strict;
use warnings;
use Getopt::Long;
use POE::Component::IKC::ClientLite;
use Spectre::Admin;
use WebGUI::Config;
$|=1; # disable output buffering
my $help;
my $shutdown;
my $ping;
my $daemon;
my $run;
my $debug;
my $test;
my $status;
GetOptions(
'help'=>\$help,
'ping'=>\$ping,
'shutdown'=>\$shutdown,
'stop'=>\$shutdown,
'daemon'=>\$daemon,
'start'=>\$daemon,
'debug' =>\$debug,
'status' => \$status,
'run' => \$run,
'test' => \$test
);
pod2usage( verbose => 2 ) if $help;
pod2usage() unless ($ping||$shutdown||$daemon||$run||$test||$status);
require File::Spec;
# Convert to absolute since we'll be changing directory
my $config = WebGUI::Config->new(File::Spec->rel2abs($webguiRoot),"spectre.conf",1);
unless (defined $config) {
print <<STOP;
Cannot open the Spectre config file.
Check that spectre.conf exists, and that it has the proper
privileges to be read by the Spectre server.
STOP
exit;
}
if ($shutdown) {
local $/;
my $pidFileName = $config->get('pidFile');
if (! $pidFileName) {
warn "No pidFile specified in spectre.conf; please add one. Trying /var/run/spectre.pid instead.\n";
$pidFileName = '/var/run/spectre.pid';
}
open my $pidFile, '<', $pidFileName or
die "Unable to open pidFile ($pidFileName) for reading: $!\n";
my $spectrePid = <$pidFile>;
close $pidFile or
die "Unable to close pidFile ($pidFileName) after reading: $!\n";
chomp $spectrePid;
kill 15, $spectrePid;
sleep 1;
kill 9, $spectrePid;
unlink $pidFileName or
die "Unable to remove PID file\n";
}
elsif ($ping) {
my $res = ping();
print "Spectre is Alive!\n" unless $res;
print "Spectre is not responding.\n".$res if $res;
}
elsif ($status) {
print getStatusReport();
}
elsif ($test) {
Spectre::Admin->runTests($config);
}
elsif ($run) {
Spectre::Admin->new($config, $debug);
}
elsif ($daemon) {
my $pidFileName = $config->get('pidFile');
##Write the PID file
if (! $pidFileName) {
warn "No pidFile specified in spectre.conf; please add one. Trying /var/run/spectre.pid instead.\n";
$pidFileName = '/var/run/spectre.pid';
}
if (!ping()) {
die "Spectre is already running.\n";
}
elsif (-e $pidFileName){
die "pidFile $pidFileName already exists\n";
}
#fork and exit(sleep(1) and print((ping())?"Spectre failed to start!\n":"Spectre started successfully!\n")); #Can't have right now.
require POSIX;
fork and exit;
POSIX::setsid();
open my $pidFile, '>', $pidFileName or
die "Unable to open pidFile ($pidFileName) for writing: $!\n";
chdir "/";
open STDIN, "+>", File::Spec->devnull;
open STDOUT, "+>&STDIN";
open STDERR, "+>&STDIN";
fork and exit;
print $pidFile $$."\n";
close $pidFile or
die "Unable to close pidFile ($pidFileName) after writing: $!\n";
Spectre::Admin->new($config, $debug);
}
sub ping {
my $remote = create_ikc_client(
port=>$config->get("port"),
ip=>$config->get("ip"),
name=>rand(100000),
timeout=>10
);
return $POE::Component::IKC::ClientLite::error unless $remote;
my $result = $remote->post_respond('admin/ping');
return $POE::Component::IKC::ClientLite::error unless defined $result;
$remote->disconnect;
undef $remote;
return 0 if ($result eq "pong");
return 1;
}
sub getStatusReport {
my $remote = create_ikc_client(
port=>$config->get("port"),
ip=>$config->get("ip"),
name=>rand(100000),
timeout=>10
);
return $POE::Component::IKC::ClientLite::error unless $remote;
my $result = $remote->post_respond('workflow/getStatus');
return $POE::Component::IKC::ClientLite::error unless defined $result;
$remote->disconnect;
undef $remote;
return $result;
}
__END__
=head1 NAME
spectre - WebGUI's workflow and scheduling.
=head1 SYNOPSIS
spectre {--daemon | --start | --run} [--debug]
spectre --shutdown | --stop
spectre --ping
spectre --status
spectre --test
spectre --help
=head1 DESCRIPTION
S.P.E.C.T.R.E. is the Supervisor of Perplexing Event-handling
Contraptions for Triggering Relentless Executions. It triggers
WebGUI's workflow and scheduling functions.
Spectre's configuration file, B<spectre.conf>, is located under
the WebGUI filesystem hierarchy.
=over
=item B<--daemon>
Starts the Spectre server forking as a background daemon. This
can be done by hand, but it is usually handled by a startup
script.
=item B<--run>
Starts Spectre in the foreground without forking as a daemon.
=item B<--debug>
If this option is specified at startup either in B<--daemon>
or B<--run> mode, Spectre will provide verbose debug to standard
output so that you can see exactly what it's doing.
=item B<--shutdown>
Stops the running Spectre server.
=item B<--ping>
Pings Spectre to see if it is alive. If Spectre is alive, you'll get
confirmation with a message like
Spectre is alive!
If Spectre doesn't seem to be alive, you'll get a message like
Spectre is not responding.
Unable to connect to <IP-address>:<Port>
where B<IP-address> is the IP address and B<Port> is the port number
where Spectre should be listening for connections on according to
B<spectre.conf>.
=item B<--start>
Alias for --daemon.
=item B<--status>
Shows a summary of Spectre's internal status. The summary contains
a tally of suspended, waiting and running WebGUI Workflows.
=item B<--stop>
Alias for --shutdown.
=item B<--test>
Tests whether Spectre can connect to WebGUI. Both Spectre
and the Apache server running WebGUI must be running for this
option to work. It will test the connection between every site
and Spectre, by looking for configuration files in WebGUI's
configuration directory, showing success or failure in each case.
=item B<--help>
Shows this documentation, then exits.
=back
=head1 AUTHOR
Copyright 2001-2009 Plain Black Corporation.
=cut