-
-
Notifications
You must be signed in to change notification settings - Fork 834
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
wip queue implementation #1096
wip queue implementation #1096
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of Flarum. | ||
* | ||
* (c) Toby Zerner <toby.zerner@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Flarum\Core\Jobs; | ||
|
||
use Flarum\Core\Queue\AbstractJob; | ||
use Illuminate\Contracts\Mail\Mailer; | ||
use Illuminate\Mail\Message; | ||
|
||
class MailJob extends AbstractJob | ||
{ | ||
/** | ||
* Body of the mail message. | ||
* | ||
* @var string | ||
*/ | ||
protected $body; | ||
|
||
/** | ||
* Receiver email address. | ||
* | ||
* @var string | ||
*/ | ||
protected $to; | ||
|
||
/** | ||
* Subject of the mail message. | ||
* | ||
* @var string | ||
*/ | ||
protected $subject; | ||
|
||
public function __construct($subject, $body, $to) | ||
{ | ||
$this->subject = $subject; | ||
$this->body = $body; | ||
$this->to = $to; | ||
} | ||
|
||
public function handle(Mailer $mailer) | ||
{ | ||
$mailer->raw($this->body, function (Message $message) { | ||
$message | ||
->to($this->to) | ||
->subject($this->subject); | ||
}); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of Flarum. | ||
* | ||
* (c) Toby Zerner <toby.zerner@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Flarum\Core\Queue; | ||
|
||
use Illuminate\Contracts\Bus\SelfHandling; | ||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
use Illuminate\Queue\InteractsWithQueue; | ||
use Illuminate\Queue\SerializesModels; | ||
|
||
abstract class AbstractJob implements ShouldQueue, SelfHandling | ||
{ | ||
use InteractsWithQueue, SerializesModels; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of Flarum. | ||
* | ||
* (c) Toby Zerner <toby.zerner@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Flarum\Core\Queue; | ||
|
||
use Illuminate\Queue\QueueManager as Manager; | ||
|
||
class QueueManager extends Manager | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function getConfig($name) | ||
{ | ||
if ($name === 'sync') { | ||
return ['driver' => 'sync']; | ||
} | ||
|
||
return parent::getConfig($name); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getDefaultDriver() | ||
{ | ||
return 'sync'; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of Flarum. | ||
* | ||
* (c) Toby Zerner <toby.zerner@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Flarum\Core\Queue; | ||
|
||
use Illuminate\Bus\Dispatcher; | ||
use Illuminate\Contracts\Bus\Dispatcher as DispatcherContract; | ||
use Illuminate\Encryption\Encrypter; | ||
use Illuminate\Encryption\McryptEncrypter; | ||
use Illuminate\Queue\QueueServiceProvider as Provider; | ||
|
||
class QueueServiceProvider extends Provider | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function registerManager() | ||
{ | ||
$this->registerEncrypter(); | ||
$this->registerQueue(); | ||
$this->registerDispatcher(); | ||
} | ||
|
||
protected function registerEncrypter() | ||
{ | ||
$this->app->singleton('encrypter', function ($app) { | ||
$key = md5($app->url()); | ||
|
||
$cipher = 'AES-256-CBC'; | ||
|
||
if (Encrypter::supported($key, $cipher)) { | ||
return new Encrypter($key, $cipher); | ||
} elseif (McryptEncrypter::supported($key, $cipher)) { | ||
return new McryptEncrypter($key, $cipher); | ||
} else { | ||
throw new RuntimeException('No supported encrypter found. The cipher and / or key length are invalid.'); | ||
} | ||
}); | ||
} | ||
|
||
protected function registerQueue() | ||
{ | ||
$this->app->singleton('queue', function ($app) { | ||
// Once we have an instance of the queue manager, we will register the various | ||
// resolvers for the queue connectors. These connectors are responsible for | ||
// creating the classes that accept queue configs and instantiate queues. | ||
$manager = new QueueManager($app); | ||
|
||
// We've disabled all other queue connectors for now. Once we're able to | ||
// configure any of the other connectors, we can easily reset the old | ||
// behavior of the parent provider by registering all connectors. | ||
$this->registerSyncConnector($manager); | ||
|
||
return $manager; | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we need the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @franzliedke so you want me to only set up the default connection and then call the QueueServiceProvider of Illuminate? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No. Only register the connection instance. We don't need a manager at all, I think, because we don't need the dynamic switching between different connections. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For simplicity purposes we can go that direction. Although I would know of situations where an extension might be happy to use separate queue drivers. Ok, thanks! |
||
|
||
$this->app->singleton('queue.connection', function ($app) { | ||
return $app['queue']->connection(); | ||
}); | ||
} | ||
|
||
protected function registerDispatcher() | ||
{ | ||
$this->app->singleton(DispatcherContract::class, function ($app) { | ||
return new Dispatcher($app, function () use ($app) { | ||
return $app['queue']->connection(); | ||
}); | ||
}); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of Flarum. | ||
* | ||
* (c) Toby Zerner <toby.zerner@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Flarum\Core\Support; | ||
|
||
use Flarum\Core\Queue\AbstractJob; | ||
use Illuminate\Contracts\Bus\Dispatcher; | ||
|
||
trait DispatchJobsTrait | ||
{ | ||
/** | ||
* @var Dispatcher | ||
*/ | ||
protected $queue; | ||
|
||
/** | ||
* @param AbstractJob $job | ||
* @return mixed | ||
*/ | ||
public function dispatch(AbstractJob $job) | ||
{ | ||
return $this->queue->dispatch($job); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Honestly, I don't think we need this trait, if all it does is shortening
$this->queue->dispatch()
to$this->dispatch()
, at the cost of one more step to find out where this method is coming from, and why we need to inject the seemingly-unused$queue
attribute. :)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You might want to create an issue then to remove the need of a DispatchesEventTrait as well, it does exactly the same useless shortcut :) It might make more sense if the trait would have loaded the bound Dispatcher from my perspective.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To be fair, that one at least encapsulates some logic ;)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Must have missed that 👍