josegonzalez/cakephp-queuesadilla

Passing several arguments doesn't work

Closed this issue · 7 comments

Hello!

When using the mysqlEngine and the function:

// a dummy function
function some_job($first_param, $second_param) {
	var_dump($first_param);
	var_dump($second_param);
	//var_dump($job->data());
}		

When doing

Queue::push([__CLASS__,'some_job'],[
			'id',
			'message'
		], []);

the second param is ignored and i get:

Warning Error: Missing argument 2 for App\Controller\PagesController::some_job(), called in .../vendor/josegonzalez/queuesadilla/src/josegonzalez/Queuesadilla/Worker/SequentialWorker.php on line 99 and defined in [../src/Controller/PagesController.php, line 30]

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 jobobject.
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.

hmic commented

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!