]> jfr.im git - irc/SurrealServices/srsv.git/blob - branches/erry-devel/SrSv/RunLevel.pm
initial commit of erry's Insp work.
[irc/SurrealServices/srsv.git] / branches / erry-devel / SrSv / RunLevel.pm
1 # This file is part of SurrealServices.
2 #
3 # SurrealServices is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
7 #
8 # SurrealServices is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with SurrealServices; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16
17 package SrSv::RunLevel;
18
19 =head1 NAME
20
21 SrSv::RunLevel - Control system state.
22
23 =cut
24
25 use strict;
26
27 use Exporter 'import';
28
29 BEGIN {
30 my %constants = (
31 ST_NORMAL => 1,
32 ST_SHUTDOWN => 2,
33 );
34
35 our @EXPORT_OK = (qw($runlevel main_shutdown emerg_shutdown), keys(%constants));
36 our %EXPORT_TAGS = (levels => [keys(%constants)]);
37
38 require constant; import constant (\%constants);
39 }
40
41 # FIXME: Uncommenting this breaks $ircd_ready for some reason.
42 #use SrSv::IRCd::IO qw(ircd_disconnect);
43 use SrSv::Process::Worker qw(ima_worker shutdown_all_workers kill_all_workers call_in_parent);
44 use SrSv::Timer 'stop_timer';
45
46 our $runlevel = ST_NORMAL;
47
48 sub main_shutdown() {
49 call_in_parent(__PACKAGE__.'::_main_shutdown');
50 }
51
52 sub emerg_shutdown() {
53 $runlevel = ST_SHUTDOWN;
54 stop_timer;
55 shutdown_all_workers sub { exit; };
56
57 Event->timer(after => 5, cb => sub {
58 kill_all_workers;
59
60 exit;
61 });
62 }
63
64 sub _main_shutdown() {
65 ircd::agent_quit_all("Shutting down.");
66
67 emerg_shutdown;
68 }
69
70 1;
71
72 __END__
73
74 =head1 SYNOPSIS
75
76 use SrSv::RunLevel;
77
78 main_shutdown;
79