Skip to content

Commit

Permalink
Fixed import lock
Browse files Browse the repository at this point in the history
  • Loading branch information
korridor committed Dec 18, 2024
1 parent 02fe89d commit da612d0
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
11 changes: 7 additions & 4 deletions app/Service/Import/ImportService.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,13 @@ public function import(Organization $organization, string $importerType, string
$lock = Cache::lock('import:'.$organization->getKey(), config('octane.max_execution_time', 60) + 1);

if ($lock->get()) {
DB::transaction(function () use (&$importer, &$data, &$timezone): void {
$importer->importData($data, $timezone);
});
$lock->release();
try {
DB::transaction(function () use (&$importer, &$data, &$timezone): void {
$importer->importData($data, $timezone);
});
} finally {
$lock->release();
}
} else {
throw new ImportException('Import is already in progress');
}
Expand Down
2 changes: 2 additions & 0 deletions tests/Unit/Endpoint/Api/V1/ImportEndpointTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public function test_import_fails_if_user_does_not_have_permission(): void

public function test_import_fails_if_data_can_not_be_base64_decoded(): void
{
// Arrange
$user = $this->createUserWithPermission([
'import',
]);
Expand All @@ -98,6 +99,7 @@ public function test_import_fails_if_data_can_not_be_base64_decoded(): void

public function test_import_return_error_message_if_import_fails(): void
{
// Arrange
$user = $this->createUserWithPermission([
'import',
]);
Expand Down
21 changes: 21 additions & 0 deletions tests/Unit/Service/Import/ImportServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public function test_import_gets_importer_from_provider_runs_importer_and_return
$report = $importService->import($organization, 'toggl_time_entries', $data, $timezone);

// Assert
$lock = Cache::lock('import:'.$organization->getKey());
$this->assertTrue($lock->get());
$this->assertSame(2, $report->timeEntriesCreated);
$this->assertSame(2, $report->tagsCreated);
$this->assertSame(1, $report->tasksCreated);
Expand All @@ -43,6 +45,25 @@ public function test_import_gets_importer_from_provider_runs_importer_and_return
$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
{
// Arrange
Expand Down

0 comments on commit da612d0

Please sign in to comment.