2017 © Pedro Peláez
 

library yii2-tasks

Yii2 background task component

image

alex-salt/yii2-tasks

Yii2 background task component

  • Thursday, May 11, 2017
  • by alex_salt
  • Repository
  • 1 Watchers
  • 0 Stars
  • 974 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 1 Open issues
  • 3 Versions
  • 1 % Grown

The README.md

yii2-tasks

Component for running background tasks inside a react/event-loop., (*1)

Install

composer require alex-salt/yii2-amqp

Usage

Configure application:

return [
    'components' => [
        'rmq' => [
            'class' => \alexsalt\amqp\Connection::class,
            'host' => 'localhost',
        ],
        'tasks' => [
            'class' => \alexsalt\tasks\TaskQueueComponent::class,
            'rmq' => 'rmq',
            'defaultQueue' => 'tasks',
        ],
    ],
];

Create a task

use alexsalt\tasks\TaskInterface;

class MyTask implements TaskInterface {
    public $a;

    public $b = 1;

    public function run() {
        // your magic code
    }
}

Send a task:

$task = new MyTask([ 'a' => 1, 'b' => 5 ]);
Yii::$app->tasks->send($task);

A task worker:

use alexsalt\tasks\TaskWorker;

class WorkerController extends \yii\console\Controller {
    public function actionMyTask() {
        $worker = new TaskWorker([
            'queue' => 'tasks',
        ]);
        $worker->start();
    }
}

Custom worker

use alexsalt\tasks\BaseMessage;
use alexsalt\tasks\AbstractWorker;

// declare message class with structure
class MyMessage extends BaseMessage {
    public $a;

    public $b;
}

// declare worker with our handler
class MyWorker extends AbstractWorker {
    /**
     * @param MyMessage $msg
     */
    public function processMessage(BaseMessage $msg) {
        if ($msg->a - $msg->b) {
            return $this->ack($msg);
        } else if (!$msg->b) {
            Yii::error('dont like the message');
            return $this->drop($msg);
        } else {
            return $this->nack($msg);
        }
    }
}

// create and send message
$msg = new MyMessage([ 'a' => 1 ]);
Yii::$app->rmq->sendToQueue($msg, 'my-queue');

// start worker that does stuff
$worker = new MyWorker([ 'queue' => 'my-queue' ]);
$worker->start();

The Versions

11/05 2017

dev-master

9999999-dev https://github.com/holycheater/yii2-tasks

Yii2 background task component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Alex Salt

rabbitmq yii2 react amqp tasks event-loop

11/05 2017

v1.0.1

1.0.1.0 https://github.com/holycheater/yii2-tasks

Yii2 background task component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Alex Salt

rabbitmq yii2 react amqp tasks event-loop

10/04 2017

v1.0.0

1.0.0.0 https://github.com/holycheater/yii2-tasks

Yii2 background task component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Alex Salt

rabbitmq yii2 react amqp tasks event-loop