Change cron scheduling for SubscribersEngagementScore

[MAILPOET-3585]
This commit is contained in:
Jan Lysý
2021-04-29 11:16:23 +02:00
committed by Veljko V
parent 43058ce38a
commit cfa29c9df1
2 changed files with 30 additions and 6 deletions

View File

@ -28,21 +28,31 @@ class SubscribersEngagementScore extends SimpleWorker {
} }
public function processTaskStrategy(ScheduledTask $task, $timer) { public function processTaskStrategy(ScheduledTask $task, $timer) {
$subscribers = $this->subscribersRepository->findBy( $subscribers = $this->subscribersRepository->findByUpdatedScoreNotInLastMonth(SubscribersEngagementScore::BATCH_SIZE);
['engagementScoreUpdatedAt' => null],
[],
SubscribersEngagementScore::BATCH_SIZE
);
foreach ($subscribers as $subscriber) { foreach ($subscribers as $subscriber) {
$this->statisticsOpensRepository->recalculateSubscriberScore($subscriber); $this->statisticsOpensRepository->recalculateSubscriberScore($subscriber);
} }
if ($subscribers) { if ($subscribers) {
$this->scheduleImmediately();
} else {
$this->schedule(); $this->schedule();
} }
return true; return true;
} }
public function getNextRunDate() { public function scheduleImmediately(): void {
$this->cronWorkerScheduler->schedule(static::TASK_TYPE, $this->getNextRunDateImmediately());
}
public function getNextRunDateImmediately(): Carbon {
return Carbon::createFromTimestamp($this->wp->currentTime('timestamp')); return Carbon::createFromTimestamp($this->wp->currentTime('timestamp'));
} }
public function getNextRunDate() {
// random day of the next week
$date = Carbon::createFromTimestamp($this->wp->currentTime('timestamp'));
$date->addDay();
$date->setTime(mt_rand(0, 23), mt_rand(0, 59));
return $date;
}
} }

View File

@ -8,6 +8,7 @@ use MailPoet\Entities\SubscriberCustomFieldEntity;
use MailPoet\Entities\SubscriberEntity; use MailPoet\Entities\SubscriberEntity;
use MailPoet\Entities\SubscriberSegmentEntity; use MailPoet\Entities\SubscriberSegmentEntity;
use MailPoet\WP\Functions as WPFunctions; use MailPoet\WP\Functions as WPFunctions;
use MailPoetVendor\Carbon\Carbon;
use MailPoetVendor\Doctrine\DBAL\Connection; use MailPoetVendor\Doctrine\DBAL\Connection;
use MailPoetVendor\Doctrine\ORM\EntityManager; use MailPoetVendor\Doctrine\ORM\EntityManager;
use MailPoetVendor\Doctrine\ORM\Query\Expr\Join; use MailPoetVendor\Doctrine\ORM\Query\Expr\Join;
@ -292,4 +293,17 @@ class SubscribersRepository extends Repository {
} }
return $this->findOneBy(['wpUserId' => $wpUser->ID]); return $this->findOneBy(['wpUserId' => $wpUser->ID]);
} }
public function findByUpdatedScoreNotInLastMonth(int $limit): array {
$dateTime = (new Carbon())->subMonths(1);
return $this->entityManager->createQueryBuilder()
->select('s')
->from(SubscriberEntity::class, 's')
->where('s.engagementScoreUpdatedAt IS NULL')
->orWhere('s.engagementScoreUpdatedAt < :dateTime')
->setParameter('dateTime', $dateTime)
->getQuery()
->setMaxResults($limit)
->getResult();
}
} }