瀏覽代碼

优化队列

huangsanjia 1 年之前
父節點
當前提交
b267e18528
共有 2 個文件被更改,包括 17 次插入10 次删除
  1. 16 9
      application/api/controller/StaffSalary.php
  2. 1 1
      application/job/InsertDataJob.php

+ 16 - 9
application/api/controller/StaffSalary.php

@@ -7,6 +7,7 @@ use think\Db;
 use app\job\InsertDataJob;
 use think\Queue;
 use think\Cache;
+use think\cache\driver\Redis;
 /**
  * 员工计件工资核算
 */
@@ -38,6 +39,21 @@ class StaffSalary extends Api
         if (Request::instance()->isPost() == false){
             $this->error('非法请求');
         }
+        $options = [
+            'host'       => '127.0.0.1',
+            'port'       => 6379,
+            'password'   => '',
+            'select'     => 15,
+            'timeout'    => 0,
+            'expire'     => 0,
+            'persistent' => false,
+            'prefix'     => '',
+        ];
+        $redis = new Redis($options);
+        $queueKey =  $redis->handler()->llen('queues:default');
+        if ($queueKey){
+            $this->success('数据正在处理中,请等待...');
+        }
         $params = Request::instance()->param();
         if (!isset($params['start_date']) || !isset($params['end_date']) || empty($params['start_date']) || empty($params['end_date']) || !isset($params['date']) || empty($params['date']))
         {
@@ -479,19 +495,10 @@ class StaffSalary extends Api
             $data[$key]['法定天数'] = $params['days'];
             $data[$key]['sczl_type'] = trim($item['sczl_type']);
         }
-//        $num = round(count($data)/10000);
-//        for ($i = 0; $i <= $num; $i++){
-//            $start = $i * 10000;
-//            $res = array_slice($data,$start,10000);
-//            $job = new InsertDataJob($res); // 创建任务实例
-//            Queue::push($job,'','default'.$i); // 推送任务到队列
-//        }
 
         $job = new InsertDataJob($data); // 创建任务实例
         Queue::push($job,'','default'); // 推送任务到队列
         $this->success('数据正在处理中,请等待...');
-
-
     }
 
 }

+ 1 - 1
application/job/InsertDataJob.php

@@ -55,7 +55,7 @@ class InsertDataJob
         $res = Db::query($sql);
         if ($res !== false){
             // 获取队列的键名
-            $queueKey = 'default0';
+            $queueKey = 'default';
             // 删除队列
             Cache::store('redis')->handler()->del($queueKey);
         }