From d91abf50f70a8a37ddca2863abec79c6062491fa Mon Sep 17 00:00:00 2001 From: epriestley Date: Thu, 25 Jun 2020 17:45:48 -0700 Subject: [PATCH] Add "--background" and "--count" flags to "bin/webhook call" Summary: See PHI1794, which reports an issue where a large number of queued webhook calls led to connection exhaustion. To make this easier to reproduce and test, add "--count" and "--background" flags to "bin/webhook call". This primarily supports "bin/webook call ... --background --count 10000" to quickly fill the queue with a bunch of calls. Test Plan: Ran `bin/webhook call` in foreground and background modes, with and without counts. Saw appropriate console and queue behavior. Differential Revision: https://secure.phabricator.com/D21368 --- .../HeraldWebhookCallManagementWorkflow.php | 80 +++++++++++++++---- 1 file changed, 64 insertions(+), 16 deletions(-) diff --git a/src/applications/herald/management/HeraldWebhookCallManagementWorkflow.php b/src/applications/herald/management/HeraldWebhookCallManagementWorkflow.php index 10249cca68..1b5b21482f 100644 --- a/src/applications/herald/management/HeraldWebhookCallManagementWorkflow.php +++ b/src/applications/herald/management/HeraldWebhookCallManagementWorkflow.php @@ -28,6 +28,17 @@ final class HeraldWebhookCallManagementWorkflow 'name' => 'secure', 'help' => pht('Set the "secure" flag on the request.'), ), + array( + 'name' => 'count', + 'param' => 'N', + 'help' => pht('Make a total of __N__ copies of the call.'), + ), + array( + 'name' => 'background', + 'help' => pht( + 'Instead of making calls in the foreground, add the tasks '. + 'to the daemon queue.'), + ), )); } @@ -41,6 +52,17 @@ final class HeraldWebhookCallManagementWorkflow 'Specify a webhook to call with "--id".')); } + $count = $args->getArg('count'); + if ($count === null) { + $count = 1; + } + + if ($count <= 0) { + throw new PhutilArgumentUsageException( + pht( + 'Specified "--count" must be larger than 0.')); + } + $hook = id(new HeraldWebhookQuery()) ->setViewer($viewer) ->withIDs(array($id)) @@ -69,6 +91,8 @@ final class HeraldWebhookCallManagementWorkflow $object = head($objects); } + $is_background = $args->getArg('background'); + $xaction_query = PhabricatorApplicationTransactionQuery::newQueryForObject($object); @@ -80,25 +104,49 @@ final class HeraldWebhookCallManagementWorkflow $application_phid = id(new PhabricatorHeraldApplication())->getPHID(); - $request = HeraldWebhookRequest::initializeNewWebhookRequest($hook) - ->setObjectPHID($object->getPHID()) - ->setIsTestAction(true) - ->setIsSilentAction((bool)$args->getArg('silent')) - ->setIsSecureAction((bool)$args->getArg('secure')) - ->setTriggerPHIDs(array($application_phid)) - ->setTransactionPHIDs(mpull($xactions, 'getPHID')) - ->save(); + if ($is_background) { + echo tsprintf( + "%s\n", + pht( + 'Queueing webhook calls...')); + $progress_bar = id(new PhutilConsoleProgressBar()) + ->setTotal($count); + } else { + echo tsprintf( + "%s\n", + pht( + 'Calling webhook...')); + PhabricatorWorker::setRunAllTasksInProcess(true); + } - PhabricatorWorker::setRunAllTasksInProcess(true); - $request->queueCall(); + for ($ii = 0; $ii < $count; $ii++) { + $request = HeraldWebhookRequest::initializeNewWebhookRequest($hook) + ->setObjectPHID($object->getPHID()) + ->setIsTestAction(true) + ->setIsSilentAction((bool)$args->getArg('silent')) + ->setIsSecureAction((bool)$args->getArg('secure')) + ->setTriggerPHIDs(array($application_phid)) + ->setTransactionPHIDs(mpull($xactions, 'getPHID')) + ->save(); - $request->reload(); + $request->queueCall(); - echo tsprintf( - "%s\n", - pht( - 'Success, got HTTP %s from webhook.', - $request->getErrorCode())); + if ($is_background) { + $progress_bar->update(1); + } else { + $request->reload(); + + echo tsprintf( + "%s\n", + pht( + 'Success, got HTTP %s from webhook.', + $request->getErrorCode())); + } + } + + if ($is_background) { + $progress_bar->done(); + } return 0; }