]> gitweb.erp-flowers.ru Git - erp24_rep/yii-erp24/.git/commitdiff
Автостарт очередей
authorfomichev <vladimir.fomichev@erp-flowers.ru>
Fri, 6 Dec 2024 09:21:21 +0000 (12:21 +0300)
committerfomichev <vladimir.fomichev@erp-flowers.ru>
Fri, 6 Dec 2024 09:21:21 +0000 (12:21 +0300)
docker-compose.yml
docker/php/Dockerfile
docker/php/conf/supervisor-conf.d/yii-queue.conf [new file with mode: 0644]
docker/php/conf/supervisord.conf [new file with mode: 0644]
docker/php/conf/wait-for-rabbitmq.sh [new file with mode: 0644]
erp24/config/console.php
erp24/config/web.php
erp24/controllers/TgController.php
erp24/jobs/SendTelegramMessageJob.php
erp24/views/tg/send-message.php

index 08de255c84c72f52585889e414db217df14227ad..5f79ffea55756138af8ea10f55befbecc06bdc65 100644 (file)
@@ -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:
index 1215d6f1e64680f76b9de0677b0f34a0d0fac112..22dbf8b1d5ea7eb098e1ad777d599da0feddd8a2 100644 (file)
@@ -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 (file)
index 0000000..5c3195e
--- /dev/null
@@ -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 (file)
index 0000000..5f59ff3
--- /dev/null
@@ -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 (file)
index 0000000..7ef901c
--- /dev/null
@@ -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
index a4874e0adbcb4ad7d8b32cd5e3ec700f46b4a91a..2d59e228b49b6e6cdf60fbfae6df361d1501c1f9 100755 (executable)
@@ -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',
index 294229230528ca938fdf12713e2b295c1105f430..b1736d9a9dd6bdd1884962ee6ee82116d4449ae4 100644 (file)
@@ -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' => [
index 7cf91f35e433d5bf4f2fc519bd1134e158756520..a7385135f3da39c2fc37d5224fa86b17396a4970 100644 (file)
@@ -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) {
index 9ada0fed4dbe33b1fe350e3d84873e4c6486150a..57c5951729bd15f3743fd16ee277e64368a6641f 100644 (file)
@@ -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
+}
index ee40a97b3285abf1c441d02f4ddd4eb9c9701933..d832a588d383b618d7bdb9cca18f0ac7e75cde02 100644 (file)
@@ -5,7 +5,7 @@ use yii\widgets\ActiveForm;
 $this->title = 'Отправить сообщение в Telegram';
 ?>
 
-<div class="send-message-form">
+<div class="send-message-form p-4">
     <h1><?= Html::encode($this->title) ?></h1>
 
     <?php if (Yii::$app->session->hasFlash('success')): ?>