[PROMOÇÃO] Assine com + 30% de desconto ANUAL MENSAL (últimas horas)
Johnny
Criador Johnny 28/11/2023

No Projeto que estou trabalhando a cada minuto preciso executar uma tarefa para atualizar o banco postgres.

para tanto entro no container:

johnny@g8765:~/projetos/bvbus-api$ sudo docker exec -it 5113fbc7a2b bash

e executo manualmente o comando abaixo: 

johnny@5113fbc7a2b6:/var/www$ php artisan schedule:work

Schedule worker started successfully.

[2023-11-28T16:09:02-04:00] Execution #1 output:

[2023-11-28T16:09:01-04:00] Running scheduled command: App\Jobs\BusLocation

...

o problema: 

A tarefa aparentimente executa normalmente, mas o banco de dados não atualiza.

Quando projeto estava fora dos container atualizava normalmente.

 

 

Criador Johnny 28/11/2023
services:
# image project
app:
build:
context: .
dockerfile: Dockerfile
restart: unless-stopped
working_dir: /var/www/
volumes:
- ./:/var/www
depends_on:
- redis
networks:
- laravel
 
 
# queue
queue:
image: especializati/laravel9-app
restart: unless-stopped
command: "php artisan queue:work"
volumes:
- ./:/var/www
depends_on:
- redis
- app
networks:
- laravel
 
Johnny
Criador Johnny 28/11/2023

env:

BROADCAST_DRIVER=log
 
CACHE_DRIVER=redis
QUEUE_CONNECTION=redis
SESSION_DRIVER=redis
SESSION_LIFETIME=120
Johnny
Manager Carlos Ferreira 28/11/2023

Olá, Johnny! Tudo bem?

O seu banco de dados também está em container?

Qual o resultado deste comando? php artisan schedule:list

Carlos Ferreira
Criador Johnny 28/11/2023

o banco está em container:

#db postgres
postgres:
image: postgres:latest
restart: unless-stopped
environment:
POSTGRES_PASSWORD: ${DB_PASSWORD}
POSTGRES_USER: ${DB_USERNAME}
POSTGRES_DB: ${DB_DATABASE}
ports:
- '5435:5432'
volumes:
- ./.docker/postgresql/pgdata:/var/lib/pgdata
networks:
- laravel

johnny@5113fbc7a2b6:/var/www$ php artisan schedule:list

+---------+-----------+----------------------+----------------------------+

| Command | Interval  | Description          | Next Due                   |

+---------+-----------+----------------------+----------------------------+

|         | * * * * * | App\Jobs\BusLocation | 2023-11-28 16:56:00 -04:00 |

+---------+-----------+----------------------+----------------------------+

Johnny
Manager Carlos Ferreira 28/11/2023

Qual o código desse job BusLocation?

Carlos Ferreira
Criador Johnny 28/11/2023
<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Http;


class BusLocation implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

public $timeout = 0;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct()
{
//
}

/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$json = Http::post('http://api', [
"key" => "key",
"user" => "user",
"pass" => "senha"
])->json();

if ($json['success'] === true) {
foreach ($json['data'] as $bus) {
$bus_location = \App\Models\BusLocation::where(['data->vehicleId' => $bus['vehicleId']])->first();

if(!empty($bus_location)) {
$bus_location->update(['data' => $bus]);
} else {
\App\Models\BusLocation::create(['data' => $bus]);
}
}
}
}
}
Johnny
Manager Carlos Ferreira 28/11/2023

Coloca alguns logs neste job, só pra saber os valores e onde está chegando. \Log::debug();

Carlos Ferreira
Criador Johnny 28/11/2023

johnny@d4348f02aa35:/var/www$ tail -f storage/logs/laravel.log

...

 

[2023-12-04 06:33:05] local.DEBUG: antes de executar a consulta  

[2023-12-04 06:33:07] local.DEBUG: esta executando a consulta  

 

[2023-12-04 06:33:07] local.ERROR: could not find driver (SQL: insert into "failed_jobs" ("uuid", "connection", "queue", "payload", "exception", "failed_at") values (0a009ba2-c8c1-4ecc-9bb1-e8af98b5c109, redis, default, {"uuid":"0a009za2-c8c1-4ecc-9bb1-e8af98b5c109","displayName":"App\\Jobs\\BusLocation","job":"Illuminate\\Queue\\CallQueuedHandler@call","maxTries":null,"maxExceptions":null,"failOnTimeout":false,"backoff":null,"timeout":0,"retryUntil":null,"data":{"commandName":"App\\Jobs\\BusLocation","command":"O:20:\"App\\Jobs\\BusLocation\":11:{s:7:\"timeout\";i:0;s:3:\"job\";N;s:10:\"connection\";N;s:5:\"queue\";N;s:15:\"chainConnection\";N;s:10:\"chainQueue\";N;s:19:\"chainCatchCallbacks\";N;s:5:\"delay\";N;s:11:\"afterCommit\";N;s:10:\"middleware\";a:0:{}s:7:\"chained\";a:0:{}}"},"id":"c1Yqetvyh23lWYeNirOzEqkpYCg07Alk","attempts":0}, PDOException: could not find driver in /var/www/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70

...

 

Next Illuminate\Database\QueryException: could not find driver (SQL: select * from "bus_locations" where ("data"->>'vehicleId' = 35) limit 1) in /var/www/vendor/laravel/framework/src/Illuminate/Database/Connection.php:712

...

 

[previous exception] [object] (PDOException(code: 0): could not find driver at /var/www/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70)

Johnny
Manager Carlos Ferreira 28/11/2023

Ah sim, é porque essa imagem não tem o drive do MySQL instalado, é uma imagem mais simples.

Crie o seu container de queue, mas essa forma:

queue:

    build:

        context: .

        dockerfile: Dockerfile

    restart: unless-stopped

    command: "php artisan queue:work"

    working_dir: /var/www/

    volumes:

        - ./:/var/www

    depends_on:

        - redis

    networks:

        - laravel

Carlos Ferreira
Criador Johnny 28/11/2023

no dockerfile:

# Install PHP extensions
RUN docker-php-ext-install pdo_mysql pdo_pgsql mbstring exif pcntl bcmath gd sockets
# Install and enable the PostgreSQL extension
RUN apt-get update && apt-get install -y libpq-dev && docker-php-ext-install pgsql pdo_pgsql
RUN docker-php-ext-enable pgsql

 

executei o docker-compose build, docker-compose down, docker-compose up -d, restaurei o backup.

mas não está atulizando o banco atraves da schedule! os erros não mudam!

 

Johnny
Criador Johnny 28/11/2023

johnny@d4348f02aa35:/var/www$ docker-php-ext-enable pdo_pgsql

 

warning: pdo_pgsql (pdo_pgsql) is already loaded!

 

Johnny
Manager Carlos Ferreira 28/11/2023

Deleta os containers antigos: docker compose down queue

Recria do zero: docker compose up -d --build queue

Carlos Ferreira
Sabe a Solução? Ajude a resolver!

Precisa estar logado para conseguir responder a este ticket!

Clique Aqui Para Entrar!