Browse Source

add queue and crontab

stanley-king 4 years ago
parent
commit
0918a4feb1

+ 0 - 15
conf/crontab/debug-root

@@ -1,15 +0,0 @@
-# do daily/weekly/monthly maintenance
-# min	hour	day	month	weekday	command
-*/15	*	*	*	*	run-parts /etc/periodic/15min
-0	*	*	*	*	run-parts /etc/periodic/hourly
-0	2	*	*	*	run-parts /etc/periodic/daily
-0	3	*	*	6	run-parts /etc/periodic/weekly
-0	5	1	*	*	run-parts /etc/periodic/monthly
-0   3   *   *   *   php /var/www/html/crontab/index.php date index
-0   9   *   *   *   php /var/www/html/crontab/index.php date nine_clock
-0   10  *   *   *   php /var/www/html/crontab/index.php date ten_clock
-#*    *   *   *   *   php /var/www/html/crontab/index.php minutes index
-0   10-20/1 *   *   *   php /var/www/html/crontab/index.php hour index
-0   3   1   */1 *   php /var/www/html/crontab/index.php month index
-*    *   *   *   *   echo "test" >> /var/test.log
-

+ 3 - 4
conf/crontab/docker-start

@@ -2,9 +2,8 @@
 
 crond
 
-time=$(date "+%Y%m%d")
-if [ ! -f "/var/www/html/xyzshop/data/log/${time}-crontab.log" ]; then
-    touch "/var/www/html/xyzshop/data/log/${time}-crontab.log"
+if [ ! -f "/var/www/html/crontab/fortail.txt" ]; then
+    touch "/var/www/html/crontab/fortail.txt"
 fi
 
-tail -f "/var/www/html/xyzshop/data/log/${time}-crontab.log"
+tail -f "/var/www/html/crontab/fortail.txt"

+ 2 - 13
conf/crontab/root

@@ -1,15 +1,4 @@
 # do daily/weekly/monthly maintenance
 # min	hour	day	month	weekday	command
-*/15	*	*	*	*	run-parts /etc/periodic/15min
-0	*	*	*	*	run-parts /etc/periodic/hourly
-0	2	*	*	*	run-parts /etc/periodic/daily
-0	3	*	*	6	run-parts /etc/periodic/weekly
-0	5	1	*	*	run-parts /etc/periodic/monthly
-0   3   *   *   *   php /var/www/html/crontab/index.php date index
-0   9   *   *   *   php /var/www/html/crontab/index.php date nine_clock
-0   10  *   *   *   php /var/www/html/crontab/index.php date ten_clock
-*    *   *   *   *   php /var/www/html/crontab/index.php minutes index
-0   10-20/1 *   *   *   php /var/www/html/crontab/index.php hour index
-0   3   1   */1 *   php /var/www/html/crontab/index.php month index
-*    *   *   *   *   echo "test" >> /var/test.log
-
+*/15	*	*	*	*	php /var/www/html/crontab/index.php
+*    *   *   *   *   echo "test" >> /var/test.log

+ 1 - 1
conf/nginx/nginx-debug-http.conf

@@ -58,7 +58,7 @@ http
 	        add_header Cache-Control no-store;
 	        set  $folder_name /var/www/html/public;
             root           $folder_name;
-            fastcgi_pass   devrfpm:9000;
+            fastcgi_pass   accfpm:9000;
             fastcgi_index  index.php;
             fastcgi_split_path_info ^(.+\.php)(.*)$;
             fastcgi_param PATH_INFO $fastcgi_path_info;

+ 0 - 96
conf/nginx/nginx-debug-https.conf

@@ -1,96 +0,0 @@
-
-user nginx;
-worker_processes  1;
-error_log   /var/error.log  info;
-#pid        logs/nginx.pid;
-worker_rlimit_nofile 1024;
-
-events {
-    worker_connections  1024;
-}
-
-http 
-{
-    include       mime.types;
-    default_type  application/octet-stream;
-    fastcgi_buffer_size 32k;
-    fastcgi_buffers 8 32k;
-    underscores_in_headers on;
-    client_max_body_size 1024M;
-
-    sendfile            on;
-    keepalive_timeout   65;
-
-    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
-                      '$status $body_bytes_sent "$http_referer" '
-                      '"$http_user_agent" "$http_x_forwarded_for"';
-    server
-    {
-        listen       443 ssl;
-        set  $folder_name /var/www/html;
-        server_name www.xyzshops.cn;
-        root $folder_name;
-
-        ssl_certificate      cert/www.xyzshops.cn.pem;
-        ssl_certificate_key  cert/www.xyzshops.cn.key;
-
-        ssl_session_cache    shared:SSL:1m;
-        ssl_session_timeout  5m;
-
-        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;  #使用此加密套件。
-        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;   #使用该协议进行配置。
-        ssl_prefer_server_ciphers on;
-
-        index index.html index.php;
-    	client_max_body_size 100m;
-    	proxy_connect_timeout 500s;
-        proxy_read_timeout 500s;
-        proxy_send_timeout 500s;
-        fastcgi_connect_timeout 75;
-        fastcgi_read_timeout 600;
-        fastcgi_send_timeout 600;
-
-        charset utf-8;
-
-        location /logs {
-            deny all;
-            return 403;
-        }
-
-        location /data/log {
-            deny all;
-            return 403;
-        }
-        location / {
-            index  index.html index.htm index.php;
-        }
-
-        location /mshop {
-            root $folder_name;
-            autoindex on;
-            index  index.html index.htm index.php;
-	        try_files $uri $uri/ /mshop/index.html;
-        }
-
-        location ~ /mobile/[/\w]+\.php$ {
-            root           $folder_name;
-            fastcgi_pass   mobilesrv:9100;
-            fastcgi_index  index.php;
-            fastcgi_param  SCRIPT_FILENAME  $folder_name$fastcgi_script_name;
-            include fastcgi_params;
-        }
-
-        location ~ /mobile/[/\w]+\.html$ {
-            try_files $uri $uri/ /mobile/index.html;
-        }
-
-        location ~ \.php$ {
-	        add_header Cache-Control no-store;
-            root           $folder_name;
-            fastcgi_pass   fpmsrv:9000;
-            fastcgi_index  index.php;
-            fastcgi_param  SCRIPT_FILENAME  $folder_name$fastcgi_script_name;
-            include        fastcgi_params;
-        }
-    }
-}

+ 0 - 117
conf/nginx/nginx.conf.default

@@ -1,117 +0,0 @@
-
-#user  nobody;
-worker_processes  1;
-
-#error_log  logs/error.log;
-#error_log  logs/error.log  notice;
-#error_log  logs/error.log  info;
-
-#pid        logs/nginx.pid;
-
-
-events {
-    worker_connections  1024;
-}
-
-
-http {
-    include       mime.types;
-    default_type  application/octet-stream;
-
-    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
-    #                  '$status $body_bytes_sent "$http_referer" '
-    #                  '"$http_user_agent" "$http_x_forwarded_for"';
-
-    #access_log  logs/access.log  main;
-
-    sendfile        on;
-    #tcp_nopush     on;
-
-    #keepalive_timeout  0;
-    keepalive_timeout  65;
-
-    #gzip  on;
-
-    server {
-        listen       80;
-        server_name  localhost;
-
-        #charset koi8-r;
-
-        #access_log  logs/host.access.log  main;
-
-        location / {
-            root   html;
-            index  index.html index.htm;
-        }
-
-        #error_page  404              /404.html;
-
-        # redirect server error pages to the static page /50x.html
-        #
-        error_page   500 502 503 504  /50x.html;
-        location = /50x.html {
-            root   html;
-        }
-
-        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
-        #
-        #location ~ \.php$ {
-        #    proxy_pass   http://127.0.0.1;
-        #}
-
-        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
-        #
-        #location ~ \.php$ {
-        #    root           html;
-        #    fastcgi_pass   127.0.0.1:9000;
-        #    fastcgi_index  index.php;
-        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
-        #    include        fastcgi_params;
-        #}
-
-        # deny access to .htaccess files, if Apache's document root
-        # concurs with nginx's one
-        #
-        #location ~ /\.ht {
-        #    deny  all;
-        #}
-    }
-
-
-    # another virtual host using mix of IP-, name-, and port-based configuration
-    #
-    #server {
-    #    listen       8000;
-    #    listen       somename:8080;
-    #    server_name  somename  alias  another.alias;
-
-    #    location / {
-    #        root   html;
-    #        index  index.html index.htm;
-    #    }
-    #}
-
-
-    # HTTPS server
-    #
-    #server {
-    #    listen       443 ssl;
-    #    server_name  localhost;
-
-    #    ssl_certificate      cert.pem;
-    #    ssl_certificate_key  cert.key;
-
-    #    ssl_session_cache    shared:SSL:1m;
-    #    ssl_session_timeout  5m;
-
-    #    ssl_ciphers  HIGH:!aNULL:!MD5;
-    #    ssl_prefer_server_ciphers  on;
-
-    #    location / {
-    #        root   html;
-    #        index  index.html index.htm;
-    #    }
-    #}
-
-}

+ 2 - 2
conf/php-fpm/php.ini

@@ -1323,8 +1323,8 @@ bcmath.scale = 0
 [Session]
 ; Handler used to store/retrieve data.
 ; http://php.net/session.save-handler
-session.save_handler = files
-session.save_path = "/tmp"
+session.save_handler = redis
+session.save_path = "tcp://accredis:6379"
 
 ;session.save_handler = files
 

+ 2 - 22
conf/queue/docker-queue-start

@@ -1,24 +1,4 @@
 #!/bin/sh
-set -e
-
-<<<<<<< HEAD
-php /var/www/html/xyzshop/queue/index.php queue index
-
-time=$(date "+%Y%m%d")
-
-if [ ! -f "/var/www/html/xyzshop/data/log/${time}-queue.log" ]; then
-    touch "/var/www/html/xyzshop/data/log/${time}-queue.log"
-fi
 
-tail -f "/var/www/html/xyzshop/data/log/${time}-queue.log"
-=======
-php /var/www/html/queue/index.php queue index
-
-time=$(date "+%Y%m%d")
-
-if [ ! -f "/var/www/html/data/log/${time}-queue.log" ]; then
-    touch "/var/www/html/data/log/${time}-queue.log"
-fi
-
-tail -f "/var/www/html/data/log/${time}-queue.log"
->>>>>>> 228203e29f1a9f93eda3c1d8badd5fc640808e8c
+set -e
+php /var/www/html/queue/index.php

+ 3 - 0
crontab/config.ini.php

@@ -0,0 +1,3 @@
+<?php
+
+require_once(BASE_PATH . '/config.mac.php');

+ 6 - 0
crontab/config.mac.php

@@ -0,0 +1,6 @@
+<?php
+
+$config['net_queue']['name'] = 'net_access_queue';
+$config['net_queue']['host'] = 'host.docker.internal';
+$config['net_queue']['port'] = 6380;
+

+ 1 - 0
crontab/config.prod.php

@@ -0,0 +1 @@
+<?php

+ 1 - 0
crontab/config.test.php

@@ -0,0 +1 @@
+<?php

+ 0 - 0
crontab/fortail.txt


+ 12 - 0
crontab/index.php

@@ -0,0 +1,12 @@
+<?php
+
+// [ 应用入口文件 ]
+namespace think;
+
+// 加载基础文件
+require __DIR__ . '/../thinkphp/base.php';
+
+define('BASE_ROOT_PATH',str_replace('/crontab','',dirname(__FILE__)));
+define('BASE_PATH',BASE_ROOT_PATH . '/crontab');
+
+require_once(BASE_PATH . '/config.ini.php');

+ 32 - 11
docker-compose-mac.yml

@@ -1,7 +1,7 @@
 version: "3.7"
 
 services:
-  devrnginx:
+  accnginx:
     image: nginx:alpine
     ports:
       - "8088:8088"
@@ -10,11 +10,11 @@ services:
       - $PWD/conf/nginx/nginx-debug-http.conf:/etc/nginx/nginx.conf:ro
       - $PWD:/var/www/html
     links:
-      - devrfpm
-    container_name: "devr-nginx"
+      - accfpm
+    container_name: "acc-nginx"
     command: [nginx,'-g','daemon off;']
 
-  devrfpm:
+  accfpm:
     image: php-fpm:alpine
     volumes:
       - $PWD/conf/etc/localtime:/etc/localtime:ro
@@ -22,24 +22,45 @@ services:
       - $PWD/conf/php-fpm/php-debug.ini:/usr/local/etc/php/php.ini
       - $PWD/conf/php-fpm/php-fpm.conf:/usr/local/etc/php-fpm.conf
       - $PWD/conf/php-fpm/docker-php-fpm-start:/usr/local/bin/docker-php-fpm-start
-    container_name: "devr-web"
+    container_name: "acc-web"
     command: [docker-php-fpm-start]
     depends_on:
-      - devrredis
+      - accredis
 
-  devrphp:
+  accphp:
     image: php-zts-debug:7.3.18
     volumes:
       - $PWD/conf/etc/localtime:/etc/localtime:ro
       - $PWD/conf/php/php-debug.ini:/usr/local/etc/php/php.ini
       - $PWD:/var/www/html
     links:
-      - devrredis
-    container_name: "devr-php"
+      - accredis
+    container_name: "acc-php"
 
-  devrredis:
+  accredis:
     image: redis:alpine
     volumes:
       - $PWD/conf/etc/localtime:/etc/localtime:ro
-    container_name: "devr-redis"
+    container_name: "acc-redis"
     command: [ "redis-server"]
+
+  accqueue:
+    image: php-zts-debug:7.3.18
+    volumes:
+      - $PWD/conf/etc/localtime:/etc/localtime:ro
+      - $PWD:/var/www/html
+      - $PWD/conf/php/php-debug.ini:/usr/local/etc/php/php.ini
+      - $PWD/conf/queue/docker-queue-start:/usr/local/bin/docker-queue-start
+    container_name: "acc-queue"
+    command: [docker-queue-start]
+
+  acccrontab:
+    image: php-zts-debug:7.3.18
+    volumes:
+        - $PWD/conf/etc/localtime:/etc/localtime:ro
+        - $PWD:/var/www/html
+        - $PWD/conf/php/php-debug.ini:/usr/local/etc/php/php.ini
+        - $PWD/conf/crontab/root:/var/spool/cron/crontabs/root
+        - $PWD/conf/crontab/docker-start:/usr/local/bin/docker-start
+    container_name: "acc-crontab"
+    command: [docker-start]

+ 11 - 11
docker-compose-test.yml

@@ -1,7 +1,7 @@
 version: "3.7"
 
 services:
-  devrnginx:
+  accnginx:
     image: nginx:alpine
     ports:
       - "8088:8088"
@@ -10,11 +10,11 @@ services:
       - $PWD/conf/nginx/nginx-debug-http.conf:/etc/nginx/nginx.conf:ro
       - $PWD:/var/www/html
     links:
-      - devrfpm
-    container_name: "devr-nginx"
+      - accfpm
+    container_name: "acc-nginx"
     command: [nginx,'-g','daemon off;']
 
-  devrfpm:
+  accfpm:
     image: php-fpm:alpine
     volumes:
       - $PWD/conf/etc/localtime:/etc/localtime:ro
@@ -22,24 +22,24 @@ services:
       - $PWD/conf/php-fpm/php-debug.ini:/usr/local/etc/php/php.ini
       - $PWD/conf/php-fpm/php-fpm.conf:/usr/local/etc/php-fpm.conf
       - $PWD/conf/php-fpm/docker-php-fpm-start:/usr/local/bin/docker-php-fpm-start
-    container_name: "devr-web"
+    container_name: "acc-web"
     command: [docker-php-fpm-start]
     depends_on:
-      - devrredis
+      - accredis
 
-  devrphp:
+  accphp:
     image: php-zts-debug:7.3.18
     volumes:
       - $PWD/conf/etc/localtime:/etc/localtime:ro
       - $PWD/conf/php/php-debug.ini:/usr/local/etc/php/php.ini
       - $PWD:/var/www/html
     links:
-      - devrredis
-    container_name: "devr-php"
+      - accredis
+    container_name: "acc-php"
 
-  devrredis:
+  accredis:
     image: redis:alpine
     volumes:
       - $PWD/conf/etc/localtime:/etc/localtime:ro
-    container_name: "devr-redis"
+    container_name: "acc-redis"
     command: [ "redis-server"]

+ 3 - 0
queue/config.ini.php

@@ -0,0 +1,3 @@
+<?php
+
+require_once(BASE_PATH . '/config.mac.php');

+ 6 - 0
queue/config.mac.php

@@ -0,0 +1,6 @@
+<?php
+
+$config['net_queue']['name'] = 'net_access_queue';
+$config['net_queue']['host'] = 'host.docker.internal';
+$config['net_queue']['port'] = 6380;
+

+ 1 - 0
queue/config.prod.php

@@ -0,0 +1 @@
+<?php

+ 1 - 0
queue/config.test.php

@@ -0,0 +1 @@
+<?php

+ 0 - 26
queue/daemon.php

@@ -1,26 +0,0 @@
-<?php
-
-class Daemon{
-
-    protected $handler = null;
-
-    public function init(){
-        $this->handler = new \Redis();
-        $this->handler->connect('39.97.239.116', 6379);
-        $this->handler->auth('huaxiashangmeng');
-        $this->handler->select(1);
-    }
-
-    public function btoc($listname){
-        $result = $this->handler->BRPOP($listname,0);
-        if($result){
-
-        }
-    }
-}
-
-$daemon = new Daemon();
-$daemon->init();
-while (true){
-
-}

+ 0 - 0
queue/fortail.txt


+ 20 - 0
queue/index.php

@@ -0,0 +1,20 @@
+<?php
+
+// [ 应用入口文件 ]
+namespace think;
+
+use queuehandler;
+
+// 加载基础文件
+require __DIR__ . '/../thinkphp/base.php';
+
+define('BASE_ROOT_PATH',str_replace('/queue','',dirname(__FILE__)));
+define('BASE_PATH',BASE_ROOT_PATH . '/queue');
+
+require_once(BASE_PATH . '/config.ini.php');
+require_once(BASE_ROOT_PATH . '/extend/queue.php');
+require_once(BASE_ROOT_PATH . '/extend/queue.logic.php');
+require_once(BASE_PATH . '/queuehandler.php');
+
+$handler = new queuehandler();
+$handler->run();

+ 75 - 0
queue/queuehandler.php

@@ -0,0 +1,75 @@
+<?php
+declare(strict_types=0);
+
+//此行代码会导致bug
+//ini_set('default_socket_timeout', -1);
+
+class queuehandler
+{
+    private $_stop = false;
+
+    public function run()
+    {
+        global $config;
+        $queue_name = $config['net_queue']['name'];
+        $host = $config['net_queue']['host'];
+        $port = $config['net_queue']['port'];
+
+        if (ob_get_level()) ob_end_clean();
+
+        pcntl_signal(SIGINT,  [$this,'sig_handler']);
+        pcntl_signal(SIGHUP,  [$this,'sig_handler']);
+        pcntl_signal(SIGQUIT, [$this,'sig_handler']);
+        pcntl_signal(SIGTERM, [$this,'sig_handler']);
+
+        $logic_queue = new queue_logic();
+        $worker = new QueueServer($queue_name,$host,$port);
+        $queues = $worker->scan();
+
+        while (true)
+        {
+            pcntl_signal_dispatch();
+            try
+            {
+                if ($this->_stop) break;
+
+                $content = $worker->pop($queues, 1);
+                if(is_array($content))
+                {
+                    $method = key($content);
+                    $arg = current($content);
+
+                    $argstr = json_encode($arg,JSON_UNESCAPED_UNICODE);
+                    Log::record("method={$method} args={$argstr}",'debug');
+
+                    $result = $logic_queue->$method($arg);
+                    if (!$result['state']) {
+                        Log::record("{$method} run error: {$result['msg']}",'debug');
+                    }
+                }
+            }
+            catch (Exception $e)
+            {
+                $err = $e->getMessage();
+                $code = $e->getCode();
+                Log::record("QueueDB pop err: code={$code} err={$err}",'debug');
+                break;
+            }
+        }
+    }
+
+    private function sig_handler($signo)
+    {
+        Log::record("queue quit at sig_handler.",Log::DEBUG);
+        switch($signo) {
+            case SIGINT:
+            case SIGHUP:
+            case SIGQUIT:
+            case SIGTERM:
+                $this->_stop = true;
+                break;
+            default:
+                break;
+        }
+    }
+}

+ 1 - 1
thinkphp/library/think/cache/driver/Redis.php

@@ -23,7 +23,7 @@ use think\cache\Driver;
 class Redis extends Driver
 {
     protected $options = [
-        'host'       => '127.0.0.1',
+        'host'       => 'accredis',
         'port'       => 6379,
         'password'   => '',
         'select'     => 0,