Browse Source

提交数据按小时执行的队列

lvhao 3 months ago
parent
commit
83043db40c
2 changed files with 149 additions and 0 deletions
  1. 137 0
      core/CoreApp/controllers/Zzhjobs.php
  2. 12 0
      core/CoreApp/models/Model_zzhjobs.php

+ 137 - 0
core/CoreApp/controllers/Zzhjobs.php

@@ -0,0 +1,137 @@
+<?php
+defined('BASEPATH') or exit('No direct script access allowed');
+/**
+ * 此类是为了解决erp没有队列的问题,为了解决这个问题,先写一下这个类,避免后期写那么多定时任务类   这里只能解决每间隔多少时间执行一会的
+ */
+class Zzhjobs extends Start_Controller
+{
+    private $serect_str = "erphjob";
+    private $user_agent = 'Xcly250422/Jobh (Erp)';
+    private $api = "ly202504220727";
+    private $ip = ['127.0.0.1','47.105.156.18'];
+    public function __construct()
+    {
+        parent::__construct();
+        $this->load->_model("Model_zzhjobs","zzhjobs");
+        $this->load->_model("Model_logic_tools",'logic_tools');
+    }
+    public function _remap($arg, $arg_array)
+    {
+        $ip = $_SERVER['REMOTE_ADDR'];
+       
+        if(!in_array($ip,$this->ip)){
+            exit("Unauthorized access");
+        }
+        $user_agent = empty($_SERVER['HTTP_USER_AGENT'])?"":$_SERVER['HTTP_USER_AGENT'];
+        $token = empty($_SERVER['HTTP_X_AUTH_TOKEN'])?"":$_SERVER['HTTP_X_AUTH_TOKEN'];
+        $time =  $this->input->get('time',true);
+        $api = $this->input->get('api',true);
+        $this->checkAuth($user_agent,$token,$time,$api);
+        if ($arg == 'jobs') //调出单
+        {
+            $this->_jobs();
+        } else {
+            $this->_index();
+        }
+    }
+    /**
+     * 校验是否有权限可以执行
+     * $user_agent   自定义的请求客户端名称
+     * $token        自定义的清华客户端的token
+     * $time         请求的时间
+     * 
+     */
+    private function checkAuth($user_agent,$token,$time,$api){
+        if($api != $this->api){
+            exit("Access denied due to insufficient permissions");
+        }
+        $check_str = date("Ymd")."¥_".$this->serect_str."_¥".$time;
+        if(empty($user_agent)){
+            exit('No direct script access allowed');
+        }
+        if($user_agent != $this->user_agent){
+            exit('Illegal request');
+        }
+        if(empty($token)){
+            exit('No access permission');
+        }
+        
+        $sercet_str = md5($check_str);
+        
+        if($sercet_str != $token){
+            exit("Request permission is illegal");
+        }
+       
+        
+    }
+    private function _index() {}
+     //一般每一分钟都执行的 每分钟就执行10条   
+     //每五分钟 就执行100条
+     
+     //其他的就不限制了
+    private function _jobs() {
+        $m = (int)date("i");//当前分钟
+        // 1分钟的队列
+        $list_1 =  $this->zzjobs->find_all('do_interval = 1 and status = 0');
+        // 5分钟的队列
+        $list_5 = [];
+        if($m%5 == 0){
+            $list_5 = $this->zzjobs->find_all('do_interval = 5 and status = 0');
+        }
+        // 10分钟的队列
+        $list_10 = [];
+        if($m%10 == 0){
+            $list_10 = $this->zzjobs->find_all('do_interval = 10 and status = 0');
+        }
+        // 30分钟的队列
+        $list_30 = [];
+        if($m%30 == 0){
+            $list_30 = $this->zzjobs->find_all('do_interval = 10 and status = 0');
+        }
+        $list = array_merge($list_1,$list_5,$list_10,$list_30);
+        $do_list_ids = array_column($list,'id');
+       
+        if(empty($do_list_ids)){
+            exit("No jobs to do");
+        }
+        $this->db->query("update crowd_zzjobs set status = 1 where id  in (".implode(",",$do_list_ids).")");
+        foreach($list as $k=>$v){
+            $this->tasksAssign($v);
+        }
+    }
+    private function tasksAssign($info){
+        switch($info['quque']){
+            case '1m':
+                $this->zzjobs->save([
+                    'result'=>$this->logic_tools->ret_json(1,"1m队列已经执行"),
+                    'last_time'=>time()
+                ],$info['id']);
+                break;
+            case '5m':
+                $this->zzjobs->save([
+                    'result'=>$this->logic_tools->ret_json(1,"5m队列已经执行"),
+                    'last_time'=>time()
+                ],$info['id']);
+                break;
+            case '10m':
+                $this->zzjobs->save([
+                    'result'=>$this->logic_tools->ret_json(1,"10m队列已经执行"),
+                    'last_time'=>time()
+                ],$info['id']);
+                break;
+            case '30m':
+                $this->zzjobs->save([
+                    'result'=>$this->logic_tools->ret_json(1,"30m队列已经执行"),
+                    'last_time'=>time()
+                ],$info['id']);
+                break;
+            default:
+                    $this->zzjobs->save([
+                        'result'=>$this->logic_tools->ret_json(-1,"没有对应发方法执行任务"),
+                        'last_time'=>time()
+                    ],$info['id']);
+                break;
+        }
+
+    } 
+}

+ 12 - 0
core/CoreApp/models/Model_zzhjobs.php

@@ -0,0 +1,12 @@
+<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 
+class Model_zzhjobs extends Lin_Model 
+{
+	function __construct(){
+		parent::__construct();
+		$this->load->database();
+		$this->table = 'zzhjobs';
+		$this->load_table('zzhjobs');
+
+	}
+	
+}