D
D
d999992021-04-10 13:45:40
Laravel
d99999, 2021-04-10 13:45:40

Why doesn't dispatch work in Lumen?

Service
In the service I create an event

dispatch(new ProcessWorkableRequest($firstCandidate, $newJob, $jobSearchCandidateDTO))->onQueue(getenv('RABBITMQ_QUEUE'));

// Lumen doesn't even enter this section of the code. This is clear from the value in the database. otherwise the status would change
class ProcessWorkableRequest extends Job
{
    private $candidate;
    private $workableService;
    private $stopListCompaniesService;
    private $contractorsService;
    private $jobService;
    private $currentJob;
    private $jobSearchCandidateDTO;

    public function __construct(Candidate $candidate, JobDB $currentJob, JobSearchCandidateDTO $jobSearchCandidateDTO)
    {
        $this->onQueue(getenv('RABBITMQ_QUEUE'));
        $this->candidate = $candidate;
        $this->stopListCompaniesService = app()->make(StopListCompaniesServiceInterface::class);
        $this->contractorsService = app()->make(ContractorsServiceInterface::class);
        $this->workableService = app()->make(WorkableServiceInterface::class);
        $this->jobService = app()->make(JobServiceInterface::class);
        $this->currentJob = $currentJob;
        $this->jobSearchCandidateDTO = $jobSearchCandidateDTO;
    }

    public function handle()
    {
            $workableCandidate = $this->workableService->getDataByUrl($this->currentJob->url);

            if (!$workableCandidate) {
                return [
                    'does_not_exist' => [
                        'label' => 'User does not exist in workable.com',
                        'value' => ''
                    ]
                ];
            }

            $this->currentJob->status = JobDB::STATUSES['IN_PROGRESS'];
            $this->currentJob->save();

            //When we search by candidate name it can be
            $profileUrls = [];
            if (count($this->jobSearchCandidateDTO->candidates) > 1) {
                foreach ($this->jobSearchCandidateDTO->candidates as $candidate) {
                    $profileUrls[] = $candidate->profile_url;
                }
                array_shift($profileUrls);
            }

            $candidate = (new CandidateTransformer)->transform($workableCandidate->candidate);

            $contractor = !empty($candidate->fullname) ? $this->contractorsService->search($candidate->fullname) : [
                'does_not_exist' => [
                    'label' => 'User does not exist',
                    'value' => true
                ]
            ];

            $response = array_merge(
                $candidate,
                $contractor,
                [
                    'search_by' => !empty($candidateFromWorkable) ? $this->jobSearchCandidateDTO->searchedByField : implode(',', CandidateService::SEARCH_BY),
                    'other_candidates_using_current_search_by' => $profileUrls,
                    'is_company_from_stop_list' => $this->stopListCompaniesService->checkIsCompanyInStopList((string)$this->jobSearchCandidateDTO->currentCompany)
                ]
            );

            $this->currentJob->response = \json_encode($response);
            $this->currentJob->status = JobDB::STATUSES['DONE'];
            $this->currentJob->save();
    }
}

job
<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class Job implements ShouldQueue
{
    use InteractsWithQueue, Queueable, SerializesModels;
}

queue.php
return [
    'connections' => [
        'rabbitmq' => [
            'driver' => 'rabbitmq',
            'queue' => env('RABBITMQ_QUEUE', 'default'),
            'connection' => PhpAmqpLib\Connection\AMQPLazyConnection::class,
            'hosts' => [
                [
                    'host' => env('RABBITMQ_HOST', 'wl-extension-rabbitmq'),
                    'port' => env('RABBITMQ_PORT', 5772),
                    'user' => env('RABBITMQ_USER', 'wl_user'),
                    'password' => env('RABBITMQ_PASSWORD', 'jh7k56t23h2op'),
                    'vhost' => env('RABBITMQ_VHOST', '/'),
                ],
            ],
            'options' => [
                'ssl_options' => [
                    'cafile' => env('RABBITMQ_SSL_CAFILE', null),
                    'local_cert' => env('RABBITMQ_SSL_LOCALCERT', null),
                    'local_key' => env('RABBITMQ_SSL_LOCALKEY', null),
                    'verify_peer' => env('RABBITMQ_SSL_VERIFY_PEER', true),
                    'passphrase' => env('RABBITMQ_SSL_PASSPHRASE', null),
                ],
                'queue' => [
                    'job' => VladimirYuldashev\LaravelQueueRabbitMQ\Queue\Jobs\RabbitMQJob::class,
                ],
            ],
            'worker' => env('RABBITMQ_WORKER', 'default'),
        ],
        'redis' => [
            'driver' => 'redis',
            'connection' => 'default',
            'queue' => 'workable',
            'retry_after' => 10,
            'block_for' => 5,
        ],
    ]
];

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
d99999, 2021-04-13
@d99999

in app.php added
$app->register(VladimirYuldashev\LaravelQueueRabbitMQ\LaravelQueueRabbitMQServiceProvider::class);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question