Skip to content

Commit

Permalink
deletes temporary files when stopped by Ctrl+C / SIGINT
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Aug 29, 2021
1 parent df95cef commit bff1d3b
Showing 1 changed file with 29 additions and 18 deletions.
47 changes: 29 additions & 18 deletions src/Deployment/Deployer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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));
}
}
}

Expand Down Expand Up @@ -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) {
Expand All @@ -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)';
Expand All @@ -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]);
}
}

Expand Down

0 comments on commit bff1d3b

Please sign in to comment.