huuuk/phalcon-queues 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

huuuk/phalcon-queues

最新稳定版本:0.1.2

Composer 安装命令:

composer require huuuk/phalcon-queues

包简介

README 文档

README

  1. Instalation
  2. Configuration
  3. Drivers
  4. Usage
  5. Pushing Jobs
  6. Worker
  7. Todo

##Instalation Install via Composer.

"require": {
    "huuuk/phalcon-queues": "^0.1"
}

##Configuration ####Settings First off all you should deside which [driver] suits you best.
Then add appropriate settings in your config.ini file. ####Registering service Usually it happens in app/config/services.php file.

use Huuuk\Queues\QueueManager;

// other services

$queueManager = new QueueManager($config);
$di->set('queue', $queueManager->getQueue());

Since Phalcon web and console applications has different bootstrap points, you should register queue in your console app bootstrap file too.

How to set up cli application?
You can look over official docs
Also we have an example for you.

####Console command Make QueueTask class and place it in app/tasks directory

Don't forget to include this directory in autoloader of your console app

<?php

class QueueTask extends \Huuuk\Queues\QueueTask
{
    
}

Make sure that you done it properly

php app/cli queue help

##Drivers For now we support only 3 types of drivers: ####Null It's some kind of stub. All jobs pushed to this queue will never be fired.
To use this driver set driver property to null in queue section of your config.ini file

[queue]
driver = null

####Synchronus All jobs pushed to this queue will be fired immediatly.
To use this driver set driver property to sync in queue section of your config.ini file

[queue]
driver = sync

####Database All jobs pushed to this queue will be stored in database table. And will be processed by worker.
To use this driver set driver property to database, and database.table in queue section of your config.ini file

[queue]
driver = database
database.jobs_table = queue_jobs

Then you need to create queue table.
Just run queue task

php app/cli queue dbtable --create
# for a bit more info run
# php app/cli queue --help

Or you can do it manually by executing SQL query

CREATE TABLE IF NOT EXISTS `YOUR_TABLE_NAME` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `queue` varchar(255) NOT NULL,
  `payload` longtext NOT NULL,
  `available_at` int(10) unsigned NOT NULL,
  `created_at` int(10) unsigned NOT NULL,
  `attemps` tinyint(3) unsigned NOT NULL,
  `done` tinyint(1) NOT NULL,
  `failed` tinyint(1) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `id` (`id`)
);

##Usage ###Pushing Jobs You can push job to queue by following code

// where di it's your DI instance
$di->getQueue()->push(function() {
    // do yuor magic
    });

if you need access to DI instance, just pass it to the closure

// where di it's your DI instance
$di->getQueue()->push(function($di) {
    $di->getMailer()->send($address);
    });

NOTE: If you use null or sync driver, DI instance defined in your web application bootstrap file will be passed to the job.
In other cases to thw job will be passed DI instance from your caonsole app.
So it's all up to you to manage services that requires your jobs.
For example in your web app you have Mailer service

// app/config/services.php
$di->set('mailer', '\SomeNamespace\SomeClassName');

and some of your jobs uses it

$di->getQueue()->push(function($di) {
    $di->getMailer()->send($address);
    });

So to fire job properly, you need to register the same service in your console app DI too.

// app/cli
$di->set('mailer', '\SomeNamespace\SomeClassName');

As all anonymous functions, our "jobs" supports variables from the parent scope.

$user = User::findFirstById($id);
$di->getQueue()->push(function($di) use($user) {
    $user->sendNotification();
    });

You can also wrap your job logic in specific class

use Huuuk\Queues\Job;

class SendMail extends Job
{
    protected $address;
    
    function __construct($address)
    {
        $this->$address = $address;
    }

    public function handle()
    {
        // Place here yor logic
        // to access DI, call
        // $this->getDi();
        $this->getDi()->getMailer()->send($this->address);
    }
}

and push instance of this class to thr queue

$user = User::findFirstById($id);
$job = new SendMail($user->email);
$di->getQueue()->push($job);

Sometimes you need to split your jobs into different queues, for some reasons. You can pass addition parameter to push method.

$di->getQueue()->push(new SimpleJob); // queue name = 'default'
$di->getQueue()->push(new SimpleJob, null); // queue name = 'default'
$di->getQueue()->push(new SenDMail, 'mail');
$di->getQueue()->push(new ImageResize, 'images');

Also sometimes useful to delay job firing.

$di->getQueue()->push(new SimpleJob, null, 30); //  no earlier than 30 seconds
$di->getQueue()->push(new SenDMail, 'mail', 60*60); //  no earlier than 30 seconds an hour
$di->getQueue()->push(new ImageResize, 'images'); // as soon as worker will be run

###Worker To start working on queue run

php app/cli queue work

For more info run

php app/cli queue help

##Todo

  • Benstalk driver
  • Redis driver

统计信息

  • 总下载量: 2.78k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-07-19

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固