Reputation: 11782
I have a code which needs to send notifications to all users in the database
function sendPushNotificationToUsers($users, $message)
{
$iPhoneUsers = array();
$androidUsers = array();
foreach ($users as $user)
{
if($user['deviceType'] == "iPhone")
$iPhoneUsers[] = $user;
else if($user['deviceType'] == "Android")
$androidUsers[] = $user;
}
sendApplePushNotifications($iPhoneUsers, $message);
//sendGooglePushNotifications($androidUsers, $message);
return true;
}
Its usually going to take 2-3 hours. How can i run this code inside a script which can run in background while i navigate other things in php ? I run this code from a form.
Upvotes: 1
Views: 976
Reputation: 1
javascript
<script language="javascript">
function checking()
{
var user=document.getElementById("user").value;
var message=document.getElementById("message").value;
$.ajax({
type: "POST",cache: false,
url: "url_to_php_file",
data: "user="+user+"&message="+message,
success: function(data) {
if(data==true)
{
//do simting
}else {
//do simting
}
});
}}
</script>
form
<input type="text" id="user" name="user" value="">
<input type="text" id="message" name="message" value="">
<input type="submit" value="Submit" onclick="checking();">
**add to PHP **
$users = $_POST['user'];
$message = $_POST['message'];
Upvotes: 0
Reputation: 11
While other solutions suggested here could work, you will eventually encounter "scaling" issues as your user base gets larger and larger.
The suggested approach in these cases is to move the required functionality into a "queue" for background processing. this way it does not hold the main application thread/server process. (more about "messages queues" here: https://en.wikipedia.org/wiki/Message_queue)
Using queues means you send a small payload that requires processing (a message) into a buffer, that is maintained by the queue service. then "workers", which are individual processes, take these messages and process whatever it is that needs processing. once a single processing work is done, the message is removed for the queue. This type of implementation allows your add more and more workers to speed up the overall processing of all messages in the queue.
If you're using AWS your could look into SQS (simple queue service) https://aws.amazon.com/sqs/
Other open source alternatives are RabbitMQ, Gearman or ZeroMQ
Upvotes: 0
Reputation: 98921
Use the following at the top of your php script:
set_time_limit(0);
ignore_user_abort(true);
The above code ensures that, even if you close your browser/ssh session
, the script will run until it finishes or the web server service is restarted.
Set the number of seconds a script is allowed to run. If this is reached, the script returns a fatal error. The default limit is 30 seconds or, if it exists, the max_execution_time value defined in the php.ini.
When called, set_time_limit() restarts the timeout counter from zero. In other words, if the timeout is the default 30 seconds, and 25 seconds into script execution a call such as set_time_limit(20) is made, the script will run for a total of 45 seconds before timing out.
Sets whether a client disconnect should cause a script to be aborted.
When running PHP as a command line script, and the script's tty goes away without the script being terminated then the script will die the next time it tries to write anything, unless value is set to TRUE
Upvotes: 2
Reputation: 12608
Perhaps you can run your php from the command line. So the normal page starts a new process that will actually send the notifications.
http://php.net/manual/en/features.commandline.php
Upvotes: 0