Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Limit the processing time of archiving jobs #22979

Open
wants to merge 1 commit into
base: 5.x-dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions core/CronArchive.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,15 @@ class CronArchive
*/
public $maxArchivesToProcess = null;

/**
* Time in seconds how long an archiving job is allowed to start new archiving processes.
michalkleiner marked this conversation as resolved.
Show resolved Hide resolved
michalkleiner marked this conversation as resolved.
Show resolved Hide resolved
* When time limit is reached, the archiving job will wrap after current processes are finished up instead of
* continuing with the next invalidation requests.
*
* @var int
*/
public $stopProcessingAfter = -1;

private $archivingStartingTime;

private $formatter;
Expand Down Expand Up @@ -418,6 +427,11 @@ public function run()
$this->logger->info("Maximum number of archives to process per execution has been reached.");
break;
}

if ($this->stopProcessingAfter > 0 && $this->stopProcessingAfter < $timer->getTime()) {
$this->logger->info("Maximum time limit per execution has been reached.");
break;
}
}

$this->disconnectDb();
Expand Down
7 changes: 7 additions & 0 deletions plugins/CoreConsole/Commands/CoreArchiver.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ protected function makeArchiver($url)
$archiver->shouldArchiveAllSites = $input->getOption('force-all-websites');
$archiver->maxSitesToProcess = $input->getOption('max-websites-to-process');
$archiver->maxArchivesToProcess = $input->getOption('max-archives-to-process');
$archiver->stopProcessingAfter = $input->getOption('stop-processing-after');
$archiver->setUrlToPiwik($url);

$archiveFilter = new CronArchive\ArchiveFilter();
Expand Down Expand Up @@ -162,6 +163,12 @@ public static function configureArchiveCommand(ConsoleCommand $command)
null,
"Maximum number of archives to process during a single execution of the archiver. Can be used to limit the process lifetime e.g. to avoid increasing memory usage."
);
$command->addOptionalValueOption(
'stop-processing-after',
null,
"Number of seconds how long a job is allowed to start new archiving processes. When limit is reached job will wrap up after finishing current processes.",
michalkleiner marked this conversation as resolved.
Show resolved Hide resolved
60 * 60 * 24 // 24 hours, to ensure archiving is started once a day
);
$command->addNoValueOption(
'disable-scheduled-tasks',
null,
Expand Down
21 changes: 21 additions & 0 deletions plugins/CoreConsole/tests/System/ArchiveCronTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,27 @@ public function testArchivePhpScriptDoesNotFailWhenCommandHelpRequested()
self::assertStringNotContainsString("Starting Piwik reports archiving...", $output);
}

public function testArchivePhpCronWithTimeLimit()
{
self::$fixture->getTestEnvironment()->overrideConfig('General', 'enable_browser_archiving_triggering', 0);
self::$fixture->getTestEnvironment()->overrideConfig('General', 'browser_archiving_disabled_enforce', 1);
self::$fixture->getTestEnvironment()->save();

Config::getInstance()->General['enable_browser_archiving_triggering'] = 0;
Config::getInstance()->General['browser_archiving_disabled_enforce'] = 1;

Rules::setBrowserTriggerArchiving(false);

// track a visit so archiving will go through
$tracker = Fixture::getTracker(1, '2005-09-04');
$tracker->setUrl('http://example.com/test/url2');
Fixture::checkResponse($tracker->doTrackPageView('jkl'));

$output = $this->runArchivePhpCron(['-vvv --stop-processing-after=1' => null]);

self::assertStringContainsString('Maximum time limit per execution has been reached.', $output);
}

private function runArchivePhpCron($options = array(), $archivePhpScript = false)
{
$archivePhpScript = $archivePhpScript ?: PIWIK_INCLUDE_PATH . '/tests/PHPUnit/proxy/archive.php';
Expand Down
Loading