diff --git a/src/applications/harbormaster/editor/HarbormasterBuildTransactionEditor.php b/src/applications/harbormaster/editor/HarbormasterBuildTransactionEditor.php --- a/src/applications/harbormaster/editor/HarbormasterBuildTransactionEditor.php +++ b/src/applications/harbormaster/editor/HarbormasterBuildTransactionEditor.php @@ -93,20 +93,37 @@ return; } - id(new HarbormasterBuildCommand()) + // establish separate connection to avoid HarbormasterBuildCommand + // being created in transaction which already is happening, otherwise + // Worker can be started before HarbormasterBuildCommand is committed + $conn = id(new HarbormasterBuildCommand()) + ->establishConnection('w', $force_new = true); + + $build_command = id(new HarbormasterBuildCommand()) + ->setForcedConnection($conn) ->setAuthorPHID($xaction->getAuthorPHID()) ->setTargetPHID($build->getPHID()) ->setCommand($command) ->save(); - PhabricatorWorker::scheduleTask( - 'HarbormasterBuildWorker', - array( - 'buildID' => $build->getID(), - ), - array( - 'objectPHID' => $build->getPHID(), - )); + try { + PhabricatorWorker::scheduleTask( + 'HarbormasterBuildWorker', + array( + 'buildID' => $build->getID(), + ), + array( + 'objectPHID' => $build->getPHID(), + )); + } catch (Exception $e) { + // if we can't schedule task, let's delete command + $build_command->delete(); + throw $e; + } catch (Throwable $e) { + // if we can't schedule task, let's delete command + $build_command->delete(); + throw $e; + } } protected function applyCustomExternalTransaction(