Improve readability of scheduler code

[MAILPOET-6142]
This commit is contained in:
Rostislav Wolny
2024-08-13 14:36:05 +02:00
committed by Aschepikov
parent 07221d9c68
commit bb7ab59ed5
4 changed files with 30 additions and 21 deletions

View File

@@ -34,13 +34,13 @@ class Scheduler {
public function getPreviousRunDate($schedule) {
// User enters time in WordPress site timezone, but we need to calculate it in UTC before we save it to DB
// 1) As the initial time we use time in site timezone (gmt is false)
// 1) As the initial time we use time in site timezone via current_datetime
// 2) We use CronExpression to calculate previous run (still in site's timezone)
// 3) We convert the calculated time to UTC
$fromTimestamp = $this->wp->currentTime('timestamp', false);
$from = $this->wp->currentDatetime();
try {
$schedule = \Cron\CronExpression::factory($schedule);
$previousRunDate = $schedule->getPreviousRunDate(Carbon::createFromTimestamp($fromTimestamp, $this->wp->wpTimezone()));
$previousRunDate = $schedule->getPreviousRunDate(Carbon::instance($from));
$previousRunDate->setTimezone(new \DateTimeZone('UTC'));
$previousRunDate = $previousRunDate->format('Y-m-d H:i:s');
} catch (\Exception $e) {
@@ -88,13 +88,14 @@ class Scheduler {
*/
public function getNextRunDateTime($schedule) {
// User enters time in WordPress site timezone, but we need to calculate it in UTC before we save it to DB
// 1) As the initial time we use time in site timezone (gmt is false)
// 1) As the initial time we use time in site timezone via current_datetime
// 2) We use CronExpression to calculate next run (still in site's timezone)
// 3) We convert the calculated time to UTC
$fromTimestamp = $this->wp->currentTime('timestamp', false);
//$fromTimestamp = $this->wp->currentTime('timestamp', false);
$from = $this->wp->currentDatetime();
try {
$schedule = \Cron\CronExpression::factory($schedule);
$nextRunDate = $schedule->getNextRunDate(Carbon::createFromTimestamp($fromTimestamp, $this->wp->wpTimezone()));
$nextRunDate = $schedule->getNextRunDate(Carbon::instance($from));
$nextRunDate->setTimezone(new \DateTimeZone('UTC'));
} catch (\Exception $e) {
$nextRunDate = false;

View File

@@ -140,6 +140,10 @@ class Functions {
return current_time($type, $gmt);
}
public function currentDatetime() {
return current_datetime();
}
public function wpTimezone() {
return wp_timezone();
}

View File

@@ -188,7 +188,7 @@ class PostNotificationTest extends \MailPoetTest {
$scheduleOption = $newsletter->getOption(NewsletterOptionFieldEntity::NAME_SCHEDULE);
$this->assertInstanceOf(NewsletterOptionEntity::class, $scheduleOption);
$scheduler = $this->getSchedulerWithMockedTime(1483275600); // Sunday, 1 January 2017 @ 1:00pm (UTC)
$scheduler = $this->getSchedulerWithMockedTime('2017-01-01 13:00+00:00'); // Sunday, 1 January 2017 @ 1:00pm (UTC)
verify($scheduler->getNextRunDate($scheduleOption->getValue()))
->equals('2017-01-01 14:00:00');
@@ -228,7 +228,7 @@ class PostNotificationTest extends \MailPoetTest {
$scheduleOption = $newsletter->getOption(NewsletterOptionFieldEntity::NAME_SCHEDULE);
$this->assertInstanceOf(NewsletterOptionEntity::class, $scheduleOption);
$scheduler = $this->getSchedulerWithMockedTime(1483275600); // Sunday, 1 January 2017 @ 1:00pm (UTC)
$scheduler = $this->getSchedulerWithMockedTime('2017-01-01 13:00+00:00'); // Sunday, 1 January 2017 @ 1:00pm (UTC)
verify($scheduler->getNextRunDate($scheduleOption->getValue()))
->equals('2017-01-03 14:00:00');
@@ -268,7 +268,7 @@ class PostNotificationTest extends \MailPoetTest {
$this->postNotificationScheduler->processPostNotificationSchedule($newsletter);
$scheduleOption = $newsletter->getOption(NewsletterOptionFieldEntity::NAME_SCHEDULE);
$this->assertInstanceOf(NewsletterOptionEntity::class, $scheduleOption);
$scheduler = $this->getSchedulerWithMockedTime(1483275600); // Sunday, 1 January 2017 @ 1:00pm (UTC)
$scheduler = $this->getSchedulerWithMockedTime('2017-01-01 13:00+00:00'); // Sunday, 1 January 2017 @ 1:00pm (UTC)
verify($scheduler->getNextRunDate($scheduleOption->getValue()))
->equals('2017-01-19 14:00:00');
@@ -307,7 +307,7 @@ class PostNotificationTest extends \MailPoetTest {
$this->postNotificationScheduler->processPostNotificationSchedule($newsletter);
$scheduleOption = $newsletter->getOption(NewsletterOptionFieldEntity::NAME_SCHEDULE);
$this->assertInstanceOf(NewsletterOptionEntity::class, $scheduleOption);
$scheduler = $this->getSchedulerWithMockedTime(1485694800);// Sunday, 29 January 2017 @ 1:00pm (UTC)
$scheduler = $this->getSchedulerWithMockedTime('2017-01-29 13:00+00:00');// Sunday, 29 January 2017 @ 1:00pm (UTC)
verify($scheduler->getNextRunDate($scheduleOption->getValue()))
->equals('2017-02-25 14:00:00');
@@ -346,7 +346,7 @@ class PostNotificationTest extends \MailPoetTest {
$this->postNotificationScheduler->processPostNotificationSchedule($newsletter);
$scheduleOption = $newsletter->getOption(NewsletterOptionFieldEntity::NAME_SCHEDULE);
$this->assertInstanceOf(NewsletterOptionEntity::class, $scheduleOption);
$scheduler = $this->getSchedulerWithMockedTime(1483275600);
$scheduler = $this->getSchedulerWithMockedTime('2017-01-01 13:00+00:00'); // Sunday, 1 January 2017 @ 1:00pm (UTC)
verify($scheduler->getNextRunDate($scheduleOption->getValue()))
->equals('2017-01-01 13:01:00');
}
@@ -445,9 +445,9 @@ class PostNotificationTest extends \MailPoetTest {
return $newsletterPost;
}
private function getSchedulerWithMockedTime(int $timestamp): Scheduler {
private function getSchedulerWithMockedTime(string $datetimeString): Scheduler {
$wpMock = $this->createMock(WPFunctions::class);
$wpMock->method('currentTime')->willReturn($timestamp);
$wpMock->method('currentDatetime')->willReturn(new \DateTimeImmutable($datetimeString));
return $this->getServiceWithOverrides(Scheduler::class, [
'wp' => $wpMock,
]);

View File

@@ -52,8 +52,9 @@ class SchedulerTest extends \MailPoetTest {
public function testItCanGetCorrectNextRunDateInGMTTimezone() {
$currentTime = '2024-08-09 09:00:00';
$wpMock = $this->createMock(WPFunctions::class);
$wpMock->method('currentTime')->willReturn(strtotime($currentTime));
$wpMock->method('wpTimezone')->willReturn(new \DateTimeZone('UTC'));
$wpMock->method('currentDatetime')->willReturn(
new \DateTimeImmutable($currentTime, new \DateTimeZone('UTC')),
);
$scheduler = $this->getServiceWithOverrides(Scheduler::class, ['wp' => $wpMock]);
verify($scheduler->getNextRunDate('0 10 * * *'))
->equals('2024-08-09 10:00:00');
@@ -62,8 +63,9 @@ class SchedulerTest extends \MailPoetTest {
public function testItCanGetCorrectNextRunDateInNewYorkTimezone() {
$currentTime = '2024-08-09 09:00:00';
$wpMock = $this->createMock(WPFunctions::class);
$wpMock->method('currentTime')->willReturn(strtotime($currentTime));
$wpMock->method('wpTimezone')->willReturn(new \DateTimeZone('America/New_York'));
$wpMock->method('currentDatetime')->willReturn(
new \DateTimeImmutable($currentTime, new \DateTimeZone('America/New_York'))
);
$scheduler = $this->getServiceWithOverrides(Scheduler::class, ['wp' => $wpMock]);
// Cron was set by user in NewYork timezone (0 10 * * *) in UTC it would be (0 14 * * *)
verify($scheduler->getNextRunDate('0 10 * * *'))
@@ -83,8 +85,9 @@ class SchedulerTest extends \MailPoetTest {
public function testItCanGetCorrectPreviousRunDateInGMTTimezone() {
$currentTime = '2024-08-09 09:00:00';
$wpMock = $this->createMock(WPFunctions::class);
$wpMock->method('currentTime')->willReturn(strtotime($currentTime));
$wpMock->method('wpTimezone')->willReturn(new \DateTimeZone('UTC'));
$wpMock->method('currentDatetime')->willReturn(
new \DateTimeImmutable($currentTime, new \DateTimeZone('UTC'))
);
$scheduler = $this->getServiceWithOverrides(Scheduler::class, ['wp' => $wpMock]);
verify($scheduler->getPreviousRunDate('0 10 * * *'))
->equals('2024-08-08 10:00:00');
@@ -93,8 +96,9 @@ class SchedulerTest extends \MailPoetTest {
public function testItCanGetCorrectPreviousRunDateInNewYorkTimezone() {
$currentTime = '2024-08-09 09:00:00';
$wpMock = $this->createMock(WPFunctions::class);
$wpMock->method('currentTime')->willReturn(strtotime($currentTime));
$wpMock->method('wpTimezone')->willReturn(new \DateTimeZone('America/New_York'));
$wpMock->method('currentDatetime')->willReturn(
new \DateTimeImmutable($currentTime, new \DateTimeZone('America/New_York'))
);
$scheduler = $this->getServiceWithOverrides(Scheduler::class, ['wp' => $wpMock]);
// Cron was set by user in NewYork timezone (0 10 * * *) in UTC it would be (0 14 * * *)
verify($scheduler->getPreviousRunDate('0 10 * * *'))