Minion: getting data out.

30 views
Skip to first unread message

BobbyBrown

unread,
Oct 21, 2019, 7:46:01 AM10/21/19
to Mojolicious
Hello all,

I am having a problem that I don't understand. I've got the following minion task in a helper in a plugin:

  $app->minion->add_task( passtest => sub {                                                                                    
                           
my ( $job, $aa, $bb ) = @_;                                                                        
                           
my $cc = "$aa $bb";                                                                                
                            $job
->note( cc => "$cc" );                                                                          
                            $job
->finish("All went well!, $cc");                                                                
                         
} );


I call it from the controller thus:
Saisissez le code i  my $id2 = $c->minion->enqueue( passtest => [ "bla", "url" ] );                                                                
 
my $job = $c->minion->job("$id2");                                                                                            
  say
"New Method : " . pp($job->info);

But the output shows :


New Method : {
  args    
=> ["bla", "url"],
  attempts
=> 1,
  children
=> [],
  created  
=> 1571654552.89089,
  delayed  
=> 1571654552.89089,
  finished
=> undef,
  id      
=> 195520,
  notes    
=> {},
  parents  
=> [],
  priority
=> 0,
  queue    
=> "default",
  result  
=> undef,
  retried  
=> undef,
  retries  
=> 0,
  started  
=> undef,
  state    
=> "inactive",
  task    
=> "passtest",
  time    
=> 1571654552.89342,
  worker  
=> undef,
}

No data has been caught. The strange thing is that from the minion admin I can see the data:
Saisissez le code ici...{
 
"args" => [
   
"bla",
   
"url"
 
],
 
"attempts" => 1,
 
"children" => [],
 
"created" => "2019-10-21T10:42:32.89089Z",
 
"delayed" => "2019-10-21T10:42:32.89089Z",
 
"finished" => "2019-10-21T10:42:32.90722Z",
 
"id" => 195520,
 
"notes" => {
   
"cc" => "bla url"
 
},
 
"parents" => [],
 
"priority" => 0,
 
"queue" => "default",
 
"result" => "All went well!, bla url",
 
"retried" => undef,
 
"retries" => 0,
 
"started" => "2019-10-21T10:42:32.89251Z",
 
"state" => "finished",
 
"task" => "passtest",
 
"time" => "2019-10-21T10:45:09.66926Z",
 
"worker" => 134
}

This is the point when I decide to ask for help. I've tried to pass hash referenced to the minion task as well, but they were not changed outside of the scope of the task, unlike in a subroutine. What should I be reading up on to better understand what is going on in such cases?

Kind regards,

Sebastian Riedel

unread,
Oct 21, 2019, 7:49:36 AM10/21/19
to mojol...@googlegroups.com
> state => "inactive",
>
> "state" => "finished",

Look at the different states, the job is simply not finished yet at the time.

--
Sebastian Riedel
https://mojolicious.org
https://github.com/kraih
https://twitter.com/kraih

BobbyBrown

unread,
Oct 21, 2019, 10:09:06 AM10/21/19
to Mojolicious

@sri

Thank you, this explains the perceived strangeness.

I still have not figured out how to reliably get data out of a minion task, I'll have to work on events and read up on non-blocking.
If I understand the code correctly the linkcheck example simply fails if the minion job has not finished when it looks up the result?

For the time being though I can get data from one minion process to another, because {parents = "$id"} keeps things in order. So I'll continue building like this, and see if in the end I can get everything in place. (I suppose I'll write final data processed to a database, and set up something non-blocking to update the page rendered from the database. I've still got lots to learn.)

Kind regards,

Justin Hawkins

unread,
Oct 22, 2019, 8:22:07 AM10/22/19
to mojol...@googlegroups.com


On 22 Oct 2019, at 12:31 am, BobbyBrown <bboby....@gmail.com> wrote:

I still have not figured out how to reliably get data out of a minion task, I'll have to work on events and read up on non-blocking.
If I understand the code correctly the linkcheck example simply fails if the minion job has not finished when it looks up the result?

The minion job is completely independent of your web request - which is the entire point.

If you are doing something which you expect to be completed in the time of the request, then you may as well just execute it immediately and block waiting for it (or look at Mojo::IOLoop::Subprocess).

Otherwise, you would check back on the minion job later, perhaps via a REST request or similar.

A typical flow:

* request causes minion job to be enqueued
* job id is returned to the user's browser
* user's browser starts polling a REST endpoint looking for job completion (via javascript)
* once the job is complete, the browser redirects to a new URL with the job data

This is far from the only way to do it, and javascript is not a requirement.

Cheers,

Justin
Reply all
Reply to author
Forward
0 new messages