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 %} |
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
Thank you very much @seregazhuk, @rpkamp. I added your changes!