public-inbox.git  about / heads / tags
an "archives first" approach to mailing lists
blob 49f8651a5fed49f6e7bb874cbb1d01528d118b9c 982 bytes (raw)
$ git show HEAD:lib/PublicInbox/Aspawn.pm	# shows this blob on the CLI

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
 
# Copyright (C) all contributors <meta@public-inbox.org>
# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>

# async system()/qx() which takes callback
package PublicInbox::Aspawn;
use v5.12;
use parent qw(Exporter);
use PublicInbox::DS qw(awaitpid);
use PublicInbox::Spawn qw(spawn);
our @EXPORT_OK = qw(run_await);

sub _await_cb { # awaitpid cb
	my ($pid, $cmd, $env, $opt, $cb, @args) = @_;
	PublicInbox::Spawn::read_out_err($opt);
	if ($? && !$opt->{quiet}) {
		my ($status, $sig) = ($? >> 8, $? & 127);
		my $msg = '';
		$msg .= " (-C=$opt->{-C})" if defined $opt->{-C};
		$msg .= " status=$status" if $status;
		$msg .= " signal=$sig" if $sig;
		warn "E: @$cmd", $msg, "\n";
	}
	$cb->($pid, $cmd, $env, $opt, @args) if $cb;
}

sub run_await {
	my ($cmd, $env, $opt, $cb, @args) = @_;
	$opt->{1} //= \(my $out);
	my $pid = spawn($cmd, $env, $opt);
	awaitpid($pid, \&_await_cb, $cmd, $env, $opt, $cb, @args);
	awaitpid($pid); # synchronous for non-$in_loop
}

1;

git clone https://public-inbox.org/public-inbox.git
git clone http://7fh6tueqddpjyxjmgtdiueylzoqt6pt7hec3pukyptlmohoowvhde4yd.onion/public-inbox.git