PHP code example of kaidenchen / yii2-resque-enhance

1. Go to this page and download the library: Download kaidenchen/yii2-resque-enhance 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/ */

    

kaidenchen / yii2-resque-enhance example snippets


        namespace app\commands;
        use Yii;
        use yii\console\Controller;
        use resque\lib\ResqueAutoloader;
        use resque\lib\Resque\Resque_Worker;
        /**
         *
         * @author Sprytechies
         * @since 2.0
         */
        class ResqueController extends Controller
        {
            /**
             * This command echoes what you have entered as the message.
             * @param string $message the message to be echoed.
             */
            public function actionIndex()
            {

                    $includeFiles=getenv('INCLUDE_FILES');
                    if ($includeFiles) {
                        $includeFiles = explode(',', $includeFiles);
                        foreach ($includeFiles as $file) {
                                                         $REDIS_BACKEND_DB = (!empty($REDIS_BACKEND_DB)) ? $REDIS_BACKEND_DB : 0;
                        Resque::setBackend($REDIS_BACKEND, $REDIS_BACKEND_DB, $REDIS_AUTH);
                    }

                    $logLevel = 0;
                    $LOGGING = getenv('LOGGING');
                    $VERBOSE = getenv('VERBOSE');
                    $VVERBOSE = getenv('VVERBOSE');
                    if(!empty($LOGGING) || !empty($VERBOSE)) {
                        $logLevel = Resque_Worker::LOG_NORMAL;
                    } else if(!empty($VVERBOSE)) {
                        $logLevel = Resque_Worker::LOG_VERBOSE;
                    }

                    $logger = null;
                    $LOG_HANDLER = getenv('LOGHANDLER');
                    $LOG_HANDLER_TARGET = getenv('LOGHANDLERTARGET');

                    if (class_exists('MonologInit_MonologInit')) {
                        if (!empty($LOG_HANDLER) && !empty($LOG_HANDLER_TARGET)) {
                            $logger = new MonologInit_MonologInit($LOG_HANDLER, $LOG_HANDLER_TARGET);
                        } else {
                            fwrite(STDOUT, '*** loghandler or logtarget is not set.'."\n");    
                        }
                    } else {
                        fwrite(STDOUT, '*** MonologInit_MonologInit logger cannot be found, continue without loghandler.'."\n");
                    }

                    $interval = 5;
                    $INTERVAL = getenv('INTERVAL');
                    if(!empty($INTERVAL)) {
                        $interval = $INTERVAL;
                    }

                    $count = 1;
                    $COUNT = getenv('COUNT');
                    if(!empty($COUNT) && $COUNT > 1) {
                        $count = $COUNT;
                    }

                    $PREFIX = getenv('PREFIX');
                    if(!empty($PREFIX)) {
                        fwrite(STDOUT, '*** Prefix set to '.$PREFIX."\n");
                        Resque::redis()->prefix($PREFIX);
                    }

                    if($count > 1) {
                        for($i = 0; $i < $count; ++$i) {
                            $pid = Resque::fork();
                            if($pid == -1) {
                                die("Could not fork worker ".$i."\n");
                            }
                            // Child, start the worker
                            else if(!$pid) {
                                startWorker($QUEUE, $logLevel, $logger, $interval);
                                break;
                            }
                        }
                    }
                    // Start a single worker
                    else {
                        $PIDFILE = getenv('PIDFILE');
                        if ($PIDFILE) {
                            file_put_contents($PIDFILE, getmypid()) or
                                die('Could not write PID information to ' . $PIDFILE);
                        }

                        $this->startWorker($QUEUE, $logLevel, $logger, $interval);
                    }
            }

            function startWorker($QUEUE, $logLevel, $logger, $interval)
            {
                $queues = explode(',', $QUEUE);
                $worker = new Resque_Worker($queues);

                if (!empty($logger)) {
                    $worker->registerLogger($logger);    
                } else {
                    fwrite(STDOUT, '*** Starting worker '.$worker."\n");
                }

                $worker->logLevel = $logLevel;
                $worker->work($interval);
            }
        }

    

    'components' => [
        ...
        'resque' => [ 
            'class' => '\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
        ], 
        ...
    ]
    

    namespace app\components;
    class ClassWorker
    {
        public function setUp()
        {
            # Set up environment for this job
        }

        public function perform()
        {

            echo "Hello World";
            # Run task
        }

        public function tearDown()
        {
            # Remove environment for this job
        }
    }
    

        Yii::$app->resque->createJob('queue_name', 'ClassWorker', $args = []);
    

        $time = 1332067214;
        Yii::$app->resque->enqueueJobAt($time, 'queue_name', 'ClassWorker', $args = []);
    

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

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