Skip to content

Commit

Permalink
Minor refactoring for the batching.
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Erkelens committed Oct 20, 2017
1 parent 499d218 commit b7948f5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
11 changes: 8 additions & 3 deletions code/Jobs/AnalyticsUpdateJob.php
Expand Up @@ -13,6 +13,11 @@ class AnalyticsUpdateJob extends AbstractQueuedJob
*/
protected $service;

/**
* @var PageUpdateService
*/
protected $updateService;

/**
* AnalyticsUpdateJob constructor.
* @param array $params
Expand Down Expand Up @@ -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");
}
Expand All @@ -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']);
Expand Down
7 changes: 6 additions & 1 deletion code/Services/PageUpdateService.php
Expand Up @@ -6,6 +6,11 @@
class PageUpdateService
{

/**
* @var bool
*/
public $batched = true;

/**
* @param array $rows
* @return int
Expand All @@ -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;
Expand Down

0 comments on commit b7948f5

Please sign in to comment.