Skip to content

Commit 193dbf1

Browse files
author
epriestley
committedFeb 10, 2011
Very basic daemon infrastructure, plus MetaMTA daemon.
Summary: Amazon SES seems to be working well, except that it takes more than a second to send mail in-process. Kick it out of process. (Between this and the ImplementationAdapter layer, MetaMTA almost makes sense. :/) Test Plan: Ran the daemon and got a flood of unsent test email. Reviewers: CC:
1 parent b7801c4 commit 193dbf1

10 files changed

+96
-15
lines changed
 

‎scripts/__init_script__.php

+9
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,12 @@
3030
}
3131

3232
phutil_load_library(dirname(__FILE__).'/../src/');
33+
34+
function phabricator_read_config_file($config) {
35+
$root = dirname(dirname(__FILE__));
36+
$conf = include $root.'/conf/'.$config.'.conf.php';
37+
if ($conf === false) {
38+
throw new Exception("Failed to read config file '{$config}'.");
39+
}
40+
return $conf;
41+
}

‎scripts/daemon/run_daemon.php

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/usr/bin/env php
2+
<?php
3+
4+
/*
5+
* Copyright 2011 Facebook, Inc.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
20+
$root = dirname(dirname(dirname(__FILE__)));
21+
require_once $root.'/scripts/__init_script__.php';
22+
23+
$env = getenv('PHABRICATOR_ENV');
24+
if (!$env) {
25+
echo "Define PHABRICATOR_ENV before running scripts.\n";
26+
exit(1);
27+
}
28+
29+
$conf = phabricator_read_config_file($env);
30+
$conf['phabricator.env'] = $env;
31+
32+
phutil_require_module('phabricator', 'infrastructure/env');
33+
PhabricatorEnv::setEnvConfig($conf);
34+
phutil_require_module('phutil', 'symbols');
35+
36+
PhutilSymbolLoader::loadClass('PhabricatorMetaMTADaemon');
37+
$daemon = new PhabricatorMetaMTADaemon();
38+
$daemon->run();

‎src/__phutil_library_map__.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@
196196
'PhabricatorMailImplementationPHPMailerLiteAdapter' => 'applications/metamta/adapter/phpmailerlite',
197197
'PhabricatorMetaMTAController' => 'applications/metamta/controller/base',
198198
'PhabricatorMetaMTADAO' => 'applications/metamta/storage/base',
199+
'PhabricatorMetaMTADaemon' => 'applications/metamta/daemon/mta',
199200
'PhabricatorMetaMTAListController' => 'applications/metamta/controller/list',
200201
'PhabricatorMetaMTAMail' => 'applications/metamta/storage/mail',
201202
'PhabricatorMetaMTAMailingList' => 'applications/metamta/storage/mailinglist',
@@ -390,7 +391,7 @@
390391
'PhabricatorLiskDAO' => 'LiskDAO',
391392
'PhabricatorLoginController' => 'PhabricatorAuthController',
392393
'PhabricatorLogoutController' => 'PhabricatorAuthController',
393-
'PhabricatorMailImplementationAmazonSESAdapter' => 'PhabricatorMailImplementationAdapter',
394+
'PhabricatorMailImplementationAmazonSESAdapter' => 'PhabricatorMailImplementationPHPMailerLiteAdapter',
394395
'PhabricatorMailImplementationPHPMailerLiteAdapter' => 'PhabricatorMailImplementationAdapter',
395396
'PhabricatorMetaMTAController' => 'PhabricatorController',
396397
'PhabricatorMetaMTADAO' => 'PhabricatorLiskDAO',

‎src/applications/metamta/adapter/amazonses/PhabricatorMailImplementationAmazonSESAdapter.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function __construct() {
2828
$this->mailer->customMailer = $this;
2929
}
3030

31-
public function executeSend($body) {
31+
public function executeSend($body) {
3232
$key = PhabricatorEnv::getEnvConfig('amazon-ses.access-key');
3333
$secret = PhabricatorEnv::getEnvConfig('amazon-ses.secret-key');
3434

‎src/applications/metamta/adapter/amazonses/__init__.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@
66

77

88

9-
phutil_require_module('phabricator', 'applications/metamta/adapter/base');
9+
phutil_require_module('phabricator', 'applications/metamta/adapter/phpmailerlite');
1010
phutil_require_module('phabricator', 'infrastructure/env');
1111

1212
phutil_require_module('phutil', 'moduleutils');
13-
phutil_require_module('phutil', 'utils');
1413

1514

1615
phutil_require_source('PhabricatorMailImplementationAmazonSESAdapter.php');

‎src/applications/metamta/controller/send/PhabricatorMetaMTASendController.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,7 @@ public function processRequest() {
3434
$mail->setIsHTML($request->getInt('html'));
3535
$mail->save();
3636
if ($request->getInt('immediately')) {
37-
$mail->sendNow(
38-
$force_send = true,
39-
new PhabricatorMailImplementationPHPMailerLiteAdapter());
37+
$mail->sendNow($force_send = true);
4038
}
4139

4240
return id(new AphrontRedirectResponse())

‎src/applications/metamta/controller/send/__init__.php

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88

99
phutil_require_module('phabricator', 'aphront/response/redirect');
10-
phutil_require_module('phabricator', 'applications/metamta/adapter/phpmailerlite');
1110
phutil_require_module('phabricator', 'applications/metamta/controller/base');
1211
phutil_require_module('phabricator', 'applications/metamta/storage/mail');
1312
phutil_require_module('phabricator', 'view/form/base');

‎scripts/daemons/metamta/metamta_mta.php ‎src/applications/metamta/daemon/mta/PhabricatorMetaMTADaemon.php

+18-1
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,21 @@
1616
* limitations under the License.
1717
*/
1818

19-
// Placeholder so I don't forget about this, hopefully.
19+
class PhabricatorMetaMTADaemon {
20+
21+
public function run() {
22+
echo "OK. Sending mail";
23+
do {
24+
$mail = id(new PhabricatorMetaMTAMail())->loadAllWhere(
25+
'status = %s AND nextRetry <= %d LIMIT 10',
26+
PhabricatorMetaMTAMail::STATUS_QUEUE,
27+
time());
28+
foreach ($mail as $message) {
29+
$message->sendNow();
30+
echo ".";
31+
}
32+
sleep(1);
33+
} while (true);
34+
}
35+
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
/**
3+
* This file is automatically generated. Lint this module to rebuild it.
4+
* @generated
5+
*/
6+
7+
8+
9+
phutil_require_module('phabricator', 'applications/metamta/storage/mail');
10+
11+
phutil_require_module('phutil', 'utils');
12+
13+
14+
phutil_require_source('PhabricatorMetaMTADaemon.php');

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

+12-6
Original file line numberDiff line numberDiff line change
@@ -122,19 +122,25 @@ public function save() {
122122
$ret = parent::save();
123123

124124
if ($try_send) {
125-
$class_name = PhabricatorEnv::getEnvConfig('metamta.mail-adapter');
126-
PhutilSymbolLoader::loadClass($class_name);
127-
$mailer = newv($class_name, array());
128-
$this->sendNow($force_send = false, $mailer);
125+
$this->sendNow();
129126
}
130127

131128
return $ret;
132129
}
133130

131+
private function buildDefaultMailer() {
132+
$class_name = PhabricatorEnv::getEnvConfig('metamta.mail-adapter');
133+
PhutilSymbolLoader::loadClass($class_name);
134+
return newv($class_name, array());
135+
}
134136

135137
public function sendNow(
136138
$force_send = false,
137-
PhabricatorMailImplementationAdapter $mailer) {
139+
PhabricatorMailImplementationAdapter $mailer = null) {
140+
141+
if ($mailer === null) {
142+
$mailer = $this->buildDefaultMailer();
143+
}
138144

139145
if (!$force_send) {
140146
if ($this->getStatus() != self::STATUS_QUEUE) {
@@ -166,7 +172,7 @@ public function sendNow(
166172

167173
$handles = id(new PhabricatorObjectHandleData($phids))
168174
->loadHandles();
169-
175+
170176
$params = $this->parameters;
171177
$default = PhabricatorEnv::getEnvConfig('metamta.default-address');
172178
if (empty($params['from'])) {

0 commit comments

Comments
 (0)
Failed to load comments.