thinkphp6+swool4.*示例

1:首先确定php安装环境,是否有swool扩展

thinkphp6+swool4.*示例_第1张图片

2: 安装thinkphp的swool扩展

git地址:https://github.com/top-think/think-swoole

参考文章:https://segmentfault.com/a/1190000019385216?utm_source=tag-newest

3.windows上安装swoole

Cygwin 官方地址:http://www.cygwin.com/

swoole 官方下载地址:https://github.com/swoole/swoole-src/releases

cygwin下载根据系统我的64位

thinkphp6+swool4.*示例_第2张图片

swoole下载tar.gz的包

thinkphp6+swool4.*示例_第3张图片

首先安装cygwin一直点默认到了选择包的界面

 

thinkphp6+swool4.*示例_第4张图片thinkphp6+swool4.*示例_第5张图片

我也不知道当初安装了什么,反正安装了许多

也没事只要安装swoole失败,但是安装包别删除,可以重新安装覆盖之前安装的,但是记得把运行的cgywin关掉之后,重新安装之前的可以不在选择,直接选择缺少的选择上就够,去掉选择哪个默认的就ok

安装完成之后就是写代码了,正常写php代码一样例如我写的订单计划任务2小时跑一次

exec('set names utf8');
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
    exit();
}

swoole_timer_tick(428400000, function ($timer_id) use ($db) {
    $currenttime=time();
    $db->beginTransaction();
    $res=$db->query('SELECT order_id,store_id,order_no,uid,`type`,`status`,from_unixtime(add_time),paid_time FROM `ph_order` WHERE  `type` = 0  AND `status` = 1 and (add_time+7200) < '.$currenttime);
    $arr = $res->fetchAll(PDO::FETCH_ASSOC);
    //print_r($arr);
    write_log( "执行单个订单开始时间".date('Y-m-d H:i:s')."\n");
    $logstring="";
    foreach ($arr as $key=>$val){

        $iscoupondata=$db->query('SELECT * FROM ph_order_coupon WHERE order_id = '.$val['order_id'].' AND uid = '.$val['uid']);
        $coupondata=$iscoupondata->fetchAll(PDO::FETCH_ASSOC);

        $isorderproduct=$db->query('SELECT * FROM ph_order_product WHERE order_id = '.$val['order_id']);
        $orderproduct=$isorderproduct->fetchAll(PDO::FETCH_ASSOC);
      //  print_r($orderproduct);

       if(!empty($coupondata)){
           $savecoupon=$db->exec('UPDATE `ph_user_coupon`  SET `is_use` = 0 , `use_time` = 0 , `use_order_id` = 0, `delete_flg` = 0  WHERE  `uid` = '.$coupondata[0]['uid'].' and store_id = '.$coupondata[0]['store_id'].' and coupon_id = "'.$coupondata[0]['coupon_id'].'" and id = '.$coupondata[0]['user_coupon_id']);
           //echo "修改订单优惠券状态".$savecoupon."\n";
           $logstring.="修改订单优惠券状态".$savecoupon."\n";
       }
       //库存返反
       if(!empty($orderproduct)){
           $orderidlist=array_column($orderproduct,'product_id');
           if(count($orderidlist) == 1){
               $saveproduct=$db->exec('UPDATE `ph_product`  SET `sales` = sales - 1 , `soldout` = 0 , `quantity` = quantity +1  WHERE  `product_id` = '.$orderidlist[0]);
             //  echo "返回商品库存状态".$saveproduct."\n";
               $logstring.="返回商品库存状态".$saveproduct."\n";
               if(!empty($orderproduct[0]['sku_id'])){
                   $saveproductsku=$db->exec('UPDATE `ph_product_sku`  SET `sales` = sales - 1 , `quantity` = quantity +1  WHERE  `product_id` = '.$orderproduct[0]['product_id'].' and sku_id = '.$orderproduct[0]['sku_id']);
                //   echo "返回商品规格库存状态".$saveproductsku."\n";
                   $logstring.="返回商品规格库存状态".$saveproductsku."\n";
               }
           }else {
               $saveproduct=$db->exec('UPDATE `ph_product`  SET `sales` = sales - 1 , `soldout` = 0 , `quantity` = quantity +1  WHERE  `product_id` in ('.implode(',',$orderidlist).')');
              // echo "返回商品库存状态".$saveproduct."\n";
               $logstring.="返回商品库存状态".$saveproduct."\n";
               $orderskuidlist=array_column($orderproduct,'sku_id');
               $orderskuidlist=array_filter($orderskuidlist,function ($val){return !empty($val)?true:false;});
               if(!empty($orderskuidlist) && count($orderskuidlist) ==1){
                   $saveproductsku=$db->exec('UPDATE `ph_product_sku`  SET `sales` = sales - 1 , `quantity` = quantity +1  WHERE  sku_id = '.$orderskuidlist[0]);
                //   echo "返回商品规格库存状态".$saveproductsku."\n";
                   $logstring.="返回商品规格库存状态".$saveproductsku."\n";
               }else if(!empty($orderskuidlist)){
                   $saveproductsku=$db->exec('UPDATE `ph_product_sku`  SET `sales` = sales - 1 , `quantity` = quantity +1  WHERE sku_id in ('.implode(',',$orderskuidlist).')');
                //   echo "返回商品规格库存状态".$saveproductsku."\n";
                   $logstring.="返回商品规格库存状态".$saveproductsku."\n";
               }
           }
       }

         $res=$db->exec('UPDATE `ph_order`  SET `cancel_time` = "'.time().'" , `status` = 9 , `cancel_method` = 0, `bak` = "订单超时自动取消"  WHERE  `order_id` = '.$val['order_id'].' and uid = '.$val['uid']);
       // echo "返回订单修改状态".$res.'/n';
        $logstring.="返回订单修改状态".$res.'/n';
       $logres=$db->exec('INSERT INTO `ph_order_check_log` SET `order_id` = '.$val['order_id'].' , `order_no` = '.$val['order_no'].' , `store_id` = '.$val['store_id'].' , `description` = \'{\"status\":9}\' , `admin_uid` = 0 , `ip` = "127.0.0.1" , `timestamp` = '.time());
       // echo "返回订单修改记录日志状态".$logres."\n";
        $logstring.="返回订单修改记录日志状态".$logres."\n";

//
    }
    $db->commit();
   // echo "swoole-tick-".date('Y-m-d H:i:s')."\n";
    $logstring.="swoole-tick-".date('Y-m-d H:i:s')."\n";
    write_log( $logstring);

});


function write_log($data){
    $years = date('Y-m');
    //设置路径目录信息
    $url = '../runtime/swoole/'.date('Ymd').'_shell_log.txt';
    $dir_name=dirname($url);
    //目录不存在就创建
    if(!file_exists($dir_name))
    {
        //iconv防止中文名乱码
        $res = mkdir(iconv("UTF-8", "GBK", $dir_name),0777,true);
    }
    $fp = fopen($url,"a");//打开文件资源通道 不存在则自动创建
    fwrite($fp,var_export($data,true)."\r\n");//写入文件
    fclose($fp);//关闭资源通道
}

如果在win上运行没问题了,在直接放到服务器上去掉所有的打印,联调的时候建议还是保留

运行时php 计划任务文件

如果需要在centos后台运行nohup php socket.php &

如果想关掉查询进程id直接ps -ef

thinkphp6+swool4.*示例_第6张图片

在后台运行了直接去查看日志就好,看看是否在后台运行,建议刚开始时间调为1分钟,看看一分钟后日志记录没有 

结束 进程可以kill -9 进程id 

 原谅我标题党,因为我占时还没有用到后台调用swoole位置,什么时候有消息推送和其他方面需求在补上demo吧!我实在是太懒了

你可能感兴趣的:(php)