队列的简单 demo

基于文档稍微写一个很小的 demo 然后面的新手直接能用

    1:创建model 和 数据库

         php artisan make:model Podcast -m

        我就创建两个字段 id & status ,database目录下新建表的 run 方法:
             public function up()
                {
                    Schema::create('podcasts', function (Blueprint $table) {
                        $table->increments('id');
                        $table->tinyInteger('status')->unsigned();
                        $table->timestamps();
                    });
                }

    2:创建控制器,增加 story 方法  
        php artisan make:controller:API\PodcastController
        增加一个 
        public function store(  ) {
            $model = new Podcast();
            $model->status = 0;
            $model->save();
            ProcessPodcast::dispatch($model)->delay(now()->addMinutes(3));
        }

    3:修改 Job / ProcessPodcast.php  类,

        protected $podcast ;
        public function __construct(Podcast $podcast)\
        {
            $this->podcast = $podcast;
        }

         public function handle()\
        {
            $this->podcast->status = 1;
            $this->podcast->update();
        }

    3:配置 .env 
        QUEUE_DRIVER=redis

    4:job 怎么创建文档上已经写的很清楚了,就只是差控制器 & 数据库 & 模型 没有创建。

    5:在 routes/api.php 增加路由
        Route::get('queue','API\PodcastController@store');

    6:在服务器上执行 php artisan queue:work  

总结下 虽然很简单 但是对于还是有同学看文档 我之前也看不懂 现在看懂了 所以想帮助没有看懂的人

执行结果和过程

队列的简单 demo

队列的简单 demo

《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 3

基于楼主的demo,重新写了一份详细点的

队列的简单demo

references

队列《Laravel 5.2 中文文档》

分享:队列的简单 demo

model

$ php artisan make:model podcast
Model created successfully.

创建表

CREATE TABLE `podcast` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `status` tinyint(1) NOT NULL,
  `updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP COMMENT 'CURRENT_TIMESTAMP',
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Podcast extends Model
{
    protected $connection = '34w';
    protected $table = 'podcast';
}

job

$ php artisan make:job ProcessPodcast
Job created successfully.
<?php

namespace App\Jobs;

use App\Jobs\Job;
use App\Podcast;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;

class ProcessPodcast extends Job implements ShouldQueue
{
    use InteractsWithQueue, SerializesModels;

    protected $podcast;

    public function __construct(Podcast $podcast)
    {
        $this->podcast = $podcast;
    }

    public function handle()
    {
        $this->podcast->status = 1;
        $this->podcast->update();
    }
}

Controller

$ php artisan make:controller API/PodcastController
Controller created successfully.
<?php

namespace App\Http\Controllers\Api;

use App\Podcast;
use App\Jobs\ProcessPodcast;
use App\Http\Controllers\Controller;

class PodcastController extends Controller
{
    public function store(  ) {
        $model = new Podcast();
        $model->status = 0;
        $model->save();
        $job = new ProcessPodcast($model);
        $job->delay(10);;
        $this->dispatch($job);
    }
}

routes

在route文件中新增

Route::get('queue','Api\PodcastController@store');

运行

请求api

http://www.newadminpk.abc/queue

运行命令

$ php artisan queue:work
[2020-02-24 17:55:28] Processed: App\Jobs\ProcessPodcast

或者监听

$ php artisan queue:listen
[2020-02-24 18:23:45] Processed: App\Jobs\ProcessPodcast
[2020-02-24 18:23:46] Processed: App\Jobs\ProcessPodcast
[2020-02-24 18:23:47] Processed: App\Jobs\ProcessPodcast
[2020-02-24 18:23:48] Processed: App\Jobs\ProcessPodcast

数据库

id    status    updated_at    created_at
1    1    2020-02-24 17:49:06    2020-02-24 17:49:00
7    0    2020-02-24 18:00:52    2020-02-24 18:00:52
8    0    2020-02-24 18:00:54    2020-02-24 18:00:54
5年前 评论

写得真乱,让人家越看不明白

5年前 评论
威尔 5年前

基于楼主的demo,重新写了一份详细点的

队列的简单demo

references

队列《Laravel 5.2 中文文档》

分享:队列的简单 demo

model

$ php artisan make:model podcast
Model created successfully.

创建表

CREATE TABLE `podcast` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `status` tinyint(1) NOT NULL,
  `updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP COMMENT 'CURRENT_TIMESTAMP',
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Podcast extends Model
{
    protected $connection = '34w';
    protected $table = 'podcast';
}

job

$ php artisan make:job ProcessPodcast
Job created successfully.
<?php

namespace App\Jobs;

use App\Jobs\Job;
use App\Podcast;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;

class ProcessPodcast extends Job implements ShouldQueue
{
    use InteractsWithQueue, SerializesModels;

    protected $podcast;

    public function __construct(Podcast $podcast)
    {
        $this->podcast = $podcast;
    }

    public function handle()
    {
        $this->podcast->status = 1;
        $this->podcast->update();
    }
}

Controller

$ php artisan make:controller API/PodcastController
Controller created successfully.
<?php

namespace App\Http\Controllers\Api;

use App\Podcast;
use App\Jobs\ProcessPodcast;
use App\Http\Controllers\Controller;

class PodcastController extends Controller
{
    public function store(  ) {
        $model = new Podcast();
        $model->status = 0;
        $model->save();
        $job = new ProcessPodcast($model);
        $job->delay(10);;
        $this->dispatch($job);
    }
}

routes

在route文件中新增

Route::get('queue','Api\PodcastController@store');

运行

请求api

http://www.newadminpk.abc/queue

运行命令

$ php artisan queue:work
[2020-02-24 17:55:28] Processed: App\Jobs\ProcessPodcast

或者监听

$ php artisan queue:listen
[2020-02-24 18:23:45] Processed: App\Jobs\ProcessPodcast
[2020-02-24 18:23:46] Processed: App\Jobs\ProcessPodcast
[2020-02-24 18:23:47] Processed: App\Jobs\ProcessPodcast
[2020-02-24 18:23:48] Processed: App\Jobs\ProcessPodcast

数据库

id    status    updated_at    created_at
1    1    2020-02-24 17:49:06    2020-02-24 17:49:00
7    0    2020-02-24 18:00:52    2020-02-24 18:00:52
8    0    2020-02-24 18:00:54    2020-02-24 18:00:54
5年前 评论

大佬们有没有碰到过laravel队列同步可以执行,换成database异步就只是添加到jobs表里了,不执行任务。 :cry: :cry:

4年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!