PHP code example of urbanindo / yii2-queue

1. Go to this page and download the library: Download urbanindo/yii2-queue library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

urbanindo / yii2-queue example snippets


return [
    // ...
    'controllerMap' => [
        'queue' => [
            'class' => 'UrbanIndo\Yii2\Queue\Console\Controller',
            //'sleepTimeout' => 1
        ],
    ],
];

'modules' => [
    'task' => [
        'class' => 'app\modules\task\Module',
    ]
]

'components' => [
    'queue' => [
        'class' => 'UrbanIndo\Yii2\Queue\Queues\SqsQueue',
            'module' => 'task',
            'url' => 'https://sqs.ap-southeast-1.amazonaws.com/123456789012/queue',
            'config' => [
                'credentials' => [
                    'key' => 'AKIA1234567890123456',
                    'secret' => '1234567890123456789012345678901234567890'
                ],
                'region' => 'ap-southeast-1',
                'version' => 'latest'
            ]
        ]
    ]
]

'components' => [
    'db' => [
        // the db component
    ],
    'queue' => [
        'class' => 'UrbanIndo\Yii2\Queue\Queues\DbQueue',
        'db' => 'db',
        'tableName' => 'queue',
        'module' => 'task',
        // sleep for 10 seconds if there's no item in the queue (to save CPU)
        'waitSecondsIfNoQueue' => 10,
    ]
]

class FooController extends UrbanIndo\Yii2\Queue\Worker\Controller
{
    public function actionBar($param1, $param2)
    {
        echo $param1;
    }
}

class FooController extends UrbanIndo\Yii2\Queue\Worker\Controller
{
    public function actionBar($param1, $param2)
    {
        try {
            // do some stuff
        } catch (\Exception $ex) {
            \Yii::error('Ouch something just happened');
            return false;
        }
    }
}

use UrbanIndo\Yii2\Queue\Job;

$route = 'foo/bar';
$data = ['param1' => 'foo', 'param2' => 'bar'];
Yii::$app->queue->post(new Job(['route' => $route, 'data' => $data]));

Yii::$app->queue->post(new Job(function() {
    echo 'Hello World!';
}));

    public function behaviors()
    {
        return [
            [
                'class' => \UrbanIndo\Yii2\Queue\Behaviors\DeferredEventBehavior::class,
                'events' => [
                    self::EVENT_AFTER_VALIDATE => 'deferAfterValidate',
                ]
            ]
        ];
    }

    public function deferAfterValidate()
    {
        // do something here
    }

'controllerMap' => [
    'queue' => [
        /* @var $queue UrbanIndo\Yii2\Queue\Web\Controller */
        'class' => 'UrbanIndo\Yii2\Queue\Web\Controller'
    ]
],

'controllerMap' => [
    'queue' => [
        /* @var $queue UrbanIndo\Yii2\Queue\Web\Controller */
        'class' => 'UrbanIndo\Yii2\Queue\Web\Controller',
        'as access' => [
            'class' => '\yii\filters\AccessControl',
            'rules' => [
                [
                    'allow' => true,
                    'ips' => [
                        '127.0.0.1'
                    ]
                ]
            ]
        ]
    ]
],

php composer.phar