I have a website that lets user select test scripts from radio buttons, and after submitting the desired test to the remote server, using PHP:
- It inserts sends a row into MySQL database in the test_activity_log table, with id, username, test name, gateway name, and timestamp.
- It then sends a
ssh2_exec
to execute the desired script on the remote gateway
Everything above I already have working, however, in the case when multiple users are executing scripts from the webpage simultaneously, I need to find a way to queue them up in order and execute them one after another. What would be the best way to do this?
Any ideas would be much appreciated.
EDIT:
testexe.php
<?php
...
...
//while there are still jobs that are not done..
while (1) {
//wait for 10 seconds and query again...
sleep(10);
$statusResult = mysqli_query($dbConnection, $qStatus);
//if there are undone jobs we pick them and execute them
if (mysqli_num_rows($statusResult) > 0){
//query the next undone test case
$testResult = mysqli_query($dbConnection, $qNextTest);
//fetch the returned query object and store needed parameters
$row = $testResult->fetch_assoc();
$id = $row['id'];
$test_script = $row['test_script'];
$gateway = $row['gateway'];
if ($connection = @ssh2_connect($gateway, 22)) {
ssh2_auth_password($connection, $user, $password);
//execute the test case
$stream = ssh2_exec($connection, "/my/path/to/script.sh");
//fetch output stream and stderr
stream_set_blocking($stream, true);
$stream_out = ssh2_fetch_stream($stream, SSH2_STREAM_STDIO);
$stream_err = ssh2_fetch_stream($stream, SSH2_STREAM_STDERR);
while($line = fgets($stream_out)) {flush(); echo '<pre>' . $line . '</pre>';}
echo '<pre>' . "------------------------\n" . '</pre>';
while($line = fgets($stream_err)) {flush(); echo '<pre>' . $line . '</pre>';}
fclose($stream);
//update the table after the above script is done
$qUpdateStatus = "UPDATE table SET status = 1 WHERE id = $id";
$updateresult = mysqli_query($dbConnection, $qUpdateStatus);
}
}
}
?>