Skip to content

Commit 2fce587

Browse files
committed
Support multiple operations for myriad.pl commandline invocation
1 parent 04518d3 commit 2fce587

File tree

3 files changed

+52
-31
lines changed

3 files changed

+52
-31
lines changed

lib/Myriad.pm

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -311,18 +311,7 @@ async method configure_from_argv (@args) {
311311
# At this point, we expect `@args` to contain only the plain
312312
# parameters such as the service name or a request to run an RPC
313313
# 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-
}
314+
await $commands->from_array(@args);
326315

327316
$self->on_start(async sub {
328317
await $config->listen_for_updates;
@@ -785,7 +774,8 @@ async method run () {
785774
# Set shutdown future before starting commands.
786775
$self->shutdown_future();
787776

788-
$commands->run_cmd->retain()->on_fail(sub {
777+
$commands->run_commands->retain()->on_fail(sub ($e, @) {
778+
$log->errorf('Command failed: %s', $e);
789779
$self->shutdown->await();
790780
});
791781

lib/Myriad/Commands.pm

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,25 +27,42 @@ use Myriad::Util::UUID;
2727
use Myriad::Service::Remote;
2828

2929
has $myriad;
30-
has $cmd;
30+
has $commands { [] };
3131

3232
BUILD (%args) {
3333
weaken(
3434
$myriad = $args{myriad} // die 'needs a Myriad parent object'
3535
);
3636
}
3737

38+
async method from_array (@args) {
39+
while(@args) {
40+
my $method = shift @args;
41+
$log->infof('Attempting command %s with remaining %s', $method, \@args);
42+
if($self->can($method)) {
43+
await $self->$method(\@args);
44+
} else {
45+
# Put this back in and try again
46+
unshift @args, $method;
47+
await $self->service(\@args);
48+
}
49+
}
50+
}
51+
3852
=head2 service
3953
4054
Attempts to load and start one or more services.
4155
4256
=cut
4357

44-
async method service (@args) {
58+
async method service ($args) {
4559
my (@modules, $namespace);
46-
while(my $entry = shift @args) {
60+
return unless $args->@*;
61+
62+
my @pending = split /,/, shift(@$args);
63+
while(my $entry = shift @pending) {
4764
if($entry =~ /,/) {
48-
unshift @args, split /,/, $entry;
65+
unshift @pending, split /,/, $entry;
4966
} elsif(my ($base, $hierarchy) = $entry =~ m{^([a-z0-9_:]+)::(\*(?:::\*)*)?$}i) {
5067
$namespace = $base . '::' if $hierarchy;
5168
require Module::Pluggable::Object;
@@ -93,7 +110,7 @@ async method service (@args) {
93110
}
94111
}, foreach => \@services_modules, concurrent => 4);
95112

96-
$cmd = {
113+
push $commands->@*, {
97114
code => async sub {
98115
try {
99116
await fmap0 {
@@ -111,6 +128,7 @@ async method service (@args) {
111128
},
112129
params => {},
113130
};
131+
return;
114132
}
115133

116134
=head2 remote_service
@@ -121,7 +139,7 @@ method remote_service {
121139
return Myriad::Service::Remote->new(
122140
myriad => $myriad,
123141
service_name => $myriad->registry->make_service_name(
124-
$myriad->config->service_name->as_string
142+
$myriad->config->service_name('')->as_string, ''
125143
) // die 'no service name found'
126144
);
127145
}
@@ -133,7 +151,7 @@ method remote_service {
133151
async method rpc ($rpc, @args) {
134152
my $remote_service = $self->remote_service;
135153
die 'RPC args should be passed as (key value) pairs' unless @args % 2 == 0;
136-
$cmd = {
154+
push $commands->@*, {
137155
code => async sub {
138156
my $params = shift;
139157
my ($remote_service, $rpc, $args) = map { $params->{$_} } qw(remote_service rpc args);
@@ -145,7 +163,11 @@ async method rpc ($rpc, @args) {
145163
}
146164
await $myriad->shutdown;
147165
},
148-
params => { rpc => $rpc, args => \@args, remote_service => $remote_service}
166+
params => {
167+
rpc => $rpc,
168+
args => \@args,
169+
remote_service => $remote_service
170+
}
149171
};
150172
}
151173

@@ -158,7 +180,7 @@ async method subscription ($stream, @args) {
158180
my $uuid = Myriad::Util::UUID::uuid();
159181
my $subscription = await $remote_service->subscribe($stream, "$0/$uuid");
160182
$log->infof('Subscribing to: %s | %s', $remote_service->service_name, $stream);
161-
$cmd = {
183+
push $commands->@*, {
162184
code => async sub {
163185
my $params = shift;
164186
my ($subscription, $args) = @{$params}{qw(subscription args)};
@@ -178,7 +200,7 @@ async method subscription ($stream, @args) {
178200

179201
async method storage ($action, $key, $extra = undef) {
180202
my $remote_service = Myriad::Service::Remote->new(myriad => $myriad, service_name => $myriad->registry->make_service_name($myriad->config->service_name->as_string));
181-
$cmd = {
203+
push $commands->@*, {
182204
code => async sub {
183205
my $params = shift;
184206
my ($remote_service, $action, $key, $extra) = map { $params->{$_} } qw(remote_service action key extra);
@@ -192,15 +214,22 @@ async method storage ($action, $key, $extra = undef) {
192214

193215
await $myriad->shutdown;
194216
},
195-
params => { action => $action, key => $key, extra => $extra, remote_service => $remote_service} };
217+
params => { action => $action, key => $key, extra => $extra, remote_service => $remote_service}
218+
};
196219
}
197220

198-
=head2 run_cmd
221+
=head2 run_commands
222+
223+
Execute all the pending commands collected up so far.
199224
200225
=cut
201226

202-
async method run_cmd () {
203-
await $cmd->{code}->($cmd->{params}) if exists $cmd->{code};
227+
async method run_commands () {
228+
await Future->needs_all(
229+
map {;
230+
$_->{code} ? $_->{code}->($_->{params}) : ()
231+
} splice @$commands, 0
232+
);
204233
}
205234

206235
1;

t/commands.t

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,17 +73,19 @@ subtest "service command" => sub {
7373
$metaclass->get_field('$config')->value($myriad) = Myriad::Config->new();
7474

7575
# Wrong Service(module) name
76-
like( exception { wait_for_future( $command->service('Ta-wrong') )->get } , qr/unsupported/, 'Died when passing wrong format name');
77-
like( exception { wait_for_future( $command->service('Ta_wrong') )->get } , qr/not found/, 'Died when passing module that does not exist');
76+
like( exception { wait_for_future( $command->service(['Ta-wrong']) )->get } , qr/unsupported/, 'Died when passing wrong format name');
77+
like( exception { wait_for_future( $command->service(['Ta_wrong']) )->get } , qr/not found/, 'Died when passing module that does not exist');
7878

7979
# Running multiple services
80-
wait_for_future( $command->service('Ta::')->get->{code}->() )->get;
80+
wait_for_future( $command->service(['Ta::']) )->get;
81+
wait_for_future( $command->run_commands )->get;
8182
cmp_deeply(\@added_services_modules, ['Ta::Sibling1', 'Ta::Sibling2'], 'Added both modules');
8283
# Clear it for next test.
8384
@added_services_modules = ();
8485

8586
# Running services under the same namespace
86-
wait_for_future( $command->service('Ta::*')->get->{code}->() )->get;
87+
wait_for_future( $command->service(['Ta::*']) )->get;
88+
wait_for_future( $command->run_commands )->get;
8789
cmp_deeply(\@added_services_modules, ['Ta::Sibling1', 'Ta::Sibling2'], 'Added modules under the namespace');
8890
# Clear it for next test.
8991
@added_services_modules = ();

0 commit comments

Comments
 (0)