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

64 views
Skip to first unread message

Nacho B

unread,
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

unread,
Oct 8, 2020, 10:49:02 AM10/8/20
to mojol...@googlegroups.com
I do something like this in the MojoMySQL.pm 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};

                $log->logdie($msg);
        }
        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 mojolicious...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mojolicious/0b90c0f5-3cb7-4647-8f5e-c1413b713725n%40googlegroups.com.

Nacho B

unread,
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.


Regards,
Nacho B.

Mike Lieman

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

Mike Lieman

unread,
Oct 11, 2020, 10:15:44 AM10/11/20
to mojol...@googlegroups.com
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

unread,
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.

Mail Delivery Subsystem

unread,
Feb 2, 2022, 8:39:51 AM2/2/22
to Mojolicious
Reply all
Reply to author
Forward
0 new messages