HEX
Server: LiteSpeed
System: Linux s1049.use1.mysecurecloudhost.com 4.18.0-477.27.2.lve.el8.x86_64 #1 SMP Wed Oct 11 12:32:56 UTC 2023 x86_64
User: xedaptot (3356)
PHP: 8.3.31
Disabled: NONE
Upload Files
File: /home/xedaptot/ai.naniguide.com/app/Jobs/HandleDuplicateEmails.php
<?php

namespace App\Jobs;

use Illuminate\Bus\Batchable;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use App\Library\Contracts\CampaignInterface;
use App\Library\Traits\Trackable;
use App\Model\TrackingLog;

class HandleDuplicateEmails implements ShouldQueue
{
    use Trackable;
    use Batchable;
    use Dispatchable;
    use InteractsWithQueue;
    use Queueable;
    use SerializesModels;

    // @important: set the "retry_after" setting in config/queue.php to a value that is greater than $timeout;
    public $timeout = 86400; // need time to dispatch hundreds of jobs
    public $failOnTimeout = true;
    public $tries = 1;
    public $maxExceptions = 1;

    protected CampaignInterface $campaign;
    protected $customer;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct(CampaignInterface $campaign)
    {
        $this->campaign = $campaign;
        $this->customer = $this->campaign->customer;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $this->customer->setUserDbConnection();

        /*
        if ($this->batch()->cancelled()) {
            return;
        }
        */

        $duplicateTable = $this->campaign->getDuplicateTable();

        // get a random sending server
        // throw an exception if campaign does not have one
        $server = $this->campaign->pickSendingServer();

        $excluded = $this->campaign->subscribersToSend()->leftJoin($duplicateTable, function ($join) use ($duplicateTable) {
            $join->on("{$duplicateTable}.email", '=', 'subscribers.email');
        })->whereNotNull("{$duplicateTable}.email")->where("{$duplicateTable}.selected_id", '!=', \DB::raw('subscribers.id'))->get();

        foreach ($excluded as $subscriber) {
            $this->campaign->trackMessage(
                new \App\SendingServers\Drivers\SendResult(
                    runtimeMessageId: null,
                    status: \App\SendingServers\Drivers\DeliveryStatus::DUPLICATE,
                ),
                $subscriber,
                $server,
                $msgId = null,
                $trigger = null,
            );

            $this->campaign->logger()->info("Subscriber {$subscriber->email} duplicate, recorded in delivery log as 'duplicate'");
        }
    }
}