0

我有一个网站,允许用户从单选按钮中选择测试脚本,并在将所需的测试提交到远程服务器后,使用 PHP:

  1. 它在 test_activity_log 表中向 MySQL 数据库中插入一行,其中包含 id、用户名、测试名称、网关名称和时间戳。
  2. 然后它发送一个ssh2_exec在远程网关上执行所需的脚本

上面的所有东西我已经开始工作了,但是,在多个用户同时从网页执行脚本的情况下,我需要找到一种方法将它们按顺序排列并一个接一个地执行。最好的方法是什么?

任何想法将不胜感激。

编辑:

测试程序文件

<?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);
        }

    }
}

?>
4

0 回答 0