Handle an exception that occurred while the job was running.

@param  string  $connectionName
@param  \Illuminate\Contracts\Queue\Job  $job
@param  \Illuminate\Queue\WorkerOptions  $options
@param  \Exception  $e
@return void

@throws \Exception
760157
protected function handleJobException($connectionName, $job, WorkerOptions $options, $e)
    {
        try {
            // First, we will go ahead and mark the job as failed if it will exceed the maximum
            // attempts it is allowed to run the next time we process it. If so we will just
            // go ahead and mark it as failed now so we do not have to release this again.
            if (! $job->hasFailed()) {
                $this->markJobAsFailedIfWillExceedMaxAttempts(
                    $connectionName, $job, (int) $options->maxTries, $e
                );
            }

            $this->raiseExceptionOccurredJobEvent(
                $connectionName, $job, $e
            );
        } finally {
            // If we catch an exception, we will attempt to release the job back onto the queue
            // so it is not lost entirely. This'll let the job be retried at a later time by
            // another listener (or this same one). We will re-throw this exception after.
            if (! $job->isDeleted() && ! $job->isReleased() && ! $job->hasFailed()) {
                $job->release(
                    method_exists($job, 'delaySeconds') && ! is_null($job->delaySeconds())
                                ? $job->delaySeconds()
                                : $options->delay
                );
            }
        }

        throw $e;
    }
843580
public function process($connector, Job $job, $maxTries = 0, $delay = 0)
    {
        try {
            $this->event->trigger(new JobProcessing($connector, $job));

            $this->markJobAsFailedIfAlreadyExceedsMaxAttempts(
                $connector, $job, (int) $maxTries
            );

            $job->fire();

            $this->event->trigger(new JobProcessed($connector, $job));
        } catch (Exception | Throwable $e) {
            try {
                if (!$job->hasFailed()) {
                    $this->markJobAsFailedIfWillExceedMaxAttempts($connector, $job, (int) $maxTries, $e);
                }

                $this->event->trigger(new JobExceptionOccurred($connector, $job, $e));
            } finally {
                if (!$job->isDeleted() && !$job->isReleased() && !$job->hasFailed()) {
                    $job->release($delay);
                }
            }

            throw $e;
        }
    }
461376
protected function runJob($job, $connectorName, $maxTries, $delay)
    {
        try {
            $this->process($connectorName, $job, $maxTries, $delay);
        } catch (Exception | Throwable $e) {
            $this->handle->report($e);
        }
    }
772423
protected function handleException(Throwable $e, array $record)
    {
        if (!$this->exceptionHandler) {
            throw $e;
        }

        call_user_func($this->exceptionHandler, $e, $record);
    }
356062
protected function handleJobException($job, QueueMessage $message, Structure $options, $e)
    {
        if (!$job instanceof JobInterface) {
            $job = new NullJob();
        }

        $this->logger->error(
            sprintf(
                'Job [%s] (%s) failed: %s - Class: %s',
                $job->getName(),
                $message->getId(),
                $e->getMessage(),
                get_class($job)
            )
        );

        if (method_exists($job, 'failed')) {
            $job->failed($e);
        }

        $maxTries = (int) $options->get('tries', 5);

        // Delete and log error if reach max attempts.
        if ($maxTries !== 0 && $maxTries <= $message->getAttempts()) {
            $this->manager->delete($message);
            $this->logger->error(
                sprintf(
                    'Max attempts exceeded. Job: %s (%s) - Class: %s',
                    $job->getName(),
                    $message->getId(),
                    get_class($job)
                )
            );
        }

        $this->dispatcher->triggerEvent(
            'onWorkerJobFailure',
            [
                'worker' => $this,
                'exception' => $e,
                'job' => $job,
                'message' => $message,
            ]
        );
    }
1348506
public function fail($exception = null)
    {
        if ($this->job) {
            FailingJob::handle($this->job->getConnectionName(), $this->job, $exception);
        }
    }
666768
protected function raiseBeforeJobEvent($connection, Job $job)
    {
        if (isset($this->events)) {
            $this->events->dispatch('nova.queue.processing', array($connection, $job));
        }
    }
1333551
public function runNextJob($workCommand, $connectionName, $queue, WorkerOptions $options)
    {
        $job = $this->getNextJob(
            $this->manager->connection($connectionName), $queue
        );

        // If we're able to pull a job off of the stack, we will process it and then return
        // from this method. If there is no job on the queue, we will "sleep" the worker
        // for the specified number of seconds, then keep processing jobs after sleep.
        if ($job) {
            return $this->runJob($workCommand, $job, $connectionName, $options);
        }

        $this->sleep($options->sleep);
    }
1310088
protected function raiseFailedJobEvent( $connection, Job $job, $failedId )
	{
		$data = json_decode( $job->getRawBody(), true );
		Framework::hooks()->trigger( 'queue.job.failed', compact( 'connection', 'job', 'data', 'failedId' ) );
	}
296996
public function daemon($connectionName, $queue, WorkerOptions $options)
    {
        $lastRestart = $this->getTimestampOfLastQueueRestart();

        $this->registerSignalListener();

        while (true) {
            $this->registerTimeoutHandler($options);

            if ($this->daemonShouldRun($options)) {
                $this->runNextJob($connectionName, $queue, $options);
            } else {
                $this->sleep($options->sleep);
            }

            $this->callSignalHandler();

            if ($this->memoryExceeded($options->memory) ||
                $this->queueShouldRestart($lastRestart) ||
                $this->queueShouldExit()
            ) {
                $this->stop();
            }
        }
    }
505048
public function onIOError(Connection $conn, \Exception $ex)
    {
        $this->onPublishResponse($conn, "IO_ERROR", $ex);

        try {
            $this->lookup->removeConnection($conn);
            $conn->tryClose();
        } catch (\Throwable $ignore) {
            sys_echo("nsq({$conn->getAddr()}) onIOError exception: {$ex->getMessage()}");
        } catch (\Exception $ignore) {
            sys_echo("nsq({$conn->getAddr()}) onIOError exception: {$ex->getMessage()}");
        }
    }
179665
protected function raiseExceptionOccurredJobEvent($connectionName, $job, $e)
    {
        $this->events->dispatch(new Events\JobExceptionOccurred(
            $connectionName, $job, $e
        ));
    }
655076
protected function executeJob(Job $job): ?\Throwable
    {
        try {
            $this->jobExecutor->executeJob($job, $this->retryLimit);
        } catch (\Throwable $e) {
            return $e;
        }

        return null;
    }
924467
protected function handleModelNotFound(Job $job, $e)
    {
        $class = $job->resolveName();

        try {
            $shouldDelete = (new ReflectionClass($class))
                    ->getDefaultProperties()['deleteWhenMissingModels'] ?? false;
        } catch (Exception $e) {
            $shouldDelete = false;
        }

        if ($shouldDelete) {
            return $job->delete();
        }

        FailingJob::handle(
            $job->getConnectionName(), $job, $e
        );
    }
1372906
public function process($job, WorkerOptions $options)
    {

        try {
            $this->markJobAsFailedIfAlreadyExceedsMaxAttempts(
                $job,
                (int) $options->maxTries
            );
            $job->fire();
        } catch (Exception $e) {
            $this->handleJobException($job, $options, $e);
        }
    }
501960
protected function tryAgainIfCausedByLostConnection(Exception $e, $dsn, $username, $password, $options)
    {
        if ($this->causedByLostConnection($e)) {
            return $this->createPdoConnection($dsn, $username, $password, $options);
        }

        throw $e;
    }
1315085
public function process( $connection, Job $job, $maxTries = 0, $delay = 0 )
	{
		if ( $maxTries > 0 && $job->attempts() > $maxTries )
			return $this->logFailedJob( $connection, $job );

		try
		{
			$this->raiseBeforeJobEvent( $connection, $job );

			// First we will fire off the job. Once it is done we will see if it will be
			// automatically deleted after processing and if so we'll fire the delete
			// method on the job. Otherwise, we will just keep on running our jobs.
			$job->fire();

			$this->raiseAfterJobEvent( $connection, $job );

			return ['job' => $job, 'failed' => false];
		}
		catch ( Exception $e )
		{
			$this->handleJobException( $connection, $job, $delay, $e );
		}
		catch ( Throwable $e )
		{
			$this->handleJobException( $connection, $job, $delay, $e );
		}

		return null;
	}