• Stars
    star
    555
  • Rank 77,480 (Top 2 %)
  • Language
    PHP
  • License
    Apache License 2.0
  • Created about 8 years ago
  • Updated 11 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

ThinkPHP 队列支持

think-queue for ThinkPHP6

安装

composer require topthink/think-queue

配置

配置文件位于 config/queue.php

公共配置

[
    'default'=>'sync' //驱动类型,可选择 sync(默认):同步执行,database:数据库驱动,redis:Redis驱动//或其他自定义的完整的类名
]

创建任务类

推荐使用 app\job 作为任务类的命名空间 也可以放在任意可以自动加载到的地方

任务类不需继承任何类,如果这个类只有一个任务,那么就只需要提供一个fire方法就可以了,如果有多个小任务,就写多个方法,下面发布任务的时候会有区别
每个方法会传入两个参数 think\queue\Job $job(当前的任务对象) 和 $data(发布任务时自定义的数据)

还有个可选的任务失败执行的方法 failed 传入的参数为$data(发布任务时自定义的数据)

下面写两个例子

namespace app\job;

use think\queue\Job;

class Job1{
    
    public function fire(Job $job, $data){
    
            //....这里执行具体的任务 
            
             if ($job->attempts() > 3) {
                  //通过这个方法可以检查这个任务已经重试了几次了
             }
            
            
            //如果任务执行成功后 记得删除任务,不然这个任务会重复执行,直到达到最大重试次数后失败后,执行failed方法
            $job->delete();
            
            // 也可以重新发布这个任务
            $job->release($delay); //$delay为延迟时间
          
    }
    
    public function failed($data){
    
        // ...任务达到最大重试次数后,失败了
    }

}
namespace app\lib\job;

use think\queue\Job;

class Job2{
    
    public function task1(Job $job, $data){
    
          
    }
    
    public function task2(Job $job, $data){
    
          
    }
    
    public function failed($data){
    
          
    }

}

发布任务

think\facade\Queue::push($job, $data = '', $queue = null)think\facade\Queue::later($delay, $job, $data = '', $queue = null) 两个方法,前者是立即执行,后者是在$delay秒后执行

$job 是任务名
命名空间是app\job的,比如上面的例子一,写Job1类名即可
其他的需要些完整的类名,比如上面的例子二,需要写完整的类名app\lib\job\Job2
如果一个任务类里有多个小任务的话,如上面的例子二,需要用@+方法名app\lib\job\Job2@task1app\lib\job\Job2@task2

$data 是你要传到任务里的参数

$queue 队列名,指定这个任务是在哪个队列上执行,同下面监控队列的时候指定的队列名,可不填

监听任务并执行

&> php think queue:listen

&> php think queue:work

两种,具体的可选参数可以输入命令加 --help 查看

可配合supervisor使用,保证进程常驻

More Repositories

1

think

ThinkPHP Framework ——十年匠心的高性能PHP框架
PHP
7,795
star
2

thinkphp

ThinkPHP3.2 ——基于PHP5的简单快速的面向对象的PHP框架
PHP
2,879
star
3

framework

ThinkPHP Framework
PHP
2,671
star
4

think-swoole

Swoole extend for thinkphp
PHP
444
star
5

think-orm

Think ORM——the PHP Database&ORM Framework
PHP
385
star
6

think-awesome

awesome for thinkphp
242
star
7

think-worker

Workerman extend for ThinkPHP
PHP
184
star
8

think-throttle

thinkphp 限制访问频率的中间件
PHP
176
star
9

think-captcha

thinkphp 验证码类库
PHP
112
star
10

think-mongo

mongodb driver for thinkphp
PHP
101
star
11

think-migration

thinkphp 数据库迁移工具
PHP
89
star
12

think-helper

thinkphp常用的助手类 助手函数
PHP
78
star
13

thinkphp-extend

PHP
73
star
14

think-image

thinkphp5 图像处理类
PHP
59
star
15

think-validate

think Validate
PHP
51
star
16

think-template

the php template engine
PHP
49
star
17

think-annotation

ThinkPHP6注解
PHP
43
star
18

think-angular

think-angular模板引擎
PHP
40
star
19

think-testing

ThinkPHP 5 应用单元测试组件
PHP
40
star
20

think-oracle

Oracle数据库驱动
PHP
36
star
21

think-cache

PHP Cache Service
PHP
31
star
22

think-ide-helper

PHP
24
star
23

think-api

官方API服务SDK
PHP
22
star
24

think-multi-app

thinkphp6 multi app support
PHP
21
star
25

think-debugbar

ThinkPHP Debugbar (Integrates PHP Debug Bar)
PHP
17
star
26

think-glide

ThinkPHP adapter for using Glide image manipulation library
PHP
16
star
27

think-seaslog

SeasLog driver for thinkphp
PHP
15
star
28

think-editor

The ThinkPHP5 Editor Package
PHP
15
star
29

think-view

thinkphp driver for think-template
PHP
14
star
30

think-installer

PHP
14
star
31

think-container

PHP Container & Facade Manager
PHP
14
star
32

thinkng-webapp

Thinkng的应用模板
PHP
12
star
33

think-sae

sae support for thinkphp5
PHP
11
star
34

think-workflow

ThinkPHP6状态机
PHP
11
star
35

think-log

PHP
10
star
36

think-trace

debug trace bar for thinkphp6
PHP
9
star
37

think-filesystem

thinkphp 文件系统
PHP
8
star
38

think-tracing

PHP
8
star
39

think-one

一个tp5的辅助开发插件
HTML
7
star
40

think-csrf

thinkphp5 CSRF 保护组件
5
star
41

think-yaconf

PHP
4
star
42

think-socketlog

socketlog driver for thinkphp
PHP
4
star
43

cache-bridge

PSR-16 to PSR-6 Bridge
PHP
3
star
44

think-factory

thinkphp 工厂类
PHP
2
star