From bff1d3b85487e8d41391d777e9f75cf6a1b06d0e Mon Sep 17 00:00:00 2001 From: David Grudl Date: Sat, 28 Aug 2021 19:29:32 +0200 Subject: [PATCH] deletes temporary files when stopped by Ctrl+C / SIGINT --- src/Deployment/Deployer.php | 47 +++++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 18 deletions(-) diff --git a/src/Deployment/Deployer.php b/src/Deployment/Deployer.php index 1039dda..7f1e759 100644 --- a/src/Deployment/Deployer.php +++ b/src/Deployment/Deployer.php @@ -137,25 +137,25 @@ public function deploy(): void $this->runJobs($runBefore[0]); } - if ($toUpload) { - $this->logger->log("\nUploading:"); - $this->uploadPaths($toUpload); - if ($this->runAfterUpload) { - $this->logger->log("\nAfter-upload-jobs:"); - $this->runJobs($this->runAfterUpload); + try { + $tempFiles = []; + if ($toUpload) { + $this->logger->log("\nUploading:"); + $this->uploadPaths($toUpload, $tempFiles); + if ($this->runAfterUpload) { + $this->logger->log("\nAfter-upload-jobs:"); + $this->runJobs($this->runAfterUpload); + } } - } - $this->logger->log("Creating remote file $this->deploymentFile.running"); - $runningFile = "$this->remoteDir/$this->deploymentFile.running"; - $this->server->createDir(str_replace('\\', '/', dirname($runningFile))); - $this->server->writeFile(tempnam($this->tempDir, 'deploy'), $runningFile); + $this->logger->log("Creating remote file $this->deploymentFile.running"); + $runningFile = "$this->remoteDir/$this->deploymentFile.running"; + $this->server->createDir(str_replace('\\', '/', dirname($runningFile))); + $this->server->writeFile(tempnam($this->tempDir, 'deploy'), $runningFile); - try { if ($toUpload) { $this->logger->log("\nRenaming:"); - $this->renamePaths($toUpload); - unlink($deploymentFile); + $this->renamePaths($toUpload, $tempFiles); } if ($toDelete) { @@ -182,8 +182,17 @@ public function deploy(): void } } finally { - $this->logger->log("\nDeleting remote file $this->deploymentFile.running"); - $this->server->removeFile($runningFile); + if (isset($runningFile)) { + $this->logger->log("\nDeleting remote file $this->deploymentFile.running"); + $this->server->removeFile($runningFile); + } + if (isset($deploymentFile)) { + unlink($deploymentFile); + } + if ($tempFiles) { + $this->logger->log("\nDeleting temporary files:"); + $this->deletePaths(array_keys($tempFiles)); + } } } @@ -245,7 +254,7 @@ public function writeDeploymentFile(array $localPaths): string * Uploades files and creates directories. * @param string[] $paths relative paths, starts with / */ - private function uploadPaths(array $paths): void + private function uploadPaths(array $paths, array &$tempFiles): void { $prevDir = null; foreach ($paths as $num => $path) { @@ -264,6 +273,7 @@ private function uploadPaths(array $paths): void continue; } + $tempFiles[$path . self::TEMPORARY_SUFFIX] = true; $localFile = $this->preprocess($path); if ($localFile !== $this->localDir . $path) { $path .= ' (filters applied)'; @@ -285,13 +295,14 @@ function ($percent) use ($num, $paths, $path) { * Renames uploaded files. * @param string[] $paths relative paths, starts with / */ - private function renamePaths(array $paths): void + private function renamePaths(array $paths, array &$tempFiles): void { $files = array_values(array_filter($paths, fn($path) => substr($path, -1) !== '/')); foreach ($files as $num => $file) { $this->writeProgress($num + 1, count($files), "Renaming $file", null, 'olive'); $remoteFile = $this->remoteDir . $file; $this->server->renameFile($remoteFile . self::TEMPORARY_SUFFIX, $remoteFile); + unset($tempFiles[$file . self::TEMPORARY_SUFFIX]); } }