Make static methods in cron workers non-static

[MAILPOET-2538]
This commit is contained in:
Jan Jakeš
2019-11-18 13:16:02 +01:00
committed by Jack Kitterhing
parent ba41f4901e
commit cd93940940
10 changed files with 44 additions and 43 deletions

View File

@ -42,7 +42,7 @@ class Beamer extends SimpleWorker {
return true;
}
static function getNextRunDate() {
function getNextRunDate() {
$wp = new WPFunctions;
$date = Carbon::createFromTimestamp($wp->currentTime('timestamp'));
return $date->hour(11)->minute(00)->second(00)->addDay();

View File

@ -33,14 +33,14 @@ class InactiveSubscribers extends SimpleWorker {
function processTaskStrategy(ScheduledTask $task) {
$tracking_enabled = (bool)$this->settings->get('tracking.enabled');
if (!$tracking_enabled) {
self::schedule();
$this->schedule();
return true;
}
$days_to_inactive = (int)$this->settings->get('deactivate_subscriber_after_inactive_days');
// Activate all inactive subscribers in case the feature is turned off
if ($days_to_inactive === 0) {
$this->inactive_subscribers_controller->reactivateInactiveSubscribers();
self::schedule();
$this->schedule();
return true;
}
// Handle activation/deactivation within interval
@ -60,7 +60,7 @@ class InactiveSubscribers extends SimpleWorker {
while ($this->inactive_subscribers_controller->markActiveSubscribers($days_to_inactive, self::BATCH_SIZE) === self::BATCH_SIZE) {
$this->cron_helper->enforceExecutionLimit($this->timer);
};
self::schedule();
$this->schedule();
return true;
}
}

View File

@ -17,12 +17,12 @@ class Migration extends SimpleWorker {
function checkProcessingRequirements() {
// if migration was completed, don't run it again
$completed_tasks = self::getCompletedTasks();
$completed_tasks = $this->getCompletedTasks();
return empty($completed_tasks);
}
function prepareTask(ScheduledTask $task) {
$unmigrated_columns = self::checkUnmigratedColumnsExist();
$unmigrated_columns = $this->checkUnmigratedColumnsExist();
$unmigrated_queues_count = 0;
$unmigrated_queue_subscribers = [];
@ -84,7 +84,7 @@ class Migration extends SimpleWorker {
return true;
}
static function checkUnmigratedColumnsExist() {
private function checkUnmigratedColumnsExist() {
global $wpdb;
$existing_columns = $wpdb->get_col('DESC ' . SendingQueue::$_table);
return in_array('type', $existing_columns);
@ -243,7 +243,7 @@ class Migration extends SimpleWorker {
return true;
}
static function getNextRunDate($wp = null) {
function getNextRunDate($wp = null) {
if (is_null($wp)) {
$wp = new WPFunctions();
}

View File

@ -47,12 +47,12 @@ abstract class SimpleWorker {
$this->init();
$scheduled_tasks = self::getDueTasks();
$running_tasks = self::getRunningTasks();
$scheduled_tasks = $this->getDueTasks();
$running_tasks = $this->getRunningTasks();
if (!$scheduled_tasks && !$running_tasks) {
if (static::AUTOMATIC_SCHEDULING) {
self::schedule();
$this->schedule();
}
return false;
}
@ -75,7 +75,7 @@ abstract class SimpleWorker {
return true;
}
static function schedule() {
function schedule() {
$already_scheduled = ScheduledTask::where('type', static::TASK_TYPE)
->whereNull('deleted_at')
->where('status', ScheduledTask::STATUS_SCHEDULED)
@ -87,7 +87,7 @@ abstract class SimpleWorker {
$task->type = static::TASK_TYPE;
$task->status = ScheduledTask::STATUS_SCHEDULED;
$task->priority = ScheduledTask::PRIORITY_LOW;
$task->scheduled_at = static::getNextRunDate();
$task->scheduled_at = $this->getNextRunDate();
$task->save();
return $task;
}
@ -183,7 +183,7 @@ abstract class SimpleWorker {
return false;
}
static function getNextRunDate() {
function getNextRunDate() {
$wp = new WPFunctions();
$date = Carbon::createFromTimestamp($wp->currentTime('timestamp'));
// Random day of the next week
@ -192,15 +192,15 @@ abstract class SimpleWorker {
return $date;
}
static function getDueTasks() {
function getDueTasks() {
return ScheduledTask::findDueByType(static::TASK_TYPE, self::TASK_BATCH_SIZE);
}
static function getRunningTasks() {
function getRunningTasks() {
return ScheduledTask::findRunningByType(static::TASK_TYPE, self::TASK_BATCH_SIZE);
}
static function getCompletedTasks() {
function getCompletedTasks() {
return ScheduledTask::findCompletedByType(static::TASK_TYPE, self::TASK_BATCH_SIZE);
}
}

View File

@ -152,7 +152,7 @@ class AutomatedEmails extends SimpleWorker {
return $context;
}
static function getNextRunDate() {
function getNextRunDate() {
$wp = new WPFunctions;
$date = Carbon::createFromTimestamp($wp->currentTime('timestamp'));
return $date->endOfMonth()->next(Carbon::MONDAY)->midDay();

View File

@ -22,12 +22,12 @@ class SubscriberLinkTokens extends SimpleWorker {
sprintf('UPDATE %s SET link_token = SUBSTRING(MD5(CONCAT(?, email)), 1, ?) WHERE link_token IS NULL LIMIT ?', Subscriber::$_table),
[$auth_key, Subscriber::OBSOLETE_LINK_TOKEN_LENGTH, self::BATCH_SIZE]
);
self::schedule();
$this->schedule();
}
return true;
}
static function getNextRunDate() {
function getNextRunDate() {
$wp = new WPFunctions();
return Carbon::createFromTimestamp($wp->currentTime('timestamp'));
}

View File

@ -49,7 +49,7 @@ class UnsubscribeTokens extends SimpleWorker {
return count($instances);
}
static function getNextRunDate() {
function getNextRunDate() {
$wp = new WPFunctions;
return Carbon::createFromTimestamp($wp->currentTime('timestamp'));
}

View File

@ -31,7 +31,7 @@ class WooCommercePastOrders extends SimpleWorker {
}
function checkProcessingRequirements() {
return $this->woocommerce_helper->isWooCommerceActive() && empty(self::getCompletedTasks()); // run only once
return $this->woocommerce_helper->isWooCommerceActive() && empty($this->getCompletedTasks()); // run only once
}
function processTaskStrategy(ScheduledTask $task) {
@ -71,7 +71,7 @@ class WooCommercePastOrders extends SimpleWorker {
return false;
}
static function getNextRunDate() {
function getNextRunDate() {
return Carbon::createFromTimestamp(WPFunctions::get()->currentTime('timestamp')); // schedule immediately
}
}

View File

@ -18,10 +18,7 @@ class MigrationTest extends \MailPoetTest {
function _before() {
parent::_before();
// Alter table to test migration
if (!Migration::checkUnmigratedColumnsExist()) {
$this->downgradeTable();
$this->altered = true;
}
$this->downgradeTable();
$this->subscriber_to_process = Subscriber::createOrUpdate([
'status' => Subscriber::STATUS_SUBSCRIBED,
@ -122,7 +119,7 @@ class MigrationTest extends \MailPoetTest {
},
]);
$next_run_date = Migration::getNextRunDate($wp);
$next_run_date = $this->worker->getNextRunDate($wp);
expect($next_run_date->timestamp)->equals($timestamp);
}
@ -200,9 +197,7 @@ class MigrationTest extends \MailPoetTest {
\ORM::raw_execute('TRUNCATE ' . Subscriber::$_table);
// Restore table after testing
if (!empty($this->altered)) {
$this->restoreTable();
$this->altered = false;
}
$this->restoreTable();
$this->altered = false;
}
}

View File

@ -52,49 +52,55 @@ class SimpleWorkerTest extends \MailPoetTest {
function testItSchedulesTask() {
expect(ScheduledTask::where('type', MockSimpleWorker::TASK_TYPE)->findMany())->isEmpty();
MockSimpleWorker::schedule();
(new MockSimpleWorker())->schedule();
expect(ScheduledTask::where('type', MockSimpleWorker::TASK_TYPE)->findMany())->notEmpty();
}
function testItDoesNotScheduleTaskTwice() {
$worker = new MockSimpleWorker();
expect(count(ScheduledTask::where('type', MockSimpleWorker::TASK_TYPE)->findMany()))->equals(0);
MockSimpleWorker::schedule();
$worker->schedule();
expect(count(ScheduledTask::where('type', MockSimpleWorker::TASK_TYPE)->findMany()))->equals(1);
MockSimpleWorker::schedule();
$worker->schedule();
expect(count(ScheduledTask::where('type', MockSimpleWorker::TASK_TYPE)->findMany()))->equals(1);
}
function testItCanGetScheduledTasks() {
expect(MockSimpleWorker::getDueTasks())->isEmpty();
$worker = new MockSimpleWorker();
expect($worker->getDueTasks())->isEmpty();
$this->createScheduledTask();
expect(MockSimpleWorker::getDueTasks())->notEmpty();
expect($worker->getDueTasks())->notEmpty();
}
function testItCanGetABatchOfScheduledTasks() {
$worker = new MockSimpleWorker();
for ($i = 0; $i < MockSimpleWorker::TASK_BATCH_SIZE + 5; $i += 1) {
$this->createScheduledTask();
}
expect(count(MockSimpleWorker::getDueTasks()))->equals(MockSimpleWorker::TASK_BATCH_SIZE);
expect(count($worker->getDueTasks()))->equals(MockSimpleWorker::TASK_BATCH_SIZE);
}
function testItCanGetRunningTasks() {
expect(MockSimpleWorker::getRunningTasks())->isEmpty();
$worker = new MockSimpleWorker();
expect($worker->getRunningTasks())->isEmpty();
$this->createRunningTask();
expect(MockSimpleWorker::getRunningTasks())->notEmpty();
expect($worker->getRunningTasks())->notEmpty();
}
function testItCanGetBatchOfRunningTasks() {
$worker = new MockSimpleWorker();
for ($i = 0; $i < MockSimpleWorker::TASK_BATCH_SIZE + 5; $i += 1) {
$this->createRunningTask();
}
expect(count(MockSimpleWorker::getRunningTasks()))->equals(MockSimpleWorker::TASK_BATCH_SIZE);
expect(count($worker->getRunningTasks()))->equals(MockSimpleWorker::TASK_BATCH_SIZE);
}
function testItCanGetBatchOfCompletedTasks() {
$worker = new MockSimpleWorker();
for ($i = 0; $i < MockSimpleWorker::TASK_BATCH_SIZE + 5; $i += 1) {
$this->createCompletedTask();
}
expect(count(MockSimpleWorker::getCompletedTasks()))->equals(MockSimpleWorker::TASK_BATCH_SIZE);
expect(count($worker->getCompletedTasks()))->equals(MockSimpleWorker::TASK_BATCH_SIZE);
}
function testItFailsToProcessWithoutTasks() {
@ -264,7 +270,7 @@ class SimpleWorkerTest extends \MailPoetTest {
function testItCalculatesNextRunDateWithinNextWeekBoundaries() {
$current_date = Carbon::createFromTimestamp(current_time('timestamp'));
$next_run_date = MockSimpleWorker::getNextRunDate();
$next_run_date = (new MockSimpleWorker())->getNextRunDate();
$difference = $next_run_date->diffInDays($current_date);
// Subtract days left in the current week
$difference -= (Carbon::DAYS_PER_WEEK - $current_date->format('N'));