workman定时任务+redis任务队列 实现7天自动收货

  • 2,023

创建启动文件 thinkphp根目录创建 server.php

<?php
define('APP_PATH', __DIR__ . '/application/');
define("BIND_MODULE", "server/Worker");
// 加载框架引导文件
require __DIR__ . '/thinkphp/start.php';

创建进程 application/server/controller/Worker.php

<?php
namespace app\server\controller;
use think\worker\Server;

class Worker extends Server
{
  /**
   * 每个进程启动
   * @param $worker
   */
    public function onWorkerStart($work)
    {
        $handle=new Collection();
        $handle->add_timer();
    }
}

业务操作 application/server/controller/Collection.php

<?php
namespace app\server\controller;
use think\Controller;
use Workerman\Lib\Timer;
use think\Db;
class Collection extends Controller{

    public function __construct(){
          parent::__construct();
    }

    public function add_timer(){
        Timer::add(1, array($this, 'index'), array(), true);//时间间隔过小,运行会崩溃
    }
    /**
     * 采集数据
     */

    public function index(){
       if(date('H:i:s',time()) == '02:00:00'){
         $this->insert_queue();
       }else{
         $this->select_queue();
       }



       //return ajaxReturn(['msg'=>'is time :'.date('Y-m-d H:i:s',time())]);
    }

    /**
     * 添加队列
     * 先进先出
     */
    protected function insert_queue(){
          $Redis=new \Redis();
          $Redis->connect("127.0.0.1","6379");
          $this->del_queue();
          $t = strtotime(date("Y-m-d",strtotime("-7 day")));//昨天开始时间戳
          //echo '0点时间   ',date('Y-m-d H:i:s',$t).'<br>';
          $out_time = $t+24 * 60 * 60-1;//结束时间戳
          //echo '24点时间  '.date('Y-m-d H:i:s',$out_time);die;
          $order_status_confirm = Db::name('order')->where('add_time','<',$out_time)->where(['order_status'=>1,'shipping_status'=>1,'pay_status'=>1])->field('order_id,order_sn,shipping_time,store_id,order_amount')->select();
          if($order_status_confirm){
            foreach ($order_status_confirm as $key => $value) {
              $Redis->rpush("order_confirm",json_encode($value));
            }
            ajaxReturn(['msg'=>'queue inset ok! queue length '.$Redis->llen('order_confirm')]);
          }else{
            ajaxReturn(['msg'=>'NO quere! queue length '.$Redis->llen('order_confirm')]);
          }
      }
      /**
       * 清空队列
       */
      protected function del_queue(){
        $Redis=new \Redis();
        $Redis->connect("127.0.0.1","6379");
        $Redis->ltrim('order_confirm',100,-1);
      }
      /**
       * 队列输出
       */
      protected function select_queue(){
        $Redis=new \Redis();
        $Redis->connect("127.0.0.1","6379");
        //list类型出队操作
        $value = $Redis->lpop('order_confirm');
        if($value){
          //array(3) { ["order_id"]=> int(12) ["order_sn"]=> string(18) "201905121023475171" ["shipping_time"]=> int(1557627827) }
          $arr = json_decode($value,true);
          /**
           *  echo date('Y-m-d H:i:s',$arr['shipping_time']).'<br>';   发货时间
           *  echo date('Y-m-d H:i:s',strtotime("+7day",$arr['shipping_time'])); +7天确认时间
           */
          $confirm_time = strtotime("+7day",$arr['shipping_time']);
          if($confirm_time < time()){
            //var_dump($arr);
            // echo $Redis->llen('order_confirm');
            // ajaxReturn(['msg'=>"====>>>>>".json_encode($arr).'==============='.$Redis->llen('order_confirm')]);
            // exit;


            $data['order_status'] = 2; // 已收货
            $data['confirm_time'] = $confirm_time; // 收货确认时间
            if(Db::name('order')->where(array('order_id'=>$arr['order_id']))->value('order_status') != 2){
              Db::name('order')->where(array('order_id'=>$arr['order_id']))->update($data);
                logOrder($arr['order_id'],'系统自动确认收到货','订单完成');
              $data = [
                'order_sn' => $arr['order_sn'],
                'order_id' => $arr['order_id'],
                'store_id' => $arr['store_id'],
                'descs' => '系统自动确认收货订单完成商家收益+',
                'money' => $arr['order_amount'],
                'addtime' => time()
              ];
              Db::name('account_store')->insert($data);
              //店铺可用余额追加
              Db::name('store')->where('store_id',$arr['store_id'])->setInc('store_money', $arr['order_amount']);
            }
            ajaxReturn(['msg'=>"confirm receipt success"]);
          }else{
            $Redis->lPush('order_confirm',$value);
            ajaxReturn(['msg'=>'queue length :'.$Redis->llen('order_confirm').'>>>>>>'.date('Y-m-d H:i:s',time()).'<<<<<<']);
          }
        }else{
          //ajaxReturn(['msg'=>"team success"]);
          ajaxReturn(['msg'=>'queue length :'.$Redis->llen('order_confirm').'>>>>>>'.date('Y-m-d H:i:s',time()).'<<<<<<']);

        }
      }



}





DEBUG启动方法:php server.php start
正式上线:php server.php start -d
停止:php server.php stop
重启:php server.php restart
查看状态:php server.php status

正式上线注释掉 所有ajaxReturn开头的代码 防止写workman.log日志造成服务器负担

weinxin
我的微信
这是我的微信扫一扫
开拓者博主
  • 本文由 发表于 2020年1月27日16:28:27
  • 转载请务必保留本文链接:https://www.150643.com/535.html
匿名

发表评论

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

评论:1   其中:访客  1   博主  0
    • bjlb bjlb 3

      快递单号网查询 单号购买网www.88danhw.com