| 1 | package Myriad::Commands; | ||
| 2 | |||
| 3 | 13 | ▶ | use Myriad::Class; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | |||
| 5 | our $VERSION = '1.001'; # VERSION | ||
| 6 | our $AUTHORITY = 'cpan:DERIV'; # AUTHORITY | ||
| 7 | |||
| 8 | =encoding utf8 | ||
| 9 | |||
| 10 | =head1 NAME | ||
| 11 | |||
| 12 | Myriad::Commands - common abstraction for user interface commands | ||
| 13 | |||
| 14 | =head1 DESCRIPTION | ||
| 15 | |||
| 16 | Provides top-level commands, such as loading a service or making an RPC call. | ||
| 17 | |||
| 18 | =cut | ||
| 19 | |||
| 20 | 13 | ▶ | use Unicode::UTF8 qw(decode_utf8); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 21 | |||
| 22 | 13 | ▶ | use Future::Utils qw(fmap0); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 23 | |||
| 24 | 13 | ▶ | use Module::Runtime qw(require_module); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 25 | |||
| 26 | 13 | ▶ | use Myriad::Util::UUID; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 27 | 0 | ▶ | use Myriad::Service::Remote; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 28 | |||
| 29 | has $myriad; | ||
| 30 | has $cmd; | ||
| 31 | |||
| 32 | BUILD (%args) { | ||
| 33 | weaken( | ||
| 34 | $myriad = $args{myriad} // die 'needs a Myriad parent object' | ||
| 35 | ); | ||
| 36 | } | ||
| 37 | |||
| 38 | =head2 service | ||
| 39 | |||
| 40 | Attempts to load and start one or more services. | ||
| 41 | |||
| 42 | =cut | ||
| 43 | |||
| 44 | async method service (@args) { | ||
| 45 | my (@modules, $namespace); | ||
| 46 | while(my $entry = shift @args) { | ||
| 47 | if($entry =~ /,/) { | ||
| 48 | unshift @args, split /,/, $entry; | ||
| 49 | } elsif(my ($base, $hierarchy) = $entry =~ m{^([a-z0-9_:]+)::(\*(?:::\*)*)?$}i) { | ||
| 50 | $namespace = $base . '::' if $hierarchy; | ||
| 51 | require Module::Pluggable::Object; | ||
| 52 | my $search = Module::Pluggable::Object->new( | ||
| 53 | search_path => [ $base ] | ||
| 54 | ); | ||
| 55 | push @modules, $search->plugins; | ||
| 56 | } elsif($entry =~ /^[a-z0-9_:]+[a-z0-9_]$/i) { | ||
| 57 | push @modules, $entry; | ||
| 58 | } else { | ||
| 59 | die 'unsupported ' . $entry; | ||
| 60 | } | ||
| 61 | } | ||
| 62 | |||
| 63 | # Load modules to compile | ||
| 64 | my @services_modules; | ||
| 65 | for my $module (@modules) { | ||
| 66 | try { | ||
| 67 | require_module($module); | ||
| 68 | next unless $module->isa('Myriad::Service'); | ||
| 69 | die 'loaded ' . $module . ' but it cannot ->new?' unless $module->can('new'); | ||
| 70 | push @services_modules, $module; | ||
| 71 | } catch ($e) { | ||
| 72 | Future::Exception->throw(sprintf 'Service module %s not found', $module) if $e =~ /Can't locate.*(\Q$module\E)/; | ||
| 73 | Future::Exception->throw(sprintf 'Failed to load module for service %s - %s', $module, $e); | ||
| 74 | } | ||
| 75 | } | ||
| 76 | |||
| 77 | # Load services into Myriad but don't start them yet | ||
| 78 | |||
| 79 | await fmap0(async sub { | ||
| 80 | my ($module) = @_; | ||
| 81 | try { | ||
| 82 | # No need to pass namespaces here | ||
| 83 | my $default_service_name = $myriad->registry->make_service_name($module,''); | ||
| 84 | |||
| 85 | # Check if we should override the servicename | ||
| 86 | if (my $service_name = $myriad->config->service_name($default_service_name)) { | ||
| 87 | await $myriad->add_service($module, namespace => $namespace, name => $service_name); | ||
| 88 | } else { | ||
| 89 | await $myriad->add_service($module, namespace => $namespace); | ||
| 90 | } | ||
| 91 | } catch ($e) { | ||
| 92 | Future::Exception->throw(sprintf 'Failed to add service %s - %s', $module, $e); | ||
| 93 | } | ||
| 94 | }, foreach => \@services_modules, concurrent => 4); | ||
| 95 | |||
| 96 | $cmd = { | ||
| 97 | code => async sub { | ||
| 98 | try { | ||
| 99 | await fmap0 { | ||
| 100 | my $service = shift; | ||
| 101 | $log->infof('Starting service %s', $service->service_name); | ||
| 102 | $service->start->transform(fail => sub { | ||
| 103 | return $service->service_name . ' : ' . shift; | ||
| 104 | }); | ||
| 105 | } foreach => [values $myriad->services->%*], concurrent => 4; | ||
| 106 | |||
| 107 | } catch($e) { | ||
| 108 | $log->warnf('Failed to start services, error: %s', $e); | ||
| 109 | await $myriad->shutdown; | ||
| 110 | } | ||
| 111 | }, | ||
| 112 | params => {}, | ||
| 113 | }; | ||
| 114 | } | ||
| 115 | |||
| 116 | =head2 remote_service | ||
| 117 | |||
| 118 | =cut | ||
| 119 | |||
| 120 | method remote_service { | ||
| 121 | return Myriad::Service::Remote->new( | ||
| 122 | myriad => $myriad, | ||
| 123 | service_name => $myriad->registry->make_service_name( | ||
| 124 | $myriad->config->service_name->as_string | ||
| 125 | ) // die 'no service name found' | ||
| 126 | ); | ||
| 127 | } | ||
| 128 | |||
| 129 | =head2 rpc | ||
| 130 | |||
| 131 | =cut | ||
| 132 | |||
| 133 | async method rpc ($rpc, @args) { | ||
| 134 | my $remote_service = $self->remote_service; | ||
| 135 | die 'RPC args should be passed as (key value) pairs' unless @args % 2 == 0; | ||
| 136 | $cmd = { | ||
| 137 | code => async sub { | ||
| 138 | my $params = shift; | ||
| 139 | my ($remote_service, $rpc, $args) = map { $params->{$_} } qw(remote_service rpc args); | ||
| 140 | try { | ||
| 141 | my $response = await $remote_service->call_rpc($rpc, @$args); | ||
| 142 | $log->infof('RPC response is %s', $response); | ||
| 143 | } catch ($e) { | ||
| 144 | $log->warnf('RPC command failed due: %s', $e); | ||
| 145 | } | ||
| 146 | await $myriad->shutdown; | ||
| 147 | }, | ||
| 148 | params => { rpc => $rpc, args => \@args, remote_service => $remote_service} | ||
| 149 | }; | ||
| 150 | } | ||
| 151 | |||
| 152 | =head2 subscription | ||
| 153 | |||
| 154 | =cut | ||
| 155 | |||
| 156 | async method subscription ($stream, @args) { | ||
| 157 | my $remote_service = $self->remote_service; | ||
| 158 | my $uuid = Myriad::Util::UUID::uuid(); | ||
| 159 | my $subscription = await $remote_service->subscribe($stream, "$0/$uuid"); | ||
| 160 | $log->infof('Subscribing to: %s | %s', $remote_service->service_name, $stream); | ||
| 161 | $cmd = { | ||
| 162 | code => async sub { | ||
| 163 | my $params = shift; | ||
| 164 | my ($subscription, $args) = @{$params}{qw(subscription args)}; | ||
| 165 | $subscription->each(sub { | ||
| 166 | my $info = shift; | ||
| 167 | $log->infof('Subscription event received: %s', $info->{data}); | ||
| 168 | })->completed; | ||
| 169 | }, | ||
| 170 | params => { subscription => $subscription, args => \@args} | ||
| 171 | }; | ||
| 172 | |||
| 173 | } | ||
| 174 | |||
| 175 | =head2 storage | ||
| 176 | |||
| 177 | =cut | ||
| 178 | |||
| 179 | async method storage ($action, $key, $extra = undef) { | ||
| 180 | my $remote_service = Myriad::Service::Remote->new(myriad => $myriad, service_name => $myriad->registry->make_service_name($myriad->config->service_name->as_string)); | ||
| 181 | $cmd = { | ||
| 182 | code => async sub { | ||
| 183 | my $params = shift; | ||
| 184 | my ($remote_service, $action, $key, $extra) = map { $params->{$_} } qw(remote_service action key extra); | ||
| 185 | |||
| 186 | try { | ||
| 187 | my $response = await $remote_service->storage->$action($key, $extra // () ); | ||
| 188 | $log->infof('Storage resposne is: %s', $response); | ||
| 189 | } catch ($e) { | ||
| 190 | $log->warnf('Error: %s', $e); | ||
| 191 | } | ||
| 192 | |||
| 193 | await $myriad->shutdown; | ||
| 194 | }, | ||
| 195 | params => { action => $action, key => $key, extra => $extra, remote_service => $remote_service} }; | ||
| 196 | } | ||
| 197 | |||
| 198 | =head2 run_cmd | ||
| 199 | |||
| 200 | =cut | ||
| 201 | |||
| 202 | async method run_cmd () { | ||
| 203 | await $cmd->{code}->($cmd->{params}) if exists $cmd->{code}; | ||
| 204 | } | ||
| 205 | |||
| 206 | 1; | ||
| 207 | |||
| 208 | =head1 AUTHOR | ||
| 209 | |||
| 210 | Deriv Group Services Ltd. C<< DERIV@cpan.org >>. | ||
| 211 | |||
| 212 | See L<Myriad/CONTRIBUTORS> for full details. | ||
| 213 | |||
| 214 | =head1 LICENSE | ||
| 215 | |||
| 216 | Copyright Deriv Group Services Ltd 2020-2022. Licensed under the same terms as Perl itself. | ||