X-Git-Url: http://dxcluster.net/gitweb/gitweb.cgi?a=blobdiff_plain;f=perl%2FDXCommandmode.pm;h=cec310961b3feda189d3059536cf369a142fa0c9;hb=0ce79a125db0acb043fceaa641d8b3a9eae71a41;hp=7147b35c24ad6d1f1bab960b4be7a25d9ea583d9;hpb=0ba8b8bf6f01f405661aef65a070a8557e98dd3b;p=spider.git diff --git a/perl/DXCommandmode.pm b/perl/DXCommandmode.pm index 7147b35c..cec31096 100644 --- a/perl/DXCommandmode.pm +++ b/perl/DXCommandmode.pm @@ -38,6 +38,10 @@ use VE7CC; use DXXml; use AsyncMsg; +use Mojo::IOLoop; +use Mojo::IOLoop::ForkCall; +use Mojo::UserAgent; + use strict; use vars qw(%Cache %cmd_cache $errstr %aliases $scriptbase %nothereslug $maxbadcount $msgpolltime $default_pagelth $cmdimportdir); @@ -1241,5 +1245,57 @@ sub send_motd } $self->send_file($motd) if -e $motd; } + +# Punt off a long running command into a separate process +# +# Hhis is called from commands to run some potentially long running +# function. The process forks and then runs the function and returns +# the result back to the cmd. +# +# call: $self->spawn_cmd(\, [cb => sub{...}], [prefix => "cmd> "], [progress => 0|1], [args => [...]]); +sub spawn_cmd +{ + my $self = shift; + my $cmdref = shift; + my $call = $self->{call}; + my %args = @_; + my @out; + + my $cb = delete $args{cb}; + my $prefix = delete $args{prefix}; + my $progress = delete $args{progress}; + my $args = delete $args{args}; + + no strict 'refs'; + + my $fc = Mojo::IOLoop::ForkCall->new; + $fc->run( + sub {my @args = @_; my @res = $cmdref->(@args); return @res}, + $args, + sub { + my ($fc, $err, @res) = @_; + my $dxchan = DXChannel::get($call); + return unless $dxchan; + + if (defined $err) { + my $s = "DXCommand::spawn_cmd: call $call error $err"; + dbg($s) if isdbg('chan'); + $dxchan->send($s); + return; + } + if ($cb) { + $cb->($dxchan, @res); + } else { + return unless @res; + if (defined $prefix) { + $dxchan->send(map {"$prefix$_"} @res); + } else { + $dxchan->send(@res); + } + } + }); + return @out; +} + 1; __END__