Fixed import lock

This commit is contained in:
Constantin Graf
2024-12-18 11:22:33 -05:00
committed by Constantin Graf
parent 29929467f6
commit 62270382dc
3 changed files with 30 additions and 4 deletions

View File

@@ -31,10 +31,13 @@ class ImportService
$lock = Cache::lock('import:'.$organization->getKey(), config('octane.max_execution_time', 60) + 1); $lock = Cache::lock('import:'.$organization->getKey(), config('octane.max_execution_time', 60) + 1);
if ($lock->get()) { if ($lock->get()) {
DB::transaction(function () use (&$importer, &$data, &$timezone): void { try {
$importer->importData($data, $timezone); DB::transaction(function () use (&$importer, &$data, &$timezone): void {
}); $importer->importData($data, $timezone);
$lock->release(); });
} finally {
$lock->release();
}
} else { } else {
throw new ImportException('Import is already in progress'); throw new ImportException('Import is already in progress');
} }

View File

@@ -78,6 +78,7 @@ class ImportEndpointTest extends ApiEndpointTestAbstract
public function test_import_fails_if_data_can_not_be_base64_decoded(): void public function test_import_fails_if_data_can_not_be_base64_decoded(): void
{ {
// Arrange
$user = $this->createUserWithPermission([ $user = $this->createUserWithPermission([
'import', 'import',
]); ]);
@@ -98,6 +99,7 @@ class ImportEndpointTest extends ApiEndpointTestAbstract
public function test_import_return_error_message_if_import_fails(): void public function test_import_return_error_message_if_import_fails(): void
{ {
// Arrange
$user = $this->createUserWithPermission([ $user = $this->createUserWithPermission([
'import', 'import',
]); ]);

View File

@@ -35,6 +35,8 @@ class ImportServiceTest extends TestCase
$report = $importService->import($organization, 'toggl_time_entries', $data, $timezone); $report = $importService->import($organization, 'toggl_time_entries', $data, $timezone);
// Assert // Assert
$lock = Cache::lock('import:'.$organization->getKey());
$this->assertTrue($lock->get());
$this->assertSame(2, $report->timeEntriesCreated); $this->assertSame(2, $report->timeEntriesCreated);
$this->assertSame(2, $report->tagsCreated); $this->assertSame(2, $report->tagsCreated);
$this->assertSame(1, $report->tasksCreated); $this->assertSame(1, $report->tasksCreated);
@@ -43,6 +45,25 @@ class ImportServiceTest extends TestCase
$this->assertSame(1, $report->clientsCreated); $this->assertSame(1, $report->clientsCreated);
} }
public function test_import_releases_lock_if_an_exception_happens_during_the_import(): void
{
// Arrange
Storage::fake(config('filesystems.default'));
$organization = Organization::factory()->create();
$timezone = 'Europe/Vienna';
$data = 'Invalid CSV data';
// Act
$importService = app(ImportService::class);
try {
$importService->import($organization, 'toggl_time_entries', $data, $timezone);
} catch (ImportException) {
// Assert
$lock = Cache::lock('import:'.$organization->getKey());
$this->assertTrue($lock->get());
}
}
public function test_import_throws_exception_if_import_is_already_in_progress(): void public function test_import_throws_exception_if_import_is_already_in_progress(): void
{ {
// Arrange // Arrange