diff --git a/lib/API/JSON/v1/Newsletters.php b/lib/API/JSON/v1/Newsletters.php index c0173d7811..816ca78a2d 100644 --- a/lib/API/JSON/v1/Newsletters.php +++ b/lib/API/JSON/v1/Newsletters.php @@ -166,12 +166,15 @@ class Newsletters extends APIEndpoint { // if there are past due notifications, reschedule them for the next send date if($newsletter->type === Newsletter::TYPE_NOTIFICATION && $status === Newsletter::STATUS_ACTIVE) { $next_run_date = Scheduler::getNextRunDate($newsletter->schedule); - $newsletter->queue()->findOne()->task() - ->whereLte('scheduled_at', Carbon::createFromTimestamp(current_time('timestamp'))) - ->where('status', SendingQueue::STATUS_SCHEDULED) - ->findResultSet() - ->set('scheduled_at', $next_run_date) - ->save(); + $queue = $newsletter->queue()->findOne(); + if($queue) { + $queue->task() + ->whereLte('scheduled_at', Carbon::createFromTimestamp(current_time('timestamp'))) + ->where('status', SendingQueue::STATUS_SCHEDULED) + ->findResultSet() + ->set('scheduled_at', $next_run_date) + ->save(); + } } return $this->successResponse( diff --git a/lib/API/JSON/v1/SendingQueue.php b/lib/API/JSON/v1/SendingQueue.php index 4610fd70b7..5321b4fd70 100644 --- a/lib/API/JSON/v1/SendingQueue.php +++ b/lib/API/JSON/v1/SendingQueue.php @@ -47,6 +47,7 @@ class SendingQueue extends APIEndpoint { // add newsletter to the sending queue $queue = SendingQueueModel::joinWithTasks() + ->where('queues.newsletter_id', $newsletter->id) ->whereNull('tasks.status') ->findOne(); @@ -84,6 +85,7 @@ class SendingQueue extends APIEndpoint { APIError::UNKNOWN => __('There are no subscribers in that list!', 'mailpoet') )); } + $queue->updateCount(); $queue->status = null; $queue->scheduled_at = null; diff --git a/lib/Cron/Workers/Scheduler.php b/lib/Cron/Workers/Scheduler.php index 91c24e35cd..4334c85a2c 100644 --- a/lib/Cron/Workers/Scheduler.php +++ b/lib/Cron/Workers/Scheduler.php @@ -102,6 +102,7 @@ class Scheduler { $finder = new SubscribersFinder(); $subscribers_count = $finder->addSubscribersToTaskFromSegments($queue->task(), $segments); // update current queue + $queue->updateCount(); $queue->status = null; $queue->save(); // update newsletter status diff --git a/tests/_data/acceptanceDump.sql b/tests/_data/acceptanceDump.sql index 225559757f..b2cd77f7c5 100644 --- a/tests/_data/acceptanceDump.sql +++ b/tests/_data/acceptanceDump.sql @@ -315,6 +315,9 @@ CREATE TABLE `mp_mailpoet_scheduled_tasks` ( PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci; +INSERT INTO `mp_mailpoet_scheduled_tasks` (`id`, `type`, `status`, `created_at`, `updated_at`) VALUES +(1, 'migration', 'completed', '2017-03-02 11:20:00', '2017-03-02 16:21:00'); + DROP TABLE IF EXISTS `mp_mailpoet_scheduled_task_subscribers`; CREATE TABLE `mp_mailpoet_scheduled_task_subscribers` ( @@ -346,18 +349,14 @@ INSERT INTO `mp_mailpoet_segments` (`id`, `name`, `type`, `description`, `create DROP TABLE IF EXISTS `mp_mailpoet_sending_queues`; CREATE TABLE `mp_mailpoet_sending_queues` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, - `type` varchar(90) COLLATE utf8mb4_unicode_520_ci DEFAULT NULL, + `task_id` int(11) unsigned NOT NULL, `newsletter_id` int(11) unsigned NOT NULL, `newsletter_rendered_body` longtext COLLATE utf8mb4_unicode_520_ci, `newsletter_rendered_subject` varchar(250) COLLATE utf8mb4_unicode_520_ci DEFAULT NULL, `subscribers` longtext COLLATE utf8mb4_unicode_520_ci, - `status` varchar(12) COLLATE utf8mb4_unicode_520_ci DEFAULT NULL, - `priority` mediumint(9) NOT NULL DEFAULT '0', `count_total` int(11) unsigned NOT NULL DEFAULT '0', `count_processed` int(11) unsigned NOT NULL DEFAULT '0', `count_to_process` int(11) unsigned NOT NULL DEFAULT '0', - `scheduled_at` timestamp NULL DEFAULT NULL, - `processed_at` timestamp NULL DEFAULT NULL, `created_at` timestamp NULL DEFAULT NULL, `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, `deleted_at` timestamp NULL DEFAULT NULL, diff --git a/tests/acceptance/ManageSubscriptionLinkCest.php b/tests/acceptance/ManageSubscriptionLinkCest.php index 0a6d7fe846..6dab8666b9 100644 --- a/tests/acceptance/ManageSubscriptionLinkCest.php +++ b/tests/acceptance/ManageSubscriptionLinkCest.php @@ -40,7 +40,7 @@ class ManageSubscriptionLinkCest { $I->fillField($search_field_element, 'WordPress Users'); $I->pressKey($search_field_element, \WebDriverKeys::ENTER); $I->click('Send'); - $I->waitForText('Sent to 1 of 1', 20); + $I->waitForText('Sent to 1 of 1', 60); } function manageSubscriptionLink(\AcceptanceTester $I) {