vendredi 27 juillet 2018

How to retry a job after x minutes?

I need to retry a job after 7 minutes after failed. I try with $this->release(7); but on sometimes the job is running more that 1 time.

<?php

namespace Froakie\Listeners;

use Froakie\Components\Locks\LocksFactory;
use Froakie\Components\CRM\CrmFactory;
use Froakie\Events\NewLeadDataIncoming;
use Froakie\Services\LeadsService;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;

/**
 * Class CreateLead
 *
 * @package Froakie\Listeners
 * @author Miguel Borges <miguel.borges@edirectinsure.com>
 */
class CreateOrUpdateLead implements ShouldQueue
{
    use InteractsWithQueue;

    const LOCKS_PREFIX = 'lead_event_lock_';

    /**
     * @var \Froakie\Services\LeadsService
     */
    protected $leadsService;

    /**
     * Create the event listener.
     *
     * @param \Froakie\Services\LeadsService $leadsService
     */
    public function __construct(LeadsService $leadsService)
    {
        $this->leadsService = $leadsService;
    }

    /**
     * Handle the event.
     *
     * @param \Froakie\Events\NewLeadDataIncoming $event
     * @throws \Exception
     */
    public function handle(NewLeadDataIncoming $event)
    {
        app('log')->debug('CreateOrUpdateLead listener has catch a NewLeadDataIncoming', ['event' => $event]);

        $lead = $this->leadsService->getLeadById($event->leadId);

        LocksFactory::getInstance()->getMutexAdapter(self::LOCKS_PREFIX . $lead->getId(), null, 60)
            ->synchronized(function () use ($lead, $event) {
                if (!$lead->isCreated()) {
                    $lead->setCreated(true)->save();

                    try {
                        $lead->crm_id = CrmFactory::getInstance()->getCRMLeadAdapter($lead->getCrm())
                            ->createLead($event->leadDto);
                        $lead->save();

                        app('log')->info("A new lead has been created in {$lead->getCrm()}", [
                            'reference' => $lead->getReference(),
                            'crm_id' => $lead->getCrmId()
                        ]);

                        return;
                    } catch (\Exception $exception) {
                        $lead->setCreated(false)->save();
                        throw $exception;
                    }
                }
            });

        if (null !== $lead->getCrmId()) {
            CrmFactory::getInstance()->getCRMLeadAdapter($lead->getCrm())
                ->updateLead($lead->getCrmId(), $event->leadDto);

            app('log')->info("A lead has been updated in {$lead->getCrm()}", [
                'reference' => $lead->getReference(),
                'crm_id' => $lead->getCrmId()
            ]);

            return;
        }

        $this->release(7);
    }
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire