From 058ea5695a0637420798ca15d1616208fe149ba9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Schw=C3=B6rer?= Date: Wed, 22 Apr 2020 11:24:48 +0200 Subject: [PATCH] update extgitgraph --- www/commands/extendedgitgraph_refresh.php | 17 +++++++-- www/extern/egg/EGGDatabase.php | 24 +++++++++++-- www/extern/egg/EGGException.php | 14 ++++++++ www/extern/egg/ExtendedGitGraph2.php | 44 ++++++++++++++++++++--- www/extern/egg/RemoteSource.php | 4 +-- www/extern/egg/Utils.php | 2 +- www/protected/vendor/.gitkeep | 0 7 files changed, 93 insertions(+), 12 deletions(-) create mode 100644 www/extern/egg/EGGException.php create mode 100644 www/protected/vendor/.gitkeep diff --git a/www/commands/extendedgitgraph_refresh.php b/www/commands/extendedgitgraph_refresh.php index a65c9fe..eff8dcd 100644 --- a/www/commands/extendedgitgraph_refresh.php +++ b/www/commands/extendedgitgraph_refresh.php @@ -7,6 +7,19 @@ require_once (__DIR__ . '/../internals/website.php'); set_time_limit(900); // 15min -$SITE->modules->ExtendedGitGraph()->update(); -$SITE->modules->ExtendedGitGraph()->updateCache(); +$r1 = $SITE->modules->ExtendedGitGraph()->update(); +if (!$r1) +{ + http_response_code(500); + echo 'EGG::update failed.'; +} +$r2 = $SITE->modules->ExtendedGitGraph()->updateCache(); +if (!$r2) +{ + http_response_code(500); + echo 'EGG::updateCache failed.'; +} + +http_response_code(200); +echo 'Done.'; \ No newline at end of file diff --git a/www/extern/egg/EGGDatabase.php b/www/extern/egg/EGGDatabase.php index a05d7fd..87574d7 100644 --- a/www/extern/egg/EGGDatabase.php +++ b/www/extern/egg/EGGDatabase.php @@ -45,6 +45,26 @@ class EGGDatabase if(!$exists) $this->init(); } + public function openReadOnly() + { + $this->open(); + } + + public function beginTransaction() + { + $this->pdo->beginTransaction(); + } + + public function commitTransaction() + { + $this->pdo->commit(); + } + + public function abortTransactionIfExists() + { + if ($this->pdo !== null) $this->pdo->rollBack(); + } + private function init() { $this->logger->proclog("Initialize new database '" . $this->path . "'"); @@ -133,7 +153,7 @@ class EGGDatabase [":url", $url, PDO::PARAM_STR], ]); - if (count($repo) === 0) throw new Exception("No repo after insert [" . $source . "|" . $name . "]"); + if (count($repo) === 0) throw new EGGException("No repo after insert [" . $source . "|" . $name . "]"); $this->logger->proclog("Inserted (new) repository [" . $source . "|" . $name . "] into database"); } @@ -180,7 +200,7 @@ class EGGDatabase [":nam", $name, PDO::PARAM_STR], ]); - if (count($branch) === 0) throw new Exception("No branch after insert [" . $source . "|" . $repo->Name . "|" . $name . "]"); + if (count($branch) === 0) throw new EGGException("No branch after insert [" . $source . "|" . $repo->Name . "|" . $name . "]"); $this->logger->proclog("Inserted (new) branch [" . $source . "|" . $repo->Name . "|" . $name . "] into database"); } diff --git a/www/extern/egg/EGGException.php b/www/extern/egg/EGGException.php new file mode 100644 index 0000000..9052185 --- /dev/null +++ b/www/extern/egg/EGGException.php @@ -0,0 +1,14 @@ +egg_message = $message; + } +} \ No newline at end of file diff --git a/www/extern/egg/ExtendedGitGraph2.php b/www/extern/egg/ExtendedGitGraph2.php index 62b796f..263995a 100644 --- a/www/extern/egg/ExtendedGitGraph2.php +++ b/www/extern/egg/ExtendedGitGraph2.php @@ -1,6 +1,7 @@ getName(), $sourcenames)) throw new Exception("Duplicate source name: " . $newsrc->getName()); + if (array_key_exists($newsrc->getName(), $sourcenames)) throw new EGGException("Duplicate source name: " . $newsrc->getName()); $this->sources []= $newsrc; $sourcenames []= $newsrc->getName(); @@ -61,6 +62,7 @@ class ExtendedGitGraph2 implements ILogger try { $this->db->open(); + $this->db->beginTransaction(); $this->proclog("Start incremental data update"); $this->proclog(); @@ -78,7 +80,21 @@ class ExtendedGitGraph2 implements ILogger $this->proclog("Update finished."); + $this->db->commitTransaction(); + $this->proclog("Data written."); + $this->db->close(); + + return true; + } + catch (EGGException $exception) + { + $this->proclog("ExtendedGitGraph2::update failed:"); + $this->proclog($exception->egg_message); + + $this->db->abortTransactionIfExists(); + + return false; } catch (Exception $exception) { @@ -87,14 +103,19 @@ class ExtendedGitGraph2 implements ILogger $this->proclog($exception->getMessage()); $this->proclog(); $this->proclog($exception->getTraceAsString()); + + $this->db->abortTransactionIfExists(); + + return false; } } - public function updateCache(): string + public function updateCache(): ?string { try { - $this->db->open(); + $this->db->openReadOnly(); + $this->db->beginTransaction(); $this->proclog("Start update cache"); $this->proclog(); @@ -106,6 +127,15 @@ class ExtendedGitGraph2 implements ILogger return $data; } + catch (EGGException $exception) + { + $this->proclog("ExtendedGitGraph2::updateCache failed:"); + $this->proclog($exception->egg_message); + + $this->db->abortTransactionIfExists(); + + return null; + } catch (Exception $exception) { $this->proclog("(!) FATAL ERROR -- UNCAUGHT EXCEPTION THROWN"); @@ -113,6 +143,8 @@ class ExtendedGitGraph2 implements ILogger $this->proclog($exception->getMessage()); $this->proclog(); $this->proclog($exception->getTraceAsString()); + + return null; } } @@ -123,10 +155,12 @@ class ExtendedGitGraph2 implements ILogger { try { - $this->db->open(); + $this->db->openReadOnly(); $data = $this->outputter->loadFromCache(); + $this->db->close(); + return $data; } catch (Exception $exception) diff --git a/www/extern/egg/RemoteSource.php b/www/extern/egg/RemoteSource.php index f5b9868..ca519ff 100644 --- a/www/extern/egg/RemoteSource.php +++ b/www/extern/egg/RemoteSource.php @@ -379,13 +379,13 @@ class GithubConnection extends StandardGitConnection $url = Utils::sharpFormat(self::URL_OAUTH_TOKEN, ['id'=>$this->oauth_id, 'secret'=>$this->oauth_secret, 'code'=>'egg']); $fullresult = $result = file_get_contents($url); + if (Utils::startsWith($fullresult, 'error=')) throw new EGGException('GitHub Auth failed: ' . $fullresult); + $result = str_replace('access_token=', '', $result); $result = str_replace('&scope=&token_type=bearer', '', $result); $this->logger->proclog("Updated Github API token"); - if (Utils::startsWith($result, "error=")) throw new Exception($fullresult); - if ($result!=='' && $result !== null && $this->apitokenpath !== null) file_put_contents($this->apitokenpath, $result); diff --git a/www/extern/egg/Utils.php b/www/extern/egg/Utils.php index ae8002f..7820e4a 100644 --- a/www/extern/egg/Utils.php +++ b/www/extern/egg/Utils.php @@ -136,7 +136,7 @@ class Utils { $logger->proclog("Error recieving json: '" . $url . "'"); $logger->proclog(print_r(error_get_last(), true)); - return []; + throw new EGGException("Error recieving json: '" . $url . "'"); } return json_decode($response); diff --git a/www/protected/vendor/.gitkeep b/www/protected/vendor/.gitkeep new file mode 100644 index 0000000..e69de29