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?