diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -1306,6 +1306,7 @@ '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 @@ '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 --- a/src/applications/harbormaster/management/HarbormasterManagementWriteLogWorkflow.php +++ b/src/applications/harbormaster/management/HarbormasterManagementWriteLogWorkflow.php @@ -39,7 +39,6 @@ $target_id)); } - $log = HarbormasterBuildLog::initializeNewBuildLog($target); $log->openBuildLog(); @@ -50,6 +49,12 @@ $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 --- a/src/applications/harbormaster/storage/build/HarbormasterBuildLog.php +++ b/src/applications/harbormaster/storage/build/HarbormasterBuildLog.php @@ -52,17 +52,24 @@ 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 @@ 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 --- /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(); + } + } + +}