I have over a thousand data written in a text file and currently using remote upload API to import them to a Server. However, server can only handle 50 slots at a time.
Here's the script I made so far:
$txtfile = 'data.txt'; // this is the data (with over a thousand lines)
$fileData = function() { // I made a function to read the text file LINE by LINE
$file = fopen($txtfile, 'r');
if (!$file)
die('file does not exist or cannot be opened');
while (($line = fgets($file)) !== false) {
yield $line;
}
fclose($file);
};
$server_slot = file_get_contents('https://someserver.com/api/current_slots/') //get remaining slot from the server
foreach ($fileData() as $line) { //process each line
//import the data on that specific line
$remote_upload_api = 'https://someserver.com/api/upload?data='.$line;
$send_data = file_get_contents($remote_upload_api);
}
This script works so far but it sends all data to the server. Some process failed because slots are already full. Is it possible to TEMPORARILY stop the loop and check $server_slot overtime (probably another loop) until available slot is given then proceed to the loop again?
Im thinking of doing do-while statement but I find it hard to understand the right logic with my given situation.
My Answer:
You can achieve this by adding a check for the available server slots inside the loop and then waiting until a slot becomes available before proceeding with the next iteration. Here's an updated version of your script with this logic:
php
$txtfile = 'data.txt'; // this is the data (with over a thousand lines)
$fileData = function() use ($txtfile) { // I made a function to read the text file LINE by LINE
$file = fopen($txtfile, 'r');
if (!$file)
die('file does not exist or cannot be opened');
while (($line = fgets($file)) !== false) {
yield $line;
}
fclose($file);
};
$server_slot = file_get_contents('https://someserver.com/api/current_slots/'); //get remaining slot from the server
foreach ($fileData() as $line) { //process each line
$slot_available = false;
while (!$slot_available) {
$server_slot = file_get_contents('https://someserver.com/api/current_slots/'); //get remaining slot from the server
if ($server_slot > 0) {
$slot_available = true;
} else {
sleep(5); // wait for 5 seconds before checking again
}
}
//import the data on that specific line
$remote_upload_api = 'https://someserver.com/api/upload?data='.$line;
$send_data = file_get_contents($remote_upload_api);
}
In this updated script, the loop will keep checking for available server slots before proceeding with the next iteration. It will wait for 5 seconds between each check to avoid overwhelming the server with requests. Once a slot becomes available, the loop will continue with the next iteration and send the data to the server.
Rate this post
4 of 5 based on 7563 votesComments