Skip to content

Commit

Permalink
Merge pull request #207 from opcodesio/bug/fix-infinite-scanning-bug
Browse files Browse the repository at this point in the history
Bug / fix infinite scanning bug
  • Loading branch information
arukompas authored Mar 5, 2023
2 parents e688bb6 + f15d19b commit 18d32d0
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "opcodesio/log-viewer",
"version": "v2.2.0",
"version": "v2.2.1",
"description": "Fast and easy-to-use log viewer for your Laravel application",
"keywords": [
"arukompas",
Expand Down
12 changes: 12 additions & 0 deletions src/LogReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ class LogReader
*/
protected $fileHandle = null;

protected int $mtimeBeforeScan;

protected string $direction = Direction::Forward;

public function __construct(LogFile $file)
Expand Down Expand Up @@ -292,6 +294,8 @@ public function scan(int $maxBytesToScan = null, bool $force = false): self
$this->index()->clearCache();
}

$this->mtimeBeforeScan = $this->file->mtime();

// we don't care about the selected levels here, we should scan everything
$logIndex = $this->index();
$levels = self::getDefaultLevels();
Expand Down Expand Up @@ -565,6 +569,14 @@ public function numberOfNewBytes(): int

public function requiresScan(): bool
{
if (isset($this->mtimeBeforeScan) && ($this->file->mtime() > $this->mtimeBeforeScan || $this->file->mtime() === time())) {
// The file has been modified since the last scan in this request.
// Let's only request another scan if it's not the last chunk (smaller than lazyScanChunkSize).
// The last chunk will be scanned until the end before hitting this logic again,
// and by then the only appended bytes will be from the current request and thus return false.
return $this->numberOfNewBytes() >= LogViewer::lazyScanChunkSize();
}

return $this->numberOfNewBytes() !== 0;
}

Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/LogReaderTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

use Illuminate\Support\Facades\File;
use Opcodes\LogViewer\LogFile;
use Opcodes\LogViewer\LogReader;

beforeEach(function () {
$this->file = generateLogFile();
Expand All @@ -27,7 +27,7 @@
File::append($this->file->path, PHP_EOL.makeLogEntry());

// re-instantiate the log reader to make sure we don't have anything cached
$this->file = new LogFile($this->file->path);
LogReader::clearInstance($this->file);
$logReader = $this->file->logs();
expect($logReader->requiresScan())->toBeTrue();

Expand Down

0 comments on commit 18d32d0

Please sign in to comment.