lib/Myriad/Service.pm

filter: Statementstmt 88.8% 16 / 18 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod n/a totaltotal 91.6% 22 / 24 CC 1 SCAR 0.5
CC
1
Cov
89%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« lib/Myriad/RPC/Implementation/Memory.pm ↑ Summary lib/Myriad/RPC/Implementation/Redis.pm »
1 package Myriad::Service;
2  
3 5 ▶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::Service - starting point for building microservices
13  
14 =head1 SYNOPSIS
15  
16 package Example::Service;
17 use Myriad::Service;
18  
19 async method startup {
20 $log->infof('Starting %s', __PACKAGE__);
21 }
22  
23 # Trivial RPC call, provides the `example` method
24 async method example : RPC {
25 return { ok => 1 };
26 }
27  
28 # Slightly more useful - return all the original parameters.
29 # Due to an unfortunate syntactical choice in core Perl, the
30 # whitespace before the (%args) is *mandatory*, without that
31 # you're actually passing (%args) to the RPC attribute...
32 async method echo : RPC (%args) {
33 return \%args;
34 }
35  
36 # Default internal diagnostics checks are performed automatically,
37 # this method is called after the microservice status such as Redis
38 # connections, exception status etc. are verified
39 async method diagnostics ($level) {
40 my ($self, $level) = @_;
41 return 'ok';
42 }
43  
44 1;
45  
46 =head1 DESCRIPTION
47  
48 Since this is a framework, by default it attempts to enforce a common standard on all microservice
49 modules. See L<Myriad::Class> for the details.
50  
51 The calling package will be marked as an L<Object::Pad> class, providing the
52 L<Object::Pad/method>, L<Object::Pad/has> and C<async method> keywords.
53  
54 This also makes available a L<Log::Any> instance in the C<$log> package variable,
55 and for L<OpenTracing::Any> support you get C<$tracer> as an L<OpenTracing::Tracer>
56 instance.
57  
58 =head2 Custom language features
59  
60 B<You can disable the language behaviour defaults> by specifying C<< :custom >> as an L</import> parameter:
61  
62 package Example::Service;
63 use strict;
64 use warnings;
65 use Myriad::Service qw(:custom);
66 use Log::Any qw($log);
67  
68 This will only apply the L<Myriad::Service::Implementation> parent class, and avoid
69 any changes to syntax or other features.
70  
71 =cut
72  
73 5 ▶use Heap;
Subroutine BEGIN: called CC=1 SCAR=0.0
74 5 ▶use IO::Async::Notifier;
Subroutine BEGIN: called CC=1 SCAR=0.0
75 5 ▶use IO::Async::SSL;
Subroutine BEGIN: called CC=1 SCAR=0.0
76 5 ▶use Net::Async::HTTP;
Subroutine BEGIN: called CC=1 SCAR=0.0
77  
78 0 ▶use Myriad::Service::Implementation;
Subroutine BEGIN: called CC=1 SCAR=2.6
79 use Myriad::Config;
80  
81 use Myriad::Exception::Builder category => 'service';
82  
83 declare_exception SecureDefaultValue => (
84 message => 'Secure configuration parameter may not have a default value'
85 );
86  
87 our %SLOT;
88  
89 sub import ($called_on, @args) {
90 my $class = __PACKAGE__;
91 my $pkg = caller(0);
92 $INC{($pkg =~ s{::}{/}gr) . '.pm'} //= 1;
93  
94 if(grep { $_ eq ':custom' } @args) {
95 push @{$pkg . '::ISA' }, 'Myriad::Service::Implementation', 'Myriad::Service';
96 return;
97 }
98  
99 my $version = 1;
100 if(@args and $args[0] =~ /^:v([0-9]+)/) {
101 $version = $1;
102 }
103  
104 my $meta = Myriad::Class->import(
105 ":v$version",
106 target => $pkg,
107 extends => 'Myriad::Service::Implementation',
108 );
109  
110 # Now we populate various slots, to be filled in when instantiating.
111 # Currently we have `$api`, but might be helpful to provide `$storage`
112 # and others directly here.
113 $SLOT{$pkg} = {
114 map { $_ => $meta->add_field('$' . $_) } qw(
115 api
116 )
117 };
118  
119 {
120 no strict 'refs';
121  
122 push @{$pkg . '::ISA' }, 'Myriad::Service';
123  
124 *{$pkg . '::config'} = sub {
125 my ($varname, %args) = @_;
126 die 'config name is required' unless $varname;
127  
128 Myriad::Exception::Service::SecureDefaultValue->throw
129 if defined($args{default}) and $args{secure};
130  
131 $Myriad::Config::SERVICES_CONFIG{$pkg}->{$varname} = \%args;
132  
133 $log->tracef("registered config %s for service %s", $varname, $pkg);
134 }
135 }
136 return;
137 }
138  
139 1;
140  
141 =head1 AUTHOR
142  
143 Deriv Group Services Ltd. C<< DERIV@cpan.org >>.
144  
145 See L<Myriad/CONTRIBUTORS> for full details.
146  
147 =head1 LICENSE
148  
149 Copyright Deriv Group Services Ltd 2020-2022. Licensed under the same terms as Perl itself.
150  
« lib/Myriad/RPC/Implementation/Memory.pm ↑ Summary lib/Myriad/RPC/Implementation/Redis.pm »