4

Hello Good Developers,

I've tried using solution from This Question but it didn't solve my problem hence I am asking it as a separate question.

TL;DR - I want my subscriber Listeners to get executed in a queue asynchronously. My Subscriber calls method from different class on for each Event. I tried to add implements ShouldQueue in my SubscriberClass | My Listener Class | renamed method to handle in Each Class but nothing works. they are still executed synchronously without queue.

I've a Event Subscriber Class: TesterAPI. I added it in EventServiceProvider subscribe array.

I've an Event Class SourceAPIEvents

class SourceAPIEvents
{
    const PROJECT_CREATED = 'project.created';
    const PROJECT_PAUSED = 'project.paused';
    const PROJECT_RESUME = 'project.resume';
    const PROJECT_CLOSED = 'project.closed';

    public $project;
    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct(Project $project)
    {
        $this->project = $project;
    }
}

TesterAPI - Subscriber

class TesterAPI implements ShouldQueue
{

    /**
     * Register the listeners for the subscriber.
     *
     * @param \Illuminate\Events\Dispatcher $events
     */
    public function subscribe($events)
    {
        $events->listen(
            SourceAPIEvents::PROJECT_CREATED,
            'App\Listeners\Internal\Project\SourceAPI\Tester\ProjectCreate@handle'
        );

        $events->listen(
            SourceAPIEvents::PROJECT_RESUME,
            'App\Listeners\Internal\Project\SourceAPI\Tester\ProjectResume@handle'
        );

        $events->listen(
            SourceAPIEvents::PROJECT_PAUSED,
            'App\Listeners\Internal\Project\SourceAPI\Tester\ProjectPause@handle'
        );

        $events->listen(
            SourceAPIEvents::PROJECT_CLOSED,
            'App\Listeners\Internal\Project\SourceAPI\Tester\ProjectClose@handle'
        );
    }
}

Here are my Listener Classes:

class ProjectCreate extends TesterBase implements ShouldQueue
{
    public function handle(SourceAPIEvents $sourceAPIEvent)
    {
        //Do Something
    }

}

class ProjectPause extends TesterBase implements ShouldQueue
{
    public function handle(SourceAPIEvents $sourceAPIEvent)
    {
        //Do Something
    }

}

class ProjectResume extends TesterBase implements ShouldQueue
{
    public function handle(SourceAPIEvents $sourceAPIEvent)
    {
        //Do Something
    }

}

I am calling these events like this

event(SourceAPIEvents::PROJECT_CREATED, new SourceAPIEvents($project));

event(SourceAPIEvents::PROJECT_RESUME, new SourceAPIEvents($project));

Help me understand what I am doing wrong.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.