Skip to content

Commit da612d0

Browse files
committed
Fixed import lock
1 parent 02fe89d commit da612d0

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

app/Service/Import/ImportService.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,13 @@ public function import(Organization $organization, string $importerType, string
3131
$lock = Cache::lock('import:'.$organization->getKey(), config('octane.max_execution_time', 60) + 1);
3232

3333
if ($lock->get()) {
34-
DB::transaction(function () use (&$importer, &$data, &$timezone): void {
35-
$importer->importData($data, $timezone);
36-
});
37-
$lock->release();
34+
try {
35+
DB::transaction(function () use (&$importer, &$data, &$timezone): void {
36+
$importer->importData($data, $timezone);
37+
});
38+
} finally {
39+
$lock->release();
40+
}
3841
} else {
3942
throw new ImportException('Import is already in progress');
4043
}

tests/Unit/Endpoint/Api/V1/ImportEndpointTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ public function test_import_fails_if_user_does_not_have_permission(): void
7878

7979
public function test_import_fails_if_data_can_not_be_base64_decoded(): void
8080
{
81+
// Arrange
8182
$user = $this->createUserWithPermission([
8283
'import',
8384
]);
@@ -98,6 +99,7 @@ public function test_import_fails_if_data_can_not_be_base64_decoded(): void
9899

99100
public function test_import_return_error_message_if_import_fails(): void
100101
{
102+
// Arrange
101103
$user = $this->createUserWithPermission([
102104
'import',
103105
]);

tests/Unit/Service/Import/ImportServiceTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ public function test_import_gets_importer_from_provider_runs_importer_and_return
3535
$report = $importService->import($organization, 'toggl_time_entries', $data, $timezone);
3636

3737
// Assert
38+
$lock = Cache::lock('import:'.$organization->getKey());
39+
$this->assertTrue($lock->get());
3840
$this->assertSame(2, $report->timeEntriesCreated);
3941
$this->assertSame(2, $report->tagsCreated);
4042
$this->assertSame(1, $report->tasksCreated);
@@ -43,6 +45,25 @@ public function test_import_gets_importer_from_provider_runs_importer_and_return
4345
$this->assertSame(1, $report->clientsCreated);
4446
}
4547

48+
public function test_import_releases_lock_if_an_exception_happens_during_the_import(): void
49+
{
50+
// Arrange
51+
Storage::fake(config('filesystems.default'));
52+
$organization = Organization::factory()->create();
53+
$timezone = 'Europe/Vienna';
54+
$data = 'Invalid CSV data';
55+
56+
// Act
57+
$importService = app(ImportService::class);
58+
try {
59+
$importService->import($organization, 'toggl_time_entries', $data, $timezone);
60+
} catch (ImportException) {
61+
// Assert
62+
$lock = Cache::lock('import:'.$organization->getKey());
63+
$this->assertTrue($lock->get());
64+
}
65+
}
66+
4667
public function test_import_throws_exception_if_import_is_already_in_progress(): void
4768
{
4869
// Arrange

0 commit comments

Comments
 (0)