mirror of
https://github.com/solidtime-io/solidtime.git
synced 2026-08-18 13:12:16 +01:00
Fixed import lock
This commit is contained in:
committed by
Constantin Graf
parent
29929467f6
commit
62270382dc
@@ -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');
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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',
|
||||||
]);
|
]);
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
Reference in New Issue
Block a user