From b7948f5c1b8fca6842c631b3ea1f86ff3a367e2e Mon Sep 17 00:00:00 2001 From: Simon Erkelens Date: Fri, 20 Oct 2017 17:01:43 +1300 Subject: [PATCH] Minor refactoring for the batching. --- code/Jobs/AnalyticsUpdateJob.php | 11 ++++++++--- code/Services/PageUpdateService.php | 7 ++++++- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/code/Jobs/AnalyticsUpdateJob.php b/code/Jobs/AnalyticsUpdateJob.php index bb547d2..46a7fdf 100644 --- a/code/Jobs/AnalyticsUpdateJob.php +++ b/code/Jobs/AnalyticsUpdateJob.php @@ -13,6 +13,11 @@ class AnalyticsUpdateJob extends AbstractQueuedJob */ protected $service; + /** + * @var PageUpdateService + */ + protected $updateService; + /** * AnalyticsUpdateJob constructor. * @param array $params @@ -56,11 +61,11 @@ protected function getReport($client) $reports = $this->service->getReport(); $count = 0; - $updateService = new PageUpdateService(); + $this->updateService = new PageUpdateService(); foreach ($reports as $report) { /** @var array $rows */ $rows = $report->getData()->getRows(); - $count += $updateService->updateVisits($rows); + $count += $this->updateService->updateVisits($rows); } $this->addMessage("$count Pages updated with Google Analytics visit count"); } @@ -70,7 +75,7 @@ protected function getReport($client) */ public function afterComplete() { - if ($this->service->batched) { + if ($this->service->batched && $this->updateService->batched) { /** @var AnalyticsUpdateJob $nextJob */ $nextJob = Injector::inst()->get('AnalyticsUpdateJob'); $nextJob->setJobData(1, 0, false, new stdClass(), ['Batched data from Google']); diff --git a/code/Services/PageUpdateService.php b/code/Services/PageUpdateService.php index 0e72b92..0a5536c 100644 --- a/code/Services/PageUpdateService.php +++ b/code/Services/PageUpdateService.php @@ -6,6 +6,11 @@ class PageUpdateService { + /** + * @var bool + */ + public $batched = true; + /** * @param array $rows * @return int @@ -28,7 +33,7 @@ public function updateVisits($rows) } // If we're not getting any results back, we're out of data from Google. // Stop the batching process. - if ($count === 0) { + if ($count === 0 || $count > 20) { $this->batched = false; } return $count;