Code Monkey home page Code Monkey logo

yii-resque's Introduction

Yii Resque

Yii resque is a component for Yii to queue your background jobs, this component based on php-resque and php-resque-scheduler with some enhancement for support phpredis, I'm also added log handler using Monolog, already tested with ResqueBoard.

Requirement

  • php pnctl extension.
  • Redis.io
  • phpredis extension for better performance, otherwise it'll automatically using Credis as fallback.
  • Yii Framework >1.1.x

Configuration

  • Copy files to each folder
  • Add to your config.php and your console.php
    ...
    'components'=>array(
        ...
        'resque'=>array(
            'class' => 'application.components.yii-resque.RResque',
            'server' => 'localhost',    // Redis server address
            'port' => '6379',           // Redis server port
            'database' => 0,             // Redis database number
            'password' => ''            // Redis password auth, set to '' or null when no auth needed
        ),
        ...
    )
    ...
  • Change path for Yii framework folder in components/yii-resque/bin/resque

  • You may want to add these additional lines to your console.php for fixing auto loading and enabling the usage of models and helpers inside your workers

    ...
    'import' => array(
        'application.models.*',
        'application.helpers.*',
        'application.components.*',
    ),

How to

Create job and Workers

You can put this line where ever you want to add jobs to queue

    Yii::app()->resque->createJob('queue_name', 'Worker_ClassWorker', $args = array());

Put your workers inside Worker folder and name the class with Worker_ as prefix, e.g you want to create worker with name SendEmail then you can create file inside Worker folder and name it SendEmail.php, class inside this file must be Worker_SendEmail

Create Delayed Job

You can run job at specific time

    $time = 1332067214;
    Yii::app()->resque->enqueueJobAt($time, 'queue_name', 'Worker_ClassWorker', $args = array());

or run job after n second

    $in = 3600;
    $args = array('id' => $user->id);
    Yii::app()->resque->enqueueIn($in, 'email', 'Worker_ClassWorker', $args);

Create Recurring Job

This is some trick that sometime useful if you want to do some recurring job like sending weekly newsletter, I just made some modification in my worker tearDown event

    public function tearDown()
    {
        $interval = 3600; # This job will repeat every hour

        # Add next job queue based on interval
        Yii::app()->resque->enqueueJobIn($interval, 'queue_name', 'Worker_Newsletter', $args = array());
    }

So everytime job has done completely the worker will send queue for same job.

Get Current Queues

This will return all job in queue (EXCLUDE all active job)

    Yii::app()->resque->getQueues();

Start and Stop workers

Run this command from your console/terminal :

Start queue

    yiic rresque start

or

    yiic rresque start --queue=queue_name --interval=5 --verbose=0

Start delayed or scheduled queue

    yiic rresque startrecurring

Stop queue

    yiic rresque stop

Stop queue with QUIT signal

    yiic rresque stop --quit=true

Start Worker Options

This is available options for starting worker using yiic command :

  • Set queue name
--queue=[queue_name]

This option default to * means all queue.

  • Set interval time
--interval=[time in second]

Set your interval time for checking new job.

  • Run in verbose mode
--verbose=[1 or 0]

Set to 1 if you want to see more information in log file.

  • Number of worker
--count=[integer]
  • Log handler name and log handler target
--loghandler=[string] --logtarget=[string]

You can see available log handler at Monolog-Init.

Copyrights

Copyright (c) 2013 Rolies106

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Contributors

  • rolies106
  • chrisschrijver
  • summic
  • anasAsh
  • You

yii-resque's People

Contributors

dbanck avatar rolies106 avatar

Watchers

 avatar  avatar  avatar

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.