File: /home/xedaptot/ai.naniguide.com/app/Jobs/VerifySubscriber.php
<?php
namespace App\Jobs;
use Illuminate\Bus\Batchable;
use App\Library\Exception\NoCreditsLeft;
use App\Library\Exception\RateLimitExceeded;
use App\Library\Exception\VerificationTakesLongerThanNormal;
use App\Library\Exception\RateLimitReservedByAnotherFileSystem;
use App\Model\EmailVerificationServer;
use Exception;
use Closure;
use function App\Helpers\execute_with_limits;
class VerifySubscriber extends Base
{
use Batchable;
public $timeout = 300;
public $maxExceptions = 1; // This is required if retryUntil is used, otherwise, the default value is 255
public $failOnTimeout = true;
protected $server;
protected $subscriber;
protected $subscription;
protected $customer;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($subscriber, EmailVerificationServer $server, $subscription)
{
$this->subscriber = $subscriber;
$this->server = $server;
$this->subscription = $subscription;
$this->customer = $this->subscriber->mailList->customer;
}
/**
* Determine the time at which the job should timeout.
*
* @return \DateTime
*/
public function retryUntil()
{
return now()->addHours(72);
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
if ($this->batch()->cancelled()) {
return;
}
$this->customer->setUserDbConnection();
$this->doVerify();
}
public function doVerify(Closure $exceptionCallback = null)
{
try {
// Count related quota trackers
$rateTrackers = [
$this->server->getRateTracker()
];
// Credit limit tracker — null means no subscription OR plan doesn't
// grant the verify credit; either way skip the credit gate.
$creditTrackers = [];
if ($this->subscription !== null) {
$verifyTracker = app(\App\Services\Plans\Credits\CreditsService::class)
->trackerFor($this->subscription, \App\Services\Plans\Credits\CreditKey::VERIFY_CONTACT);
if ($verifyTracker !== null) {
$creditTrackers[] = $verifyTracker;
}
}
execute_with_limits($rateTrackers, $pool = null, $creditTrackers, function () {
$this->subscriber->verify($this->server);
});
} catch (RateLimitReservedByAnotherFileSystem $ex) {
$this->release(1);
} catch (VerificationTakesLongerThanNormal $ex) {
// Just ignore and return
// Warn user that there are certain subscribers that are skipped
// @important: silently quit leaving subscribers not verified
return;
} catch (RateLimitExceeded $ex) {
if (!is_null($exceptionCallback)) {
return $exceptionCallback($ex);
}
// Release the job, have it try again after 60 seconds and (hopefully) the quota limits will be lifted then as time goes by
$this->release(60);
} catch (Exception $ex) {
if (!is_null($exceptionCallback)) {
return $exceptionCallback($ex);
}
throw $ex;
}
}
}