Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow the subscription broadcast job queue to be configured #1301

Merged
merged 10 commits into from
Apr 19, 2020
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"require": {
"php": ">= 7.1",
"ext-json": "*",
"illuminate/bus": "5.5.* || 5.6.* || 5.7.* || 5.8.* || ^6.0 || ^7.0",
"illuminate/contracts": "5.5.* || 5.6.* || 5.7.* || 5.8.* || ^6.0 || ^7.0",
"illuminate/http": "5.5.* || 5.6.* || 5.7.* || 5.8.* || ^6.0 || ^7.0",
"illuminate/pagination": "5.5.* || 5.6.* || 5.7.* || 5.8.* || ^6.0 || ^7.0",
Expand Down
7 changes: 5 additions & 2 deletions src/Subscriptions/Events/BroadcastSubscriptionEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
namespace Nuwave\Lighthouse\Subscriptions\Events;

use Illuminate\Queue\SerializesModels;
use Nuwave\Lighthouse\Schema\Types\GraphQLSubscription as Subscription;
use Nuwave\Lighthouse\Schema\Types\GraphQLSubscription;

/**
* @deprecated will be removed in v5 and replaced with a Job
*/
class BroadcastSubscriptionEvent
{
use SerializesModels;
Expand All @@ -21,7 +24,7 @@ class BroadcastSubscriptionEvent

public $root;

public function __construct(Subscription $subscription, string $fieldName, $root)
public function __construct(GraphQLSubscription $subscription, string $fieldName, $root)
{
$this->subscription = $subscription;
$this->fieldName = $fieldName;
Expand Down
28 changes: 17 additions & 11 deletions src/Subscriptions/Events/BroadcastSubscriptionListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,23 @@

namespace Nuwave\Lighthouse\Subscriptions\Events;

use Illuminate\Contracts\Queue\ShouldQueue;
use Nuwave\Lighthouse\Subscriptions\Contracts\BroadcastsSubscriptions;
use Illuminate\Contracts\Bus\Dispatcher as BusDispatcher;
use Nuwave\Lighthouse\Subscriptions\Job\BroadcastSubscriptionJob;

class BroadcastSubscriptionListener implements ShouldQueue
/**
* @deprecated This class is just here to preserve backwards compatiblity with v4.
* TODO remove the event and handle subscriptions as commands only in v5
*/
class BroadcastSubscriptionListener
{
/**
* @var \Nuwave\Lighthouse\Subscriptions\Contracts\BroadcastsSubscriptions
* @var \Illuminate\Contracts\Events\Dispatcher
*/
protected $broadcaster;
protected $busDispatcher;

public function __construct(BroadcastsSubscriptions $broadcaster)
public function __construct(BusDispatcher $busDispatcher)
{
$this->broadcaster = $broadcaster;
$this->busDispatcher = $busDispatcher;
}

/**
Expand All @@ -24,10 +28,12 @@ public function __construct(BroadcastsSubscriptions $broadcaster)
*/
public function handle(BroadcastSubscriptionEvent $event): void
{
$this->broadcaster->broadcast(
$event->subscription,
$event->fieldName,
$event->root
$this->busDispatcher->dispatch(
(new BroadcastSubscriptionJob(
$event->subscription,
$event->fieldName,
$event->root
))->onQueue(config('lighthouse.subscriptions.broadcasts_queue_name', null))
);
}
}
50 changes: 50 additions & 0 deletions src/Subscriptions/Job/BroadcastSubscriptionJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Nuwave\Lighthouse\Subscriptions\Job;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Nuwave\Lighthouse\Schema\Types\GraphQLSubscription;
use Nuwave\Lighthouse\Subscriptions\Contracts\BroadcastsSubscriptions;

class BroadcastSubscriptionJob implements ShouldQueue
{
use Queueable;
stayallive marked this conversation as resolved.
Show resolved Hide resolved

/**
* The subscription field that was requested.
*
* @var \Nuwave\Lighthouse\Schema\Types\GraphQLSubscription
*/
public $subscription;

/**
* The name of the field.
*
* @var string
*/
public $fieldName;

/**
* The root element to be passed when resolving the subscription.
*
* @var mixed
*/
public $root;

public function __construct(GraphQLSubscription $subscription, string $fieldName, $root)
{
$this->subscription = $subscription;
$this->fieldName = $fieldName;
$this->root = $root;
}

public function handle(BroadcastsSubscriptions $broadcaster): void
{
$broadcaster->broadcast(
$this->subscription,
$this->fieldName,
$this->root
);
}
}
1 change: 1 addition & 0 deletions src/Subscriptions/SubscriptionBroadcaster.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public function __construct(
*/
public function queueBroadcast(GraphQLSubscription $subscription, string $fieldName, $root): void
{
// TODO replace with a job dispatch
$this->eventsDispatcher->dispatch(
new BroadcastSubscriptionEvent($subscription, $fieldName, $root)
);
Expand Down
7 changes: 6 additions & 1 deletion src/lighthouse.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,11 @@
*/
'queue_broadcasts' => env('LIGHTHOUSE_QUEUE_BROADCASTS', true),

/*
* Determines the queue to use for broadcasting queue jobs.
*/
'broadcasts_queue_name' => env('LIGHTHOUSE_BROADCASTS_QUEUE_NAME', null),

/*
* Default subscription storage.
*
Expand All @@ -254,7 +259,7 @@
'storage' => env('LIGHTHOUSE_SUBSCRIPTION_STORAGE', 'redis'),

/*
* Default subscription storage time to live.
* Default subscription storage time to live in seconds.
*
* Indicates how long a subscription can be active before it's automatically removed from storage.
* Setting this to `null` means the subscriptions are stored forever. This may cause
Expand Down