From 5300b6dd4c292f8b85e27f0db00a301222efc66c Mon Sep 17 00:00:00 2001 From: fomichev Date: Fri, 6 Dec 2024 12:21:21 +0300 Subject: [PATCH] =?utf8?q?=D0=90=D0=B2=D1=82=D0=BE=D1=81=D1=82=D0=B0=D1=80?= =?utf8?q?=D1=82=20=D0=BE=D1=87=D0=B5=D1=80=D0=B5=D0=B4=D0=B5=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- docker-compose.yml | 24 +++++------ docker/php/Dockerfile | 43 +++++++++++++++++-- .../php/conf/supervisor-conf.d/yii-queue.conf | 11 +++++ docker/php/conf/supervisord.conf | 11 +++++ docker/php/conf/wait-for-rabbitmq.sh | 9 ++++ erp24/config/console.php | 5 ++- erp24/config/web.php | 4 +- erp24/controllers/TgController.php | 7 ++- erp24/jobs/SendTelegramMessageJob.php | 27 ++++++++++-- erp24/views/tg/send-message.php | 2 +- 10 files changed, 118 insertions(+), 25 deletions(-) create mode 100644 docker/php/conf/supervisor-conf.d/yii-queue.conf create mode 100644 docker/php/conf/supervisord.conf create mode 100644 docker/php/conf/wait-for-rabbitmq.sh diff --git a/docker-compose.yml b/docker-compose.yml index 08de255c..5f79ffea 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -54,6 +54,18 @@ services: - ./docker/nginx_media/conf:/etc/nginx environment: TZ: Europe/Moscow + rabbitmq-yii_erp24: + image: rabbitmq:3-management + restart: always + ports: + - "5672:5672" # Порт для AMQP-протокола + - "15672:15672" # Порт для панели управления RabbitMQ + environment: + RABBITMQ_DEFAULT_USER: admin # Логин для доступа + RABBITMQ_DEFAULT_PASS: admin # Пароль для доступа + TZ: Europe/Moscow + volumes: + - rabbitmq_data:/var/lib/rabbitmq # Сохранение данных RabbitMQ php-yii_erp24: build: ./docker/php restart: always @@ -120,18 +132,6 @@ services: TZ: Europe/Moscow command: npm run dev - rabbitmq-yii_erp24: - image: rabbitmq:3-management - restart: always - ports: - - "5672:5672" # Порт для AMQP-протокола - - "15672:15672" # Порт для панели управления RabbitMQ - environment: - RABBITMQ_DEFAULT_USER: admin # Логин для доступа - RABBITMQ_DEFAULT_PASS: admin # Пароль для доступа - TZ: Europe/Moscow - volumes: - - rabbitmq_data:/var/lib/rabbitmq # Сохранение данных RabbitMQ volumes: mysqldata: diff --git a/docker/php/Dockerfile b/docker/php/Dockerfile index 1215d6f1..22dbf8b1 100644 --- a/docker/php/Dockerfile +++ b/docker/php/Dockerfile @@ -15,15 +15,52 @@ RUN apk --no-cache --update --repository http://dl-cdn.alpinelinux.org/alpine/v$ autoconf dpkg-dev dpkg file g++ gcc libc-dev make pkgconf re2c postgresql-dev RUN docker-php-ext-install pdo_pgsql +RUN docker-php-ext-install bcmath sockets +RUN docker-php-ext-enable bcmath sockets + + #add composer RUN wget https://getcomposer.org/installer \ && php installer \ && mv composer.phar /usr/local/bin/composer \ && rm installer -#add xdebug -RUN apk add --no-cache $PHPIZE_DEPS \ +# Установка зависимостей для компиляции и Xdebug +RUN apk add --no-cache --update --virtual .build-deps \ + $PHPIZE_DEPS \ + autoconf \ + dpkg-dev \ + dpkg \ + file \ + g++ \ + gcc \ + libc-dev \ + make \ + pkgconf \ + re2c \ + postgresql-dev \ && pecl install xdebug-3.1.6 \ && docker-php-ext-enable xdebug \ - && apk del $PHPIZE_DEPS + && apk del .build-deps + +# Установка Supervisor +#RUN apk add --no-cache supervisor + +# Создаем каталог для логов Supervisor +#RUN mkdir -p /var/log/supervisor + +# Копирование скрипта ожидания RabbitMQ +#COPY /conf/wait-for-rabbitmq.sh /usr/local/bin/wait-for-rabbitmq +#RUN chmod +x /usr/local/bin/wait-for-rabbitmq + +# Копирование конфигурации Supervisor +#COPY /conf/supervisord.conf /etc/supervisor/supervisord.conf +#COPY /conf/supervisor-conf.d/ /etc/supervisor/conf.d/ + + +# Set working directory WORKDIR /www + +# Установка CMD для запуска Supervisor через скрипт ожидания +#CMD ["/usr/local/bin/wait-for-rabbitmq", "supervisord", "-c", "/etc/supervisor/supervisord.conf"] +#CMD ["/usr/local/bin/wait-for-rabbitmq", "php", "/www/yii", "queue/listen"] \ No newline at end of file diff --git a/docker/php/conf/supervisor-conf.d/yii-queue.conf b/docker/php/conf/supervisor-conf.d/yii-queue.conf new file mode 100644 index 00000000..5c3195eb --- /dev/null +++ b/docker/php/conf/supervisor-conf.d/yii-queue.conf @@ -0,0 +1,11 @@ +[program:yii-queue] +command=php /www/yii queue/listen +directory=/www +autostart=true +autorestart=true +stderr_logfile=/var/log/supervisor/yii-queue.err.log +stdout_logfile=/var/log/supervisor/yii-queue.out.log +numprocs=1 +user=www-data +stopasgroup=true +killasgroup=true \ No newline at end of file diff --git a/docker/php/conf/supervisord.conf b/docker/php/conf/supervisord.conf new file mode 100644 index 00000000..5f59ff3a --- /dev/null +++ b/docker/php/conf/supervisord.conf @@ -0,0 +1,11 @@ +[supervisord] +nodaemon=true +logfile=/var/log/supervisor/supervisord.log +logfile_maxbytes=50MB +logfile_backups=10 +loglevel=info +pidfile=/var/run/supervisord.pid +childlogdir=/var/log/supervisor + +[include] +files = /etc/supervisor/conf.d/*.conf \ No newline at end of file diff --git a/docker/php/conf/wait-for-rabbitmq.sh b/docker/php/conf/wait-for-rabbitmq.sh new file mode 100644 index 00000000..7ef901c0 --- /dev/null +++ b/docker/php/conf/wait-for-rabbitmq.sh @@ -0,0 +1,9 @@ +#!/bin/bash +echo "Waiting for RabbitMQ to start..." +until nc -z rabbitmq-yii_erp24 5672; do + echo "RabbitMQ is unavailable - sleeping" + sleep 2 +done + +echo "RabbitMQ is up - executing command" +exec "$@" \ No newline at end of file diff --git a/erp24/config/console.php b/erp24/config/console.php index a4874e0a..2d59e228 100755 --- a/erp24/config/console.php +++ b/erp24/config/console.php @@ -43,9 +43,10 @@ $config = [ 'queue' => [ 'class' => Queue::class, 'dsn' => 'amqp://admin:admin@rabbitmq-yii_erp24:5672', - 'queueName' => 'queue-name', + 'queueName' => 'telegram-queue', 'as log' => \yii\queue\LogBehavior::class, - + 'ttr' => 300, // Время для выполнения задания + 'attempts' => 3, // Количество попыток ], 'cache' => [ 'class' => 'yii\caching\FileCache', diff --git a/erp24/config/web.php b/erp24/config/web.php index 29422923..b1736d9a 100644 --- a/erp24/config/web.php +++ b/erp24/config/web.php @@ -42,8 +42,10 @@ $config = [ 'queue' => [ 'class' => Queue::class, 'dsn' => 'amqp://admin:admin@rabbitmq-yii_erp24:5672', - 'queueName' => 'queue-name', + 'queueName' => 'telegram-queue', 'as log' => \yii\queue\LogBehavior::class, + 'ttr' => 300, // Время для выполнения задания + 'attempts' => 3, // Количество попыток ], 'cache' => [ diff --git a/erp24/controllers/TgController.php b/erp24/controllers/TgController.php index 7cf91f35..a7385135 100644 --- a/erp24/controllers/TgController.php +++ b/erp24/controllers/TgController.php @@ -21,8 +21,11 @@ class TgController extends Controller // Массив с ID чатов (в будущем можно заменить на запрос из базы данных) $chatIds = [ - '730432579', // Chat ID 1 - '6207259989', // Chat ID 2 + '730432579', + '6207259989', + '337084327', + '923226593', + '5489795686', ]; if ($request->isPost) { diff --git a/erp24/jobs/SendTelegramMessageJob.php b/erp24/jobs/SendTelegramMessageJob.php index 9ada0fed..57c59517 100644 --- a/erp24/jobs/SendTelegramMessageJob.php +++ b/erp24/jobs/SendTelegramMessageJob.php @@ -3,16 +3,35 @@ namespace app\jobs; +use yii\queue\JobInterface; use yii_app\services\TelegramService; -class SendTelegramMessageJob extends \yii\base\BaseObject implements \yii\queue\JobInterface +class SendTelegramMessageJob extends \yii\base\BaseObject implements JobInterface { public $chatId; public $message; + public static $messagesSent = 0; + public static $lastResetTime; + public function execute($queue) { - $telegramService = new TelegramService(); - $telegramService->sendMessageToTelegramClient($this->chatId, $this->message); + if (!self::$lastResetTime || (microtime(true) - self::$lastResetTime) > 1) { + self::$lastResetTime = microtime(true); + self::$messagesSent = 0; + } + + if (self::$messagesSent >= 30) { + $delay = 1 - (microtime(true) - self::$lastResetTime); + if ($delay > 0) { + usleep($delay * 1e6); // Спим оставшееся время + } + self::$lastResetTime = microtime(true); + self::$messagesSent = 0; + } + + TelegramService::sendMessageToTelegramClient($this->chatId, $this->message); + + self::$messagesSent++; } -} \ No newline at end of file +} diff --git a/erp24/views/tg/send-message.php b/erp24/views/tg/send-message.php index ee40a97b..d832a588 100644 --- a/erp24/views/tg/send-message.php +++ b/erp24/views/tg/send-message.php @@ -5,7 +5,7 @@ use yii\widgets\ActiveForm; $this->title = 'Отправить сообщение в Telegram'; ?> -
+

title) ?>

session->hasFlash('success')): ?> -- 2.39.5