Skip to content

optional setting number of mails to send in command param #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions MailQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,18 @@ public function init()
/**
* Sends out the messages in email queue and update the database.
*
* @param int $qty the number of mails to be sent
* @return boolean true if all messages are successfully sent out
*/
public function process()
public function process($qty = null)
{
if (Yii::$app->db->getTableSchema($this->table) == null) {
throw new \yii\base\InvalidConfigException('"' . $this->table . '" not found in database. Make sure the db migration is properly done and the table is created.');
}

$success = true;

$items = Queue::find()->where(['and', ['sent_time' => NULL], ['<', 'attempts', $this->maxAttempts], ['<=', 'time_to_send', date('Y-m-d H:i:s')]])->orderBy(['created_at' => SORT_ASC])->limit($this->mailsPerRound);
$items = Queue::find()->where(['and', ['sent_time' => NULL], ['<', 'attempts', $this->maxAttempts], ['<=', 'time_to_send', date('Y-m-d H:i:s')]])->orderBy(['created_at' => SORT_ASC])->limit($qty ?: $this->mailsPerRound);
foreach ($items->each() as $item) {
if ($message = $item->toMessage()) {
$attributes = ['attempts', 'last_attempt_time'];
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ In one of your controller actions:

```php

public function actionSend()
public function actionSend($qty = null)
{
Yii::$app->mailqueue->process();
Yii::$app->mailqueue->process($qty);
}

```
Expand All @@ -98,7 +98,7 @@ Set a CRON job to run console command:

```

*/10 * * * * php /var/www/html/myapp/yii mailqueue/process
*/10 * * * * php /var/www/html/myapp/yii mailqueue/process 25

```

Expand Down
4 changes: 2 additions & 2 deletions commands/MailQueueController.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ class MailQueueController extends Controller
/**
* This command processes the mail queue
*/
public function actionProcess()
public function actionProcess($qty = null)
{
\Yii::$app->mailqueue->process();
\Yii::$app->mailqueue->process($qty);
}
}