diff --git a/MailQueue.php b/MailQueue.php index 5ae1b84..a41b0d8 100644 --- a/MailQueue.php +++ b/MailQueue.php @@ -91,7 +91,7 @@ public function process() $success = true; - $items = Queue::find()->where(['and', ['sent_time' => NULL], ['<', 'attempts', $this->maxAttempts]])->orderBy(['created_at' => SORT_ASC])->limit($this->mailsPerRound)->all(); + $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)->all(); // dd($items); if(!empty($items)) { foreach($items as $item) { @@ -115,4 +115,4 @@ public function process() return $success; } -} \ No newline at end of file +} diff --git a/Message.php b/Message.php index 35c510c..8368a2c 100644 --- a/Message.php +++ b/Message.php @@ -19,10 +19,11 @@ class Message extends \yii\swiftmailer\Message { /** * Enqueue the message storing it in database. - * + * + * @param timestamp $time_to_send * @return boolean true on success, false otherwise */ - public function queue() + public function queue($time_to_send = time()) { $item = new Queue(); @@ -34,6 +35,7 @@ public function queue() $item->charset = $this->getCharset(); $item->subject = $this->getSubject(); $item->attempts = 0; + $item->time_to_send = date('Y-m-d H:i:s', $time_to_send); $parts = $this->getSwiftMessage()->getChildren(); // if message has no parts, use message @@ -61,4 +63,4 @@ public function queue() return $item->save(); } -} \ No newline at end of file +} diff --git a/migrations/m160118_081152_add_timetosend_column.php b/migrations/m160118_081152_add_timetosend_column.php new file mode 100644 index 0000000..a562cb6 --- /dev/null +++ b/migrations/m160118_081152_add_timetosend_column.php @@ -0,0 +1,18 @@ +addColumn('mail_queue', 'time_to_send', $this->dateTime()->notNull()); + $this->createIndex('IX_time_to_send', 'mail_queue', 'time_to_send'); + } + + public function down() + { + $this->dropIndex('IX_time_to_send', 'mail_queue'); + $this->dropColumn('mail_queue', 'time_to_send'); + } +} diff --git a/models/Queue.php b/models/Queue.php index 11aaae3..6537093 100644 --- a/models/Queue.php +++ b/models/Queue.php @@ -23,6 +23,7 @@ * @property integer $attempts * @property integer $last_attempt_time * @property integer $sent_time + * @property string $time_to_send */ class Queue extends ActiveRecord { @@ -58,6 +59,7 @@ public function rules() { return [ [['created_at', 'attempts', 'last_attempt_time', 'sent_time'], 'integer'], + [['time_to_send'], 'required'], [['to', 'cc', 'bcc', 'subject', 'html_body', 'text_body', 'charset'], 'safe'], ]; } @@ -106,4 +108,4 @@ public function toMessage() return null; } -} \ No newline at end of file +}