josegonzalez / cakephp-queuesadilla

CakePHP3: easily run background jobs on various message backends
https://cakephp-queuesadilla.readthedocs.org/en/latest/
MIT License
34 stars 14 forks source link

Passing several arguments doesn't work #10

Closed juniorionut closed 7 years ago

juniorionut commented 7 years ago

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]
cleptric commented 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 jobobject. Inside your callable, you can access the variables with $job->data('foo')

juniorionut commented 7 years ago

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 ?

cleptric commented 7 years ago

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 7 years ago

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', ...]]);
juniorionut commented 7 years ago

@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!

josegonzalez commented 7 years ago

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.

juniorionut commented 7 years ago

Got it, thanks!