Improve readability of scheduler code
[MAILPOET-6142]
This commit is contained in:
committed by
Aschepikov
parent
07221d9c68
commit
bb7ab59ed5
@@ -34,13 +34,13 @@ class Scheduler {
|
|||||||
|
|
||||||
public function getPreviousRunDate($schedule) {
|
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
|
// 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)
|
// 2) We use CronExpression to calculate previous run (still in site's timezone)
|
||||||
// 3) We convert the calculated time to UTC
|
// 3) We convert the calculated time to UTC
|
||||||
$fromTimestamp = $this->wp->currentTime('timestamp', false);
|
$from = $this->wp->currentDatetime();
|
||||||
try {
|
try {
|
||||||
$schedule = \Cron\CronExpression::factory($schedule);
|
$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->setTimezone(new \DateTimeZone('UTC'));
|
||||||
$previousRunDate = $previousRunDate->format('Y-m-d H:i:s');
|
$previousRunDate = $previousRunDate->format('Y-m-d H:i:s');
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
@@ -88,13 +88,14 @@ class Scheduler {
|
|||||||
*/
|
*/
|
||||||
public function getNextRunDateTime($schedule) {
|
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
|
// 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)
|
// 2) We use CronExpression to calculate next run (still in site's timezone)
|
||||||
// 3) We convert the calculated time to UTC
|
// 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 {
|
try {
|
||||||
$schedule = \Cron\CronExpression::factory($schedule);
|
$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'));
|
$nextRunDate->setTimezone(new \DateTimeZone('UTC'));
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
$nextRunDate = false;
|
$nextRunDate = false;
|
||||||
|
@@ -140,6 +140,10 @@ class Functions {
|
|||||||
return current_time($type, $gmt);
|
return current_time($type, $gmt);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function currentDatetime() {
|
||||||
|
return current_datetime();
|
||||||
|
}
|
||||||
|
|
||||||
public function wpTimezone() {
|
public function wpTimezone() {
|
||||||
return wp_timezone();
|
return wp_timezone();
|
||||||
}
|
}
|
||||||
|
@@ -188,7 +188,7 @@ class PostNotificationTest extends \MailPoetTest {
|
|||||||
|
|
||||||
$scheduleOption = $newsletter->getOption(NewsletterOptionFieldEntity::NAME_SCHEDULE);
|
$scheduleOption = $newsletter->getOption(NewsletterOptionFieldEntity::NAME_SCHEDULE);
|
||||||
$this->assertInstanceOf(NewsletterOptionEntity::class, $scheduleOption);
|
$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()))
|
verify($scheduler->getNextRunDate($scheduleOption->getValue()))
|
||||||
->equals('2017-01-01 14:00:00');
|
->equals('2017-01-01 14:00:00');
|
||||||
|
|
||||||
@@ -228,7 +228,7 @@ class PostNotificationTest extends \MailPoetTest {
|
|||||||
|
|
||||||
$scheduleOption = $newsletter->getOption(NewsletterOptionFieldEntity::NAME_SCHEDULE);
|
$scheduleOption = $newsletter->getOption(NewsletterOptionFieldEntity::NAME_SCHEDULE);
|
||||||
$this->assertInstanceOf(NewsletterOptionEntity::class, $scheduleOption);
|
$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()))
|
verify($scheduler->getNextRunDate($scheduleOption->getValue()))
|
||||||
->equals('2017-01-03 14:00:00');
|
->equals('2017-01-03 14:00:00');
|
||||||
|
|
||||||
@@ -268,7 +268,7 @@ class PostNotificationTest extends \MailPoetTest {
|
|||||||
$this->postNotificationScheduler->processPostNotificationSchedule($newsletter);
|
$this->postNotificationScheduler->processPostNotificationSchedule($newsletter);
|
||||||
$scheduleOption = $newsletter->getOption(NewsletterOptionFieldEntity::NAME_SCHEDULE);
|
$scheduleOption = $newsletter->getOption(NewsletterOptionFieldEntity::NAME_SCHEDULE);
|
||||||
$this->assertInstanceOf(NewsletterOptionEntity::class, $scheduleOption);
|
$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()))
|
verify($scheduler->getNextRunDate($scheduleOption->getValue()))
|
||||||
->equals('2017-01-19 14:00:00');
|
->equals('2017-01-19 14:00:00');
|
||||||
|
|
||||||
@@ -307,7 +307,7 @@ class PostNotificationTest extends \MailPoetTest {
|
|||||||
$this->postNotificationScheduler->processPostNotificationSchedule($newsletter);
|
$this->postNotificationScheduler->processPostNotificationSchedule($newsletter);
|
||||||
$scheduleOption = $newsletter->getOption(NewsletterOptionFieldEntity::NAME_SCHEDULE);
|
$scheduleOption = $newsletter->getOption(NewsletterOptionFieldEntity::NAME_SCHEDULE);
|
||||||
$this->assertInstanceOf(NewsletterOptionEntity::class, $scheduleOption);
|
$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()))
|
verify($scheduler->getNextRunDate($scheduleOption->getValue()))
|
||||||
->equals('2017-02-25 14:00:00');
|
->equals('2017-02-25 14:00:00');
|
||||||
|
|
||||||
@@ -346,7 +346,7 @@ class PostNotificationTest extends \MailPoetTest {
|
|||||||
$this->postNotificationScheduler->processPostNotificationSchedule($newsletter);
|
$this->postNotificationScheduler->processPostNotificationSchedule($newsletter);
|
||||||
$scheduleOption = $newsletter->getOption(NewsletterOptionFieldEntity::NAME_SCHEDULE);
|
$scheduleOption = $newsletter->getOption(NewsletterOptionFieldEntity::NAME_SCHEDULE);
|
||||||
$this->assertInstanceOf(NewsletterOptionEntity::class, $scheduleOption);
|
$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()))
|
verify($scheduler->getNextRunDate($scheduleOption->getValue()))
|
||||||
->equals('2017-01-01 13:01:00');
|
->equals('2017-01-01 13:01:00');
|
||||||
}
|
}
|
||||||
@@ -445,9 +445,9 @@ class PostNotificationTest extends \MailPoetTest {
|
|||||||
return $newsletterPost;
|
return $newsletterPost;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getSchedulerWithMockedTime(int $timestamp): Scheduler {
|
private function getSchedulerWithMockedTime(string $datetimeString): Scheduler {
|
||||||
$wpMock = $this->createMock(WPFunctions::class);
|
$wpMock = $this->createMock(WPFunctions::class);
|
||||||
$wpMock->method('currentTime')->willReturn($timestamp);
|
$wpMock->method('currentDatetime')->willReturn(new \DateTimeImmutable($datetimeString));
|
||||||
return $this->getServiceWithOverrides(Scheduler::class, [
|
return $this->getServiceWithOverrides(Scheduler::class, [
|
||||||
'wp' => $wpMock,
|
'wp' => $wpMock,
|
||||||
]);
|
]);
|
||||||
|
@@ -52,8 +52,9 @@ class SchedulerTest extends \MailPoetTest {
|
|||||||
public function testItCanGetCorrectNextRunDateInGMTTimezone() {
|
public function testItCanGetCorrectNextRunDateInGMTTimezone() {
|
||||||
$currentTime = '2024-08-09 09:00:00';
|
$currentTime = '2024-08-09 09:00:00';
|
||||||
$wpMock = $this->createMock(WPFunctions::class);
|
$wpMock = $this->createMock(WPFunctions::class);
|
||||||
$wpMock->method('currentTime')->willReturn(strtotime($currentTime));
|
$wpMock->method('currentDatetime')->willReturn(
|
||||||
$wpMock->method('wpTimezone')->willReturn(new \DateTimeZone('UTC'));
|
new \DateTimeImmutable($currentTime, new \DateTimeZone('UTC')),
|
||||||
|
);
|
||||||
$scheduler = $this->getServiceWithOverrides(Scheduler::class, ['wp' => $wpMock]);
|
$scheduler = $this->getServiceWithOverrides(Scheduler::class, ['wp' => $wpMock]);
|
||||||
verify($scheduler->getNextRunDate('0 10 * * *'))
|
verify($scheduler->getNextRunDate('0 10 * * *'))
|
||||||
->equals('2024-08-09 10:00:00');
|
->equals('2024-08-09 10:00:00');
|
||||||
@@ -62,8 +63,9 @@ class SchedulerTest extends \MailPoetTest {
|
|||||||
public function testItCanGetCorrectNextRunDateInNewYorkTimezone() {
|
public function testItCanGetCorrectNextRunDateInNewYorkTimezone() {
|
||||||
$currentTime = '2024-08-09 09:00:00';
|
$currentTime = '2024-08-09 09:00:00';
|
||||||
$wpMock = $this->createMock(WPFunctions::class);
|
$wpMock = $this->createMock(WPFunctions::class);
|
||||||
$wpMock->method('currentTime')->willReturn(strtotime($currentTime));
|
$wpMock->method('currentDatetime')->willReturn(
|
||||||
$wpMock->method('wpTimezone')->willReturn(new \DateTimeZone('America/New_York'));
|
new \DateTimeImmutable($currentTime, new \DateTimeZone('America/New_York'))
|
||||||
|
);
|
||||||
$scheduler = $this->getServiceWithOverrides(Scheduler::class, ['wp' => $wpMock]);
|
$scheduler = $this->getServiceWithOverrides(Scheduler::class, ['wp' => $wpMock]);
|
||||||
// Cron was set by user in NewYork timezone (0 10 * * *) in UTC it would be (0 14 * * *)
|
// Cron was set by user in NewYork timezone (0 10 * * *) in UTC it would be (0 14 * * *)
|
||||||
verify($scheduler->getNextRunDate('0 10 * * *'))
|
verify($scheduler->getNextRunDate('0 10 * * *'))
|
||||||
@@ -83,8 +85,9 @@ class SchedulerTest extends \MailPoetTest {
|
|||||||
public function testItCanGetCorrectPreviousRunDateInGMTTimezone() {
|
public function testItCanGetCorrectPreviousRunDateInGMTTimezone() {
|
||||||
$currentTime = '2024-08-09 09:00:00';
|
$currentTime = '2024-08-09 09:00:00';
|
||||||
$wpMock = $this->createMock(WPFunctions::class);
|
$wpMock = $this->createMock(WPFunctions::class);
|
||||||
$wpMock->method('currentTime')->willReturn(strtotime($currentTime));
|
$wpMock->method('currentDatetime')->willReturn(
|
||||||
$wpMock->method('wpTimezone')->willReturn(new \DateTimeZone('UTC'));
|
new \DateTimeImmutable($currentTime, new \DateTimeZone('UTC'))
|
||||||
|
);
|
||||||
$scheduler = $this->getServiceWithOverrides(Scheduler::class, ['wp' => $wpMock]);
|
$scheduler = $this->getServiceWithOverrides(Scheduler::class, ['wp' => $wpMock]);
|
||||||
verify($scheduler->getPreviousRunDate('0 10 * * *'))
|
verify($scheduler->getPreviousRunDate('0 10 * * *'))
|
||||||
->equals('2024-08-08 10:00:00');
|
->equals('2024-08-08 10:00:00');
|
||||||
@@ -93,8 +96,9 @@ class SchedulerTest extends \MailPoetTest {
|
|||||||
public function testItCanGetCorrectPreviousRunDateInNewYorkTimezone() {
|
public function testItCanGetCorrectPreviousRunDateInNewYorkTimezone() {
|
||||||
$currentTime = '2024-08-09 09:00:00';
|
$currentTime = '2024-08-09 09:00:00';
|
||||||
$wpMock = $this->createMock(WPFunctions::class);
|
$wpMock = $this->createMock(WPFunctions::class);
|
||||||
$wpMock->method('currentTime')->willReturn(strtotime($currentTime));
|
$wpMock->method('currentDatetime')->willReturn(
|
||||||
$wpMock->method('wpTimezone')->willReturn(new \DateTimeZone('America/New_York'));
|
new \DateTimeImmutable($currentTime, new \DateTimeZone('America/New_York'))
|
||||||
|
);
|
||||||
$scheduler = $this->getServiceWithOverrides(Scheduler::class, ['wp' => $wpMock]);
|
$scheduler = $this->getServiceWithOverrides(Scheduler::class, ['wp' => $wpMock]);
|
||||||
// Cron was set by user in NewYork timezone (0 10 * * *) in UTC it would be (0 14 * * *)
|
// Cron was set by user in NewYork timezone (0 10 * * *) in UTC it would be (0 14 * * *)
|
||||||
verify($scheduler->getPreviousRunDate('0 10 * * *'))
|
verify($scheduler->getPreviousRunDate('0 10 * * *'))
|
||||||
|
Reference in New Issue
Block a user