| 1 | package Myriad; | ||
| 2 | # ABSTRACT: async microservice framework | ||
| 3 | |||
| 4 | 12 | ▶ | use Myriad::Class; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | our $VERSION = '1.001'; | ||
| 7 | our $AUTHORITY = 'cpan:DERIV'; # AUTHORITY | ||
| 8 | |||
| 9 | =encoding utf8 | ||
| 10 | |||
| 11 | =head1 NAME | ||
| 12 | |||
| 13 | Myriad - microservice coördination | ||
| 14 | |||
| 15 | =begin markdown | ||
| 16 | |||
| 17 | [](https://coveralls.io/github/binary-com/perl-Myriad?branch=master) | ||
| 18 | [](https://app.circleci.com/pipelines/github/binary-com/perl-Myriad) | ||
| 19 | [](https://hub.docker.com/r/deriv/myriad) | ||
| 20 | |||
| 21 | =end markdown | ||
| 22 | |||
| 23 | =head1 SYNOPSIS | ||
| 24 | |||
| 25 | use Myriad; | ||
| 26 | Myriad->new->run; | ||
| 27 | |||
| 28 | =head1 DESCRIPTION | ||
| 29 | |||
| 30 | Myriad provides a framework for dealing with asynchronous, microservice-based code. | ||
| 31 | It is intended for use in an environment such as Kubernetes to support horizontal | ||
| 32 | scaling for larger systems. | ||
| 33 | |||
| 34 | Overall this framework encourages - but does not enforce - single-responsibility | ||
| 35 | in each microservice: each service should integrate with at most one external system, | ||
| 36 | and integration should be kept in separate services from business logic or aggregation. | ||
| 37 | This is at odds with common microservice frameworks, so perhaps it would be more accurate | ||
| 38 | to say that this framework is aimed at developing "nanoservices" instead. | ||
| 39 | |||
| 40 | =head2 Do you need this? | ||
| 41 | |||
| 42 | If you expect to be dealing with more traffic than a single server can handle, | ||
| 43 | or you have a development team larger than 30-50 or so, this might be of interest. | ||
| 44 | |||
| 45 | For a smaller system with a handful of users, it's I<probably> overkill! | ||
| 46 | |||
| 47 | =head1 Modules and code layout | ||
| 48 | |||
| 49 | =over 4 | ||
| 50 | |||
| 51 | =item * L<Myriad::Service> - load this in your own code to turn it into a microservice | ||
| 52 | |||
| 53 | =item * L<Myriad::RPC> - the RPC abstraction layer, in C<< $self->rpc >> | ||
| 54 | |||
| 55 | =item * L<Myriad::Storage> - abstraction layer for storage, available as C<< $self->storage >> within services | ||
| 56 | |||
| 57 | =item * L<Myriad::Subscription> - the subscription handling layer, in C<< $self->subscription >> | ||
| 58 | |||
| 59 | =back | ||
| 60 | |||
| 61 | Each of the three abstractions has various implementations. You'd set one on startup | ||
| 62 | and that would provide functionality through the top-level abstraction layer. Service code | ||
| 63 | generally shouldn't need to care which implementation is applied. There may however be cases | ||
| 64 | where transactional behaviour differs between implementations, so there is some basic | ||
| 65 | functionality planned for checking whether RPC/storage/subscription use the same underlying | ||
| 66 | mechanism for transactional safety. | ||
| 67 | |||
| 68 | =head2 Storage | ||
| 69 | |||
| 70 | The L<Myriad::Storage> abstract API is a good starting point here. | ||
| 71 | |||
| 72 | For storage implementations, we have: | ||
| 73 | |||
| 74 | =over 4 | ||
| 75 | |||
| 76 | =item * L<Myriad::Storage::Redis> | ||
| 77 | |||
| 78 | =item * L<Myriad::Storage::PostgreSQL> | ||
| 79 | |||
| 80 | =item * L<Myriad::Storage::Memory> | ||
| 81 | |||
| 82 | =back | ||
| 83 | |||
| 84 | Additional transport mechanisms may be available, see CPAN for details. | ||
| 85 | |||
| 86 | =head2 RPC | ||
| 87 | |||
| 88 | Simple request/response patterns are handled with the L<Myriad::RPC> layer ("remote procedure call"). | ||
| 89 | |||
| 90 | Details on the request are in L<Myriad::RPC::Request> and the response to be sent back is in L<Myriad::RPC::Response>. | ||
| 91 | |||
| 92 | =over 4 | ||
| 93 | |||
| 94 | =item * L<Myriad::RPC::Implementation::Redis> | ||
| 95 | |||
| 96 | =item * L<Myriad::RPC::Implementation::PostgreSQL> | ||
| 97 | |||
| 98 | =item * L<Myriad::RPC::Implementation::Memory> | ||
| 99 | |||
| 100 | =back | ||
| 101 | |||
| 102 | Additional transport mechanisms may be available, see CPAN for details. | ||
| 103 | |||
| 104 | =head2 Subscriptions | ||
| 105 | |||
| 106 | The L<Myriad::Subscription> abstraction layer defines the available API here. | ||
| 107 | |||
| 108 | Subscription implementations include: | ||
| 109 | |||
| 110 | =over 4 | ||
| 111 | |||
| 112 | =item * L<Myriad::Subscription::Implementation::Redis> | ||
| 113 | |||
| 114 | =item * L<Myriad::Subscription::Implementation::PostgreSQL> | ||
| 115 | |||
| 116 | =item * L<Myriad::Subscription::Implementation::Memory> | ||
| 117 | |||
| 118 | =back | ||
| 119 | |||
| 120 | Additional transport mechanisms may be available, see CPAN for details. | ||
| 121 | |||
| 122 | =head2 Transports | ||
| 123 | |||
| 124 | Note that I<some layers don't have implementations for all transports> - MQ for example does not really provide a concept of "storage". | ||
| 125 | |||
| 126 | Each of these implementations is supposed to separate out the logic from the actual transport calls, so there's a separate ::Transport set of classes here: | ||
| 127 | |||
| 128 | =over 4 | ||
| 129 | |||
| 130 | =item * L<Myriad::Transport::Redis> | ||
| 131 | |||
| 132 | =item * L<Myriad::Transport::PostgreSQL> | ||
| 133 | |||
| 134 | =item * L<Myriad::Transport::Memory> | ||
| 135 | |||
| 136 | =back | ||
| 137 | |||
| 138 | which deal with the lower-level interaction with the protocol, connection management and so on. More details on that | ||
| 139 | can be found in L<Myriad::Transport> - but it's typically only useful for people working on the L<Myriad> implementation itself. | ||
| 140 | |||
| 141 | =head2 Other classes | ||
| 142 | |||
| 143 | Documentation for these classes may also be of use: | ||
| 144 | |||
| 145 | =over 4 | ||
| 146 | |||
| 147 | =item * L<Myriad::Exception> - generic errors, provides L<Myriad::Exception/throw> and we recommend that all service errors implement this rôle | ||
| 148 | |||
| 149 | =item * L<Myriad::Plugin> - adds specific functionality to services | ||
| 150 | |||
| 151 | =item * L<Myriad::Bootstrap> - startup used in C<myriad.pl> for providing autorestart and other functionality | ||
| 152 | |||
| 153 | =item * L<Myriad::Service> - base class for a service | ||
| 154 | |||
| 155 | =item * L<Myriad::Registry> - support for registering services and methods within the current process | ||
| 156 | |||
| 157 | =item * L<Myriad::Config> - general config support, commandline/file/storage | ||
| 158 | |||
| 159 | =back | ||
| 160 | |||
| 161 | =head1 METHODS | ||
| 162 | |||
| 163 | =cut | ||
| 164 | |||
| 165 | 12 | ▶ | use curry; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 166 | 12 | ▶ | use Future; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 167 | |||
| 168 | 0 | ▶ | use Myriad::Commands; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 169 | use Myriad::Config; | ||
| 170 | use Myriad::Exception; | ||
| 171 | use Myriad::Exception::InternalError; | ||
| 172 | use Myriad::Registry; | ||
| 173 | use Myriad::RPC; | ||
| 174 | use Myriad::RPC::Client; | ||
| 175 | use Myriad::Storage; | ||
| 176 | use Myriad::Subscription; | ||
| 177 | use Myriad::Transport::HTTP; | ||
| 178 | use Myriad::Transport::Memory; | ||
| 179 | use Myriad::Transport::Redis; | ||
| 180 | |||
| 181 | use Future::IO; | ||
| 182 | use Future::IO::Impl::IOAsync; | ||
| 183 | |||
| 184 | use IO::Async::Loop; | ||
| 185 | |||
| 186 | use Log::Any::Adapter; | ||
| 187 | |||
| 188 | use Net::Async::OpenTracing; | ||
| 189 | |||
| 190 | our $REGISTRY; | ||
| 191 | BEGIN { | ||
| 192 | $REGISTRY = Myriad::Registry->new; | ||
| 193 | } | ||
| 194 | |||
| 195 | # Enable Future time trace | ||
| 196 | $Future::TIMES = 1; | ||
| 197 | |||
| 198 | IO::Async::Loop->new->add( | ||
| 199 | $REGISTRY | ||
| 200 | ); | ||
| 201 | |||
| 202 | # The IO::Async::Loop instance | ||
| 203 | has $loop; | ||
| 204 | # Any coderefs to call when the framework starts | ||
| 205 | has $startup_tasks; | ||
| 206 | # Any coderefs to call when shutdown is requested | ||
| 207 | has $shutdown_tasks; | ||
| 208 | # The Myriad::Config instance | ||
| 209 | has $config; | ||
| 210 | # Registered commands for the management interface | ||
| 211 | has $commands; | ||
| 212 | # Our temporary Net::Async::Redis instance that should | ||
| 213 | # really be abstracted away by the ::Transport and | ||
| 214 | # storage/rpc/subscription abstractions | ||
| 215 | has $redis; | ||
| 216 | # The in-memory "transport" instance | ||
| 217 | has $memory_transport; | ||
| 218 | # The Myriad::RPC instance to serve RPC requests for | ||
| 219 | # the services in this process | ||
| 220 | has $rpc; | ||
| 221 | # The Myriad::RPC::Client instance to send requests | ||
| 222 | # to other services. | ||
| 223 | has $rpc_client; | ||
| 224 | # The Net::Async::HTTP::Server instance for endpoint | ||
| 225 | # requests | ||
| 226 | has $http; | ||
| 227 | # The Myriad::Subscription instance to emit | ||
| 228 | # and listen for events | ||
| 229 | has $subscription; | ||
| 230 | # The Myriad::Storage instance to manage data | ||
| 231 | # stored by the service or access other services data. | ||
| 232 | has $storage; | ||
| 233 | # Future representing run | ||
| 234 | has $run; | ||
| 235 | # Future for passing to things that want to react to | ||
| 236 | # run, anything outside this file | ||
| 237 | has $run_without_cancel; | ||
| 238 | # Future representing shutdown | ||
| 239 | has $shutdown; | ||
| 240 | # Future for passing to things that want to react to | ||
| 241 | # shutdown, pretty much everything outside this file | ||
| 242 | # should only be able to access this one | ||
| 243 | has $shutdown_without_cancel; | ||
| 244 | # The Net::Async::OpenTracing instance | ||
| 245 | has $tracing; | ||
| 246 | # Any service definitions which is added by registry | ||
| 247 | has $services; | ||
| 248 | # Ryu::Source that can be used to recieve commands events | ||
| 249 | has $ryu; | ||
| 250 | |||
| 251 | # Note that we don't use Object::Pad as heavily within the core framework as we | ||
| 252 | # would expect in microservices - this is mainly due to complications regarding | ||
| 253 | # rôle/inheritance behaviour, and at some future point we expect to refactor code | ||
| 254 | # to move more of these classes over to Object::Pad. | ||
| 255 | |||
| 256 | BUILD { | ||
| 257 | $startup_tasks = [ ]; | ||
| 258 | $shutdown_tasks = [ ]; | ||
| 259 | } | ||
| 260 | |||
| 261 | =head2 loop | ||
| 262 | |||
| 263 | Returns the main L<IO::Async::Loop> instance for this process. | ||
| 264 | |||
| 265 | =cut | ||
| 266 | |||
| 267 | method loop { $loop //= IO::Async::Loop->new } | ||
| 268 | |||
| 269 | =head2 services | ||
| 270 | |||
| 271 | Hashref of services that have been added to this instance, | ||
| 272 | as C<name> => C<Myriad::Service> pairs. | ||
| 273 | |||
| 274 | =cut | ||
| 275 | |||
| 276 | method services { $services //= {} } | ||
| 277 | |||
| 278 | =head2 configure_from_argv | ||
| 279 | |||
| 280 | Applies configuration from commandline parameters. | ||
| 281 | |||
| 282 | Expects a list of parameters and applies the following logic for each one: | ||
| 283 | |||
| 284 | =over 4 | ||
| 285 | |||
| 286 | =item * if it contains C<::> and a wildcard C<*>, it's treated as a service module base name, and all | ||
| 287 | modules under that immediate namespace will be loaded | ||
| 288 | |||
| 289 | =item * if it contains C<::>, it's treated as a comma-separated list of service module names to load | ||
| 290 | |||
| 291 | =item * a C<-> prefix is a standard getopt parameter | ||
| 292 | |||
| 293 | =back | ||
| 294 | |||
| 295 | =cut | ||
| 296 | |||
| 297 | async method configure_from_argv (@args) { | ||
| 298 | # Allow config parsing to extract the information | ||
| 299 | $config = Myriad::Config->new( | ||
| 300 | commandline => \@args | ||
| 301 | ); | ||
| 302 | |||
| 303 | $self->setup_logging; | ||
| 304 | $self->setup_tracing; | ||
| 305 | await $self->setup_metrics; | ||
| 306 | |||
| 307 | $commands = Myriad::Commands->new( | ||
| 308 | myriad => $self | ||
| 309 | ); | ||
| 310 | |||
| 311 | # At this point, we expect `@args` to contain only the plain | ||
| 312 | # parameters such as the service name or a request to run an RPC | ||
| 313 | # method. | ||
| 314 | my $method = 'service'; | ||
| 315 | while(@args) { | ||
| 316 | my $arg = shift @args; | ||
| 317 | if($commands->can($arg)) { | ||
| 318 | $method = $arg; | ||
| 319 | await $commands->$method(shift @args, @args); | ||
| 320 | last; | ||
| 321 | } else { | ||
| 322 | await $commands->$method($arg, @args); | ||
| 323 | last; | ||
| 324 | } | ||
| 325 | } | ||
| 326 | |||
| 327 | $self->on_start(async sub { | ||
| 328 | await $config->listen_for_updates; | ||
| 329 | }); | ||
| 330 | } | ||
| 331 | |||
| 332 | method config () { $config } | ||
| 333 | |||
| 334 | =head2 transport | ||
| 335 | |||
| 336 | Returns the L<Myriad::Transport> instance according to the config value. | ||
| 337 | |||
| 338 | it's designed to be used by tests, so be careful before using it in the framework code. | ||
| 339 | |||
| 340 | it takes a single param | ||
| 341 | |||
| 342 | =over 4 | ||
| 343 | |||
| 344 | =item * component - the RPC, Subscription or storage in lower case | ||
| 345 | |||
| 346 | =back | ||
| 347 | |||
| 348 | =cut | ||
| 349 | |||
| 350 | method transport ($component) { | ||
| 351 | my $config_method = "${component}_transport"; | ||
| 352 | my $myriad_method = $config->$config_method->as_string . '_transport'; | ||
| 353 | $self->$myriad_method; | ||
| 354 | } | ||
| 355 | |||
| 356 | =head2 redis | ||
| 357 | |||
| 358 | The L<Net::Async::Redis> (or compatible) instance used for service coördination. | ||
| 359 | |||
| 360 | =cut | ||
| 361 | |||
| 362 | method redis_transport () { | ||
| 363 | unless($redis) { | ||
| 364 | $self->loop->add( | ||
| 365 | $redis = Myriad::Transport::Redis->new( | ||
| 366 | $config ? ( | ||
| 367 | redis_uri => $config->transport_redis->as_string, | ||
| 368 | cluster => ($config->transport_cluster->as_string ? 1 : 0), | ||
| 369 | client_side_cache_size => $config->transport_redis_cache->as_number, | ||
| 370 | max_pool_count => $config->transport_pool_count->as_number, | ||
| 371 | wait_time => $config->transport_wait_time->as_number, | ||
| 372 | ) : () | ||
| 373 | ) | ||
| 374 | ); | ||
| 375 | |||
| 376 | $self->on_start(async method { | ||
| 377 | await $self->redis_transport->start; | ||
| 378 | }); | ||
| 379 | } | ||
| 380 | $redis | ||
| 381 | } | ||
| 382 | |||
| 383 | =head2 memory_transport | ||
| 384 | |||
| 385 | The L<Myriad::Transport::Memory> instance. | ||
| 386 | |||
| 387 | =cut | ||
| 388 | |||
| 389 | method memory_transport () { | ||
| 390 | unless ($memory_transport) { | ||
| 391 | $loop->add( | ||
| 392 | $memory_transport = Myriad::Transport::Memory->new | ||
| 393 | ); | ||
| 394 | } | ||
| 395 | |||
| 396 | $memory_transport; | ||
| 397 | } | ||
| 398 | |||
| 399 | =head2 rpc | ||
| 400 | |||
| 401 | The L<Myriad::RPC> instance to serve RPC requests. | ||
| 402 | |||
| 403 | =cut | ||
| 404 | |||
| 405 | method rpc () { | ||
| 406 | unless($rpc) { | ||
| 407 | $self->loop->add( | ||
| 408 | $rpc = Myriad::RPC->new( | ||
| 409 | transport => $config ? $config->rpc_transport->as_string : '', | ||
| 410 | myriad => $self, | ||
| 411 | ) | ||
| 412 | ); | ||
| 413 | |||
| 414 | $self->on_start(async method { | ||
| 415 | $rpc->start->retain->on_fail($self->$curry::weak(sub { | ||
| 416 | my ($self, $reason) = @_; | ||
| 417 | $self->shutdown_future->fail($reason) | ||
| 418 | unless $self->shutdown_future->is_ready; | ||
| 419 | })); | ||
| 420 | return; | ||
| 421 | }); | ||
| 422 | |||
| 423 | $self->on_shutdown(async method { | ||
| 424 | await $rpc->stop; | ||
| 425 | }); | ||
| 426 | } | ||
| 427 | $rpc | ||
| 428 | } | ||
| 429 | |||
| 430 | =head2 rpc_client | ||
| 431 | |||
| 432 | The L<Myriad::RPC::Client> instance to request other services RPC. | ||
| 433 | |||
| 434 | =cut | ||
| 435 | |||
| 436 | method rpc_client () { | ||
| 437 | unless($rpc_client) { | ||
| 438 | $self->loop->add( | ||
| 439 | $rpc_client = Myriad::RPC::Client->new( | ||
| 440 | # We should use same transport as $rpc. | ||
| 441 | transport => $config ? $config->rpc_transport->as_string : '', | ||
| 442 | myriad => $self, | ||
| 443 | ) | ||
| 444 | ); | ||
| 445 | |||
| 446 | $rpc_client->start->retain->on_fail(sub { | ||
| 447 | $self->shutdown_future->fail(shift); | ||
| 448 | }); | ||
| 449 | |||
| 450 | $self->on_shutdown(async method { | ||
| 451 | await $rpc_client->stop; | ||
| 452 | }); | ||
| 453 | } | ||
| 454 | $rpc_client | ||
| 455 | } | ||
| 456 | |||
| 457 | =head2 http | ||
| 458 | |||
| 459 | The L<Net::Async::HTTP::Server> (or compatible) instance used for health checks | ||
| 460 | and metrics. | ||
| 461 | |||
| 462 | =cut | ||
| 463 | |||
| 464 | method http () { | ||
| 465 | unless($http) { | ||
| 466 | $self->loop->add( | ||
| 467 | $http = Myriad::Transport::HTTP->new | ||
| 468 | ); | ||
| 469 | } | ||
| 470 | $http | ||
| 471 | } | ||
| 472 | |||
| 473 | =head2 subscription | ||
| 474 | |||
| 475 | The L<Myriad::Subscription> instance to manage events. | ||
| 476 | |||
| 477 | =cut | ||
| 478 | |||
| 479 | method subscription () { | ||
| 480 | unless ($subscription) { | ||
| 481 | $self->loop->add( | ||
| 482 | $subscription = Myriad::Subscription->new( | ||
| 483 | transport => $config ? $config->subscription_transport->as_string : '' , | ||
| 484 | myriad => $self, | ||
| 485 | ) | ||
| 486 | ); | ||
| 487 | |||
| 488 | $self->on_start(async method { | ||
| 489 | $subscription->start->retain->on_fail(sub { | ||
| 490 | $self->shutdown_future->fail(shift); | ||
| 491 | }); | ||
| 492 | }); | ||
| 493 | |||
| 494 | $self->on_shutdown(async method { | ||
| 495 | await $subscription->stop; | ||
| 496 | }); | ||
| 497 | } | ||
| 498 | $subscription; | ||
| 499 | } | ||
| 500 | |||
| 501 | =head2 storage | ||
| 502 | |||
| 503 | The L<Myriad::Storage> instance to manage data. | ||
| 504 | |||
| 505 | =cut | ||
| 506 | |||
| 507 | method storage () { | ||
| 508 | unless($storage) { | ||
| 509 | $storage = Myriad::Storage->new( | ||
| 510 | transport => $config ? $config->storage_transport->as_string : '', | ||
| 511 | myriad => $self, | ||
| 512 | ); | ||
| 513 | } | ||
| 514 | $storage | ||
| 515 | } | ||
| 516 | |||
| 517 | =head2 registry | ||
| 518 | |||
| 519 | Returns the common L<Myriad::Registry> representing the current service state. | ||
| 520 | |||
| 521 | =cut | ||
| 522 | |||
| 523 | method registry () { $REGISTRY } | ||
| 524 | |||
| 525 | =head2 add_service | ||
| 526 | |||
| 527 | Instantiates and adds a new service to the L</loop>. | ||
| 528 | |||
| 529 | Returns the service instance. | ||
| 530 | |||
| 531 | =cut | ||
| 532 | |||
| 533 | async method add_service ($srv, %args) { | ||
| 534 | return await $self->registry->add_service( | ||
| 535 | service => $srv, | ||
| 536 | myriad => $self, | ||
| 537 | %args | ||
| 538 | ); | ||
| 539 | } | ||
| 540 | |||
| 541 | =head2 service_by_name | ||
| 542 | |||
| 543 | Looks up the given service, returning the instance if it exists. | ||
| 544 | |||
| 545 | Will throw an exception if the service cannot be found. | ||
| 546 | |||
| 547 | =cut | ||
| 548 | |||
| 549 | method service_by_name ($srv) { | ||
| 550 | return $self->registry->service_by_name( | ||
| 551 | $srv, | ||
| 552 | ); | ||
| 553 | } | ||
| 554 | |||
| 555 | =head2 ryu | ||
| 556 | |||
| 557 | a source to corresponde to any high level events. | ||
| 558 | |||
| 559 | =cut | ||
| 560 | |||
| 561 | method ryu () { | ||
| 562 | unless($ryu) { | ||
| 563 | $self->loop->add( | ||
| 564 | $ryu = Ryu::Async->new | ||
| 565 | ); | ||
| 566 | } | ||
| 567 | $ryu; | ||
| 568 | } | ||
| 569 | |||
| 570 | =head2 shutdown | ||
| 571 | |||
| 572 | Requests shutdown. | ||
| 573 | |||
| 574 | =cut | ||
| 575 | |||
| 576 | async method shutdown () { | ||
| 577 | # if this is regular shutdown $run should be already resolved | ||
| 578 | $run->fail('Myriad was unable to start correctly') if $run && !$run->is_ready; | ||
| 579 | |||
| 580 | my $f = $shutdown | ||
| 581 | or die 'attempting to shut down before we have started, this will not end well'; | ||
| 582 | |||
| 583 | try { | ||
| 584 | # Each service may have its own shutdown or cleanup operations | ||
| 585 | my @shutdown_operations = map { | ||
| 586 | $services->{$_}->shutdown | ||
| 587 | } keys $services->%*; | ||
| 588 | |||
| 589 | # We also have generic tasks, such as transport or RPC/subscription | ||
| 590 | push @shutdown_operations, map { | ||
| 591 | $self->$_ | ||
| 592 | } splice $shutdown_tasks->@*; | ||
| 593 | |||
| 594 | await Future->wait_any( | ||
| 595 | Future->wait_all( | ||
| 596 | @shutdown_operations | ||
| 597 | ), | ||
| 598 | $self->loop->timeout_future(after => 5) | ||
| 599 | ); | ||
| 600 | |||
| 601 | $f->done unless $f->is_ready; | ||
| 602 | } catch ($e) { | ||
| 603 | $f->fail($e) unless $f->is_ready; | ||
| 604 | } | ||
| 605 | return $f->without_cancel; | ||
| 606 | } | ||
| 607 | |||
| 608 | =head2 on_start | ||
| 609 | |||
| 610 | Registers a coderef to be called during startup. | ||
| 611 | The coderef is expected to return a L<Future>. | ||
| 612 | |||
| 613 | =cut | ||
| 614 | |||
| 615 | method on_start ($code) { | ||
| 616 | push $startup_tasks->@*, $code; | ||
| 617 | $self; | ||
| 618 | } | ||
| 619 | |||
| 620 | =head2 on_shutdown | ||
| 621 | |||
| 622 | Registers a coderef to be called during shutdown. | ||
| 623 | |||
| 624 | The coderef is expected to return a L<Future> indicating completion. | ||
| 625 | |||
| 626 | =cut | ||
| 627 | |||
| 628 | method on_shutdown ($code) { | ||
| 629 | push $shutdown_tasks->@*, $code; | ||
| 630 | $self | ||
| 631 | } | ||
| 632 | |||
| 633 | =head2 run_future | ||
| 634 | |||
| 635 | Returns a copy of the run L<Future>. | ||
| 636 | |||
| 637 | This would resolve once the process is running and it's | ||
| 638 | ready to accept requests. | ||
| 639 | |||
| 640 | =cut | ||
| 641 | |||
| 642 | method run_future () { | ||
| 643 | return $run_without_cancel //= ( | ||
| 644 | $run //= $self->loop->new_future->set_label('run'), | ||
| 645 | )->without_cancel; | ||
| 646 | } | ||
| 647 | |||
| 648 | =head2 shutdown_future | ||
| 649 | |||
| 650 | Returns a copy of the shutdown L<Future>. | ||
| 651 | |||
| 652 | This would resolve once the process is about to shut down, | ||
| 653 | triggered by a fault or a Unix signal. | ||
| 654 | |||
| 655 | =cut | ||
| 656 | |||
| 657 | method shutdown_future () { | ||
| 658 | return $shutdown_without_cancel //= ( | ||
| 659 | $shutdown //= $self->loop->new_future->set_label('shutdown') | ||
| 660 | )->without_cancel; | ||
| 661 | } | ||
| 662 | |||
| 663 | =head2 setup_logging | ||
| 664 | |||
| 665 | Prepare for logging. | ||
| 666 | |||
| 667 | =cut | ||
| 668 | |||
| 669 | method setup_logging ($code = undef) { | ||
| 670 | state $logging = do { | ||
| 671 | my $level = $config->log_level; | ||
| 672 | $code ||= sub { | ||
| 673 | state $flushed = do { | ||
| 674 | STDERR->autoflush(1); | ||
| 675 | }; | ||
| 676 | Log::Any::Adapter->import( | ||
| 677 | qw(Stderr), | ||
| 678 | log_level => $level->as_string, | ||
| 679 | ); | ||
| 680 | }; | ||
| 681 | $level->subscribe($code); | ||
| 682 | $code->(); | ||
| 683 | }; | ||
| 684 | return; | ||
| 685 | } | ||
| 686 | |||
| 687 | =head2 setup_tracing | ||
| 688 | |||
| 689 | Prepare L<OpenTracing> collection. | ||
| 690 | |||
| 691 | =cut | ||
| 692 | |||
| 693 | method setup_tracing () { | ||
| 694 | $self->loop->add( | ||
| 695 | $tracing = Net::Async::OpenTracing->new( | ||
| 696 | host => $config->opentracing_host, | ||
| 697 | port => $config->opentracing_port, | ||
| 698 | protocol => 'jaeger', | ||
| 699 | ) | ||
| 700 | ); | ||
| 701 | $self->on_shutdown(async method { | ||
| 702 | await $tracing->sync | ||
| 703 | }); | ||
| 704 | return; | ||
| 705 | } | ||
| 706 | |||
| 707 | =head2 setup_metrics | ||
| 708 | |||
| 709 | Prepare L<Metrics::Any::Adapter> to collect metrics. | ||
| 710 | |||
| 711 | =cut | ||
| 712 | |||
| 713 | async method setup_metrics () { | ||
| 714 | my $adapter = $config->metrics_adapter; | ||
| 715 | my $host = $config->metrics_host; | ||
| 716 | my $port = $config->metrics_port; | ||
| 717 | |||
| 718 | try { | ||
| 719 | await $loop->resolver->getaddrinfo(host => $host->as_string, timeout => 10); | ||
| 720 | } catch ($e) { | ||
| 721 | $log->errorf('metrics: unable to resolve host %s - %s', $host->as_string, $e); | ||
| 722 | $host->set_string('127.0.0.1'); | ||
| 723 | } | ||
| 724 | |||
| 725 | # Metrics::Any::Adapter use a lexical variable to identify | ||
| 726 | # the adapter instance that doesn't allow easy overrides. | ||
| 727 | my $metrics_adapter; | ||
| 728 | { | ||
| 729 | no warnings 'redefine'; | ||
| 730 | *Metrics::Any::Adapter::adapter = sub { | ||
| 731 | return $metrics_adapter if ${^GLOBAL_PHASE} eq 'DESTRUCT'; | ||
| 732 | return $metrics_adapter //= Metrics::Any::Adapter->class_for_type( | ||
| 733 | $adapter->as_string, | ||
| 734 | )->new( | ||
| 735 | host => $host->as_string, | ||
| 736 | port => $port->as_numeric, | ||
| 737 | ) ; | ||
| 738 | }; | ||
| 739 | } | ||
| 740 | |||
| 741 | my $code = sub { | ||
| 742 | # Try to resolve the host first | ||
| 743 | $loop->resolver->getaddrinfo(host => $host->as_string, timeout => 10) | ||
| 744 | ->on_done(sub { | ||
| 745 | undef $metrics_adapter; | ||
| 746 | })->on_fail(sub { | ||
| 747 | $log->errorf('metrics: unable to resolve host %s - %s', $host->as_string, shift); | ||
| 748 | $host->set_string('127.0.0.1'); | ||
| 749 | })->retain(); | ||
| 750 | }; | ||
| 751 | |||
| 752 | $adapter->subscribe($code); | ||
| 753 | $host->subscribe($code); | ||
| 754 | $port->subscribe($code); | ||
| 755 | |||
| 756 | return; | ||
| 757 | } | ||
| 758 | |||
| 759 | =head2 run | ||
| 760 | |||
| 761 | Starts the main loop. | ||
| 762 | |||
| 763 | Applies signal handlers for TERM and QUIT, then starts the loop. | ||
| 764 | |||
| 765 | =cut | ||
| 766 | |||
| 767 | async method run () { | ||
| 768 | # Initiate the run future. | ||
| 769 | $self->run_future(); | ||
| 770 | |||
| 771 | for my $signal (qw(TERM INT QUIT)) { | ||
| 772 | $self->loop->attach_signal($signal => $self->$curry::weak(method { | ||
| 773 | $log->infof("%s received, exit", $signal); | ||
| 774 | $self->shutdown->await; | ||
| 775 | })) | ||
| 776 | } | ||
| 777 | |||
| 778 | try { | ||
| 779 | # Run the startup tasks, order is imporatant | ||
| 780 | for my $task ($startup_tasks->@*) { | ||
| 781 | await $self->$task; | ||
| 782 | } | ||
| 783 | } catch ($e) { | ||
| 784 | die "Startup tasks failed - $e"; | ||
| 785 | } | ||
| 786 | |||
| 787 | # Set shutdown future before starting commands. | ||
| 788 | $self->shutdown_future(); | ||
| 789 | |||
| 790 | $commands->run_cmd->retain()->on_fail(sub { | ||
| 791 | $self->shutdown->await(); | ||
| 792 | }); | ||
| 793 | |||
| 794 | # Process is running but there is a possibility | ||
| 795 | # the command above failed. | ||
| 796 | $run->done unless $run->is_ready; | ||
| 797 | |||
| 798 | await $self->shutdown_future; | ||
| 799 | } | ||
| 800 | |||
| 801 | 1; | ||
| 802 | |||
| 803 | __END__ | ||