Reputation: 577
I am new in Mojolicious and playing around with this simple application to return rows from database. It seems to work fine for a single request, however when I use "xargs -n 1 -P 4" to run curl in parallel processes I start seeing "500 Internal Server Error" or "Empty reply from server".
The debug message in the log shows a number of "ORA-01002: fetch out of sequence" and "Worker 101864 has no heartbeat (50 seconds), restarting".
Really appreciate any guidance of what mistake that I made here.
use Mojolicious::Lite -signatures;
use Mojo::Promise;
use DBI;
my $dbh = DBI->connect(...);
app->config( hypnotoad => { listen => ['http://*:3000'], workers => 4, proxy => 1 });
helper select => sub {
my $c = shift;
my ($promise, $column1) = @_;
my $query = <<~"END";
SELECT *
FROM mytable
WHERE column1 = :column1
END
my $sth = $dbh->prepare($query) or do {
$promise->reject("Failed to prepare SQL: " . $dbh->errstr);
return;
};
$sth->bind_param(":column1", $column1);
$sth->execute() or do {
$promise->reject("Unable to execute SQL: " . $dbh->errstr);
return;
};
# Fetch all rows from the result set
my $data = $sth->fetchall_arrayref({});
$sth->finish();
return $data;
};
get '/foo/bar' => sub {
my $c = shift;
my $column1 = $c->param('column1');
my $promise = Mojo::Promise->new;
Mojo::IOLoop->next_tick( sub {
my $data = $c->select( $promise, $column1 );
my @records = map { join('|', $_->{column1}, $_->{column2}) } $data->@*;
my $ret_ref = JSON->new->utf8(1)->pretty(1)->encode( { 'MYRESULT' => \@records });
$c->render(text => $ret_ref, format => 'json');
$promise->resolve;
});
$promise->wait;
};
app->start;
Upvotes: 1
Views: 161