SpartnerNL / Laravel-Excel

🚀 Supercharged Excel exports and imports in Laravel
https://laravel-excel.com
MIT License
12.26k stars 1.91k forks source link

[Bug]: ReadChunk Job Still runs when all excel rows have been imported #4205

Open codenesiareborn opened 2 weeks ago

codenesiareborn commented 2 weeks ago

Is the bug applicable and reproducable to the latest version of the package and hasn't it been reported before?

What version of Laravel Excel are you using?

3.1.30

What version of Laravel are you using?

9.19

What version of PHP are you using?

8.2.0

Describe your issue

Maatwebsite\Excel\Jobs\ReadChunk still runs when all excel rows have been imported

Screenshot 2024-09-18 at 2 07 31 AM

How can the issue be reproduced?

`class BulkConnectExcelImport implements WithMultipleSheets, ShouldQueue, WithChunkReading {

public function sheets(): array
{
    return [
        'Daftar Daerah' => new DaftarDaerahSheet(),
        'Connect' => new ConnectSheet(),
    ];
}

public function registerEvents(): array
{
    return [
        // Triggered after a successful import
        AfterImport::class => function(AfterImport $event) {
            Log::info('selesai impor');
            session()->flash('success', 'Import completed successfully!');
        },

        // Triggered if the import fails
        ImportFailed::class => function(ImportFailed $event) {
            Log::info('tidak selesai impor');

            session()->flash('error', 'The import failed: ' . $event->getException()->getMessage());
        },
    ];
}

public function failed(\Exception $exception)
{
    // Handle failure (log error, notify user, etc.)
    session()->flash('error', 'Job failed: ' . $exception->getMessage());
}

public function chunkSize(): int
{
    return 500;
}

}`

What should be the expected behaviour?

the job should stop when all rows are successfully uploaded

POWRFULCOW89 commented 2 weeks ago

I'm facing this issue too in Laravel Excel 3.1.55, using Laravel 9.52 and PHP 8.3.8:

class TestImport implements ToArray, WithStartRow, SkipsEmptyRows, WithMultipleSheets, WithChunkReading, ShouldQueue
{
    public function __construct(private readonly User $user)
    {
    }

    public function array(array $array): void
    {
        foreach ($array as $index => $row) {
            ProcessExcelQuote::dispatch($row, $this->user, $index);
        }
    }

    public function sheets(): array
    {
        return [
            '0' => $this
        ];
    }

    public function startRow(): int
    {
        return 3;
    }

    public function chunkSize(): int
    {
        return 10;
    }
}

After reading a sheet with 3 rows it keeps on reading:


2024-09-17 18:14:54 Maatwebsite\Excel\Jobs\QueueImport ................................................................................................................................................................................................................................................. RUNNING
  2024-09-17 18:14:54 Maatwebsite\Excel\Jobs\QueueImport ........................................................................................................................................................................................................................................... 158.79ms DONE
  2024-09-17 18:14:54 Maatwebsite\Excel\Jobs\ReadChunk ................................................................................................................................................................................................................................................... RUNNING
  2024-09-17 18:14:55 Maatwebsite\Excel\Jobs\ReadChunk ............................................................................................................................................................................................................................................. 508.37ms DONE
  2024-09-17 18:14:55 App\Jobs\ProcessExcelQuote ......................................................................................................................................................................................................................................................... RUNNING
  2024-09-17 18:14:57 App\Jobs\ProcessExcelQuote ................................................................................................................................................................................................................................................. 2,091.42ms FAIL
  2024-09-17 18:14:57 App\Jobs\ProcessExcelQuote ......................................................................................................................................................................................................................................................... RUNNING
  2024-09-17 18:14:58 App\Jobs\ProcessExcelQuote ................................................................................................................................................................................................................................................. 1,349.21ms FAIL
  2024-09-17 18:14:58 App\Jobs\ProcessExcelQuote ......................................................................................................................................................................................................................................................... RUNNING
  2024-09-17 18:15:00 App\Jobs\ProcessExcelQuote ................................................................................................................................................................................................................................................. 1,696.43ms FAIL
  2024-09-17 18:15:00 Maatwebsite\Excel\Jobs\ReadChunk ................................................................................................................................................................................................................................................... RUNNING
  2024-09-17 18:15:01 Maatwebsite\Excel\Jobs\ReadChunk ............................................................................................................................................................................................................................................. 422.58ms DONE
  2024-09-17 18:15:01 Maatwebsite\Excel\Jobs\ReadChunk ................................................................................................................................................................................................................................................... RUNNING
  2024-09-17 18:15:01 Maatwebsite\Excel\Jobs\ReadChunk ............................................................................................................................................................................................................................................. 399.25ms DONE

I believe it might have something to do with having created the .xlsx file from Google Sheets, which seems to append a bunch of rows at the end of the sheet

codenesiareborn commented 2 weeks ago

I'm facing this issue too in Laravel Excel 3.1.55, using Laravel 9.52 and PHP 8.3.8:

class TestImport implements ToArray, WithStartRow, SkipsEmptyRows, WithMultipleSheets, WithChunkReading, ShouldQueue
{
    public function __construct(private readonly User $user)
    {
    }

    public function array(array $array): void
    {
        foreach ($array as $index => $row) {
            ProcessExcelQuote::dispatch($row, $this->user, $index);
        }
    }

    public function sheets(): array
    {
        return [
            '0' => $this
        ];
    }

    public function startRow(): int
    {
        return 3;
    }

    public function chunkSize(): int
    {
        return 10;
    }
}

After reading a sheet with 3 rows it keeps on reading:


2024-09-17 18:14:54 Maatwebsite\Excel\Jobs\QueueImport ................................................................................................................................................................................................................................................. RUNNING
  2024-09-17 18:14:54 Maatwebsite\Excel\Jobs\QueueImport ........................................................................................................................................................................................................................................... 158.79ms DONE
  2024-09-17 18:14:54 Maatwebsite\Excel\Jobs\ReadChunk ................................................................................................................................................................................................................................................... RUNNING
  2024-09-17 18:14:55 Maatwebsite\Excel\Jobs\ReadChunk ............................................................................................................................................................................................................................................. 508.37ms DONE
  2024-09-17 18:14:55 App\Jobs\ProcessExcelQuote ......................................................................................................................................................................................................................................................... RUNNING
  2024-09-17 18:14:57 App\Jobs\ProcessExcelQuote ................................................................................................................................................................................................................................................. 2,091.42ms FAIL
  2024-09-17 18:14:57 App\Jobs\ProcessExcelQuote ......................................................................................................................................................................................................................................................... RUNNING
  2024-09-17 18:14:58 App\Jobs\ProcessExcelQuote ................................................................................................................................................................................................................................................. 1,349.21ms FAIL
  2024-09-17 18:14:58 App\Jobs\ProcessExcelQuote ......................................................................................................................................................................................................................................................... RUNNING
  2024-09-17 18:15:00 App\Jobs\ProcessExcelQuote ................................................................................................................................................................................................................................................. 1,696.43ms FAIL
  2024-09-17 18:15:00 Maatwebsite\Excel\Jobs\ReadChunk ................................................................................................................................................................................................................................................... RUNNING
  2024-09-17 18:15:01 Maatwebsite\Excel\Jobs\ReadChunk ............................................................................................................................................................................................................................................. 422.58ms DONE
  2024-09-17 18:15:01 Maatwebsite\Excel\Jobs\ReadChunk ................................................................................................................................................................................................................................................... RUNNING
  2024-09-17 18:15:01 Maatwebsite\Excel\Jobs\ReadChunk ............................................................................................................................................................................................................................................. 399.25ms DONE

I believe it might have something to do with having created the .xlsx file from Google Sheets, which seems to append a bunch of rows at the end of the sheet

how can we skip the empty rows?

jeff1326 commented 1 week ago

I'm facing this issue too in Laravel Excel 3.1.55, using Laravel 11.20.0 and PHP 8.3.11

But it's on a job named ImportTextsTranslations

$import = new TranslationImport();
Excel::import($import, storage_path('app/' . $file));
class TranslationImport implements ToCollection, WithHeadingRow, WithMultipleSheets
{
    // In case of the file having multiple sheets, this method will be called multiple times.
    #[\Override]
    public function collection(Collection $rows)
    {
        // My customer code here
    }

    // Force to ignore other sheets that translator adds that crash the import
    #[\Override]
    public function sheets(): array
    {
        return [
            0 => $this,
        ];
    }
}

image

I got this problem only while importing multiples files. If i was importing them one by one, i was fine.