mirror of
https://github.com/solidtime-io/solidtime.git
synced 2026-06-15 05:22:44 +01:00
Lock import and increase timeout
This commit is contained in:
committed by
Constantin Graf
parent
5c6b32d5bb
commit
d4e71e7c2c
@@ -10,6 +10,7 @@ use App\Service\Import\Importers\ImporterProvider;
|
|||||||
use App\Service\Import\Importers\ImportException;
|
use App\Service\Import\Importers\ImportException;
|
||||||
use App\Service\Import\Importers\ReportDto;
|
use App\Service\Import\Importers\ReportDto;
|
||||||
use Illuminate\Support\Carbon;
|
use Illuminate\Support\Carbon;
|
||||||
|
use Illuminate\Support\Facades\Cache;
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
use Illuminate\Support\Facades\Storage;
|
use Illuminate\Support\Facades\Storage;
|
||||||
use Illuminate\Support\Str;
|
use Illuminate\Support\Str;
|
||||||
@@ -27,9 +28,16 @@ class ImportService
|
|||||||
Storage::disk(config('filesystems.default'))
|
Storage::disk(config('filesystems.default'))
|
||||||
->put('import/'.Carbon::now()->toDateString().'-'.$organization->getKey().'-'.Str::uuid(), $data);
|
->put('import/'.Carbon::now()->toDateString().'-'.$organization->getKey().'-'.Str::uuid(), $data);
|
||||||
|
|
||||||
DB::transaction(function () use (&$importer, &$data, &$timezone) {
|
$lock = Cache::lock('import:'.$organization->getKey(), config('octane.max_execution_time', 60) + 1);
|
||||||
$importer->importData($data, $timezone);
|
|
||||||
});
|
if ($lock->get()) {
|
||||||
|
DB::transaction(function () use (&$importer, &$data, &$timezone) {
|
||||||
|
$importer->importData($data, $timezone);
|
||||||
|
});
|
||||||
|
$lock->release();
|
||||||
|
} else {
|
||||||
|
throw new ImportException('Import is already in progress');
|
||||||
|
}
|
||||||
|
|
||||||
return $importer->getReport();
|
return $importer->getReport();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -221,7 +221,7 @@ return [
|
|||||||
|
|
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
'max_execution_time' => 45,
|
'max_execution_time' => 120,
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Custom swoole config
|
* Custom swoole config
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ services:
|
|||||||
- "host.docker.internal:host-gateway"
|
- "host.docker.internal:host-gateway"
|
||||||
- "storage.${NGINX_HOST_NAME}:${REVERSE_PROXY_IP:-10.100.100.10}"
|
- "storage.${NGINX_HOST_NAME}:${REVERSE_PROXY_IP:-10.100.100.10}"
|
||||||
environment:
|
environment:
|
||||||
|
SUPERVISOR_PHP_COMMAND: "/usr/bin/php -d variables_order=EGPCS /var/www/html/artisan octane:start --server=frankenphp --host=0.0.0.0 --admin-port=2019 --port=80"
|
||||||
XDG_CONFIG_HOME: /var/www/html/config
|
XDG_CONFIG_HOME: /var/www/html/config
|
||||||
XDG_DATA_HOME: /var/www/html/data
|
XDG_DATA_HOME: /var/www/html/data
|
||||||
WWWUSER: '${WWWUSER}'
|
WWWUSER: '${WWWUSER}'
|
||||||
|
|||||||
@@ -6,8 +6,10 @@ namespace Tests\Unit\Service\Import;
|
|||||||
|
|
||||||
use App\Models\Organization;
|
use App\Models\Organization;
|
||||||
use App\Service\Import\Importers\ImporterProvider;
|
use App\Service\Import\Importers\ImporterProvider;
|
||||||
|
use App\Service\Import\Importers\ImportException;
|
||||||
use App\Service\Import\ImportService;
|
use App\Service\Import\ImportService;
|
||||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||||
|
use Illuminate\Support\Facades\Cache;
|
||||||
use Illuminate\Support\Facades\Storage;
|
use Illuminate\Support\Facades\Storage;
|
||||||
use PHPUnit\Framework\Attributes\CoversClass;
|
use PHPUnit\Framework\Attributes\CoversClass;
|
||||||
use PHPUnit\Framework\Attributes\UsesClass;
|
use PHPUnit\Framework\Attributes\UsesClass;
|
||||||
@@ -40,4 +42,23 @@ class ImportServiceTest extends TestCase
|
|||||||
$this->assertSame(2, $report->projectsCreated);
|
$this->assertSame(2, $report->projectsCreated);
|
||||||
$this->assertSame(1, $report->clientsCreated);
|
$this->assertSame(1, $report->clientsCreated);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function test_import_throws_exception_if_import_is_already_in_progress(): void
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
Storage::fake(config('filesystems.default'));
|
||||||
|
$organization = Organization::factory()->create();
|
||||||
|
$timezone = 'Europe/Vienna';
|
||||||
|
$data = Storage::disk('testfiles')->get('toggl_time_entries_import_test_1.csv');
|
||||||
|
Cache::lock('import:'.$organization->getKey(), 10)->get();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
$importService = app(ImportService::class);
|
||||||
|
try {
|
||||||
|
$importService->import($organization, 'toggl_time_entries', $data, $timezone);
|
||||||
|
} catch (ImportException $e) {
|
||||||
|
// Assert
|
||||||
|
$this->assertSame('Import is already in progress', $e->getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user