Export users 500k+ lines, seed, supervisord setup

This commit is contained in:
Ilya Rogozhin
2026-06-09 18:52:21 +02:00
parent 299ab41b7d
commit 792e4d7f28
15 changed files with 469 additions and 223 deletions
@@ -0,0 +1,53 @@
<?php
namespace App\Http\Controllers;
use App\Models\User;
use App\Jobs\UsersExport;
use Illuminate\Support\Facades\Cache;
class UserExportController extends Controller
{
public function status()
{
$status = Cache::remember('export_status', now()->addMinutes(2), function () {
return 'standby';
});
$lastExportFile = Cache::get('last_export_file');
return [
'export_status' => $status,
'last_export_file' => $lastExportFile
];
}
public function counter()
{
$currentCount = Cache::remember('users_count', now()->addMinutes(2), function () {
return User::count();
});
return [
'count' => $currentCount,
];
}
public function export()
{
$status = cache('export_status');
if ($status === 'progress') {
return [
'export_status' => $status,
];
}
Cache::forever('export_status', 'progress');
UsersExport::dispatch();
return [
'export_status' => 'started',
];
}
}
+63
View File
@@ -0,0 +1,63 @@
<?php
namespace App\Jobs;
use App\Models\User;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\File;
use Throwable;
class UsersExport implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Execute the job.
*/
public function handle(): void
{
try {
$exportPath = public_path('exports');
if (!File::isDirectory($exportPath)) {
File::makeDirectory($exportPath, 0755, true);
}
$oldFiles = File::glob($exportPath . '/users_export_*.csv');
foreach ($oldFiles as $file) {
File::delete($file);
}
$filename = 'users_export_' . now()->format('Y-m-d_H-i-s') . '.csv';
$filePath = $exportPath . '/' . $filename;
$handle = fopen($filePath, 'w');
fputcsv($handle, ['Имя', 'Фамилия', 'Телефон', 'E-mail']);
foreach (User::select('name', 'surname', 'phone_number', 'email')->lazy() as $user) {
fputcsv($handle, [
$user->name,
$user->surname,
$user->phone_number,
$user->email,
]);
}
fclose($handle);
Cache::forever('export_status', 'standby');
Cache::forever('last_export_file', 'exports/' . $filename);
} catch (Throwable $e) {
Cache::forever('export_status', 'failed');
report($e);
throw $e;
}
}
}