| 1 |
<?php |
| 2 |
|
| 3 |
namespace MailPoet\Tasks; |
| 4 |
|
| 5 |
if (!defined('ABSPATH')) exit; |
| 6 |
|
| 7 |
|
| 8 |
use MailPoet\Cron\Workers\Scheduler; |
| 9 |
use MailPoet\Models\Newsletter; |
| 10 |
use MailPoet\Models\ScheduledTask; |
| 11 |
use MailPoet\Models\SendingQueue; |
| 12 |
use MailPoet\Newsletter\Url as NewsletterUrl; |
| 13 |
use MailPoetVendor\Carbon\Carbon; |
| 14 |
|
| 15 |
class State { |
| 16 |
/** |
| 17 |
* @return array |
| 18 |
*/ |
| 19 |
public function getCountsPerStatus() { |
| 20 |
$stats = [ |
| 21 |
ScheduledTask::STATUS_COMPLETED => 0, |
| 22 |
ScheduledTask::STATUS_PAUSED => 0, |
| 23 |
ScheduledTask::STATUS_SCHEDULED => 0, |
| 24 |
ScheduledTask::VIRTUAL_STATUS_RUNNING => 0, |
| 25 |
]; |
| 26 |
$counts = ScheduledTask::rawQuery( |
| 27 |
"SELECT COUNT(*) as value, status |
| 28 |
FROM `" . ScheduledTask::$_table . "` |
| 29 |
WHERE deleted_at IS NULL AND `type` = 'sending' |
| 30 |
GROUP BY status;" |
| 31 |
)->findMany(); |
| 32 |
foreach ($counts as $count) { |
| 33 |
if ($count->status === null) { |
| 34 |
$stats[ScheduledTask::VIRTUAL_STATUS_RUNNING] = (int)$count->value; |
| 35 |
continue; |
| 36 |
} |
| 37 |
$stats[$count->status] = (int)$count->value; |
| 38 |
} |
| 39 |
return $stats; |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* @return array |
| 44 |
*/ |
| 45 |
public function getLatestTasks( |
| 46 |
$type = null, |
| 47 |
$statuses = [ |
| 48 |
ScheduledTask::STATUS_COMPLETED, |
| 49 |
ScheduledTask::STATUS_SCHEDULED, |
| 50 |
ScheduledTask::VIRTUAL_STATUS_RUNNING, |
| 51 |
], |
| 52 |
$limit = Scheduler::TASK_BATCH_SIZE) { |
| 53 |
$tasks = []; |
| 54 |
foreach ($statuses as $status) { |
| 55 |
$query = ScheduledTask::orderByDesc('created_at') |
| 56 |
->orderByAsc('id') // consistent order for tasks with equal timestamps |
| 57 |
->whereNull('deleted_at') |
| 58 |
->limit($limit); |
| 59 |
if ($type) { |
| 60 |
$query = $query->where('type', $type); |
| 61 |
} |
| 62 |
if ($status === ScheduledTask::VIRTUAL_STATUS_RUNNING) { |
| 63 |
$query = $query->whereNull('status'); |
| 64 |
} else { |
| 65 |
$query = $query->where('status', $status); |
| 66 |
} |
| 67 |
$tasks = array_merge($tasks, $query->findMany()); |
| 68 |
} |
| 69 |
|
| 70 |
return array_map(function ($task) { |
| 71 |
return $this->buildTaskData($task); |
| 72 |
}, $tasks); |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* @return array |
| 77 |
*/ |
| 78 |
private function buildTaskData(ScheduledTask $task) { |
| 79 |
$queue = $newsletter = null; |
| 80 |
if ($task->type === Sending::TASK_TYPE) { |
| 81 |
$queue = SendingQueue::where('task_id', $task->id)->findOne(); |
| 82 |
$newsletter = $queue instanceof SendingQueue ? $queue->newsletter()->findOne() : null; |
| 83 |
} |
| 84 |
return [ |
| 85 |
'id' => (int)$task->id, |
| 86 |
'type' => $task->type, |
| 87 |
'priority' => (int)$task->priority, |
| 88 |
'updated_at' => Carbon::createFromTimeString((string)$task->updatedAt)->timestamp, |
| 89 |
'scheduled_at' => $task->scheduledAt ? Carbon::createFromTimeString($task->scheduledAt)->timestamp : null, |
| 90 |
'status' => $task->status, |
| 91 |
'newsletter' => (($queue instanceof SendingQueue) && ($newsletter instanceof Newsletter)) ? [ |
| 92 |
'newsletter_id' => (int)$queue->newsletterId, |
| 93 |
'queue_id' => (int)$queue->id, |
| 94 |
'subject' => $queue->newsletterRenderedSubject ?: $newsletter->subject, |
| 95 |
'preview_url' => NewsletterUrl::getViewInBrowserUrl( |
| 96 |
$newsletter, |
| 97 |
null, |
| 98 |
$queue |
| 99 |
), |
| 100 |
] : [ |
| 101 |
'newsletter_id' => null, |
| 102 |
'queue_id' => null, |
| 103 |
'subject' => null, |
| 104 |
'preview_url' => null, |
| 105 |
], |
| 106 |
]; |
| 107 |
} |
| 108 |
} |
| 109 |
|