Coder Social home page Coder Social logo

garesque's Introduction

#Yii2-resque(best,easy) ##Install You need to use composer Primarily, You need to add the following code toroot package(project directory), because the newest php-resque is dev-master, the tagged version is oldest.

{
    "require":{
        ...
        "chrisboulton/php-resque": "@dev"
    }
}

Then run:

composer require ga/resque dev-master

##Set up

###For Yii2-basic

1.Copy the ResqueController.txt to command directory and renamed ResqueController.php, and then modify the namespace to app\commands.

2.Modify console.php in config directory, add the following code.

   'resque' => [
       'class' => 'ga\resque\GAResque',
       'server' => '127.0.0.1',     // Redis server address
       'port' => '6379',            // Redis server port
       'database' => 0,             // Redis database number
   ],

3.Create *Job.php in models directory.

example:

MyJob.php
<?php
namespace app\models;
class MyJob
{ 
    public function setUp()
    {
        // ... Set up environment for this job
    }    
    public function perform()
    {
        echo "1\n";
        $this->args['name'];
    }
    
    public function tearDown()
    {
        // ... Remove environment for this job
    }
}

The perform() method will deal with work,setUp() will run at begin of work, tearDown()will run at finished work. **NOTICE:**You must enqueue with \Yii::$app->resque->dequeue('default', 'app\models\MyJob', [1], true);, Yii2 find the file by autoloader according to namesapce.

For Yii2-advanced

1.Copy the ResqueController.txt to console/controllers directory and renamed ResqueController.php, and then modify the namespace to console\controllers.

2.Modify console/main.php in config directory, add the following code.

   'resque' => [
       'class' => 'ga\resque\GAResque',
       'server' => '127.0.0.1',     // Redis server address
       'port' => '6379',            // Redis server port
       'database' => 0,             // Redis database number
   ],

3.Create *Job.php in models(include console,frontend,backend,common) directory.

example:

MyJob.php
<?php
namespace backend\models;
namespace console\models;
namespace frontend\models;
namespace common\models;
class MyJob
{ 
    public function setUp()
    {
        // ... Set up environment for this job
    }    
    public function perform()
    {
        echo "1\n";
        $this->args['name'];
    }
    
    public function tearDown()
    {
        // ... Remove environment for this job
    }
}

The perform() method will deal with work,setUp() will run at begin of work, tearDown()will run at finished work.

**NOTICE:**You must enqueue with \Yii::$app->resque->dequeue('default', 'backend\models\MyJob', [1], true);, Yii2 find the file by autoloader according to namesapce.

Usage

Enqueue and dequeue the job task.

   \Yii::$app->resque->enqueue('default', 'BadJob', [1], true);
   \Yii::$app->resque->dequeue('default', 'BadJob', [1], true);
   \Yii::$app->resque->size('default');
   ...

More information you can also see the php-resque.

NOTICE: dequeue() not available now, can be use in the future.

Param default is queue name,BadJob is the class of which will complete the work. Param [1] is array params for BadJob. In the root of project directory, run following code:

nohup QUEUE=* ./yii resque &

NOTICE:* means will start all queues.You can replace * with queue name or like thisQUEUE=default1,default2 will run specified queue.

garesque's People

Contributors

liuzexin avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Forkers

chkartik

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.