代码
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
发现执行成功