-
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
feat(cron): more precise execution report #59965
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -18,6 +18,7 @@ | |||||
| use OC\Session\Memory; | ||||||
| use OC\User\Session; | ||||||
| use OCP\App\IAppManager; | ||||||
| use OCP\BackgroundJob\IJob; | ||||||
| use OCP\BackgroundJob\IJobList; | ||||||
| use OCP\Files\ISetupManager; | ||||||
| use OCP\IAppConfig; | ||||||
|
|
@@ -29,6 +30,8 @@ | |||||
| use Psr\Log\LoggerInterface; | ||||||
|
|
||||||
| class CronService { | ||||||
| private ?IJob $currentJob = null; | ||||||
|
|
||||||
| /** * @var ?callable $verboseCallback */ | ||||||
| private $verboseCallback = null; | ||||||
|
|
||||||
|
|
@@ -151,6 +154,21 @@ private function runCli(string $appMode, ?array $jobClasses): void { | |||||
| } | ||||||
| } | ||||||
|
|
||||||
| // Try to log and unlock job in case of failure (eg. Allowed memory size exhausted) | ||||||
| register_shutdown_function(function () { | ||||||
| $error = error_get_last(); | ||||||
| if ($error === null) { | ||||||
| return; | ||||||
| } | ||||||
|
|
||||||
| $message = 'Uncatched error when running job ' . $this->currentJob->getId() . ': ' . $error['message']; | ||||||
| $this->logger->error($message); | ||||||
| $this->verboseOutput($message); | ||||||
| if ($this->currentJob instanceof IJob) { | ||||||
| $this->jobList->unlockJob($this->currentJob); | ||||||
| } | ||||||
| }); | ||||||
|
|
||||||
| // We only ask for jobs for 14 minutes, because after 5 minutes the next | ||||||
| // system cron task should spawn and we want to have at most three | ||||||
| // cron jobs running in parallel. | ||||||
|
|
@@ -159,6 +177,7 @@ private function runCli(string $appMode, ?array $jobClasses): void { | |||||
| $executedJobs = []; | ||||||
|
|
||||||
| while ($job = $this->jobList->getNext($onlyTimeSensitive, $jobClasses)) { | ||||||
| $this->currentJob = $job; | ||||||
| if (isset($executedJobs[$job->getId()])) { | ||||||
| $this->jobList->unlockJob($job); | ||||||
| break; | ||||||
|
|
@@ -167,20 +186,19 @@ private function runCli(string $appMode, ?array $jobClasses): void { | |||||
| $jobDetails = get_class($job) . ' (id: ' . $job->getId() . ', arguments: ' . json_encode($job->getArgument()) . ')'; | ||||||
| $this->logger->debug('CLI cron call has selected job ' . $jobDetails, ['app' => 'cron']); | ||||||
|
|
||||||
| $timeBefore = time(); | ||||||
| $memoryBefore = memory_get_usage(); | ||||||
| $memoryPeakBefore = memory_get_peak_usage(); | ||||||
|
|
||||||
| $this->verboseOutput('Starting job ' . $jobDetails); | ||||||
|
|
||||||
| $startTime = microtime(true); | ||||||
| $referenceMemory = memory_get_usage(); | ||||||
| memory_reset_peak_usage(); | ||||||
|
|
||||||
| $job->start($this->jobList); | ||||||
|
|
||||||
| $timeAfter = time(); | ||||||
| $memoryAfter = memory_get_usage(); | ||||||
| $memoryPeakAfter = memory_get_peak_usage(); | ||||||
| $memoryIncrease = memory_get_usage() - $referenceMemory; | ||||||
| $timeSpent = microtime(true) - $startTime; | ||||||
| $jobMemoryPeak = memory_get_peak_usage() - $referenceMemory; | ||||||
|
|
||||||
| $cronInterval = 5 * 60; | ||||||
| $timeSpent = $timeAfter - $timeBefore; | ||||||
| if ($timeSpent > $cronInterval) { | ||||||
| $logLevel = match (true) { | ||||||
| $timeSpent > $cronInterval * 128 => ILogger::FATAL, | ||||||
|
|
@@ -196,13 +214,13 @@ private function runCli(string $appMode, ?array $jobClasses): void { | |||||
| ); | ||||||
| } | ||||||
|
|
||||||
| if ($memoryAfter - $memoryBefore > 50_000_000) { | ||||||
| $message = 'Used memory grew by more than 50 MB when executing job ' . $jobDetails . ': ' . Util::humanFileSize($memoryAfter) . ' (before: ' . Util::humanFileSize($memoryBefore) . ')'; | ||||||
| if ($memoryIncrease > 50 * 1024 * 1024) { | ||||||
| $message = 'Memory leak detected after executing job ' . $jobDetails . '. Memory usage grew by ' . Util::humanFileSize($memoryIncrease) . '.'; | ||||||
| $this->logger->warning($message, ['app' => 'cron']); | ||||||
| $this->verboseOutput($message); | ||||||
| } | ||||||
| if ($memoryPeakAfter > 300_000_000 && $memoryPeakBefore <= 300_000_000) { | ||||||
| $message = 'Cron job used more than 300 MB of ram after executing job ' . $jobDetails . ': ' . Util::humanFileSize($memoryPeakAfter) . ' (before: ' . Util::humanFileSize($memoryPeakBefore) . ')'; | ||||||
| if ($jobMemoryPeak > 300 * 1024 * 1024) { | ||||||
| $message = 'Cron job used more than 300 MiB of RAM after executing job ' . $jobDetails . ': ' . Util::humanFileSize($jobMemoryPeak) . ')'; | ||||||
| $this->logger->warning($message, ['app' => 'cron']); | ||||||
| $this->verboseOutput($message); | ||||||
| } | ||||||
|
|
@@ -211,16 +229,19 @@ private function runCli(string $appMode, ?array $jobClasses): void { | |||||
| $this->setupManager->tearDown(); | ||||||
| $this->tempManager->clean(); | ||||||
|
|
||||||
| $this->verboseOutput('Job ' . $jobDetails . ' done in ' . ($timeAfter - $timeBefore) . ' seconds'); | ||||||
| $this->verboseOutput('Job ' . $jobDetails . ' done in ' . number_format($timeSpent, 2) . ' seconds'); | ||||||
|
|
||||||
| $this->jobList->setLastJob($job); | ||||||
| $executedJobs[$job->getId()] = true; | ||||||
| unset($job); | ||||||
|
|
||||||
| if ($timeAfter > $endTime) { | ||||||
| if (time() > $endTime) { | ||||||
| break; | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| // Makes sure last error isn't catched by shutdown function | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| error_clear_last(); | ||||||
| } | ||||||
|
|
||||||
| private function runWeb(string $appMode): void { | ||||||
|
|
||||||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.