This repository was archived by the owner on Jan 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMailsController.php
64 lines (56 loc) · 2.03 KB
/
MailsController.php
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
<?php
namespace presentator\api\commands;
use Yii;
use yii\console\Controller;
use yii\helpers\Console;
use presentator\api\models\UserScreenCommentRel;
/**
* Manages bulk email sending commands.
*
* @author Gani Georgiev <[email protected]>
*/
class MailsController extends Controller
{
/**
* {@inheritdoc}
*/
public $color = true;
/**
* Processes unread screen comments and sends an email to the related users.
*
* Example usage:
* ```bash
* php yii mails/process-comments [sleepBatch] [sleepDuration] [createdThreshold]
* ```
*
* @param integer [$sleepBatch] The number of emails to process before sleep to occur (default to 5).
* @param integer [$sleepDuration] Sleep interval duration in seconds (default to 2).
* @param integer [$createdThreshold] Process only comments that are created before the provided interval in seconds (default to 900 /15 minutes/).
* @return integer
*/
public function actionProcessComments(int $sleepBatch = 5, int $sleepDuration = 2, int $createdThreshold = 900)
{
$relsQuery = UserScreenCommentRel::findProcessableQuery(date('Y-m-d H:i:s', strtotime('- ' . $createdThreshold . ' seconds')));
$processed = 0;
$this->stdout('Processing unread screen comments...', Console::FG_YELLOW);
$this->stdout(PHP_EOL);
foreach ($relsQuery->each() as $i => $rel) {
try {
if (
$rel->user->sendUnreadCommentEmail($rel->screenComment) &&
$rel->markAsProcessed()
) {
$processed++;
}
if (($i + 1) % $sleepBatch == 0) {
sleep($sleepDuration);
}
} catch (\Exception | \Throwable $e) {
Yii::error($e->getMessage());
}
}
$this->stdout('Processed: ' . $processed, Console::BG_GREEN, Console::FG_BLACK);
$this->stdout(PHP_EOL);
return self::EXIT_CODE_NORMAL;
}
}