From d3a0d942af3c6512cbefeb87b5b5577225b6ab83 Mon Sep 17 00:00:00 2001 From: Bert Brunekreeft Date: Mon, 18 Jan 2016 09:36:28 +0100 Subject: [PATCH 1/5] Create m160118_081152_add_timetosend_column.php --- .../m160118_081152_add_timetosend_column.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 migrations/m160118_081152_add_timetosend_column.php 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'); + } +} From 62b082b40ff2428a41de77c047de4c4320222fd9 Mon Sep 17 00:00:00 2001 From: Bert Brunekreeft Date: Mon, 18 Jan 2016 09:39:25 +0100 Subject: [PATCH 2/5] Update Queue.php --- models/Queue.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) 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 +} From 4afd88ab4fdc584cf7397abc890c94ce1978d605 Mon Sep 17 00:00:00 2001 From: Bert Brunekreeft Date: Mon, 18 Jan 2016 09:41:33 +0100 Subject: [PATCH 3/5] Update Message.php --- Message.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Message.php b/Message.php index 35c510c..4b2ace8 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 = $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 +} From d1452ef70bdd65a6d8e204f5dc488ff4000d81af Mon Sep 17 00:00:00 2001 From: Bert Brunekreeft Date: Mon, 18 Jan 2016 09:49:10 +0100 Subject: [PATCH 4/5] Update Message.php --- Message.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Message.php b/Message.php index 4b2ace8..8368a2c 100644 --- a/Message.php +++ b/Message.php @@ -35,7 +35,7 @@ public function queue($time_to_send = time()) $item->charset = $this->getCharset(); $item->subject = $this->getSubject(); $item->attempts = 0; - $item->time_to_send = $time_to_send; + $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 From 5077f79f34974e90d193c704ed4998dc3ca3eb02 Mon Sep 17 00:00:00 2001 From: Bert Brunekreeft Date: Mon, 18 Jan 2016 09:49:57 +0100 Subject: [PATCH 5/5] Update MailQueue.php --- MailQueue.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 +}