Littlesqx / aint-queue

:rocket: An async-queue library built on top of swoole, flexable multi-consumer, coroutine supported. 基于 Swoole 的一个异步队列库,可弹性伸缩的工作进程池,工作进程协程支持。
MIT License
178 stars 32 forks source link

不支持参数吗?? #26

Closed xieyuhua closed 4 years ago

xieyuhua commented 4 years ago

就是推送(new)类或者一个方法的时候,不支持传递参数,后期就直接对这个参数进行处理,

Littlesqx commented 4 years ago

类支持,构造函数没有约束,但是要注意参数内容不要太大,因为序列化后会推进队列占用内存。闭包也支持,通过 use 传递,但是闭包任务不建议生产环境使用。

例子:

class Job implements \Littlesqx\AintQueue\JobInterface 
{
    /**
     * @var int
     */
    protected $userId;

    public function __construct(int $userId)
    {
        $this->userId = $userId;
    }

    /**
     * Execute current job.
     *
     * @return mixed
     */
    public function handle(): void
    {
        echo "Hello World, {$this->userId}\n";
    }

    /**
     * Determine whether current job can retry if fail.
     *
     * @param int $attempt
     * @param $error
     *
     * @return bool
     */
    public function canRetry(int $attempt, $error): bool
    {
        return false;
    }

    /**
     * Get current job's next execution unix time after failed.
     *
     * @param int $attempt
     *
     * @return int
     */
    public function retryAfter(int $attempt): int
    {
        return 0;
    }

    /**
     * After failed, this function will be called.
     *
     * @param int   $id
     * @param array $payload
     */
    public function failed(int $id, array $payload): void
    {
        echo "job#{$id} was failed.\n";
    }

    /**
     * Get the middleware the job should pass through.
     *
     * @return \Littlesqx\AintQueue\JobMiddlewareInterface[]
     */
    public function middleware(): array
    {
        return [];
    }
};

$jobWithParams = new Job(1);

$queue->push($jobWithParams);