thinkphp消息队列topthink/think-queue的研究

代码


composer require topthink/think-migration

composer require topthink/think-queue



php think queue:table
php think queue:failed-table



zz_migrations

php think migrate:run




<?php

use think\migration\db\Column;
use think\migration\Migrator;

class CreateJobsTable extends Migrator
{
    public function change()
    {
        $this->table('jobs')
            ->addColumn(Column::string('queue'))
            ->addColumn(Column::longText('payload'))
            ->addColumn(Column::tinyInteger('attempts')->setUnsigned())
            ->addColumn(Column::unsignedInteger('reserve_time')->setNullable())
            ->addColumn(Column::unsignedInteger('available_time'))
            ->addColumn(Column::unsignedInteger('create_time'))
            ->addIndex('queue')
            ->create();
    }
}





-- auto-generated definition
create table zz_jobs
(
    id             int auto_increment
        primary key,
    queue          varchar(255)     not null,
    payload        longtext         not null,
    attempts       tinyint unsigned not null,
    reserve_time   int(11) unsigned null,
    available_time int(11) unsigned not null,
    create_time    int(11) unsigned not null
)
    engine = InnoDB
    charset = utf8;

create index queue
    on zz_jobs (queue);
    
    
    
    
    
    
    
    
    
class CreateFailedJobsTable extends Migrator
{
    public function change()
    {
        $this->table('failed_jobs')
            ->addColumn(Column::text('connection'))
            ->addColumn(Column::text('queue'))
            ->addColumn(Column::longText('payload'))
            ->addColumn(Column::longText('exception'))
            ->addColumn(Column::timestamp('fail_time')->setDefault('CURRENT_TIMESTAMP'))
            ->create();
    }
}


-- auto-generated definition
create table zz_failed_jobs
(
    id         int auto_increment
        primary key,
    connection text                                not null,
    queue      text                                not null,
    payload    longtext                            not null,
    exception  longtext                            not null,
    fail_time  timestamp default CURRENT_TIMESTAMP not null
)
    engine = InnoDB
    charset = utf8;





<?php


use x852\phinx\command\migrate\Migration;

class Jobs extends Migration
{
    public function up()
    {
        $this->execute(
            "create table `{$this->exec_table_prefix}jobs`
                (
                    `id` int not null auto_increment primary key,
                    `queue` varchar(255) not null,
                    `payload` longtext not null,
                    `attempts` tinyint unsigned not null,
                    `reserve_time`   int(11) unsigned null,
                    `available_time` int(11) unsigned not null,
                    `create_time`    int(11) unsigned not null,
                    index(queue)
                ) ENGINE=INNODB DEFAULT CHARSET=utf8mb4 collate utf8mb4_unicode_ci;"
        );
    }

    public function down()
    {
        $this->execute(
            "drop table if exists {$this->exec_table_prefix}jobs;"
        );
    }
}



<?php


use x852\phinx\command\migrate\Migration;

class FailedJobs extends Migration
{
    public function up()
    {
        $this->execute(
            "create table `{$this->exec_table_prefix}failed_jobs`
                (
                    `id` int auto_increment primary key,
                    `connection` text not null,
                    `queue` text not null,
                    `payload` longtext not null,
                    `exception` longtext not null,
                    `fail_time` timestamp default CURRENT_TIMESTAMP not null
                ) ENGINE=INNODB DEFAULT CHARSET=utf8mb4 collate utf8mb4_unicode_ci;"
        );
    }

    public function down()
    {
        $this->execute(
            "drop table if exists {$this->exec_table_prefix}failed_jobs;"
        );
    }
}

    
    
    
    
    

配置

修改配置,它默认是同步的sync
在这里插入图片描述
改为数据库作为驱动
在这里插入图片描述

添加任务和消费任务

新建一个任务

在这里插入图片描述

<?php

namespace app\admin\job;

use think\facade\Log;
use think\queue\Job;

class NoticeJob
{
    public function fire(Job $job, $notice)
    {
        sleep(5);
        Log::info('===============success===============');

    }

    public function failed($data)
    {
        //直到达到最大重试次数后失败后,执行failed方法
    }
}

找个触发的地方,我这里是在模型事件里面,你可以在任意地方触发
在这里插入图片描述

        //发布任务
        Queue::push(NoticeJob::class);

然后添加一条数据
发现jobs表里面多了一条数据
在这里插入图片描述

现在我们消费它

php think queue:work

在这里插入图片描述
在这里插入图片描述
发现执行成功

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值