How to use Laravel queue and events as standalone

  events, illuminate, laravel, php, queue

Our company’s CRM/ERP is a bit old and we are actively working on it. But we are planning to migrate to laravel soon. I had chance to migrate some illuminate packages into project to help this migration (So i don’t need to rewrite all).
I used https://github.com/mattstauffer/Torch/ for "how to"’s.
Now I have to trigger some events and send some listener over queue. I was able to make events and queue work seperately. But i want to make some listeners to be queued, like in laravel.

Codes:

<?php
// init.php
require './vendor/autoload.php';


use AppAppFactory;
use IlluminateEventsEventServiceProvider;
use IlluminateRedisRedisManager;
use IlluminateQueueCapsuleManager as Queue;
$container = AppFactory::getInstance();
$container->instance('app', $container);
(new EventServiceProvider($container))->register();

$container['events']->listen(AppEventsPublishEvent::class, AppListenersPublishListener::class);
$container['events']->listen(AppEventsPublishEvent::class, '[email protected]');


$container->bind('redis', function () use ($container) {
  return new RedisManager($container, 'predis', [
      'default' => [
          'host' => '127.0.0.1',
          'password' => null,
          'port' => 6379,
          'database' => 0,
      ],
  ]);
});

$container->bind('exception.handler', AppExceptionHandler::class);
$queue = new Queue($container);
$queue->addConnection([
    'driver' => 'redis',
    'connection' => 'default',
    'queue' => 'default',
]);

$container['queue'] = $queue->getQueueManager();
class DoThing
{
    public function fire($job, $data)
    {
        $handle = fopen('proof.txt', 'a');
        fwrite($handle, PHP_EOL . $data['string']);
        $job->delete();
    }
}

<?php
// trigger-event.php

require("init.php");
container['events']->dispatch(new AppEventsPublishEvent('data'));
<?php
// worker.php
require "init.php";

$queue = $container['queue'];
$events = $container['events'];
$handler = $container['exception.handler'];
$isDownForMaintenance = function () use ($container) {
    return $container->isDownForMaintenance();
};

$worker = new IlluminateQueueWorker($queue, $events, $handler, $isDownForMaintenance);
$options = new IlluminateQueueWorkerOptions();

$worker->runNextJob('default', 'default', $options);
<?php
// PublishEvent

namespace AppEvents;

class PublishEvent
{
    protected $data;

    public function __construct($data)
    {
        $this->data = $data;
    }

    /**
     * @return mixed
     */
    public function getData()
    {
        return $this->data;
    }
}

<?php
// Listener

namespace AppListeners;

use AppEventsPublishEvent;
use IlluminateContractsQueueShouldQueue;

class QueueListener implements ShouldQueue
{

    public function handle(PublishEvent $event)
    {
        $handle = fopen('proof.txt', 'a');
        fwrite($handle, PHP_EOL . "Queue");
    }

}


I get the following error:

 Fatal error: Uncaught IlluminateContractsContainerBindingResolutionException: Target [IlluminateContractsQueueFactory] is not instantiable.
 

I tried many different stuff but couldn’t make it work.
Thank you for help!

Source: Laravel

Leave a Reply