Sunday, April 15, 2018

Lumen Queue Job cannot connect to database - development

Lumen Queue Job cannot connect to database

I’m new to Lumen and writing some codes that will enqueue jobs using Lumen queue. (Lumen version 5.6)
Models will be used in the job.
My model works like a charm in the controller.
However, my models in the job cannot access to database when I try to query something.

Can somebody can help me because I think Lumen Job can access to any Model just like controllers.

Controller

use IlluminateHttpRequest; 
use AppJobssomeJob;
use AppHttpControllersController;

class myController extends BaseController {
    public function enqueueJob(Request $request){
        $json = [
            'some_payload' => 'test payload'
        ];
        Queue::push(new someJob($json));
    }
}

Model

namespace AppModels;

use IlluminateDatabaseEloquentModel;

class MyModel extends Model{
    protected $table = 'table_name';
    protected $connection = 'connection_name';

}

app/Jobs/Job.php

namespace AppJobs;

use IlluminateBusQueueable;
use IlluminateQueueSerializesModels;
use IlluminateQueueInteractsWithQueue;
use IlluminateContractsQueueShouldQueue;

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

app/Jobs/someJob.php

namespace AppJobs;

use AppModelsMyModel;

class someJob extends Job
{

    protected $json;
    public function __construct($json){
        $this->json = $json;
    }

    public function handle(){
        $json = json_decode($this->json);
        MyModel::find($json->id);  // Error here!
    }
}

It throws the following errors

[2018-04-15 08:51:39] production.ERROR: PDOException: PDO::__construct(): php_network_getaddresses: getaddrinfo failed: nodename nor servname provided, or not known in /path/to/project/vendor/illuminate/database/Connectors/Connector.php:68
#0 /path/to/project/vendor/illuminate/database/Connectors/Connector.php(68): PDO->__construct('mysql:host=mysq...', NULL, NULL, Array)
#1 /path/to/project/vendor/illuminate/database/Connectors/Connector.php(44): IlluminateDatabaseConnectorsConnector->createPdoConnection('mysql:host=mysq...', NULL, NULL, Array)
#2 /path/to/project/vendor/illuminate/database/Connectors/MySqlConnector.php(24): IlluminateDatabaseConnectorsConnector->createConnection('mysql:host=mysq...', Array, Array)
#3 /path/to/project/vendor/illuminate/database/Connectors/ConnectionFactory.php(183): IlluminateDatabaseConnectorsMySqlConnector->connect(Array)
#5 /path/to/project/vendor/illuminate/database/Connection.php(915): call_user_func(Object(Closure))
#6 /path/to/project/vendor/illuminate/database/Connection.php(452): IlluminateDatabaseConnection->getPdo()
#7 /path/to/project/vendor/illuminate/database/Connection.php(657): IlluminateDatabaseConnection->IlluminateDatabase{closure}('insert into `fa...', Array)
#8 /path/to/project/vendor/illuminate/database/Connection.php(624): IlluminateDatabaseConnection->runQueryCallback('insert into `fa...', Array, Object(Closure))
#9 /path/to/project/vendor/illuminate/database/Connection.php(459): IlluminateDatabaseConnection->run('insert into `fa...', Array, Object(Closure))
#10 /path/to/project/vendor/illuminate/database/Connection.php(411): IlluminateDatabaseConnection->statement('insert into `fa...', Array)
#11 /path/to/project/vendor/illuminate/database/Query/Processors/Processor.php(32): IlluminateDatabaseConnection->insert('insert into `fa...', Array)
#12 /path/to/project/vendor/illuminate/database/Query/Builder.php(2204): IlluminateDatabaseQueryProcessorsProcessor->processInsertGetId(Object(IlluminateDatabaseQueryBuilder), 'insert into `fa...', Array, NULL)
#13 /path/to/project/vendor/illuminate/queue/Failed/DatabaseFailedJobProvider.php(62): IlluminateDatabaseQueryBuilder->insertGetId(Array)
#14 /path/to/project/vendor/illuminate/queue/Console/WorkCommand.php(188): IlluminateQueueFailedDatabaseFailedJobProvider->log('redis', 'default', '{"displayName":...', 'ErrorException:...')
#15 /path/to/project/vendor/illuminate/queue/Console/WorkCommand.php(137): IlluminateQueueConsoleWorkCommand->logFailedJob(Object(IlluminateQueueEventsJobFailed))
#16 /path/to/project/vendor/illuminate/events/Dispatcher.php(360): IlluminateQueueConsoleWorkCommand->IlluminateQueueConsole{closure}(Object(IlluminateQueueEventsJobFailed))
#17 /path/to/project/vendor/illuminate/events/Dispatcher.php(209): IlluminateEventsDispatcher->IlluminateEvents{closure}('IlluminateQueu...', Array)
#18 /path/to/project/vendor/illuminate/queue/FailingJob.php(36): IlluminateEventsDispatcher->dispatch('IlluminateQueu...')
#19 /path/to/project/vendor/illuminate/queue/Worker.php(435): IlluminateQueueFailingJob::handle('redis', Object(IlluminateQueueJobsRedisJob), Object(ErrorException))
#20 /path/to/project/vendor/illuminate/queue/Worker.php(421): IlluminateQueueWorker->failJob('redis', Object(IlluminateQueueJobsRedisJob), Object(ErrorException))
#21 /path/to/project/vendor/illuminate/queue/Worker.php(353): IlluminateQueueWorker->markJobAsFailedIfWillExceedMaxAttempts('redis', Object(IlluminateQueueJobsRedisJob), 1, Object(ErrorException))
#22 /path/to/project/vendor/illuminate/queue/Worker.php(326): IlluminateQueueWorker->handleJobException('redis', Object(IlluminateQueueJobsRedisJob), Object(IlluminateQueueWorkerOptions), Object(ErrorException))
#23 /path/to/project/vendor/illuminate/queue/Worker.php(272): IlluminateQueueWorker->process('redis', Object(IlluminateQueueJobsRedisJob), Object(IlluminateQueueWorkerOptions))
#24 /path/to/project/vendor/illuminate/queue/Worker.php(229): IlluminateQueueWorker->runJob(Object(IlluminateQueueJobsRedisJob), 'redis', Object(IlluminateQueueWorkerOptions))
#25 /path/to/project/vendor/illuminate/queue/Console/WorkCommand.php(101): IlluminateQueueWorker->runNextJob('redis', 'default', Object(IlluminateQueueWorkerOptions))
#26 /path/to/project/vendor/illuminate/queue/Console/WorkCommand.php(85): IlluminateQueueConsoleWorkCommand->runWorker('redis', 'default')
#28 /path/to/project/vendor/illuminate/container/BoundMethod.php(29): call_user_func_array(Array, Array)

Thanks for your help!



from Laravel Questions and Answers https://laravelquestions.com/php/lumen-queue-job-cannot-connect-to-database/
via Lzo Media

No comments:

Post a Comment