Skip to content

Commit

Permalink
Merge branch 'asyncphp' into main
Browse files Browse the repository at this point in the history
adding the new runner
  • Loading branch information
John Doe committed Sep 30, 2024
2 parents 3947632 + 0749c83 commit ffbaf28
Show file tree
Hide file tree
Showing 177 changed files with 9,982 additions and 0 deletions.
33 changes: 33 additions & 0 deletions Async-PHP/Backend/template_realtime_output.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

require_once '../lib/AsyncRunner_realtime_output.php'; // Include your AsyncRunner script

// Set headers for server-sent events
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
header('Connection: keep-alive');

// Scripts you want to run
$path = '../async_scripts/test_db_100_create_scritps';
$scripts = [
"$path/script1.php",
"$path/script2.php",
"$path/script3.php",
"$path/script4.php",
"$path/script5.php",
"$path/script6.php",
"$path/script7.php",
"$path/script8.php",
"$path/script9.php",
"$path/script10.php",
];

// Use the included function to run scripts asynchronously
runScriptsAsync($scripts);

echo "data: All scripts executed.\n\n";
echo "event: end\n\n";
ob_flush();
flush();
?>

32 changes: 32 additions & 0 deletions Async-PHP/app/reuseable_views/footer_realtime_output.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<script>
document.addEventListener('DOMContentLoaded', function () {
const runScriptButtons = document.querySelectorAll('.runScripts');
runScriptButtons.forEach(button => {
button.addEventListener('click', function () {
const outputDiv = document.getElementById('output');
outputDiv.innerHTML = ''; // Clear previous output
const url = this.getAttribute('data-url'); // Get the URL from the button
const eventSource = new EventSource(url);
eventSource.onmessage = function (event) {
// Append output to the div
outputDiv.innerHTML += `<pre>${event.data}</pre>`;
};
eventSource.addEventListener('end', function () {
// Close the connection when scripts are done
eventSource.close();
outputDiv.innerHTML += '<p>All scripts executed.</p>';
});
eventSource.onerror = function (event) {
outputDiv.innerHTML += '<p>Error occurred while executing scripts.</p>';
eventSource.close();
};
});
});
});
</script>
13 changes: 13 additions & 0 deletions Async-PHP/app/template_realtime_output.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
$pageTitle = 'Logs'; // Set the page title
include 'reuseable_views/header.php'; ?>


<h1>Run Scripts and Get Real-time Output</h1>
<button class="runScripts btn btn-primary mb-3" data-url="../Backend/10_async_time.php">Run Scripts</button>

<div id="output"></div>


<?php include 'reuseable_views/footer_realtime_output.php'; ?>

40 changes: 40 additions & 0 deletions Async-PHP/lib/AsyncRunner_realtime_output.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
function runScriptsAsync(array $scripts) {
$descriptorspec = [
0 => ["pipe", "r"], // stdin
1 => ["pipe", "w"], // stdout
2 => ["pipe", "w"], // stderr
];

$processes = [];
$pipes = [];

// Start processes for each script
foreach ($scripts as $index => $script) {
$process = proc_open("php $script", $descriptorspec, $pipes[$index]);
if (is_resource($process)) {
$processes[$index] = $process;
} else {
echo "data: Failed to start $script\n\n";
ob_flush();
flush();
}
}

// Continuously check and output real-time results
foreach ($processes as $index => $process) {
if (is_resource($process)) {
// Read output of each script in real-time
while (!feof($pipes[$index][1])) {
$output = fgets($pipes[$index][1]);
echo "data: Script $index output: " . trim($output) . "\n\n";
ob_flush();
flush();
}
fclose($pipes[$index][1]);
fclose($pipes[$index][2]);
proc_close($process);
}
}
}
?>
26 changes: 26 additions & 0 deletions php-composer/Async-PHP/Backend/db_connect.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
$servername = "";
$username = "";
$password = "";
$dbname = "";
$port = "";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname, $port);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}




//else{
// echo "Connected successfully";
//}
// Make sure to stop connection in files where you will use this


?>

58 changes: 58 additions & 0 deletions php-composer/Async-PHP/Backend/multiple_async_get_outputs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php


require_once '../lib/AsyncRunner.php';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
echo "Received POST request.<br>"; // Debug statement
$path = '../async_scripts';
$scripts = ["$path/script1.php", "$path/script2.php", "$path/script3.php"];

// Run the scripts asynchronously
$outputs = runScriptsAsync($scripts); // Call the function directly
echo "Scripts executed. Outputs:<br>"; // Debug statement

// Initialize variables for individual outputs
$output1 = '';
$output2 = '';
$output3 = '';

// Assign outputs to individual variables
if (isset($outputs[0])) {
$output1 = trim($outputs[0]); // Script 1 output
}
if (isset($outputs[1])) {
$output2 = trim($outputs[1]); // Script 2 output
}
if (isset($outputs[2])) {
$output3 = trim($outputs[2]); // Script 3 output
}

// Output the individual outputs
echo "Output 1: " . $output1 . "<br>";
echo "Output 2: " . $output2 . "<br>";
echo "Output 3: " . $output3 . "<br>";

// Initialize the sum
$sum = 0;

// Calculate the sum only for non-empty outputs
if ($output1 !== '') {
$sum += floatval($output1);
}
if ($output2 !== '') {
$sum += floatval($output2);
}
if ($output3 !== '') {
$sum += floatval($output3);
}

// Output the total sum
echo "<strong>Total Sum: " . $sum . "</strong>";
echo "ok";
}




?>
8 changes: 8 additions & 0 deletions php-composer/Async-PHP/Backend/template.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php


echo "hellow from AsyncPHP";



?>
18 changes: 18 additions & 0 deletions php-composer/Async-PHP/Backend/template_async.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

require_once '../lib/AsyncRunner.php';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$path = '../async_scripts';
$scripts = ["$path/db_script1.php", "$path/db_script2.php", "$path/db_script3.php"];

$outputs = runScriptsAsync($scripts); // Call the function directly

foreach ($outputs as $output) {
echo "<pre>$output</pre>";
}
}

echo "Script executed successfully.";

?>
25 changes: 25 additions & 0 deletions php-composer/Async-PHP/app/.htaccess
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<IfModule mod_rewrite.c>
RewriteEngine On

# Redirect any requests with .php extension to the URL without the extension
RewriteCond %{THE_REQUEST} \s/([^.]+)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,L]

# Internally rewrite requests without .php to the actual .php file
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]


# Route /app to app/index.php
RewriteRule ^app/?$ /app/index.php [L]

# If you want to handle all requests under /app (e.g., /app/something)
RewriteRule ^app/(.*)$ /app/index.php [L,QSA]

</IfModule>

# Enable the rewrite engine
Options +FollowSymLinks

23 changes: 23 additions & 0 deletions php-composer/Async-PHP/app/reuseable_views/footer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<script>
$(document).ready(function() {
// Use event delegation to handle click events for elements with class 'runScripts'
$(document).on('click', '.runScripts', function(e) {
e.preventDefault();
var url = $(this).data('url'); // Get the URL from the data attribute
$.ajax({
url: url,
method: 'POST',
success: function(data) {
$('#output').html(data);
},
error: function(jqXHR, textStatus, errorThrown) {
$('#output').html("Error: " + textStatus + " " + errorThrown);
}
});
});
});
</script>
</body>
</html>

31 changes: 31 additions & 0 deletions php-composer/Async-PHP/app/reuseable_views/footer_async.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<script>
document.addEventListener('DOMContentLoaded', () => {
const button = document.querySelector('.runScripts');
button.addEventListener('click', (e) => {
e.preventDefault();
const url = button.getAttribute('data-url'); // Get the URL from the data attribute
fetch(url, {
method: 'POST', // Change to POST
headers: {
'Content-Type': 'application/x-www-form-urlencoded' // Set content type
}
})
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok ' + response.statusText);
}
return response.text();
})
.then(data => {
document.getElementById('output').innerHTML = data;
})
.catch(err => {
console.error('Error fetching output:', err);
document.getElementById('output').innerHTML = 'Error fetching output: ' + err.message;
});
});
});
</script>
</body>
</html>
38 changes: 38 additions & 0 deletions php-composer/Async-PHP/app/reuseable_views/footer_form.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<script>
$(document).ready(function() {
// Use event delegation to handle click events for elements with class 'runScripts'
$(document).on('click', '.runScripts', function(e) {
e.preventDefault();
// Get the URL from the data-url attribute of the button
var url = $(this).data('url');
// Get the form associated with the button that was clicked
var form = $(this).closest('form');
// Check if a form was found
if (form.length) {
// Gather form data using FormData object
var formData = new FormData(form[0]);
$.ajax({
url: url,
type: 'POST',
data: formData,
processData: false, // Do not process the data into a query string
contentType: false, // Do not set content type
success: function(data) {
$('#output').html(data); // Show response in the output div
},
error: function(jqXHR, textStatus, errorThrown) {
$('#output').html("Error: " + textStatus + " " + errorThrown);
}
});
} else {
$('#output').html("Error: No form associated with this button.");
}
});
});
</script>
</body>
</html>
30 changes: 30 additions & 0 deletions php-composer/Async-PHP/app/reuseable_views/footer_form.php.bak
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<script>
$(document).ready(function() {
// Use event delegation to handle click events for elements with class 'runScripts'
$(document).on('click', '.runScripts', function(e) {
e.preventDefault();

// Get the URL from the data-url attribute of the button
var url = $(this).data('url');

// Gather form data using FormData object
var formData = new FormData($('#userForm')[0]);

$.ajax({
url: url,
type: 'POST',
data: formData,
processData: false, // Do not process the data into a query string
contentType: false, // Do not set content type
success: function(data) {
$('#output').html(data); // Show response in the output div
},
error: function(jqXHR, textStatus, errorThrown) {
$('#output').html("Error: " + textStatus + " " + errorThrown);
}
});
});
});
</script>
</body>
</html>
Loading

0 comments on commit ffbaf28

Please sign in to comment.