Export users 500k+ lines, seed, supervisord setup
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user