laravel Job / Queue not been processed weird infinite loop

CIB picture CIB · Nov 30, 2017 · Viewed 7k times · Source

I'm trying to create a queue but it doesn't work when I run php artisan queue:work all I get in my terminal is

[2017-11-30 19:56:27] Processing: App\Jobs\ProcessCSV
[2017-11-30 19:56:27] Processing: App\Jobs\ProcessCSV
[2017-11-30 19:56:27] Processing: App\Jobs\ProcessCSV
[2017-11-30 19:56:27] Processing: App\Jobs\ProcessCSV
[2017-11-30 19:56:27] Processing: App\Jobs\ProcessCSV
[2017-11-30 19:56:27] Processing: App\Jobs\ProcessCSV
[2017-11-30 19:56:27] Processing: App\Jobs\ProcessCSV
[2017-11-30 19:56:27] Processing: App\Jobs\ProcessCSV
[2017-11-30 19:56:27] Processing: App\Jobs\ProcessCSV
[2017-11-30 19:56:27] Processing: App\Jobs\ProcessCSV

It's like an infinite loop. The id in my jobs table just goes up and up too. It does work on my laptop but not on my desktop which is very strange. I put in onto my devleopment server and it doesn't work on there either.

My code is below and any help would be appreciated.

Controller

public function upload(Request $request) {
        if($request->file('imported-file')) {

            $user = "[email protected]";

            $file = $request->file('imported-file')->store('uploads', 'public');
            $this->dispatch(new ProcessCSV($file, $user));

            Session::flash('success', 'Your file was uploaded successfully. We will email you once the locations have be imported.');
            return back();

        } else {

            Session::flash('error', 'Please select a file to upload!!!!');
            return back();

        }

    }

Job

public function handle()
    {

        $data = Excel::load($this->file, function($reader) {})->get();

        $apiKey = '';

        foreach($data as $row) {

            if(!empty($row['postcode'])) {

                $url = "https://maps.googleapis.com/maps/api/geocode/xml?address=".urlencode($row['postcode'])."&region=uk&key=";
                $tmp = file_get_contents($url);
                $xml = simplexml_load_string($tmp);

                // print_r($xml); exit;

                if((string)$xml->status == 'OK' && isset($xml->result[0])) {

                    if(isset($xml->result[0]->geometry->location->lat)) {
                        $lat = (string)$xml->result[0]->geometry->location->lat;
                    }

                    if(isset($xml->result[0]->geometry->location->lng)) {
                        $lng = (string)$xml->result[0]->geometry->location->lng;
                    }

                }

                Import::updateOrCreate(
                    [
                        'sitecode' => $row['sitecode']
                    ],
                    [
                        'sitecode' => $row['sitecode'],
                        'sitename' => $row['sitename'],
                        'address_1' => $row['address_1'],
                        'address_2' => $row['address_2'],
                        'address_town' => $row['address_town'],
                        'address_postcode' => $row['postcode'],
                        'charity' => $row['charity'],
                        'latitude' => $lat,
                        'longitude' => $lng,
                        'approved' => 1
                    ]
                );

            }

        }


    }

Answer

ZeroOne picture ZeroOne · Dec 1, 2017

instead of php artisan queue:work..you need to add option param --tries to avoid infinite looping.. it usually occurred if error happen in job. see laravel.log or failed-job table if using driver database. it will show the error

php artisan queue:work --tries=3