-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcessNotifications.php
More file actions
139 lines (117 loc) · 3.6 KB
/
ProcessNotifications.php
File metadata and controls
139 lines (117 loc) · 3.6 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
<?php
namespace Concrete\Package\QueueDemo\Src\Console\Command;
use Concrete\Core\Application\Application;
use Concrete\Core\Foundation\Queue\Queue;
use Symfony\Component\Console\Application as ConsoleApplication;
use Concrete\Core\User\UserInfoRepository;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Concrete\Core\Support\Facade\Facade;
use Core;
class ProcessNotifications extends Command
{
/**
* @var resource
*/
protected $fd;
/**
* @var InputInterface
*/
protected $input;
/**
* @var OutputInterface
*/
protected $output;
/**
* @var Application
*/
protected $app;
/**
* @var ConsoleApplication
*/
protected $console;
protected function configure()
{
$this
->setName('queue-demo:process-notifications')
->setDescription('Sends notifications')
->setHelp('Run the command and get notifications');
}
/**
* Locks the process to ensure that we don't run the same process twice
*
* @return bool
*/
protected function lock()
{
$result = false;
$this->fd = @fopen(__FILE__, 'r');
if ($this->fd) {
if (@flock($this->fd, LOCK_EX | LOCK_NB)) {
$result = true;
} else {
@fclose($this->fd);
$this->fd = null;
}
}
return $result;
}
/**
* Unlocks the process
*/
protected function unlock()
{
if ($this->fd) {
@flock($this->fd, LOCK_UN);
@fclose($this->fd);
$this->fd = null;
}
}
protected function sendNotifications()
{
$queue = Queue::get('queue-demo');
/** @var UserInfoRepository $userInfo */
$userInfo = $this->app->make(UserInfoRepository::class);
/** @var \Concrete\Core\Mail\Service $mailService */
$mailService = $this->app->make('mail');
$count = 0;
// get 10 messages from the queue and process them
for (; ;) {
$queueMessages = $queue->receive(10);
if ($queueMessages->count() < 1) {
break;
}
foreach ($queueMessages as $msg) {
$userId = $msg->body;
$userInfo = $userInfo->getByID($userId);
$userMail = $userInfo->getUserEmail();
// send mail
$mailService->reset();
$mailService->to($userMail);
$mailService->setBody(t('Hi there'));
if ($mailService->sendMail()) {
$count++;
} else {
$this->output->writeln(t('<error>Mail could not be sent to %s</error>', $userMail));
}
// delete message from queue
$queue->deleteMessage($msg);
}
}
$this->output->writeln(t2('<info>%d Notification sent</info>', '<info>%d Notifications sent</info>', $count, $count));
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->input = $input;
$this->output = $output;
$this->app = Facade::getFacadeApplication();
$this->console = $this->getApplication();
if (!$this->lock()) {
$this->output->writeln(t('<error>Failed to create lock, please make sure the process is not already running.</error>'));
} else {
$this->sendNotifications();
$this->unlock();
}
}
}