mojolicious / minion

:octopus: Perl high performance job queue
https://metacpan.org/release/Minion
Artistic License 2.0
221 stars 55 forks source link

A Mojolicious app w/ Minion::Backend::Pg always connects to PostgreSQL db on startup #121

Open augensalat opened 2 years ago

augensalat commented 2 years ago

Problem description

When Minion::Backend::Pg is instantiated inside Mojolicious::startup (typically with Mojolicious::Plugin::Minion) it connects to the PostgreSQL server to determine the server version. This can cause trouble with application commands in isolated environments, i.e. in a CI/CD workflow where Minion and its database is not available and not needed.

The respective code is https://github.com/mojolicious/minion/blob/f4bc5b2e98aa619b88cd1c6188649f281be1c49e/lib/Minion/Backend/Pg.pm#L130-L132

Currrently I'm not able to find a reasonable workaround in my application code to avoid this immediate database connection attempt, see https://github.com/mojolicious/mojo/discussions/1886 .

Steps to reproduce the behavior

A Mojolicious application using Minion (with Mojolicious::Plugin::Minion) fails at program start when the database is not accessible.

Basic application:

package MyApp;
use Mojo::Base 'Mojolicious', -signatures;

sub startup ($self) {
    $self->plugin(Minion => {Pg => 'postgresql://user:pass@db/test'});
}

1;

my_app version fails if the database is not accessible.

Expected behavior

my_app version as any other command that is not related to Minion should work without the need to access the Minion Postgres database.

At least an option should exist to disable the check for the server version in the program startup.

 $self->plugin(Minion => {Pg => $config->{database}, check_server_version => 0});

Actual behavior

$ my_app version
DBI connect('dbname=test;host=db','user',...) failed: could not translate host name "db" to address: Name does not resolve at /deps/local/lib/perl5/Mojo/Pg.pm line 73.
andrii-suse commented 2 years ago

I kind of share your concern here - but what behavior would you expect if the app starts in production, but Minion has incorrect DB parameters and will never connect to DB? It might be harder / too late to diagnose comparing to failure to start service. So, if your app is designed to work even if Minion is not able to connect - then you can just tolerate failure to load plugin at startup as well. Otherwise it is better if the failure happens sooner than later.

kraih commented 1 year ago

I'm testing a different strategy in minion.js, lets see how that works out.