| 1 | package Service::Engine::Admin; | ||
| 2 | |||
| 3 | use 5.010; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use Carp; | ||
| 7 | use Service::Engine; | ||
| 8 | use Data::Dumper; | ||
| 9 | use Module::Runtime qw(require_module); | ||
| 10 | use Service::Engine::API::Server; | ||
| 11 | use threads; | ||
| 12 | use threads::shared; | ||
| 13 | |||
| 14 | our $Config; | ||
| 15 | our $Log; | ||
| 16 | our $EngineName; | ||
| 17 | our $Commands = {}; | ||
| 18 | our $CommandList = ''; | ||
| 19 | our $Server; | ||
| 20 | our $Threads; | ||
| 21 | our $Engine; | ||
| 22 | our $Health; | ||
| 23 | |||
| 24 | share($Health); | ||
| 25 | share($Commands); | ||
| 26 | share($CommandList); | ||
| 27 | |||
| 28 | # load desired classes | ||
| 29 | # Service::Engine::Admin:* | ||
| 30 | |||
| 31 | sub new { | ||
| 32 | |||
| 33 | my ($class,$options) = @_; | ||
| 34 | |||
| 35 | # set some defaults | ||
| 36 | my $attributes = {'methods'=>{}}; | ||
| 37 | |||
| 38 | # load options | ||
| 39 | if (ref($options) eq 'HASH') { | ||
| 40 | foreach my $option (keys %{$options}) { | ||
| 41 | $attributes->{$option} = $options->{$option}; | ||
| 42 | } | ||
| 43 | } | ||
| 44 | |||
| 45 | my $self = bless $attributes, $class; | ||
| 46 | |||
| 47 | # pull in some Service::Engine globals | ||
| 48 | $Config = $Service::Engine::Config; | ||
| 49 | $Log = $Service::Engine::Log; | ||
| 50 | $EngineName = $Service::Engine::EngineName; | ||
| 51 | $Threads = $Service::Engine::Threads; | ||
| 52 | $Health = $Service::Engine::Health; | ||
| 53 | $Engine = $Service::Engine::Engine; | ||
| 54 | |||
| 55 | # create our admin methods | ||
| 56 | # these will be autoloaded from Service::Engine like this my $method = $Admin->methodname(); | ||
| 57 | # this works by adding the connection methods to $attributes->{'methods'}->{methodname} | ||
| 58 | # save the method label to $Methods->{$label} = $type; | ||
| 59 | |||
| 60 | if (ref($Config->get_config('admin')) eq 'HASH') { | ||
| 61 | |||
| 62 | if ($Config->get_config('admin')->{'enabled'}) { | ||
| 63 | foreach my $service (keys $Config->get_config('admin')->{'modules'}) { | ||
| 64 | |||
| 65 | my $service_conf = $Config->get_config('admin')->{'modules'}->{$service}; | ||
| 66 | |||
| 67 | $Log->log({msg=>Dumper($service_conf),level=>3}); | ||
| 68 | |||
| 69 | if ($service_conf->{'enabled'}) { | ||
| 70 | $Log->log({msg=>"loading $service",level=>2}); | ||
| 71 | my $package = 'Service::Engine::Admin::' . ucfirst lc $service; | ||
| 72 | |||
| 73 | if ($service_conf->{'module_name'}) { | ||
| 74 | $package = $EngineName . '::Modules::' . $service_conf->{'module_name'}; | ||
| 75 | } | ||
| 76 | my $res = eval { require_module($package) }; | ||
| 77 | if ($@) { | ||
| 78 | $Log->log({msg=>"error loading $service: $@",level=>1}); | ||
| 79 | } else { | ||
| 80 | my $obj = $package->new($self,$service_conf->{'options'}); | ||
| 81 | } | ||
| 82 | } | ||
| 83 | |||
| 84 | } | ||
| 85 | |||
| 86 | # set up commands | ||
| 87 | $self->add_command({'module'=>$Threads, 'method'=>'add_thread', 'label'=>'AddThread'}); | ||
| 88 | $self->add_command({'module'=>$self, 'method'=>'stop_engine', 'label'=>'Stop Engine'}); | ||
| 89 | $self->add_command({'module'=>$Engine, 'method'=>'start_selector', 'label'=>'Start Engine'}); | ||
| 90 | |||
| 91 | } | ||
| 92 | |||
| 93 | } | ||
| 94 | |||
| 95 | return $self; | ||
| 96 | |||
| 97 | } | ||
| 98 | |||
| 99 | sub start_admin_server { | ||
| 100 | |||
| 101 | my $self = shift; | ||
| 102 | |||
| 103 | if ($Config->get_config('admin')->{'enabled'}) { | ||
| 104 | my $host_ip = $Config->get_config('admin')->{'host_ip'}; | ||
| 105 | my $host_port = $Config->get_config('admin')->{'host_port'}; | ||
| 106 | $host_ip ||= '127.0.0.1'; | ||
| 107 | $host_port ||= '42142'; | ||
| 108 | |||
| 109 | $Server = Service::Engine::Admin::Server->new('host' => $host_ip, 'port' => $host_port); | ||
| 110 | $Server->run; | ||
| 111 | } | ||
| 112 | |||
| 113 | } | ||
| 114 | |||
| 115 | sub expand { | ||
| 116 | |||
| 117 | my $self = shift; | ||
| 118 | my $stub = shift; | ||
| 119 | |||
| 120 | my $commands = $CommandList; | ||
| 121 | my @commands = split /\n/, $commands; | ||
| 122 | |||
| 123 | foreach my $command (@commands) { | ||
| 124 | return "!$command" unless $command !~ /^$stub/i; | ||
| 125 | } | ||
| 126 | |||
| 127 | return $stub; | ||
| 128 | |||
| 129 | } | ||
| 130 | |||
| 131 | sub command_list { | ||
| 132 | my $self = shift; | ||
| 133 | return $CommandList; | ||
| 134 | } | ||
| 135 | |||
| 136 | sub add_command { | ||
| 137 | my ($self,$command) = @_; | ||
| 138 | |||
| 139 | if (ref($command) ne 'HASH') { | ||
| 140 | $Log->log({msg=>"invalid command format - must be a hash:" . Dumper($command),level=>2}); | ||
| 141 | return; | ||
| 142 | } | ||
| 143 | |||
| 144 | if (!$command->{'module'} || !$command->{'method'} || !$command->{'label'}) { | ||
| 145 | $Log->log({msg=>"invalid command format - missing module, method or label:" . Dumper($command),level=>2}); | ||
| 146 | return; | ||
| 147 | } | ||
| 148 | |||
| 149 | warn($command->{'label'} . ':' . Dumper($command->{'module'})); | ||
| 150 | |||
| 151 | $Commands->{$command->{'label'}} = shared_clone({'module'=>$command->{'module'}, 'method'=>$command->{'method'}}); | ||
| 152 | my @commands = split /\n/, $CommandList; | ||
| 153 | push @commands, $command->{'label'}; | ||
| 154 | $CommandList = join "\n", sort @commands; | ||
| 155 | |||
| 156 | return; | ||
| 157 | } | ||
| 158 | |||
| 159 | sub stop_engine { | ||
| 160 | # we do this here because it blocks while it waits; that would be bad in the Engine | ||
| 161 | my ($self,$fh) = @_; | ||
| 162 | |||
| 163 | $fh->write("shutting down\n") unless !$fh; | ||
| 164 | my $stop = $Engine->stop_selector($fh); | ||
| 165 | $fh->write("waiting for the backlog to finish processing\n") unless !$fh; | ||
| 166 | |||
| 167 | # wait for it | ||
| 168 | ###>>>TO DO - we need to do this in a separate thread so it doesn't block admin entirely | ||
| 169 | while ($Threads->get_queue_count()){}; | ||
| 170 | |||
| 171 | $fh->write("All done.") unless !$fh; | ||
| 172 | |||
| 173 | if (!$fh) { | ||
| 174 | return("{'status':'ok'}"); | ||
| 175 | } | ||
| 176 | |||
| 177 | } | ||
| 178 | |||
| 179 | our $AUTOLOAD; | ||
| 180 | sub AUTOLOAD { | ||
| 181 | my $self = shift; | ||
| 182 | # Remove qualifier from original method name... | ||
| 183 | my $called = $AUTOLOAD =~ s/.*:://r; | ||
| 184 | # Is there an attribute of that name? | ||
| 185 | # check for an expansion | ||
| 186 | $called = $self->expand($called); | ||
| 187 | |||
| 188 | if ($called =~ /^\!/) { # this is a command | ||
| 189 | $called =~ s/^\!//; | ||
| 190 | return $Commands->{$called}; | ||
| 191 | } else { | ||
| 192 | print "Hmm. $called? Available options are: \n$CommandList\n> " | ||
| 193 | unless exists $self->{'methods'}->{$called}; | ||
| 194 | # If so, return it... | ||
| 195 | return $self->{'methods'}->{$called}; | ||
| 196 | } | ||
| 197 | |||
| 198 | } | ||
| 199 | |||
| 200 | 1; |