Closed juniorionut closed 7 years ago
Please pass additional variables you need as the second parameter of Queue::push
.
The SequentialWorker::perform
method will call your defined callable and only pass the job
object.
Inside your callable, you can access the variables with $job->data('foo')
Hello and thank you for the prompt reply!
Indeed, using
Queue::push([__CLASS__,'some_job'],[
'user'=>[
'id'=>1,
'name'=> 'test'
],
'message' => 'nice'
]);
we can access the vars using
// a dummy functyion
function some_job($data) {
var_dump($data->data('user'));
var_dump($data->data('message'));
//var_dump($job->data());
}
Somehow this was "enlightened" to me but it doesn't change the fact that we might need to access functions with more then one param.
P.S. If we use "exit" in the php code the worker is terminated, is this normal ?
I'm not exactly sure why @josegonzalez build it this way. Maybe there was a good reason :)
Maybe we can discuss this at https://github.com/josegonzalez/php-queuesadilla.
Yes, calling exit
will shutdown your worker.
This is a generic wrapper to allow calling a function/method with multiple arguments:
<?php
namespace App\Lib;
class QueueWrapper {
static function extract($job) {
$callable = $job->data('callable');
if(is_array($callable) && count($callable) == 2) {
$instance = new $callable[0];
return call_user_func_array([$instance, $callable[1]], $job->data('args'));
}
return call_user_func_array($callable, $job->data('args'));
}
}
// call with: Queue::push(['App\Lib\QueueWrapper::extract'], ['callable '=> [__CLASS__, 'some_job'], 'args' => [1, 'me', ...]]);
@cleptric , indeed we can link the thread, i have added here as i have stumbled upon this in cakePHP.
@hmic , thanks, will see about it!
You get access to a job object and that object has a data
method that can be used to retrieve all the data. The Job
object also has other methods that are useful, and I think passing in the entire thing instead of just the data makes this a bit easier to work with.
See here for more details.
Got it, thanks!
Hello!
When using the mysqlEngine and the function:
When doing
the second param is ignored and i get: