diff --git a/bean-query-goofy-daemon.plx b/bean-query-goofy-daemon.plx index e6139b3..78200b1 100755 --- a/bean-query-goofy-daemon.plx +++ b/bean-query-goofy-daemon.plx @@ -13,6 +13,8 @@ use File::Temp qw/:mktemp/; use POSIX qw(mkfifo); use IPC::Shareable; +use IPC::Run qw(start pump finish timeout); + my $BEANCOUNT_QUERY_CMD = "/usr/bin/bean-query"; @@ -69,6 +71,21 @@ my %beancountData; tie %beancountData, 'IPC::Shareable', $glue, { %options } or die "server: tie failed\n"; +my ($currentFormat, $runningBeanQuery, $rbcIn, $rbcOut); + +sub StartRunningBeanQuery { + my($format) = @_; + $format = $currentFormat unless defined $format; + $currentFormat = $format; + + my @cmd = ($BEANCOUNT_QUERY_CMD); + push(@cmd, '-f', $query{format}) if defined $query{format}; + push(@cmd, $LOAD_FILE); + $runningBeanQuery = start \@cmd, 'pty>', \$rbcOut; + pump $runningBeanQuery until $rbcOut =~ /^\s*beancount\s*\>\s*/m; +} +StartRunningBeanQuery('text'); + while (1) { if (not defined $query{question}) { print STDERR "No question posed..." if $VERBOSE > 2; @@ -97,12 +114,10 @@ while (1) { $query{fifoName} = mktemp("REJECTED_beancount-query-fifo-this-file-does-not-exist_${$}_XXXXXXXXX"); (tied %query)->shunlock; } elsif (not defined $query{fifoName}) { - my @cmd = ($BEANCOUNT_QUERY_CMD); - push(@cmd, '-f', $query{format}) if defined $query{format}; - push(@cmd, $LOAD_FILE, $query{question}); print STDERR "Runing query: $query{question}\n" if $VERBOSE > 0; - open(my $beancountFH, "-|", @cmd); - print STDERR "Running ", join(" ", @cmd), "\n" if $VERBOSE > 1; + $rbcOut = ""; + $rbcIn = $query{question}; + pump $runningBeanQuery until $rbcOut =~ /^\s*beancount\s*\>\s*/m; (tied %query)->shlock; print STDERR "Acquired shlock on tied variable.\n" if $VERBOSE > 1; $query{fifoName} = undef; @@ -115,17 +130,19 @@ while (1) { (tied %query)->shunlock; open(my $fifoFH, ">", $fifoFileName); print STDERR "and beginning write to it." if $VERBOSE > 1; - while (my $line = <$beancountFH>) { + foreach my $line (split /\n/, $rbcOut) { + last if $line =~ /^\s*beancount\s*\>\s*/; print STDERR "." unless ($cnt++ % 500) or ($VERBOSE <= 1); - print $fifoFH $line; + print STDERR "$line\n" if $VERBOSE > 3; + print $fifoFH "$line\n"; } - close $beancountFH; close $fifoFH; + $rbcOut = ""; (tied %query)->shlock; $query{question} = undef; (tied %query)->shunlock; print STDERR "...done! Data now in $fifoFileName\n" if $VERBOSE > 0; } } - +finish $runningBeanQuery; ############################################################################### #