Skip to content

Commit abf2864

Browse files
committed
Merge pull request #39 from kevin-thackorie/concurrentCacheFile
Add graceful handling of cache write failure due to already existent cache file caused by race condition.
2 parents 9856010 + 522d2b8 commit abf2864

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

src/Server.php

+10-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace League\Glide;
44

55
use InvalidArgumentException;
6+
use League\Flysystem\FileExistsException;
67
use League\Flysystem\FilesystemInterface;
78
use League\Glide\Api\ApiInterface;
89
use League\Glide\Filesystem\FilesystemException;
@@ -299,10 +300,15 @@ public function makeImage()
299300
);
300301
}
301302

302-
$write = $this->cache->write(
303-
$this->getCachePath($request),
304-
$this->api->run($request, $source)
305-
);
303+
try {
304+
$write = $this->cache->write(
305+
$this->getCachePath($request),
306+
$this->api->run($request, $source)
307+
);
308+
} catch (FileExistsException $exception) {
309+
// Cache file failed to write. Fail silently.
310+
return $request;
311+
}
306312

307313
if ($write === false) {
308314
throw new FilesystemException(

tests/ServerTest.php

+19
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,25 @@ public function testMakeImageWithUnwritableCache()
279279
$this->server->makeImage('image.jpg');
280280
}
281281

282+
public function testMakeImageWithExistingCacheFile()
283+
{
284+
$this->server->setSource(Mockery::mock('League\Flysystem\FilesystemInterface', function ($mock) {
285+
$mock->shouldReceive('has')->andReturn(true)->once();
286+
$mock->shouldReceive('read')->andReturn('content')->once();
287+
}));
288+
289+
$this->server->setCache(Mockery::mock('League\Flysystem\FilesystemInterface', function ($mock) {
290+
$mock->shouldReceive('has')->andReturn(false)->once();
291+
$mock->shouldReceive('write')->andThrow(new \League\Flysystem\FileExistsException('75094881e9fd2b93063d6a5cb083091c'));
292+
}));
293+
294+
$this->server->setApi(Mockery::mock('League\Glide\Api\ApiInterface', function ($mock) {
295+
$mock->shouldReceive('run')->andReturn('content')->once();
296+
}));
297+
298+
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Request', $this->server->makeImage('image.jpg'));
299+
}
300+
282301
public function testMakeImageFromSource()
283302
{
284303
$this->server->setSource(Mockery::mock('League\Flysystem\FilesystemInterface', function ($mock) {

0 commit comments

Comments
 (0)