Skip to content

Commit c660920

Browse files
committed
add laravel queue pkg.
1 parent b7a9979 commit c660920

9 files changed

Lines changed: 402 additions & 0 deletions

File tree

‎pkg/laravel-queue/.gitignore‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
*~
2+
/composer.lock
3+
/composer.phar
4+
/phpunit.xml
5+
/vendor/
6+
/.idea/
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Enqueue\LaravelQueue;
4+
5+
use Illuminate\Queue\Connectors\ConnectorInterface;
6+
use function Enqueue\dsn_to_context;
7+
8+
class DsnToPsrQueueConnector implements ConnectorInterface
9+
{
10+
/**
11+
* {@inheritdoc}
12+
*/
13+
public function connect(array $config)
14+
{
15+
return new PsrQueue(
16+
dsn_to_context($config['dsn']),
17+
isset($config['default']) ? $config['default'] : 'default',
18+
isset($config['time_to_run']) ? $config['time_to_run'] : 0
19+
);
20+
}
21+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
namespace Enqueue\Bundle;
4+
5+
use Enqueue\AmqpExt\AmqpContext;
6+
use Enqueue\AmqpExt\Symfony\AmqpTransportFactory;
7+
use Enqueue\AmqpExt\Symfony\RabbitMqAmqpTransportFactory;
8+
use Enqueue\AsyncEventDispatcher\DependencyInjection\AsyncEventsPass;
9+
use Enqueue\AsyncEventDispatcher\DependencyInjection\AsyncTransformersPass;
10+
use Enqueue\Bundle\DependencyInjection\Compiler\BuildClientExtensionsPass;
11+
use Enqueue\Bundle\DependencyInjection\Compiler\BuildClientRoutingPass;
12+
use Enqueue\Bundle\DependencyInjection\Compiler\BuildConsumptionExtensionsPass;
13+
use Enqueue\Bundle\DependencyInjection\Compiler\BuildExclusiveCommandsExtensionPass;
14+
use Enqueue\Bundle\DependencyInjection\Compiler\BuildProcessorRegistryPass;
15+
use Enqueue\Bundle\DependencyInjection\Compiler\BuildQueueMetaRegistryPass;
16+
use Enqueue\Bundle\DependencyInjection\Compiler\BuildTopicMetaSubscribersPass;
17+
use Enqueue\Bundle\DependencyInjection\EnqueueExtension;
18+
use Enqueue\Dbal\DbalContext;
19+
use Enqueue\Dbal\Symfony\DbalTransportFactory;
20+
use Enqueue\Fs\FsContext;
21+
use Enqueue\Fs\Symfony\FsTransportFactory;
22+
use Enqueue\Redis\RedisContext;
23+
use Enqueue\Redis\Symfony\RedisTransportFactory;
24+
use Enqueue\Sqs\SqsContext;
25+
use Enqueue\Sqs\Symfony\SqsTransportFactory;
26+
use Enqueue\Stomp\StompContext;
27+
use Enqueue\Stomp\Symfony\RabbitMqStompTransportFactory;
28+
use Enqueue\Stomp\Symfony\StompTransportFactory;
29+
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
30+
use Symfony\Component\DependencyInjection\ContainerBuilder;
31+
use Symfony\Component\HttpKernel\Bundle\Bundle;
32+
33+
class EnqueueBundle extends Bundle
34+
{
35+
/**
36+
* {@inheritdoc}
37+
*/
38+
public function build(ContainerBuilder $container)
39+
{
40+
$container->addCompilerPass(new BuildConsumptionExtensionsPass());
41+
$container->addCompilerPass(new BuildClientRoutingPass());
42+
$container->addCompilerPass(new BuildProcessorRegistryPass());
43+
$container->addCompilerPass(new BuildTopicMetaSubscribersPass());
44+
$container->addCompilerPass(new BuildQueueMetaRegistryPass());
45+
$container->addCompilerPass(new BuildClientExtensionsPass());
46+
$container->addCompilerPass(new BuildExclusiveCommandsExtensionPass());
47+
48+
/** @var EnqueueExtension $extension */
49+
$extension = $container->getExtension('enqueue');
50+
51+
if (class_exists(StompContext::class)) {
52+
$extension->addTransportFactory(new StompTransportFactory());
53+
$extension->addTransportFactory(new RabbitMqStompTransportFactory());
54+
}
55+
56+
if (class_exists(AmqpContext::class)) {
57+
$extension->addTransportFactory(new AmqpTransportFactory());
58+
$extension->addTransportFactory(new RabbitMqAmqpTransportFactory());
59+
}
60+
61+
if (class_exists(FsContext::class)) {
62+
$extension->addTransportFactory(new FsTransportFactory());
63+
}
64+
65+
if (class_exists(RedisContext::class)) {
66+
$extension->addTransportFactory(new RedisTransportFactory());
67+
}
68+
69+
if (class_exists(DbalContext::class)) {
70+
$extension->addTransportFactory(new DbalTransportFactory());
71+
}
72+
73+
if (class_exists(SqsContext::class)) {
74+
$extension->addTransportFactory(new SqsTransportFactory());
75+
}
76+
77+
$container->addCompilerPass(new AsyncEventsPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 100);
78+
$container->addCompilerPass(new AsyncTransformersPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 100);
79+
}
80+
}

‎pkg/laravel-queue/Job.php‎

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
3+
namespace Enqueue\LaravelQueue;
4+
5+
use Illuminate\Container\Container;
6+
use Illuminate\Contracts\Queue\Job as JobContract;
7+
use Illuminate\Queue\Jobs\Job as BaseJob;
8+
use Interop\Queue\PsrConsumer;
9+
use Interop\Queue\PsrMessage;
10+
11+
class Job extends BaseJob implements JobContract
12+
{
13+
/**
14+
* @var PsrConsumer
15+
*/
16+
private $psrConsumer;
17+
18+
/**
19+
* @var PsrMessage
20+
*/
21+
private $psrMessage;
22+
23+
/**
24+
* @param Container $container
25+
* @param PsrConsumer $psrConsumer
26+
* @param PsrMessage $psrMessage
27+
* @param string $connectionName
28+
*/
29+
public function __construct(Container $container, PsrConsumer $psrConsumer, PsrMessage $psrMessage, $connectionName)
30+
{
31+
$this->container = $container;
32+
$this->psrConsumer = $psrConsumer;
33+
$this->psrMessage = $psrMessage;
34+
$this->connectionName = $connectionName;
35+
}
36+
37+
/**
38+
* {@inheritdoc}
39+
*/
40+
public function fire()
41+
{
42+
$this->psrMessage->setProperty('x-attempts', $this->attempts() + 1);
43+
44+
parent::fire();
45+
}
46+
47+
/**
48+
* {@inheritdoc}
49+
*/
50+
public function delete()
51+
{
52+
parent::delete();
53+
54+
$this->psrConsumer->acknowledge($this->psrMessage);
55+
}
56+
57+
/**
58+
* {@inheritdoc}
59+
*/
60+
public function release($delay = 0)
61+
{
62+
$this->psrConsumer->reject($this->psrMessage, true);
63+
}
64+
65+
/**
66+
* {@inheritdoc}
67+
*/
68+
public function getQueue()
69+
{
70+
return $this->psrConsumer->getQueue()->getQueueName();
71+
}
72+
73+
/**
74+
* {@inheritdoc}
75+
*/
76+
public function attempts()
77+
{
78+
return $this->psrMessage->getProperty('x-attempts', 1);
79+
}
80+
81+
/**
82+
* {@inheritdoc}
83+
*/
84+
public function getRawBody()
85+
{
86+
return $this->psrMessage->getBody();
87+
}
88+
}

‎pkg/laravel-queue/LICENSE‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2017 Kotliar Maksym
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is furnished
8+
to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.

‎pkg/laravel-queue/PsrQueue.php‎

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
<?php
2+
3+
namespace Enqueue\LaravelQueue;
4+
5+
use Illuminate\Contracts\Queue\Queue as QueueContract;
6+
use Illuminate\Queue\Queue;
7+
use Interop\Queue\PsrContext;
8+
9+
class PsrQueue extends Queue implements QueueContract
10+
{
11+
/**
12+
* The name of the default queue.
13+
*
14+
* @var string
15+
*/
16+
protected $default;
17+
18+
/**
19+
* The "time to run" for all pushed jobs.
20+
*
21+
* @var int
22+
*/
23+
protected $timeToRun;
24+
/**
25+
* @var PsrContext
26+
*/
27+
private $psrContext;
28+
29+
/**
30+
* @param PsrContext $psrContext
31+
* @param string $default
32+
* @param int $timeToRun
33+
*/
34+
public function __construct(PsrContext $psrContext, $default, $timeToRun)
35+
{
36+
$this->psrContext = $psrContext;
37+
$this->default = $default;
38+
$this->timeToRun = $timeToRun;
39+
}
40+
41+
/**
42+
* {@inheritdoc}
43+
*/
44+
public function size($queue = null)
45+
{
46+
return 0;
47+
}
48+
49+
/**
50+
* {@inheritdoc}
51+
*/
52+
public function push($job, $data = '', $queue = null)
53+
{
54+
return $this->pushRaw($this->createPayload($job, $data), $queue);
55+
}
56+
57+
/**
58+
* Push a new job onto the queue.
59+
*
60+
* @param string $queue
61+
* @param string $job
62+
* @param mixed $data
63+
*
64+
* @return mixed
65+
*/
66+
public function pushOn($queue, $job, $data = '')
67+
{
68+
}
69+
70+
/**
71+
* {@inheritdoc}
72+
*/
73+
public function pushRaw($payload, $queue = null, array $options = [])
74+
{
75+
return $this->psrContext->createProducer()->send(
76+
$this->getQueue($queue),
77+
$this->psrContext->createMessage($payload)
78+
);
79+
}
80+
81+
/**
82+
* {@inheritdoc}
83+
*/
84+
public function later($delay, $job, $data = '', $queue = null)
85+
{
86+
new \LogicException('to be implemented');
87+
}
88+
89+
/**
90+
* {@inheritdoc}
91+
*/
92+
public function pop($queue = null)
93+
{
94+
$queue = $this->getQueue($queue);
95+
96+
$psrConsumer = $this->psrContext->createConsumer($queue);
97+
if ($psrMessage = $psrConsumer->receive(1000)) { // 1 sec
98+
return new Job(
99+
$this->container,
100+
$psrConsumer,
101+
$psrMessage,
102+
$this->connectionName
103+
);
104+
}
105+
}
106+
107+
/**
108+
* Get the queue or return the default.
109+
*
110+
* @param string|null $queue
111+
*
112+
* @return \Interop\Queue\PsrQueue
113+
*/
114+
public function getQueue($queue)
115+
{
116+
return $this->psrContext->createQueue($queue ?: $this->default);
117+
}
118+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Enqueue\LaravelQueue;
4+
5+
use Illuminate\Support\ServiceProvider;
6+
7+
class QueueServiceProvider extends ServiceProvider
8+
{
9+
/**
10+
* {@inheritdoc}
11+
*/
12+
public function boot()
13+
{
14+
$manager = $this->app['queue'];
15+
16+
$manager->addConnector('enqueue', function () {
17+
return new DsnToPsrQueueConnector();
18+
});
19+
}
20+
21+
/**
22+
* {@inheritdoc}
23+
*/
24+
public function register()
25+
{
26+
}
27+
}

‎pkg/laravel-queue/README.md‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Enqueue Laravel Queue Extension
2+
3+
[![Gitter](https://badges.gitter.im/php-enqueue/Lobby.svg)](https://gitter.im/php-enqueue/Lobby)
4+
5+
You can use all transports built on top of [queue-interop](https://github.com/queue-interop/queue-interop) including [all supported](https://github.com/php-enqueue/enqueue-dev/tree/master/docs/transport) by Enqueue.
6+
7+
## Resources
8+
9+
* [Documentation](https://github.com/php-enqueue/enqueue-dev/blob/master/docs/index.md)
10+
* [Questions](https://gitter.im/php-enqueue/Lobby)
11+
* [Issue Tracker](https://github.com/php-enqueue/enqueue-dev/issues)
12+
13+
## Developed by Forma-Pro
14+
15+
Forma-Pro is a full stack development company which interests also spread to open source development.
16+
Being a team of strong professionals we have an aim an ability to help community by developing cutting edge solutions in the areas of e-commerce, docker & microservice oriented architecture where we have accumulated a huge many-years experience.
17+
Our main specialization is Symfony framework based solution, but we are always looking to the technologies that allow us to do our job the best way. We are committed to creating solutions that revolutionize the way how things are developed in aspects of architecture & scalability.
18+
19+
If you have any questions and inquires about our open source development, this product particularly or any other matter feel free to contact at opensource@forma-pro.com
20+
21+
## License
22+
23+
It is released under the [MIT License](LICENSE).

0 commit comments

Comments
 (0)