diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php index 2d610818e5..d43f386009 100644 --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -1306,6 +1306,7 @@ phutil_register_library_map(array( 'HarbormasterLeaseWorkingCopyBuildStepImplementation' => 'applications/harbormaster/step/HarbormasterLeaseWorkingCopyBuildStepImplementation.php', 'HarbormasterLintMessagesController' => 'applications/harbormaster/controller/HarbormasterLintMessagesController.php', 'HarbormasterLintPropertyView' => 'applications/harbormaster/view/HarbormasterLintPropertyView.php', + 'HarbormasterLogWorker' => 'applications/harbormaster/worker/HarbormasterLogWorker.php', 'HarbormasterManagementArchiveLogsWorkflow' => 'applications/harbormaster/management/HarbormasterManagementArchiveLogsWorkflow.php', 'HarbormasterManagementBuildWorkflow' => 'applications/harbormaster/management/HarbormasterManagementBuildWorkflow.php', 'HarbormasterManagementRestartWorkflow' => 'applications/harbormaster/management/HarbormasterManagementRestartWorkflow.php', @@ -6610,6 +6611,7 @@ phutil_register_library_map(array( 'HarbormasterLeaseWorkingCopyBuildStepImplementation' => 'HarbormasterBuildStepImplementation', 'HarbormasterLintMessagesController' => 'HarbormasterController', 'HarbormasterLintPropertyView' => 'AphrontView', + 'HarbormasterLogWorker' => 'HarbormasterWorker', 'HarbormasterManagementArchiveLogsWorkflow' => 'HarbormasterManagementWorkflow', 'HarbormasterManagementBuildWorkflow' => 'HarbormasterManagementWorkflow', 'HarbormasterManagementRestartWorkflow' => 'HarbormasterManagementWorkflow', diff --git a/src/applications/harbormaster/management/HarbormasterManagementWriteLogWorkflow.php b/src/applications/harbormaster/management/HarbormasterManagementWriteLogWorkflow.php index d84a9ed510..b20204670b 100644 --- a/src/applications/harbormaster/management/HarbormasterManagementWriteLogWorkflow.php +++ b/src/applications/harbormaster/management/HarbormasterManagementWriteLogWorkflow.php @@ -39,7 +39,6 @@ final class HarbormasterManagementWriteLogWorkflow $target_id)); } - $log = HarbormasterBuildLog::initializeNewBuildLog($target); $log->openBuildLog(); @@ -50,6 +49,12 @@ final class HarbormasterManagementWriteLogWorkflow $content = file_get_contents('php://stdin'); $log->append($content); + echo tsprintf( + "%s\n", + pht('Write completed. Closing log...')); + + PhabricatorWorker::setRunAllTasksInProcess(true); + $log->closeBuildLog(); echo tsprintf( diff --git a/src/applications/harbormaster/storage/build/HarbormasterBuildLog.php b/src/applications/harbormaster/storage/build/HarbormasterBuildLog.php index 290b288c6c..2826f445de 100644 --- a/src/applications/harbormaster/storage/build/HarbormasterBuildLog.php +++ b/src/applications/harbormaster/storage/build/HarbormasterBuildLog.php @@ -52,17 +52,24 @@ final class HarbormasterBuildLog throw new Exception(pht('This build log is not open!')); } - if ($this->canCompressLog()) { - $this->compressLog(); - } - $start = $this->getDateCreated(); $now = PhabricatorTime::getNow(); - return $this + $this ->setDuration($now - $start) ->setLive(0) ->save(); + + PhabricatorWorker::scheduleTask( + 'HarbormasterLogWorker', + array( + 'logPHID' => $this->getPHID(), + ), + array( + 'objectPHID' => $this->getPHID(), + )); + + return $this; } @@ -201,7 +208,7 @@ final class HarbormasterBuildLog return implode('', $full_text); } - private function canCompressLog() { + public function canCompressLog() { return function_exists('gzdeflate'); } diff --git a/src/applications/harbormaster/worker/HarbormasterLogWorker.php b/src/applications/harbormaster/worker/HarbormasterLogWorker.php new file mode 100644 index 0000000000..a55420bc2f --- /dev/null +++ b/src/applications/harbormaster/worker/HarbormasterLogWorker.php @@ -0,0 +1,50 @@ +getViewer(); + + $data = $this->getTaskData(); + $log_phid = idx($data, 'logPHID'); + + $log = id(new HarbormasterBuildLogQuery()) + ->setViewer($viewer) + ->withPHIDs(array($log_phid)) + ->executeOne(); + if (!$log) { + throw new PhabricatorWorkerPermanentFailureException( + pht('Invalid build log PHID "%s".', $log_phid)); + } + + $phid_key = PhabricatorHash::digestToLength($log_phid, 14); + $lock_key = "build.log({$phid_key})"; + $lock = PhabricatorGlobalLock::newLock($lock_key); + + try { + $lock->lock(); + } catch (PhutilLockException $ex) { + throw new PhabricatorWorkerYieldException(15); + } + + $caught = null; + try { + $this->finalizeBuildLog($log); + } catch (Exception $ex) { + $caught = $ex; + } + + $lock->unlock(); + + if ($caught) { + throw $caught; + } + } + + private function finalizeBuildLog(HarbormasterBuildLog $log) { + if ($log->canCompressLog()) { + $log->compressLog(); + } + } + +}