diff --git a/MailQueue.php b/MailQueue.php index 4069fe3..ae89eb8 100644 --- a/MailQueue.php +++ b/MailQueue.php @@ -81,9 +81,10 @@ 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.'); @@ -91,7 +92,7 @@ public function process() $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']; diff --git a/README.md b/README.md index e74aa6a..f1524fa 100644 --- a/README.md +++ b/README.md @@ -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); } ``` @@ -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 ``` diff --git a/commands/MailQueueController.php b/commands/MailQueueController.php index 93c8681..329b6d5 100644 --- a/commands/MailQueueController.php +++ b/commands/MailQueueController.php @@ -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); } }