Last active
January 4, 2018 16:00
-
-
Save webdevilopers/71162ce725576990a7437e48eed676fd to your computer and use it in GitHub Desktop.
Run ReactPHP Event Loop with database query and return Promise
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Acme\FooApplication\OfferLetter; | |
use React\EventLoop\Timer\Timer; | |
use React\Promise\Deferred; | |
use Acme\Foo\Domain\Model\Offer\OfferId; | |
use React\EventLoop\Factory; | |
use Acme\Foo\Infrastructure\Projection\Mongo\OfferOverviewFinder; | |
class OfferLetterLookup | |
{ | |
/** @var OfferOverviewFinder */ | |
private $offerOverviewFinder; | |
/** | |
* OfferLetterLookup constructor. | |
* @param OfferOverviewFinder $offerOverviewFinder | |
*/ | |
public function __construct(OfferOverviewFinder $offerOverviewFinder) | |
{ | |
$this->offerOverviewFinder = $offerOverviewFinder; | |
} | |
public function lookup(OfferId $offerId, Deferred $deferred) | |
{ | |
$i = 0; | |
$loop = Factory::create(); | |
$loop->addPeriodicTimer(2, function(Timer $timer) use (&$i, $deferred, $offerId) { | |
$deferred->notify($i++); | |
$offer = $this->offerOverviewFinder->ofOfferId($offerId); | |
if (isset($offer['offerLetterId'])) { | |
$deferred->resolve($offer['offerLetterId']); | |
$timer->cancel(); | |
} | |
if ($i >= 10) { | |
$timer->cancel(); | |
$deferred->reject(); | |
} | |
}); | |
$loop->run(); | |
return $deferred; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Acme\Intranet\Infrastructure\Symfony\IntranetBundle\Controller; | |
class OfferController extends Controller | |
{ | |
public function lookupOfferLetterAction(string $offerId) | |
{ | |
$data = ['offerLetterId' => null]; | |
$query = LookupOfferLetterQuery::with(OfferId::fromString($offerId)); | |
$offerList = $this->get('prooph_service_bus.intranet_query_bus')->dispatch($query); | |
$offerList | |
->then( | |
function($offerLetterId) use (&$data) { | |
$data['offerLetterId'] = (string)$offerLetterId; | |
} | |
); | |
return new JsonResponse($data); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% block content %} | |
<script type="text/javascript"> | |
$(document).ready(function() { | |
{% if details.offerLetterId is null %} | |
function isUUID(string){ | |
return RegExp("^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$").test(string); | |
} | |
$.ajax({ | |
type: "POST", | |
dataType: "json", | |
url: '{{ path('sps_intranet_offer_lookup_offer_letter', {'offerId': details.offerId }) }}', | |
timeout: 60000, | |
success: function(data) { | |
$('#lookup-offer-letter').hide(); | |
if (data.hasOwnProperty('offerLetterId')) { | |
if (isUUID(data['offerLetterId'])) { | |
$('#advanced-offer-options').show(); | |
return true; | |
} | |
} | |
alert('{{ 'lookup_offer_letter_error'|trans }}'); | |
}, | |
error: function(jqXHR, textStatus) { | |
$('#lookup-offer-letter').hide(); | |
if (textStatus === 'timeout') { | |
alert('{{ 'lookup_offer_letter_error'|trans }}'); | |
} | |
} | |
}); | |
{% endif %} | |
</script> | |
{% endblock %} |
Shouldn't you return
after $deferred->resolve($offer['offerLetterId']);
?
Isn't the loop automatically cancelled as soon the $deferred
is resolved @rpkamp?
@webdevilopers, @rpkamp is right. There is no need for you to continue, once you resolve
your deferred object. Maybe update with this:
$timer = $loop->addPeriodicTimer(2, function(Timer $timer) use (&$i, $deferred, $offerId) {
$deferred->notify($i++);
$offer = $this->offerOverviewFinder->ofOfferId($offerId);
if(isset($offer['offerLetterId'])) {
$deferred->resolve($offer['offerLetterId']);
$timer->cancel();
}
if ($i >= 10) {
$timer->cancel();
$deferred->reject();
}
});
And by the way, you should use
array by reference in closure:
function($offerLetterId) use ($data) {
$data['offerLetterId'] = (string)$offerLetterId;
}
This code doesn't change an array outside the closure, only the copy inside of it.
Thank you very much @seregazhuk, @rpkamp. I added your changes!
When running the EventLoop locally on Apache the process will block all other HTTP processes.
Is this an expected behaviour? Do I need sockets? Is "multi threading" possible with Docker instead?
Forgive my ignorance on this topic.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks @seregazhuk for your feedback!
My use case:
An asynch microservice generates a PDF. The process may only take a second. But there maybe are a lot jobs in the queue.
After successful generation an event is fired. It is produced by the generation microservice and passed to a RabbitMQ queue.
Then my Core Microservice subscribes to the event. The Process Manager updates my Projection and sets an ID from NULL to the value from the event.
In my application:
A repository method (ORM to mysql) returns NULL or an ID. I have to query the method for about 60 seconds waiting for it to reutrn the ID.
Otherwise it is a timeout or the PDF could (yet) not be generated. Throw message to User Interface.
I think I get your point. Instead of resolving with an empty string or null I should reject the promise. Makes sense!
Will change that! Thanks.