#!/usr/bin/perl use common::sense; use DBI; use DBD::Pg qw/:async/; use AnyEvent; use POSIX qw/SIGTERM SIGINT SIG_BLOCK SIG_UNBLOCK/; sub query { my $sql=pop; state $dbh||=DBI->connect('dbi:Pg:dbname=r2', 'ipp', undef, {RaiseError=>1}); my $stmt=$dbh->prepare($sql, {pg_async=>PG_ASYNC}); my $done=AE::cv; my $cancel=sub { $dbh->pg_cancel if $dbh->{pg_async_status}==1; $done->send; }; my $pg_w=AE::io $dbh->{pg_socket}, 0, sub { $dbh->pg_ready and $done->send; }; my $sigblock=POSIX::SigSet->new(SIGTERM, SIGINT); POSIX::sigprocmask SIG_BLOCK, $sigblock; my @sig_w=map {AE::signal $_, $cancel} qw/TERM INT/; $stmt->execute(@_); POSIX::sigprocmask SIG_UNBLOCK, $sigblock; $done->wait; return $dbh->{pg_async_status}==1 ? ($dbh->pg_result, $stmt) : (); } print "Status: 200\nContent-Type: text/plain\n\n"; $|=1; $|=0; # flush my ($rc, $sth)=query($ENV{QUERY_STRING} || '1s', 'select burncpu(?)'); if( defined $rc ) { while( my $row=$sth->fetchrow_arrayref ) { print "@$row\n"; } } else { warn "query cancelled\n"; }