I've already tried using the state feature, and the result is the same 
unfortunately, the connection is lost after every task execution.

helper dbh => sub { state $dbh = DBI->connect("dbi:Oracle:somedb", 
'someuser', 'somepasswd') };

I used very similar code on my Mojo web app and it works as expected, but 
this seems to be related to the fact that it's running as a Minion workers :

./my_app.pl minion worker -j 2

I've also tried the DBD::mysql driver, but still no success with this :

#!/bin/env perl
use Mojolicious::Lite;

plugin Minion => { SQLite => 'sqlite:test.db' };

helper dbh => sub { state $dbh = DBI->connect(
"DBI:mysql:database=test;host=localhost", "test", "test") };
  
app->minion->add_task( mytask => \&mytask );

sub mytask {    
    my ($job, $arg) = @_;
    my $dbh = $job->app->dbh;
    $dbh->selectrow_array('SELECT NOW()');
    $job->finish('documentation generation done');       
}
app->start;





Yves


On Thursday, 16 June 2016 16:19:47 UTC+2, Dan Book wrote:
>
> Your $dbh is only returned from the helper and not stored anywhere, so it 
> disconnects as soon as it goes out of scope. Try declaring `my $dbh` in the 
> global scope or using the `state` feature (roughly equivalent). A 
> Mojo::SQLite object would be preferable to store over the dbh itself. See 
> an example here: 
> https://metacpan.org/source/DBOOK/Mojo-SQLite-0.021/examples/blog/lib/Blog.pm
>
> On Thu, Jun 16, 2016 at 4:53 AM, Yves <[email protected] <javascript:>> 
> wrote:
>
>> I'm trying to implement a Minon worker that needs access to a database 
>> via DBI.
>>
>> Here is the code that I use :
>>
>>
>> use Mojolicious::Lite;
>>
>> plugin Minion => { SQLite => 'sqlite:test.db' };
>>
>> helper dbh => sub {
>>       my $dbh = DBI->connect("dbi:Oracle:somedb", 'someuser', 
>> 'somepasswd');
>>       return $dbh;
>> };
>>
>> app->minion->add_task( mytask => \&mytask );
>>
>> sub mytest {
>>     
>>     my ($job, $arg) = @_;
>>         
>>     my $dbh = $job->app->dbh;
>>     my ($time) = $dbh->selectrow_array('select sysdate from dual');    
>>     $job->finish('documentation generation done');
>>         
>> }
>> app->start;
>>
>>
>> The problem that I have, is that the connection to the database is 
>> closed/reopened on every task execution, which is very slow, and not the 
>> behavior that I expect.
>>
>> Using the same dbh helper on a Mojolicious app (not within minon task) 
>> works fine and opens one connection per worker.
>>
>> I've already tried various setups with DBI's InactiveDestroy parameter or 
>> using DBIx::Connector but no success so far.
>>
>> -- 
>> 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 [email protected] <javascript:>.
>> To post to this group, send email to [email protected] 
>> <javascript:>.
>> Visit this group at https://groups.google.com/group/mojolicious.
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>

-- 
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.

Reply via email to