Skip to content

Commit d91abf5

Browse files
author
epriestley
committedJun 26, 2020
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
1 parent 9ce1271 commit d91abf5

File tree

1 file changed

+67
-19
lines changed

1 file changed

+67
-19
lines changed
 

‎src/applications/herald/management/HeraldWebhookCallManagementWorkflow.php

+67-19
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,17 @@ protected function didConstruct() {
2828
'name' => 'secure',
2929
'help' => pht('Set the "secure" flag on the request.'),
3030
),
31+
array(
32+
'name' => 'count',
33+
'param' => 'N',
34+
'help' => pht('Make a total of __N__ copies of the call.'),
35+
),
36+
array(
37+
'name' => 'background',
38+
'help' => pht(
39+
'Instead of making calls in the foreground, add the tasks '.
40+
'to the daemon queue.'),
41+
),
3142
));
3243
}
3344

@@ -41,6 +52,17 @@ public function execute(PhutilArgumentParser $args) {
4152
'Specify a webhook to call with "--id".'));
4253
}
4354

55+
$count = $args->getArg('count');
56+
if ($count === null) {
57+
$count = 1;
58+
}
59+
60+
if ($count <= 0) {
61+
throw new PhutilArgumentUsageException(
62+
pht(
63+
'Specified "--count" must be larger than 0.'));
64+
}
65+
4466
$hook = id(new HeraldWebhookQuery())
4567
->setViewer($viewer)
4668
->withIDs(array($id))
@@ -69,6 +91,8 @@ public function execute(PhutilArgumentParser $args) {
6991
$object = head($objects);
7092
}
7193

94+
$is_background = $args->getArg('background');
95+
7296
$xaction_query =
7397
PhabricatorApplicationTransactionQuery::newQueryForObject($object);
7498

@@ -80,25 +104,49 @@ public function execute(PhutilArgumentParser $args) {
80104

81105
$application_phid = id(new PhabricatorHeraldApplication())->getPHID();
82106

83-
$request = HeraldWebhookRequest::initializeNewWebhookRequest($hook)
84-
->setObjectPHID($object->getPHID())
85-
->setIsTestAction(true)
86-
->setIsSilentAction((bool)$args->getArg('silent'))
87-
->setIsSecureAction((bool)$args->getArg('secure'))
88-
->setTriggerPHIDs(array($application_phid))
89-
->setTransactionPHIDs(mpull($xactions, 'getPHID'))
90-
->save();
91-
92-
PhabricatorWorker::setRunAllTasksInProcess(true);
93-
$request->queueCall();
94-
95-
$request->reload();
96-
97-
echo tsprintf(
98-
"%s\n",
99-
pht(
100-
'Success, got HTTP %s from webhook.',
101-
$request->getErrorCode()));
107+
if ($is_background) {
108+
echo tsprintf(
109+
"%s\n",
110+
pht(
111+
'Queueing webhook calls...'));
112+
$progress_bar = id(new PhutilConsoleProgressBar())
113+
->setTotal($count);
114+
} else {
115+
echo tsprintf(
116+
"%s\n",
117+
pht(
118+
'Calling webhook...'));
119+
PhabricatorWorker::setRunAllTasksInProcess(true);
120+
}
121+
122+
for ($ii = 0; $ii < $count; $ii++) {
123+
$request = HeraldWebhookRequest::initializeNewWebhookRequest($hook)
124+
->setObjectPHID($object->getPHID())
125+
->setIsTestAction(true)
126+
->setIsSilentAction((bool)$args->getArg('silent'))
127+
->setIsSecureAction((bool)$args->getArg('secure'))
128+
->setTriggerPHIDs(array($application_phid))
129+
->setTransactionPHIDs(mpull($xactions, 'getPHID'))
130+
->save();
131+
132+
$request->queueCall();
133+
134+
if ($is_background) {
135+
$progress_bar->update(1);
136+
} else {
137+
$request->reload();
138+
139+
echo tsprintf(
140+
"%s\n",
141+
pht(
142+
'Success, got HTTP %s from webhook.',
143+
$request->getErrorCode()));
144+
}
145+
}
146+
147+
if ($is_background) {
148+
$progress_bar->done();
149+
}
102150

103151
return 0;
104152
}

0 commit comments

Comments
 (0)
Failed to load comments.