How to manage Mojo::Pg if database do not respond at all

Skip to first unread message

Nacho B

Oct 8, 2020, 2:44:24 AM10/8/20
to Mojolicious
Hi, I am using Mojo::Pg  promises with db queries, and everything  is great.

But I do not how to manage the case in which database server is down and no response is arriving.

I presume that I should insert a timeout somewhere, in the promise, but I do not know how.

Nacho B.

Mike Lieman

Oct 8, 2020, 10:49:02 AM10/8/20
I do something like this in the plugin

  $MOJOMYSQL = Mojo::mysql->new($URL);

        # See if the damned thing is up.
        # since we're in ::startup, we don't have the config plugin available
        # ( we pass the config filename in as "app_config" )
        my $config = eval qx{cat $conf->{'app_config'}}
                or $log->logdie("Couldn't read config file: " . $conf->{'app_config'});

    eval {
                local $SIG{ALRM} = sub { die "alarm\n" }; # NB: \n required
        alarm $config->{'db_ping_timeout'};
                my $ok = $MOJOMYSQL->db->ping();
        alarm 0;

        if ($@) {
                $log->logdie($@) unless $@ eq "alarm\n";   # propagate unexpected errors
                # timed out
                my $msg = qq{DB PING FAILED! (continued below)\n}
                                        . qq{****    It seems that $conf->{'db'} on $conf->{'host'} is down!    ****\n}
                                        . qq{****    app runtime env is: $config->{'environment'}    ****\n}
                                        . qq{****    Verify correct setting in $conf->{'app_config'}    ****\n};

        else {
                # didn't
                $log->debug("DB PING: $conf->{'db'} is up");
                return $MOJOMYSQL;

} # sub mojomysql

You received this message because you are subscribed to the Google Groups "Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
To view this discussion on the web visit

Nacho B

Oct 11, 2020, 5:03:17 AM10/11/20
to Mojolicious
Thank you, MIkel,

So, this ping test must be executed for each database access?

I know pings are almost nothing, but adding a new non blocking loop... isn't it an overhead all over the application?

I wonder why this so common scenario can't be managed inside the wait loop. Maybe there is something I still do not understand in the non-blocking way of life.

Nacho B.

Mike Lieman

Oct 11, 2020, 9:09:47 AM10/11/20
I only test on startup because I was interested in catching config errors that prevented access rather than "the db went away".

Mike Lieman

Oct 11, 2020, 10:15:44 AM10/11/20
One more thing.  As I said, I only ping the DB when I startup to make sure the config is right.   It fixes the potential issue of a config error pointing the app at a non-existent dev database in production.

In the code, I do this:
$data = $db->query($query, $retval_ref->{'most_recent_rcd'})->array or $log->logdie;

Nacho B

Oct 13, 2020, 1:27:45 PM10/13/20
to Mojolicious
It does not solve my original problem, but I going to use your tip right now, because that "potential issue"... is more "usual" than "potential".

Thank you for your help, Mike:

Nacho B.
Reply all
Reply to author
0 new messages