From 950f3b1b86ed3b4e6259de68805ce109c4dc39c9 Mon Sep 17 00:00:00 2001 From: Toni Erdmann Date: Mon, 25 Nov 2024 22:17:01 +0100 Subject: [PATCH] add queue handling script for "analysis queue" issue #61 --- results/queue.php | 84 ++++++++++++++++++++++++++ script/queue.php | 146 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 230 insertions(+) create mode 100644 results/queue.php create mode 100644 script/queue.php diff --git a/results/queue.php b/results/queue.php new file mode 100644 index 0000000..4485f20 --- /dev/null +++ b/results/queue.php @@ -0,0 +1,84 @@ + + + + + + + + +
+ + + +
+ +\n"; + if ( isset($_SERVER['HTTP_REFERER']) ) { + preg_match( '/\/results\/.*\/([0-9A-ZÜa-zô_.-]+)-Analysis\.[dif.]*.*html$/', $_SERVER['HTTP_REFERER'], $matches ); + if ( isset($matches[1]) ) { + $network = $matches[1]; + $ret_val = InsertIntoAnalysisQueue( $network ); + http_response_code( $ret_val['HTTP'] ); + echo "\n"; + if ( $ret_val['HTTP'] >= 200 && $ret_val['HTTP'] < 300 ) { + echo '

Your analysis reqeuest for ' . $network . " has been accepted

\n"; + } else { + echo '

Your analysis reqeuest for ' . $network . " failed

\n"; + echo '
' . "\n"; + echo '

' . $ret_val['status'] . "

\n"; + echo "
\n"; + echo "
\n"; + } + } else { + preg_match( '/\/(statistics.php)$/', $_SERVER['HTTP_REFERER'], $matches ); + if ( !isset($matches[1]) ) { + $show_contents = 0; + http_response_code( 404 ); + echo '

Invalid analysis reqeuest: unknown ' . "'network'" . "

\n"; + echo "
\n"; + } + } + } +?> + + +

Contents of analysis queue

+
+ + + + + + + + + + + + + + + +
NetworkStateQueue AtStarted AtFinished AtChangesLogs
+ +
+ + +
+ +
+ + + +
+ + diff --git a/script/queue.php b/script/queue.php new file mode 100644 index 0000000..b222097 --- /dev/null +++ b/script/queue.php @@ -0,0 +1,146 @@ + 404, 'status' => "Not Found", 'queued' => '', 'started' => '', 'ip' => '' ); + + if ( $network && preg_match("/^[0-9A-ZÜa-zô_.-]+$/",$network) ) { + + $SqliteDb = FindAnalysisQueueSqliteDb(); + + if ( $SqliteDb != '' ) { + + try { + + $db = new SQLite3( $SqliteDb ); + + $sql = "BEGIN TRANSACTION"; + $result = $db->exec( $sql ); + + $sql = "SELECT COUNT(*) AS num FROM queue WHERE status='queued';"; + $result = $db->querySingle( $sql, true ); + if ( $result['num'] < 10 ) { + $sql = sprintf( "SELECT COUNT(*) AS num FROM queue WHERE network='%s' AND status='queued';", $network ); + $result = $db->querySingle( $sql, true ); + if ( $result['num'] == 0 ) { + $sql = sprintf( "SELECT COUNT(*) AS num FROM queue WHERE network='%s' AND status='started';", $network ); + $result = $db->querySingle( $sql, true ); + if ( $result['num'] == 0 ) { + $when = time(); + if ( isset($_SERVER['REMOTE_ADDR']) ) { + $ip = $_SERVER['REMOTE_ADDR']; + } else { + $ip = 'unknown'; + } + $sql = sprintf( "INSERT INTO queue (network,status,queued,ip) VALUES ('%s','%s',%d,'%s');", $network, 'queued', $when, $ip ); + $db->querySingle( $sql, true ); + $return_array['HTTP'] = 202; + $return_array['status'] = 'In Queue'; + $return_array['queued'] = $when; + $return_array['ip'] = $ip; + } else { + $return_array['HTTP'] = 429; + $return_array['status'] = 'Is Already Running'; + } + } else { + $return_array['HTTP'] = 429; + $return_array['status'] = 'Already In Queue'; + } + } else { + $return_array['HTTP'] = 429; + $return_array['status'] = 'Queue Is Full'; + } + $sql = "COMMIT TRANSACTION"; + $result = $db->exec( $sql ); + $db->close(); + } catch ( Exception $ex ) { + $return_array['HTTP'] = 500; + $return_array['status'] = "Server Error '" . $ex . "'"; + } + } else { + $return_array['HTTP'] = 404; + $return_array['status'] = 'Queue Not Found'; + } + } + + return $return_array; + } + + + function PrintAnalysisQueue() { + + $SqliteDb = FindAnalysisQueueSqliteDb(); + + if ( $SqliteDb != '' ) { + + try { + + $db = new SQLite3( $SqliteDb ); + + $sql = "BEGIN TRANSACTION"; + $result = $db->exec( $sql ); + + $sql = "SELECT * FROM queue ORDER BY queued DESC;"; + $result = $db->query( $sql ); + while ( $queue_infos=$result->fetchArray(SQLITE3_ASSOC) ) { + printf( "\n" ); + printf( " %s\n", $queue_infos['network'] ); + printf( " %s\n", $queue_infos['status'] ); + if ( $queue_infos['queued'] ) { + printf( " %s UTC\n", date("Y-m-d H:i:s",$queue_infos['queued']) ); + } else { + printf( "  \n" ); + } + if ( $queue_infos['started'] ) { + printf( " %s UTC\n", date("Y-m-d H:i:s",$queue_infos['started']) ); + } else { + printf( "  \n" ); + } + if ( $queue_infos['stopped'] ) { + printf( " %s UTC\n", date("Y-m-d H:i:s",$queue_infos['stopped']) ); + } else { + printf( "  \n" ); + } + if ( $queue_infos['status'] == 'stopped' ) { + printf( " %d\n", $queue_infos['changes'] ); + } else { + printf( "  \n" ); + } + if ( $queue_infos['status'] == 'started' || $queue_infos['status'] == 'stopped' ) { + printf( " logs\n", $queue_infos['network'] ); + } else if ( $queue_infos['status'] == 'locked' ) { + printf( " Another analysis for this 'network' was already running when this one was ready to be started\n" ); + } else { + printf( "  \n" ); + } + printf( "\n" ); + } + $sql = "COMMIT TRANSACTION"; + $result = $db->exec( $sql ); + $db->close(); + } catch ( Exception $ex ) { + printf( "\n" ); + printf( " Server Error: %s\n", $ex ); + printf( "\n" ); + } + } else { + printf( "\n" ); + printf( " Queue Not Found\n" ); + printf( "\n" ); + } + } + +?>