Skip to content

Instantly share code, notes, and snippets.

@webdevilopers
Last active January 4, 2018 16:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save webdevilopers/71162ce725576990a7437e48eed676fd to your computer and use it in GitHub Desktop.
Save webdevilopers/71162ce725576990a7437e48eed676fd to your computer and use it in GitHub Desktop.
Run ReactPHP Event Loop with database query and return Promise
<?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;
}
}
<?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);
}
}
{% 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 %}
@webdevilopers
Copy link
Author

@seregazhuk
Copy link

seregazhuk commented Dec 14, 2017

You can replace

if (array_key_exists('offerLetterId', $offer)) {
    if (null !== $offer['offerLetterId']) {
        $deferred->resolve($offer['offerLetterId']);
    }
}

with isset($offer['offerLetterId'])

@seregazhuk
Copy link

Are you sure you want to resolve() on the 10th iteration? Maybe reject() the promise? Is it OK for OfferController to setContent with an empty string?

@webdevilopers
Copy link
Author

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.

@rpkamp
Copy link

rpkamp commented Dec 14, 2017

Shouldn't you return after $deferred->resolve($offer['offerLetterId']);?

@webdevilopers
Copy link
Author

Isn't the loop automatically cancelled as soon the $deferred is resolved @rpkamp?

@seregazhuk
Copy link

@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();
    }
});

@seregazhuk
Copy link

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.

@webdevilopers
Copy link
Author

Thank you very much @seregazhuk, @rpkamp. I added your changes!

@webdevilopers
Copy link
Author

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