Skip to content

Commit d8c601f

Browse files
author
Nick Harper
committed
Move functionality of PhabricatorMetaMTADaemon to a worker task
Summary: This will allow sending mail to be done by task workers. See T750. Task ID: # Blame Rev: Test Plan: - started taskmaster daemon in test env - used "send new test message" feature in MetMTA (with send now unchecked) - confirmed receipt of 1 email - repeated 2 & 3 with send now checked Revert Plan: Tags: Reviewers: epriestley, jungejason Reviewed By: epriestley CC: aran, epriestley Maniphest Tasks: T388, T750 Differential Revision: https://secure.phabricator.com/D1723
1 parent 03a108e commit d8c601f

File tree

9 files changed

+84
-50
lines changed

9 files changed

+84
-50
lines changed

src/__phutil_library_map__.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,6 @@
600600
'PhabricatorMetaMTAAttachment' => 'applications/metamta/storage/mail',
601601
'PhabricatorMetaMTAController' => 'applications/metamta/controller/base',
602602
'PhabricatorMetaMTADAO' => 'applications/metamta/storage/base',
603-
'PhabricatorMetaMTADaemon' => 'applications/metamta/daemon/mta',
604603
'PhabricatorMetaMTAEmailBodyParser' => 'applications/metamta/parser',
605604
'PhabricatorMetaMTAEmailBodyParserTestCase' => 'applications/metamta/parser/__tests__',
606605
'PhabricatorMetaMTAListController' => 'applications/metamta/controller/list',
@@ -615,6 +614,7 @@
615614
'PhabricatorMetaMTASendController' => 'applications/metamta/controller/send',
616615
'PhabricatorMetaMTASendGridReceiveController' => 'applications/metamta/controller/sendgridreceive',
617616
'PhabricatorMetaMTAViewController' => 'applications/metamta/controller/view',
617+
'PhabricatorMetaMTAWorker' => 'applications/metamta/worker',
618618
'PhabricatorMySQLFileStorageEngine' => 'applications/files/engine/mysql',
619619
'PhabricatorOAuthClientAuthorization' => 'applications/oauthserver/storage/clientauthorization',
620620
'PhabricatorOAuthClientAuthorizationBaseController' => 'applications/oauthserver/controller/clientauthorization/base',
@@ -1368,7 +1368,6 @@
13681368
'PhabricatorMailImplementationTestAdapter' => 'PhabricatorMailImplementationAdapter',
13691369
'PhabricatorMetaMTAController' => 'PhabricatorController',
13701370
'PhabricatorMetaMTADAO' => 'PhabricatorLiskDAO',
1371-
'PhabricatorMetaMTADaemon' => 'PhabricatorDaemon',
13721371
'PhabricatorMetaMTAEmailBodyParserTestCase' => 'PhabricatorTestCase',
13731372
'PhabricatorMetaMTAListController' => 'PhabricatorMetaMTAController',
13741373
'PhabricatorMetaMTAMail' => 'PhabricatorMetaMTADAO',
@@ -1382,6 +1381,7 @@
13821381
'PhabricatorMetaMTASendController' => 'PhabricatorMetaMTAController',
13831382
'PhabricatorMetaMTASendGridReceiveController' => 'PhabricatorMetaMTAController',
13841383
'PhabricatorMetaMTAViewController' => 'PhabricatorMetaMTAController',
1384+
'PhabricatorMetaMTAWorker' => 'PhabricatorWorker',
13851385
'PhabricatorMySQLFileStorageEngine' => 'PhabricatorFileStorageEngine',
13861386
'PhabricatorOAuthClientAuthorization' => 'PhabricatorOAuthServerDAO',
13871387
'PhabricatorOAuthClientAuthorizationBaseController' => 'PhabricatorOAuthServerController',

src/applications/daemon/controller/console/PhabricatorDaemonConsoleController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22

33
/*
4-
* Copyright 2011 Facebook, Inc.
4+
* Copyright 2012 Facebook, Inc.
55
*
66
* Licensed under the Apache License, Version 2.0 (the "License");
77
* you may not use this file except in compliance with the License.
@@ -75,7 +75,7 @@ public function processRequest() {
7575
'ID',
7676
'Class',
7777
'Owner',
78-
'Expries',
78+
'Expires',
7979
'Failures',
8080
'',
8181
));

src/applications/metamta/daemon/mta/PhabricatorMetaMTADaemon.php

Lines changed: 0 additions & 35 deletions
This file was deleted.

src/applications/metamta/storage/mail/PhabricatorMetaMTAMail.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,15 @@ public function setSimulatedFailureCount($count) {
171171
return $this;
172172
}
173173

174+
public function getWorkerTaskID() {
175+
return $this->getParam('worker-task');
176+
}
177+
178+
public function setWorkerTaskID($id) {
179+
$this->setParam('worker-task', $id);
180+
return $this;
181+
}
182+
174183
/**
175184
* Flag that this is an auto-generated bulk message and should have bulk
176185
* headers added to it if appropriate. Broadly, this means some flavor of
@@ -223,6 +232,19 @@ public function saveAndSend() {
223232
return $ret;
224233
}
225234

235+
protected function didWriteData() {
236+
parent::didWriteData();
237+
238+
if (!$this->getWorkerTaskID()) {
239+
$mailer_task = new PhabricatorWorkerTask();
240+
$mailer_task->setTaskClass('PhabricatorMetaMTAWorker');
241+
$mailer_task->setData($this->getID());
242+
$mailer_task->save();
243+
$this->setWorkerTaskID($mailer_task->getID());
244+
$this->save();
245+
}
246+
}
247+
226248

227249
public function buildDefaultMailer() {
228250
$class_name = PhabricatorEnv::getEnvConfig('metamta.mail-adapter');

src/applications/metamta/storage/mail/__init__.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
phutil_require_module('phabricator', 'applications/people/storage/preferences');
1111
phutil_require_module('phabricator', 'applications/people/storage/user');
1212
phutil_require_module('phabricator', 'applications/phid/handle/data');
13+
phutil_require_module('phabricator', 'infrastructure/daemon/workers/storage/task');
1314
phutil_require_module('phabricator', 'infrastructure/env');
1415

1516
phutil_require_module('phutil', 'symbols');
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
/*
4+
* Copyright 2012 Facebook, Inc.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
final class PhabricatorMetaMTAWorker
20+
extends PhabricatorWorker {
21+
22+
private $message;
23+
24+
public function getRequiredLeaseTime() {
25+
$message_id = $this->getTaskData();
26+
27+
$this->message = id(new PhabricatorMetaMTAMail())->loadOneWhere(
28+
'id = %d', $this->getTaskData());
29+
if (!$this->message) {
30+
return null;
31+
}
32+
33+
$lease_duration = max($this->message->getNextRetry() - time(), 0) + 15;
34+
return $lease_duration;
35+
}
36+
37+
public function doWork() {
38+
$message = $this->message;
39+
if (!$message
40+
|| $message->getStatus() != PhabricatorMetaMTAMail::STATUS_QUEUE) {
41+
return;
42+
}
43+
$id = $message->getID();
44+
$message->sendNow();
45+
// task failed if the message is still queued
46+
// (instead of sent, void, or failed)
47+
if ($message->getStatus() == PhabricatorMetaMTAMail::STATUS_QUEUE) {
48+
throw new Exception('Failed to send message');
49+
}
50+
}
51+
}

src/applications/metamta/daemon/mta/__init__.php renamed to src/applications/metamta/worker/__init__.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77

88

99
phutil_require_module('phabricator', 'applications/metamta/storage/mail');
10-
phutil_require_module('phabricator', 'infrastructure/daemon/base');
10+
phutil_require_module('phabricator', 'infrastructure/daemon/workers/worker');
1111

1212
phutil_require_module('phutil', 'utils');
1313

1414

15-
phutil_require_source('PhabricatorMetaMTADaemon.php');
15+
phutil_require_source('PhabricatorMetaMTAWorker.php');

src/docs/configuration/configuring_outbound_email.diviner

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -133,18 +133,15 @@ disable outbound mail, if you don't want to send mail or don't want to configure
133133
it yet. Just set **metamta.mail-adapter** to
134134
"PhabricatorMailImplementationTestAdapter".
135135

136-
= Configuring the MetaMTA Daemon =
136+
= Configuring MetaMTA to Send Mail Using a Daemon =
137137

138138
Regardless of how you are sending outbound email, you can move the handoff to
139139
the MTA out of the main process and into a daemon. This will greatly improve
140140
application performance if your mailer is slow, like Amazon SES. In particular,
141141
commenting on Differential Revisions and Maniphest Tasks sends outbound email.
142142

143-
To use the MetaMTA daemon:
144-
145-
- set **metamta.send-immediately** to ##false## in your configuration; and
146-
- launch a ##metamta## daemon with ##phabricator/bin/phd launch metamta##.
147-
143+
If you set **metamta.send-immediately** to ##false## in your configuration,
144+
MetaMTA will queue mail to be send by a PhabricatorTaskmasterDaemon.
148145
For more information on using daemons, see @{article:Managing Daemons with phd}.
149146

150147
= Testing and Debugging Outbound Email =
@@ -169,4 +166,4 @@ Continue by:
169166
- @{article:Configuring Inbound Email} so users can reply to email they
170167
receive about revisions and tasks to interact with them; or
171168
- learning about daemons with @{article:Managing Daemons with phd}; or
172-
- returning to the @{article:Configuration Guide}.
169+
- returning to the @{article:Configuration Guide}.

src/docs/configuration/managing_daemons.diviner

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,6 @@ You can get a list of launchable daemons with **phd list**:
6565

6666
- **libphutil test daemons** are not generally useful unless you are
6767
developing daemon infrastructure or debugging a daemon problem;
68-
- **PhabricatorMetaMTADaemon** sends mail in the background, see
69-
@{article:Configuring Outbound Email} for details;
7068
- **PhabricatorTaskmasterDaemon** runs a generic task queue; and
7169
- **PhabricatorRepository** daemons track repositories, descriptions are
7270
available in the @{article:Diffusion User Guide}.

0 commit comments

Comments
 (0)