-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathQueueService.php
More file actions
193 lines (146 loc) · 4.47 KB
/
Copy pathQueueService.php
File metadata and controls
193 lines (146 loc) · 4.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<?php
namespace ADT\MailQueue\Service;
use ADT\MailQueue\Entity;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Console\Output\OutputInterface;
/**
* @method onQueueDrained(OutputInterface|NULL $output)
*/
class QueueService {
use \Nette\SmartObject;
const MUTEX_TIME_FORMAT = DATE_W3C;
/** @var string */
protected $queueEntryClass;
/** @var \Kdyby\Doctrine\EntityManager */
protected $em;
/** @var string */
protected $mutexFile;
/** @var string */
protected $mutexTimeFile;
/** @var \Nette\Mail\IMailer */
protected $mailer;
/** @var IMessenger */
protected $messenger;
/** @var \Tracy\Logger */
protected $logger;
/** @var NULL|callable */
protected $sendErrorHandler;
/** @var callable[] */
public $onQueueDrained = [];
/** @var int */
protected $lockTimeout;
/** @var int */
protected $limit;
/** @var \ADT\BackgroundQueue\BackgroundQueue */
protected $backgroundQueueService;
/** @var string */
protected $backgroundQueueCallbackName;
public function __construct($config, EntityManagerInterface $em) {
if (! is_dir($config['tempDir'])) {
mkdir($config['tempDir']);
}
$this->mutexFile = 'nette.safe://' . $config['tempDir'] . '/adt-mail-queue.lock';
$this->mutexTimeFile = 'nette.safe://' . $config['tempDir'] . '/adt-mail-queue.lock.timestamp';
$this->lockTimeout = $config['lockTimeout'];
$this->limit = $config['limit'];
$this->queueEntryClass = $config['queueEntityClass'];
$this->em = $em;
$this->logger = \Tracy\Debugger::getLogger();
$this->backgroundQueueService = $config['backgroundQueueService'];
$this->backgroundQueueCallbackName = $config['backgroundQueueCallbackName'];
}
public function setMailer(\Nette\Mail\IMailer $mailer) {
$this->mailer = $mailer;
return $this;
}
public function setMessenger(IMessenger $messenger) {
$this->messenger = $messenger;
return $this;
}
public function setSendErrorHandler(callable $handler) {
$this->sendErrorHandler = $handler;
return $this;
}
/**
* @param \Nette\Mail\Message $message
* @param array|callable $custom
* @return Entity\AbstractMailQueueEntry
*/
protected function createQueueEntry(\Nette\Mail\Message $message, $custom = []) {
/** @var Entity\AbstractMailQueueEntry $entry */
$entry = new $this->queueEntryClass;
$entry->setCreatedAt(new \DateTime);
$entry->setFrom(array_keys($message->getFrom())[0]);
$entry->setSubject($message->getSubject());
$entry->setMessage($message);
if (is_callable($custom)) {
$custom($entry);
} else {
foreach ($custom as $field => $value) {
$setter = 'set' . ucfirst($field);
$entry->$setter($value);
}
}
return $entry;
}
/**
* @param \Nette\Mail\Message $message
* @param array|callable $custom
* @return Entity\AbstractMailQueueEntry
*/
public function enqueue(\Nette\Mail\Message $message, $custom = [], ?int $backgroundQueuePriority = null) {
$entry = $this->createQueueEntry($message, $custom);
$this->em->persist($entry);
$this->em->flush($entry);
$this->backgroundQueueService->publish($this->backgroundQueueCallbackName, [$entry->getId()], null, null, false, null, $backgroundQueuePriority);
return $entry;
}
protected function send(Entity\AbstractMailQueueEntry $entry) {
if ($this->mailer) {
$this->mailer->send($entry->getMessage());
} else {
$this->messenger->send($entry);
}
}
/**
* @param int $entryId
* @return bool
*
* new_rabbit
*/
public function process(int $entryId) {
$entry = $this->em->find($this->queueEntryClass, $entryId);
try {
$this->send($entry);
$entry->setSentAt(new \DateTime);
} catch (\Exception $e) {
$msg = $e->getMessage();
if ($e->getPrevious()) {
$msg .= " (" . $e->getPrevious()->getMessage() . ")";
}
if ($this->sendErrorHandler) {
$sendException = $e instanceof \Nette\Mail\SendException
? $e
: new \Nette\Mail\SendException($msg, $e->getCode(), $e);
$errorHandlerResponse = call_user_func($this->sendErrorHandler, $entry, $sendException);
if (is_string($errorHandlerResponse)) {
// error handled but should be logged
$msg .= '; ' . $errorHandlerResponse;
} else if ($errorHandlerResponse === NULL) {
// error not handled
} else {
// error handled gracefully
$msg = NULL;
}
}
if ($msg !== NULL) {
// mail report
$error = 'id=' . $entry->getId() . ': ' . $msg;
$this->logger->log($error, \Tracy\Logger::ERROR);
}
}
$this->em->persist($entry);
$this->em->flush($entry);
return TRUE;
}
}