Giter Site home page Giter Site logo

yii2-queue's Introduction

##General Queue component for yii2

Currently only supports beanstalkd driver but can easily be extended to support other types

##Installation

composer require djeux/yii2-queue

##Configuration In your config/main.php (because you need to setup the queue to work in both console and web)

'components' => [
    'queue' => [
        'class' => 'djeux\queue\BeanstalkdQueueManager', // in this example we use beanstalkd driver
        'host' => '127.0.0.1',
        'worker' => [
            'listen' => ['default', /* here you define what tubes should the worker listen to */]
        ],
    ],
]

##Usage In your code you can push jobs two ways

  1. By passing an object which implements 'Queueable' interface
Yii::$app->queue->push(new MyJob("text to push"), '', 'default');

class MyJob implements Queueable
{   
    protected $pushedText;
    
    public function __construct($text)
    {
        $this->pushedText = $text;
    }
    
    /**
     * @param $job BaseJob
     */
    public function handle($job)
    {
        echo $this->pushedText;
        $job->delete();
    }
}
  1. By passing a string with class and method separated by '@'
Yii::$app->queue->push('app\components\queue\MyJob@myMethod', 'text to push', 'default');

class MyJob
{   
    public function myMethod($job, $data)
    {
        echo $data;
        $job->delete();
    }
}

Workers

You can setup workers to run from supervisord

There are two main methods

php yii queue/manager

Runs a manager which checks the configured worker and launched a process for each listed queue

php yii queue/worker <queue>

Which runs a daemon that processes queue jobs

Troubleshooting

If you have jobs that use database connection, and they're running in daemon mode, the server will eventually disconnect you. @see wait_timeout

To counter that there's a trait in \djeux\queue\helpers\TimeoutTrait which you can call to keep the jobs processing and reconnect if the connection is dropped

Same goes for swiftmailer if you sending emails. The smtp will eventually disconnect you with

421 Timeout waiting for data from client.

To counter that you can use

try {
    \Yii::$app->mailer->send($message);
} catch (\Exception $e) {
    if (strpos(strtolower($e->getMessage()), '421 timeout') !== false) {
        if (($mailer = \Yii::$app->mailer) instanceof Mailer) {
            /* @var $mailer Mailer */
            $mailer->getSwiftMailer()->getTransport()->stop();
            $this->send($job, $data);
        }
    }
}

You can add methods like "beforeJob" and "afterJob" which will be called before and after a job is processed.

#P.S. Package inspiration taken from Laravel implementation of queue system.

For any suggestions, fixes and hate posts, please write issues.

yii2-queue's People

Contributors

djeux avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

names

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.