0

I have a situation where I am making an FTP connection collecting data, looping through it registering new or finding previous enquiries using firstOrCreate. This is ran on a scheduler every 10mins and I am using Laravel Horizon to manage queues.

The problem is when I am running the scheduler I am sending out one enquiry with the correct data and one with the wrong data (there is only meant to be one email).

public static function zooplaLeads()
{
    $all_files = Storage::disk('sftp')->files('outgoing');

    foreach ($all_files as $key => $file_path) {
        $file = Storage::disk('sftp')->get($file_path);
        $parsed_file = simplexml_load_string($file);
        $lead = $parsed_file->ZooplaLead;

        if ($lead->TypeOfEnquiry == 'organise_viewing' || $lead->TypeOfEnquiry == 'request_property_details' || $lead->TypeOfEnquiry == 'looking_to_rent') {
            $user = User::where('email', (string)$lead->FromEmail)->first();

            $user_id = null;

            if ($user) {
                $user_id = $user->id;
            };

            $portal_enquiry = PortalEnquiry::firstOrCreate(
                ['listing_id' => (string)$lead->SourceListingId, 'email' => (string)$lead->FromEmail],
                [
                  'listing_id' => (string)$lead->SourceListingId,
                  'email' => (string)$lead->from_address,
                  'name' => (string)$lead->FirstName . ' ' . (string)$lead->LastName,
                  'contact_number' => (string)$lead->Phone,
                  'portal' => 'Zoopla',
                  'user_id' => $user_id
                ]
            );

            Mail::to($portal_enquiry->email)->send(new NewPortalEnquiryTenant($portal_enquiry->load('listing.property')));

            Storage::disk('sftp')->delete($file_path);
            \Log::emergency('New Zoopla lead: ' . $portal_enquiry->id);
        };
    }
    return count($all_files);
}

I have found a small workaround using eager loading and failing if the data is not correct but far from ideal.

As you can see from the image below there are two jobs created with different ID's. The 220 is correct and the 29 is incorrect.

When I log what is being looped through it is only showing one item (220).

I also haven't been able to recreate the error using a command so it seems (although not confirmed) to only happen with the scheduler.

Does anyone know why this is happening?

enter image description here

James Parsons
  • 895
  • 5
  • 18
  • 36
  • Are you using supervisor? If so have you tried to restart it? – IlGala Dec 13 '19 at 10:58
  • I am @IlGala and have tried restarting from Forge. – James Parsons Dec 13 '19 at 11:01
  • Another thing i don't understand... We're are you checking if `NewPortalEnquiryTenant` email should be sent or not? – IlGala Dec 13 '19 at 11:28
  • I have updated with the whole static function, as you can see it is part of a foreach loop so every result returned should be sent. I have checked using a log that the array count is only one. – James Parsons Dec 13 '19 at 11:35
  • If the `NewPortalEnquiryTenant` is not enqueing the e-mail, you don't see the second log because an `Exception` is thrown during the execution... But, I'm sorry... I still don't understand... If the e-mail should be sent since the `if` statement passes, why are you making it fail? – IlGala Dec 13 '19 at 11:52
  • The second email should not exist, there should only be one email sent but for some reason, a second model with an ID of 29 is being found and trying to be sent. At which point this second model is being created I can't work out. The reason is it failing is because it doesnt have the correct relations. – James Parsons Dec 13 '19 at 12:40

1 Answers1

0

I found out I had a staging environment running with the same scheduling. Meaning the same data was pulled at the same time just with different relations and causing the second email. Very stupid mistake!

James Parsons
  • 895
  • 5
  • 18
  • 36