AWSs sendEmailAsync not working in PHP application - php

I have a PHP application where I can send mails via AmazonSES by using the offical SDK.
By default sending emails works when using the sendMail method: (simplified):
$result = aws_client()->sendEmail([/* ... */]);
For the Async method I would like to use this:
$promise = aws_client()->sendEmailAsync([/* ... */]);
$promise->then(
function ( $value ) {
error_log( 'ok, fulfilled' );
},
function ( $reason ) {
error_log('on no, rejected' );
}
);
// $result = $promise->wait();
$promise->resolve();
I instantly get ok, fulfilled in my console but no email has been sent, not even after a couple of minutes.
When I use the mentioned line $result = $promise->wait(); everything works as expected but the process takes much longer (of course because of the synchronism and this is why I like to go with the async method).
So is it just not possible to run an async process in PHP or do I miss something?

AWS SDK *Async functions in PHP don't spawn a separate process for sending the email. Instead, they return a promise. This is useful if you need to do several slow processes at the same time, such as sending several emails. But the promise needs to be resolved (you need to wait for the response).
What I think is happening, AWS SDK connects to SES endpoint, starts sending your email and before it finishes, your script terminates. This also terminates the sending.

Related

PHP AJAX, return multiple values in realtime in a for loop

What I have:
foreach ($contacts as $contact) {
$this->StocklistMailer($contact, $weekOrDay, $data, $content, $itemGroup);
}
return new Response('completed', 204);
What I would like is :
foreach ($contacts as $contact) {
$this->StocklistMailer($contact, $weekOrDay, $data, $content, $itemGroup);
return new Response($contact->getEmail, 204);
}
return new Response('completed', 204);
And it returns to a AJAX call on the page, the reason I want to accomplish this is because I want realtime feedback to whom it has send an email to.
You can't send multiple responses from your application, the whole idea is that you only generate one response.
However, you can put all the information you require in one response.
$emails = [];
foreach ($contacts as $contact) {
$this->StocklistMailer($contact, $weekOrDay, $data, $content, $itemGroup);
$emails[] = $contact->getEmail;
}
return new Response(json_encode($emails), 200);
Note that I changed 204 (No content) to 200 (OK).
You would have to break it into multiple calls;
First call will return the array of contacts which you will store in your JavaScript array. (assuming you are using Client Side JS/Ajax to call this php file).
Then loop through the array and make as many calls to php as many contacts are in your array while passing each contact at a time to php.
You can show fancy progress bar as you are looping through the array :)
You can do that, but not from Symfony. Look into ReactPHP, Ratchet and related technologies.
https://blog.wyrihaximus.net/2015/03/reactphp-sockets/
http://socketo.me/docs/hello-world
You can create a websockets server that would listen on localhost for messages from your Symfony application and will be redirecting them using websockets to the browser.
The cliend would open a websocket connection to your websockets server, and send the request to your application. While the application is processing, it is sending the progress to the websocket server using a socket on the local machine. The client should be getting the progress realtime from the websocket and should be displaying it.
This way, you get a realtime interactive interface, with a long-running process.
Even better would be creating a rabbitmq worker, that would be sending the emails and reporting the progress to the websockets server. You would create the task for the worker from your Symfony application, and therefore you wouldn't be limited by the execution time limit for php requests. Another win with the rabbitmq worker is that you can have only one (or as many as you like), and therefore the tasks will queue and you won't be burning server resources by 50 processes generating and sending emails at once.

Reactphp can't get total data on stream

im using reactphp library , and im working with a device that sends packages in different sizes. My problem is when i get the data
$stream->on('data', function ($data, React\Stream\ReadableStreamInterface $stream) {
$this->respuesta .= $data;
$stream->close();
});
I only get a part of the first package. Is there a way to keep waiting until the device sends all the data?
Here, on the wireshark capture the last package is the one that i cant get with react.
$loop = React\EventLoop\Factory::create();
$dnsResolverFactory = new React\Dns\Resolver\Factory();
$dns = $dnsResolverFactory->createCached('8.8.8.8', $loop);
$connector = new React\SocketClient\Connector($loop, $dns);
$connector->create($this->host, $this->port)->then(function (React\Stream\Stream $stream) use ($hex) {
$stream->write($hex);
$stream->on('data', function ($data, React\Stream\ReadableStreamInterface $stream) {
$this->respuesta .= $data;
$stream->close();
});
});
$loop->run();
It appears you are closing the connection right after receiving the data... You should keep this connection open until after you receive all of the data. The data event is dispatched after receiving some data, not all data.
Typically this is implementation specific, if your dealing with RPC style api, the client sending the request may not care about acknowledgment from the service and cut the connection after sending the data; in this case you should accumulate your buffer on the data event, and then process it on the end event.
If you want to keep the connection open, and send discrete chunks of information, typically you either lead the data package with the size of the package, allowing you to know how much of the buffer to fill before processing, or you can send a delimiter (typically a null byte) that marks the end of the package.

Guzzle asynch request not working

I'm using Guzzle that I installed via composer and failing to do something relatively straightforward.
I might be misunderstanding the documentation but essentially what I'm wanting to do is run a POST request to a server and continue executing code without waiting for a response. Here's what I have :
$client = new \GuzzleHttp\Client(/*baseUrl, and auth credentials here*/);
$client->post('runtime/process-instances', [
'future'=>true,
'json'=> $data // is an array
]);
die("I'm done with the call");
Now lets say the runtime/process-instances runs for about 5mn, I will not get the die message before those 5mn are up... When instead I want it right after the message is sent to the server.
Now I don't have access to the server so I can't have the server respond before running the execution. I just need to ignore the response.
Any help is appreciated.
Things I've tried:
$client->post(/*blabla*/)->then(function ($response) {});
It is not possible in Guzzle to send a request and immediately exit. Asynchronous requests require that you wait for them to complete. If you do not, the request will not get sent.
Also note that you are using post instead of postAsync, the former is a synchronous (blocking) request. To asynchronously send a post request, use the latter. In your code example, by changing post to postAsync the process will exit before the request is complete, but the target will not receive that request.
Have you tried setting a low timeout?

Running php code chunk in background

I have a php script which is responsible for reading some request parameters from my iPhone app. Once I do some manipulations to it I save them in db and will need to send some push notification message using apple APNS. So currently its done like this in the code.
<?php
$param1 = $_POST['param1'];
$param2 = $_POST['param2'];
//saving part here
//push notifications
$pushService = new PushService();
$pushService -> init();
$pushService -> push($param1, $param2);
//json response
echo json_encode(array($success, $dbsavedid);
?>
Problem occurs with the push part. Now it takes lot of time for this push notification code chunk to execute because the table has grown with lot of data. Hence the iPhone app waits too long for this to execute (to get the success response to iPhone).
Hence is there any way to make this push part asynchronous and send a response to iPhone side using the echo other than using a separate script for push notifications? Also note that I need to get some data from saved records as well to iPhone side. So I will need the output to reach the iPhone side.
You can force PHP to send a response by using the flush() function for example. (there might be other possibilities to accomplish too)
So what you have to do is write with echo to the output buffer when your db operations finished (these should be really fast if you have 100-1000 records) and right after call the flush() function. Your client should get a response right away.
Also see this link about flush() itself, because there might be other parameters of your enviroment which prevents your response in reaching the client side as soon as expected.
http://php.net/manual/en/function.flush.php
<?php
$param1 = $_POST['param1'];
$param2 = $_POST['param2'];
//saving part here
//json response
echo json_encode(array($success, $dbsavedid);
//response should be sent right away, no need for wait on the pushservice operations
flush();
//push notifications
$pushService = new PushService();
$pushService -> init();
$pushService -> push($param1, $param2);
?>
The actual reasons for this is, my server provider has blocked port 2195 and port 2196 which is used by apple APNS. I believe once you allow it this will be fixed and should work like earlier.

PHP web service, send response before end of script execution

I have a web service written in PHP to which an iPhone app connects to. When the app calls the service, a series of notification messages are sent to Apple's APNs server so it can then send Push Notifications to other users of the app.
This process can be time consuming in some cases and my app has to wait a long time before getting a response. The response is totally independent of the result of the notification messages being sent to the APNs server.
Therefore, I would like the web service to send the response back to the app regardless of whether the messages to APNs have been sent.
I tried using pcntl_fork to solve the problem:
<?php
...
$pid = pcntl_fork();
if($pid == -1)
{
// Could not fork (send response anyway)
echo "response";
}
else if($pid)
{
// Parent process - send response to app
echo "response";
}
else
{
// Child process - send messages to APNs then die
sendMessageAPNs($token_array);
die();
}
?> // end of script
Unfortunately, the parent process seems to wait for the child process to end before sending the response even though I do not use pcntl_wait in the parent process. Am I doing something wrong or is this normal behaviour? If this is normal then is there another way I can solve this problem?
Thank you!
If you're hosting the PHP process in Apache then you really shouldn't use this: see this for the section that says *Process Control should not be enabled within a web server environment and unexpected results may happen if any Process Control functions are used within a web server environment. *.
You should probably set up a separate daemon in your preferred language of choice and hand the APNS communication tasks off to that. If you really really really must try using ob_flush().
I think you can send the response back before doing the "long" process. Take a look at the flush() function of PHP it'll maybe help

Categories