Laravel Jobs笔记

laravel 队列支持:

"sync", "database", "beanstalkd", "sqs", "redis", "null"

先用database测试

修改.env

QUEUE_CONNECTION=database

配置数据库模式的环境,原理保存在数据库,执行完成之后就删除了
在这里插入图片描述

php artisan queue:table
php artisan migrate

生成上图展示的数据库jobs

创建一个job

php artisan make:job test
<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;

class test implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public $data;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($data=[])
    {
        $this->data = $data;
        $this->delay(6);
        //
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        var_dump($this->data);
    }
}

两种方式:
添加一个job到队列中

  dispatch(new \App\Jobs\test('我是队列的参数'));
  test::dispatch('我是队列的参数');

上面就能跑通了,但是有时候会处理异常的情况

php artisan queue:failed-table
php artisan migrate

启动的时候有尝试的值,为如果失败了,会再次执行

 php artisan queue:work --sleep=3 --tries=3 

三秒后再次尝试

如果不加tries 则会一直不断的循环

发现了有错误,修改了bug ,想让这个队列再跑一次

这个id 为失败的id

php artisan queue:retry 1

如果想所有的错误都跑一遍则用all

php artisan queue:retry all
已标记关键词 清除标记
<div class="post-text" itemprop="text"> <p>I need a way to run some tasks asynchronously as the execution time varies between each task and I want to run the in an asynchronous way using Laravel Jobs and database as the driver.</p> <p>I created to test jobs using the command line: php artisan make:job TestOne php artisan make:job TestTwo</p> <p>TestOne.php</p> <pre><code><?php namespace App\Jobs; use App\Jobs\Job; use Illuminate\Queue\SerializesModels; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Contracts\Queue\ShouldQueue; class TestOne extends Job implements ShouldQueue { use InteractsWithQueue, SerializesModels; /** * Create a new job instance. * * @return void */ public function __construct() { // } /** * Execute the job. * * @return void */ public function handle() { sleep(5); foreach (range(1, 10) as $item) \Log::info("TestOne: item #" . $item); } } </code></pre> <p>TestTwo.php</p> <pre><code><?php namespace App\Jobs; use App\Jobs\Job; use Illuminate\Queue\SerializesModels; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Contracts\Queue\ShouldQueue; class TestTwo extends Job implements ShouldQueue { use InteractsWithQueue, SerializesModels; /** * Create a new job instance. * * @return void */ public function __construct() { // } /** * Execute the job. * * @return void */ public function handle() { foreach (range(1, 10) as $item) \Log::info("TestTwo: item #" . $item); } } </code></pre> <p>I simply log some messages in laravel's log file, and since TestOne is sleeping for 5 seconds, TestTwo should log the messages first</p> <p>HomeController.php</p> <pre><code><?php namespace App\Http\Controllers; use Queue; use App\Jobs\TestOne; use App\Jobs\TestTwo; class HomeController extends Controller { public function index() { $this->dispatch(new TestOne()); $this->dispatch(new TestTwo()); die("stop"); } } </code></pre> <p>However TestTwo job still waits until TestOne job is done:</p> <pre><code>[2017-03-04 17:00:30] local.INFO: TestOne: item #1 [2017-03-04 17:00:30] local.INFO: TestOne: item #2 [2017-03-04 17:00:30] local.INFO: TestOne: item #3 [2017-03-04 17:00:30] local.INFO: TestOne: item #4 [2017-03-04 17:00:30] local.INFO: TestOne: item #5 [2017-03-04 17:00:30] local.INFO: TestOne: item #6 [2017-03-04 17:00:30] local.INFO: TestOne: item #7 [2017-03-04 17:00:30] local.INFO: TestOne: item #8 [2017-03-04 17:00:30] local.INFO: TestOne: item #9 [2017-03-04 17:00:30] local.INFO: TestOne: item #10 [2017-03-04 17:00:30] local.INFO: TestTwo: item #1 [2017-03-04 17:00:30] local.INFO: TestTwo: item #2 [2017-03-04 17:00:30] local.INFO: TestTwo: item #3 [2017-03-04 17:00:30] local.INFO: TestTwo: item #4 [2017-03-04 17:00:30] local.INFO: TestTwo: item #5 [2017-03-04 17:00:30] local.INFO: TestTwo: item #6 [2017-03-04 17:00:30] local.INFO: TestTwo: item #7 [2017-03-04 17:00:30] local.INFO: TestTwo: item #8 [2017-03-04 17:00:30] local.INFO: TestTwo: item #9 [2017-03-04 17:00:30] local.INFO: TestTwo: item #10 </code></pre> <p>I am running the jobs with <code>php artisan queue:listen</code></p> <p>What am I doing wrong here? I really need these tasks to run asynchronously, just like, say, a JS AJAX request would work like.</p> <p>I am using Laravel 5.2. Again, I am using "database" as the queue driver and yes I have migrated the jobs table. Is it not possible using the database as the driver?</p> </div>
<div class="post-text" itemprop="text"> <p>I have a problem with one of my Jobs laravel. (This is the first time that i exepect this problem, i have anothers jobs who is working without problems...)</p> <p>I have a listener who dispatch a job when one model is updating, so : </p> <ul> <li>One model observer (Working)</li> <li>One event launch by observer (Working)</li> <li>One listener who listen the previous event (Working)</li> <li>One Job who is dispatch by the listener (Not working ...)</li> </ul> <p>The job is executed but he go to failed jobs in my database with error "ErrorException: Undefined variable: ...." ...</p> <p>It is very strange because all the variable was defined, and why does it work once in two?</p> <p>If someone have an idea about that, i will take it :D</p> <p>Thanks in advance!</p> <p>Code of the Job : </p> <pre><code><?php namespace App\Jobs; use Illuminate\Bus\Queueable; use Illuminate\Queue\SerializesModels; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; use App\Models\Transaction; use App\Repositories\TransactionRepository; use App\Repositories\InvoiceModelRepository; use App\Repositories\BalanceRepository; use Scheme; use Log; use Config; use App\Helpers\HelperNumber; use Symfony\Component\Console\Output\ConsoleOutput; /* sudo apt-get install supervisor https://laravel.com/docs/5.4/queues#supervisor-configuration php artisan queue:work --queue compute_fees --retry_after 30 --timeout 30 */ class ComputeFees implements ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; protected $id; /** * Create a new job instance. * * @param int $id * @return void */ public function __construct($id) { $this->id = $id; } /** * Get the best match of invoice models * * @param \App\Models\Transaction $transaction * @return \App\Models\InvoiceModel */ private function getFees($transaction) { $invoiceModelRepository = new InvoiceModelRepository(); //Get the fees for this site and payment type (And country if needed) $params = [ 'sites_id' => $transaction->site->id ]; return $params; } /** * Execute the job. * * @return boolean */ public function handle() { $output = new ConsoleOutput(); //Init repos $transactionRepository = new TransactionRepository(); $balanceRepository = new BalanceRepository(); //Get the transaction $transaction = $transactionRepository->getBy(['id' => $this->id]); $output->writeln('tx : '.$transaction->id); return true; } } </code></pre> <p>The complete exception : </p> <pre><code>ErrorException: Undefined variable: transaction in /var/www/testproject/app/Jobs/ComputeFees.php:53 Stack trace: #0 /var/www/testproject/app/Jobs/ComputeFees.php(53): Illuminate\Foundation\Bootstrap\HandleExceptions->handleError(8, 'Undefined varia...', '/var/www/test....', 53, Array) #1 /var/www/testproject/app/Jobs/ComputeFees.php(106): App\Jobs\ComputeFees->getFees() #2 [internal function]: App\Jobs\ComputeFees->handle() #3 /var/www/testproject/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(32): call_user_func_array(Array, Array) #4 /var/www/testproject/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(90): Illuminate\Container\BoundMethod::Illuminate\Container\{closure}() #5 /var/www/testproject/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(34): Illuminate\Container\BoundMethod::callBoundMethod(Object(Illuminate\Foundation\Application), Array, Object(Closure)) #6 /var/www/testproject/vendor/laravel/framework/src/Illuminate/Container/Container.php(580): Illuminate\Container\BoundMethod::call(Object(Illuminate\Foundation\Application), Array, Array, NULL) #7 /var/www/testproject/vendor/laravel/framework/src/Illuminate/Bus/Dispatcher.php(94): Illuminate\Container\Container->call(Array) #8 /var/www/testproject/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(128): Illuminate\Bus\Dispatcher->Illuminate\Bus\{closure}(Object(App\Jobs\ComputeFees)) #9 /var/www/testproject/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(104): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(App\Jobs\ComputeFees)) #10 /var/www/testproject/vendor/laravel/framework/src/Illuminate/Bus/Dispatcher.php(98): Illuminate\Pipeline\Pipeline->then(Object(Closure)) #11 /var/www/testproject/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php(413): Illuminate\Bus\Dispatcher->dispatchNow(Object(App\Jobs\ComputeFees), false) #12 /var/www/testproject/app/Listeners/ComputeFees.php(36): dispatch_now(Object(App\Jobs\ComputeFees)) #13 [internal function]: App\Listeners\ComputeFees->handle(Object(App\Events\TransactionChangeState)) #14 /var/www/testproject/vendor/laravel/framework/src/Illuminate/Events/CallQueuedListener.php(86): call_user_func_array(Array, Array) #15 [internal function]: Illuminate\Events\CallQueuedListener->handle(Object(Illuminate\Foundation\Application)) #16 /var/www/testproject/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(32): call_user_func_array(Array, Array) #17 /var/www/testproject/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(90): Illuminate\Container\BoundMethod::Illuminate\Container\{closure}() #18 /var/www/testproject/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(34): Illuminate\Container\BoundMethod::callBoundMethod(Object(Illuminate\Foundation\Application), Array, Object(Closure)) #19 /var/www/testproject/vendor/laravel/framework/src/Illuminate/Container/Container.php(580): Illuminate\Container\BoundMethod::call(Object(Illuminate\Foundation\Application), Array, Array, NULL) #20 /var/www/testproject/vendor/laravel/framework/src/Illuminate/Bus/Dispatcher.php(94): Illuminate\Container\Container->call(Array) #21 /var/www/testproject/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(128): Illuminate\Bus\Dispatcher->Illuminate\Bus\{closure}(Object(Illuminate\Events\CallQueuedListener)) #22 /var/www/testproject/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(104): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Events\CallQueuedListener)) #23 /var/www/testproject/vendor/laravel/framework/src/Illuminate/Bus/Dispatcher.php(98): Illuminate\Pipeline\Pipeline->then(Object(Closure)) #24 /var/www/testproject/vendor/laravel/framework/src/Illuminate/Queue/CallQueuedHandler.php(49): Illuminate\Bus\Dispatcher->dispatchNow(Object(Illuminate\Events\CallQueuedListener), false) #25 /var/www/testproject/vendor/laravel/framework/src/Illuminate/Queue/Jobs/Job.php(86): Illuminate\Queue\CallQueuedHandler->call(Object(Illuminate\Queue\Jobs\DatabaseJob), Array) #26 /var/www/testproject/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(327): Illuminate\Queue\Jobs\Job->fire() #27 /var/www/testproject/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(277): Illuminate\Queue\Worker->process('database', Object(Illuminate\Queue\Jobs\DatabaseJob), Object(Illuminate\Queue\WorkerOptions)) #28 /var/www/testproject/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(118): Illuminate\Queue\Worker->runJob(Object(Illuminate\Queue\Jobs\DatabaseJob), 'database', Object(Illuminate\Queue\WorkerOptions)) #29 /var/www/testproject/vendor/laravel/framework/src/Illuminate/Queue/Console/WorkCommand.php(102): Illuminate\Queue\Worker->daemon('database', 'default', Object(Illuminate\Queue\WorkerOptions)) #30 /var/www/testproject/vendor/laravel/framework/src/Illuminate/Queue/Console/WorkCommand.php(86): Illuminate\Queue\Console\WorkCommand->runWorker('database', 'default') #31 [internal function]: Illuminate\Queue\Console\WorkCommand->handle() #32 /var/www/testproject/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(32): call_user_func_array(Array, Array) #33 /var/www/testproject/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(90): Illuminate\Container\BoundMethod::Illuminate\Container\{closure}() #34 /var/www/testproject/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(34): Illuminate\Container\BoundMethod::callBoundMethod(Object(Illuminate\Foundation\Application), Array, Object(Closure)) #35 /var/www/testproject/vendor/laravel/framework/src/Illuminate/Container/Container.php(580): Illuminate\Container\BoundMethod::call(Object(Illuminate\Foundation\Application), Array, Array, NULL) #36 /var/www/testproject/vendor/laravel/framework/src/Illuminate/Console/Command.php(183): Illuminate\Container\Container->call(Array) #37 /var/www/testproject/vendor/symfony/console/Command/Command.php(255): Illuminate\Console\Command->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Illuminate\Console\OutputStyle)) #38 /var/www/testproject/vendor/laravel/framework/src/Illuminate/Console/Command.php(170): Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Illuminate\Console\OutputStyle)) #39 /var/www/testproject/vendor/symfony/console/Application.php(908): Illuminate\Console\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput)) #40 /var/www/testproject/vendor/symfony/console/Application.php(269): Symfony\Component\Console\Application->doRunCommand(Object(Illuminate\Queue\Console\WorkCommand), Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput)) #41 /var/www/testproject/vendor/symfony/console/Application.php(145): Symfony\Component\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput)) #42 /var/www/testproject/vendor/laravel/framework/src/Illuminate/Console/Application.php(90): Symfony\Component\Console\Application->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput)) #43 /var/www/testproject/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(122): Illuminate\Console\Application->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput)) #44 /var/www/testproject/artisan(37): Illuminate\Foundation\Console\Kernel->handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput)) #45 {main} </code></pre> </div>
©️2020 CSDN 皮肤主题: 酷酷鲨 设计师:CSDN官方博客 返回首页