| 1 | package Myriad::Registry; | ||
| 2 | |||
| 3 | 2 | ▶ | use Myriad::Class extends => 'IO::Async::Notifier'; |
|
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::Registry - track available methods and subscriptions | ||
| 13 | |||
| 14 | =head1 SYNOPSIS | ||
| 15 | |||
| 16 | =head1 DESCRIPTION | ||
| 17 | |||
| 18 | Used internally within L<Myriad> for keeping track of what services | ||
| 19 | are available, and what they can do. | ||
| 20 | |||
| 21 | =cut | ||
| 22 | |||
| 23 | 0 | ▶ | use Myriad::API; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 24 | |||
| 25 | use Myriad::Exception::Builder category => 'registry'; | ||
| 26 | |||
| 27 | declare_exception ServiceNotFound => ( | ||
| 28 | message => 'Unable to locate the given service', | ||
| 29 | ); | ||
| 30 | declare_exception UnknownClass => ( | ||
| 31 | message => 'Unable to locate the given class for component lookup', | ||
| 32 | ); | ||
| 33 | |||
| 34 | has %rpc; | ||
| 35 | has %service_by_name; | ||
| 36 | has %batch; | ||
| 37 | has %emitter; | ||
| 38 | has %receiver; | ||
| 39 | |||
| 40 | =head2 add_service | ||
| 41 | |||
| 42 | Instantiates and adds a new service to the L</loop>. | ||
| 43 | |||
| 44 | Returns the service instance. | ||
| 45 | |||
| 46 | =cut | ||
| 47 | |||
| 48 | async method add_service (%args) { | ||
| 49 | my $srv = delete $args{service}; | ||
| 50 | weaken(my $myriad = delete $args{myriad}); | ||
| 51 | |||
| 52 | my $pkg = blessed($srv) ? ref $srv : $srv; | ||
| 53 | my $namespace = delete $args{namespace}; | ||
| 54 | my $service_name = delete $args{name} // $self->make_service_name($pkg, $namespace); | ||
| 55 | |||
| 56 | $srv = $srv->new( | ||
| 57 | %args, | ||
| 58 | name => $service_name, | ||
| 59 | myriad => $myriad, | ||
| 60 | ) unless blessed($srv) and $srv->isa('Myriad::Service'); | ||
| 61 | |||
| 62 | # Inject an `$api` instance so that this service can talk | ||
| 63 | # to storage and the outside world | ||
| 64 | # also make sure that storage is initiated | ||
| 65 | $myriad->storage; | ||
| 66 | $myriad->on_start(async sub { | ||
| 67 | $Myriad::Service::SLOT{$pkg}{api}->value($srv) = Myriad::API->new( | ||
| 68 | myriad => $myriad, | ||
| 69 | service_name => $service_name, | ||
| 70 | config => await $myriad->config->service_config($pkg, $service_name), | ||
| 71 | ); | ||
| 72 | }); | ||
| 73 | |||
| 74 | { | ||
| 75 | no strict 'refs'; | ||
| 76 | ${"${pkg}::metrics"}->{name_prefix} = [$service_name]; | ||
| 77 | } | ||
| 78 | |||
| 79 | $rpc{$pkg} ||= {}; | ||
| 80 | $batch{$pkg} ||= {}; | ||
| 81 | $emitter{$pkg} ||= {}; | ||
| 82 | $receiver{$pkg} ||= {}; | ||
| 83 | $log->debugf('Adding service %s package: %s', $service_name, $pkg); | ||
| 84 | $self->loop->add( | ||
| 85 | $srv | ||
| 86 | ); | ||
| 87 | await $srv->load(); | ||
| 88 | my $k = refaddr($srv); | ||
| 89 | weaken($service_by_name{$service_name} = $srv); | ||
| 90 | weaken($myriad->services->{$k} = $srv); | ||
| 91 | |||
| 92 | return; | ||
| 93 | } | ||
| 94 | |||
| 95 | =head2 service_by_name | ||
| 96 | |||
| 97 | Looks up the given service, returning the instance if it exists. | ||
| 98 | |||
| 99 | Will throw an exception if the service cannot be found. | ||
| 100 | |||
| 101 | =cut | ||
| 102 | |||
| 103 | method service_by_name ($k) { | ||
| 104 | return $service_by_name{$k} // Myriad::Exception::Registry::ServiceNotFound->throw( | ||
| 105 | reason => 'service ' . $k . ' not found' | ||
| 106 | ); | ||
| 107 | } | ||
| 108 | |||
| 109 | =head2 add_rpc | ||
| 110 | |||
| 111 | Registers a new RPC method for the given class. | ||
| 112 | |||
| 113 | =cut | ||
| 114 | |||
| 115 | method add_rpc ($pkg, $method, $code, $args) { | ||
| 116 | $rpc{$pkg}{$method} = { | ||
| 117 | code => $code, | ||
| 118 | args => $args, | ||
| 119 | }; | ||
| 120 | } | ||
| 121 | |||
| 122 | =head2 rpc_for | ||
| 123 | |||
| 124 | Returns a hashref of RPC definitions for the given class. | ||
| 125 | |||
| 126 | =cut | ||
| 127 | |||
| 128 | method rpc_for ($pkg) { | ||
| 129 | # Exception if getting for an empty unadded service or totally unknown. | ||
| 130 | return $rpc{$pkg} // Myriad::Exception::Registry::UnknownClass->throw( | ||
| 131 | reason => 'unknown package ' . $pkg | ||
| 132 | ); | ||
| 133 | } | ||
| 134 | |||
| 135 | =head2 add_batch | ||
| 136 | |||
| 137 | Registers a new batch method for the given class. | ||
| 138 | |||
| 139 | =cut | ||
| 140 | |||
| 141 | method add_batch ($pkg, $method, $code, $args) { | ||
| 142 | $batch{$pkg}{$method} = { | ||
| 143 | code => $code, | ||
| 144 | args => $args, | ||
| 145 | }; | ||
| 146 | } | ||
| 147 | |||
| 148 | =head2 batches_for | ||
| 149 | |||
| 150 | Returns a hashref of batch methods for the given class. | ||
| 151 | |||
| 152 | =cut | ||
| 153 | |||
| 154 | method batches_for ($pkg) { | ||
| 155 | # Exception if getting for an empty unadded service or totally unknown. | ||
| 156 | return $batch{$pkg} // Myriad::Exception::Registry::UnknownClass->throw( | ||
| 157 | reason => 'unknown package ' . $pkg | ||
| 158 | ); | ||
| 159 | } | ||
| 160 | |||
| 161 | =head2 add_emitter | ||
| 162 | |||
| 163 | Registers a new emitter method for the given class. | ||
| 164 | |||
| 165 | =cut | ||
| 166 | |||
| 167 | method add_emitter ($pkg, $method, $code, $args) { | ||
| 168 | $args->{channel} //= $method; | ||
| 169 | $emitter{$pkg}{$method} = { | ||
| 170 | code => $code, | ||
| 171 | args => $args, | ||
| 172 | }; | ||
| 173 | } | ||
| 174 | |||
| 175 | =head2 emitters_for | ||
| 176 | |||
| 177 | Returns a hashref of emitter methods for the given class. | ||
| 178 | |||
| 179 | =cut | ||
| 180 | |||
| 181 | method emitters_for ($pkg) { | ||
| 182 | # Exception if getting for an empty unadded service or totally unknown. | ||
| 183 | return $emitter{$pkg} // Myriad::Exception::Registry::UnknownClass->throw( | ||
| 184 | reason => 'unknown package ' . $pkg | ||
| 185 | ); | ||
| 186 | } | ||
| 187 | |||
| 188 | =head2 add_receiver | ||
| 189 | |||
| 190 | Registers a new receiver method for the given class. | ||
| 191 | |||
| 192 | =cut | ||
| 193 | |||
| 194 | method add_receiver ($pkg, $method, $code, $args) { | ||
| 195 | $args->{channel} //= $method; | ||
| 196 | $args->{service} = $self->make_service_name($args->{service}) if $args->{service}; | ||
| 197 | $receiver{$pkg}{$method} = { | ||
| 198 | code => $code, | ||
| 199 | args => $args, | ||
| 200 | }; | ||
| 201 | } | ||
| 202 | |||
| 203 | =head2 receivers_for | ||
| 204 | |||
| 205 | Returns a hashref of receiver methods for the given class. | ||
| 206 | |||
| 207 | =cut | ||
| 208 | |||
| 209 | method receivers_for ($pkg) { | ||
| 210 | # Exception if getting for an empty unadded service or totally unknown. | ||
| 211 | return $receiver{$pkg} // Myriad::Exception::Registry::UnknownClass->throw( | ||
| 212 | reason => 'unknown package ' . $pkg | ||
| 213 | ); | ||
| 214 | } | ||
| 215 | |||
| 216 | =head2 make_service_name | ||
| 217 | |||
| 218 | Reformat a given string to make it combatible with our services | ||
| 219 | naming strategy | ||
| 220 | |||
| 221 | it takes the following args: | ||
| 222 | |||
| 223 | =over 4 | ||
| 224 | |||
| 225 | =item * name - a String with the original name | ||
| 226 | |||
| 227 | =item * namespace - If available the service name will not contain the namespace | ||
| 228 | |||
| 229 | =back | ||
| 230 | |||
| 231 | =cut | ||
| 232 | |||
| 233 | method make_service_name ($name, $namespace = '') { | ||
| 234 | $name =~ s/^\Q$namespace// if length $namespace; | ||
| 235 | return lc($name) =~ s{::}{.}gr | ||
| 236 | } | ||
| 237 | |||
| 238 | 1; | ||
| 239 | |||
| 240 | =head1 AUTHOR | ||
| 241 | |||
| 242 | Deriv Group Services Ltd. C<< DERIV@cpan.org >>. | ||
| 243 | |||
| 244 | See L<Myriad/CONTRIBUTORS> for full details. | ||
| 245 | |||
| 246 | =head1 LICENSE | ||
| 247 | |||
| 248 | Copyright Deriv Group Services Ltd 2020-2022. Licensed under the same terms as Perl itself. | ||
| 249 | |||