Skip to content

Instantly share code, notes, and snippets.

@alnutile
Last active August 29, 2015 14:06
Show Gist options
  • Save alnutile/0210bf94423a1c57205a to your computer and use it in GitHub Desktop.
Save alnutile/0210bf94423a1c57205a to your computer and use it in GitHub Desktop.

First I call in the queue and load it into a service

custom_start.php:154

$env = $core->getEnv();
require_once $core->getAppPath() . "/config/{$env}/queue.php";

$core->setQueueLogging();

This is the queue.php file above app/config/local/queue.php

<?php

use App\Queue\QueueWorker;


/**
 * Get settings from the .env files
 */

$host = $_ENV['BEAN_HOST'];
$queueName = $_ENV['BEAN_QUEUE'];


/**
 * Register Events just to show how this works and to see it in the get request results
 */

$core->getApp()['dispatcher']->addListener('queue.job.removed.success', function($event, $event_name, $eventDispatcher) use ($core) {
    //Get Event?
    $event->getApp()['monolog']->addInfo(sprintf("Event: Queue Job Removed Success %s", $event_name));
});

$core->getApp()['dispatcher']->addListener('queue.job.added.error', function($event) use ($core) {
    $event->getApp()['monolog']->addInfo("Logging from queue.job.added.error message" . $event->getMessage());
});


$core->getApp()['dispatcher']->addListener('queue.job.added.success', function($event) use ($core) {
    $event->getApp()['monolog']->addInfo("Logging from queue.job.added.success vai passed Dispatcher " . $event->getJob());
});

$core->getApp()['dispatcher']->addListener('illuminate.queue.failed', function($event, $event_name, $eventDispatcher) use ($core) {
    //Get Event?
    $message = $event->getMessage();
    $core->getApp()['monolog']->addInfo(sprintf("Queue Job Failed $message"));
});

/**
 * Setup the Illuminate Queue others can be used of course
 * once we setup a standard interface for the agreed methods
 */
$core->getApp()['container.illuminate'] = new \Illuminate\Container\Container();
//@TODO is this one needed now that I pass it to the worker?
$core->getApp()['container.illuminate']->singleton('dispatcher', $core->getApp()['dispatcher'], true);
$core->getApp()['queue.illuminate'] = function() use ($core, $host, $queueName) {
    $queue = new \Illuminate\Queue\Capsule\Manager($core->getApp()['container.illuminate']);
    $queue->addConnection([
        'driver' => 'beanstalkd',
        'host'   => $host,
        'queue'  => $queueName
    ], 'default');
    $queue->getContainer()->bind('encrypter', function(){
       return new Illuminate\Encryption\Encrypter('foobar');
    });
    $queue->setAsGlobal();
    return $queue;
};
$core->getApp()['queue.illuminate.queue_name'] = $_ENV['BEAN_QUEUE'];
$core->getApp()['queue.illuminate.worker'] = new QueueWorker($core->getApp()['queue.illuminate']->getQueueManager(), null, $core->getApp()['dispatcher']);

return $core;

custom_start.php:206

/**
 * Trigger jobs for the queue by name or default
 */
$core->getApp()->get('/trigger_queue/{queue_name}', function($queue_name) use ($core){
    try {

        //Start more than one job
        $number_of_jobs     = 0;
        $max_jobs_to_get    = 10;
        $processed_jobs     = 0;
        $count              = 0;

        /**
         * If jobs in queue = 0 it just hanges
         * need to break here if jobs = 0
         */
        while($max_jobs_to_get >= 0 || $number_of_jobs >= 0) {
            $get_job = $core->getApp()['queue.illuminate.worker']->pop('default', $queue_name, 3, 0, 3, 3);
            if(is_object($get_job['job'])) {
                $max_jobs_to_get = $max_jobs_to_get - 1;
                $number_of_jobs  = $number_of_jobs  - 1;
                $count++;
                $processed_jobs++;
            } else {
                sleep(2);
                return $core->getApp()->json("No Jobs");
            }
        }
        $job_info = 'Done with jobs count ' . $processed_jobs;
        return $core->getApp()->json("Job Cleared " . $job_info);
    } catch (\Exception $e) {
        $core->getApp()['dispatcher']->dispatch('illuminate.queue.failed', new FilteredQueueEventError($core->getApp()['queue.illuminate'], $e->getMessage(), $core->getApp()));
        return $core->getApp()->json("Error getting job ". $e->getMessage());
    }
})->value('queue_name', 'default’);

Then I have a worker running

php run_report_all_queue.php

Make sure to update your .evn file for ALL the $_ENV items above

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment