Last active
October 28, 2015 08:13
-
-
Save farconada/d59445416bafd6a73b27 to your computer and use it in GitHub Desktop.
Async HTTP integration with PHP
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
As a way to integrate microservices I want to use HTTP calls and avoid the use of a MQ (Message Queue). | |
If the app is small and contained in one host you could avoid the use of MQ. | |
Description of the scenario: | |
I have one app (made with Symfony. Call it MainApp) and one microservice (made with Sylex. Call MicroApp), both served using PHP-FPM NGINX | |
1) MainApp calls an URL POST /api/internal/do-something with Guzzle, | |
2) then MicroAPP do its work (it takes long) | |
3) MicroApp calls an URL from /api/internal/i-have-finished | |
To avoid keep MainApp waiting in 1). I've configured a very small guzzle timeout and it catches the exception. With this trick I have an Async HTTP call. | |
Are there any way to 2) in background without a MQ (and without anykind of worker proccess). | |
I'm tempted to use threads for 2) but I've read that they are only suitable for CLI (https://github.com/krakjoe/pthreads). | |
I've heard that Spring have async REST. | |
What's the best way to solve this scenario and avoid waiting for 2) ? |
this solution seems ok but only works with php-fpm I'll try it in a few minutes. thanks
Dont you think that it will be a better solution with with threads? (pthreads are only available in CLI)
Silex after() isn't the solution but $app->finish() do the work pretty well
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@farconada as @grEvenX commented, you can either implement your code as a listener to the
kernel.terminate
event or you can manually callfastcgi_finish_request
on the controller and after that point you can execute whatever logic you need in the background.You'll need to make sure to set any headers on the response and set the content, before calling fastcgi_finish_request().
I don't have an example on Silex, but I'm using this on an App with an internal framework.