使用PHP+Redis实现延迟任务,实现自动取消订单功能

吾爱主题 阅读:136 2021-09-17 12:08:00 评论:0

简单定时任务解决方案:使用redis的keyspace notifications(键失效后通知事件) 需要注意此功能是在redis 2.8版本以后推出的,因此你服务器上的reids最少要是2.8版本以上;

(A)业务场景:

1、当一个业务触发以后需要启动一个定时任务,在指定时间内再去执行一个任务(如自动取消订单,自动完成订单等功能)

2、redis的keyspace notifications 会在key失效后发送一个事件,监听此事件的的客户端就可以收到通知

(B)服务准备:

1、修改reids配置文件(redis.conf)【window系统配置文件为:redis.windows.conf 】

redis默认不会开启keyspace notifications,因为开启后会对cpu有消耗

备注:E:keyevent事件,事件以__keyevent@<db>__为前缀进行发布;

x:过期事件,当某个键过期并删除时会产生该事件;

原配置为:

?
1 notify-keyspace-events ""

更改 配置如下:

?
1 notify-keyspace-events "Ex"

保存配置后,重启Redis服务,使配置生效

?
1 2 3 4 [root@chokingwin etc]# service redis-server restart /usr/local/redis/etc/redis.conf Stopping redis-server: [ OK ] Starting redis-server: [ OK ]

window系统重启redis ,先切换到redis文件目录,然后关闭redis服务(redis-server --service-stop),再开启(redis-server --service-start)

C)文件代码:

phpredis实现订阅Keyspace notification,可实现自动取消订单,自动完成订单。以下为测试例子

创建4个文件,然后自行修改数据库和redis配置参数

db.class.php

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 <?php class mysql {   private $mysqli ;   private $result ;   /**   * 数据库连接   * @param $config 配置数组   */     public function connect()   {   $config = array (   'host' => '127.0.0.1' ,   'username' => 'root' ,   'password' => '168168' ,   'database' => 'test' ,   'port' =>3306,   );     $host = $config [ 'host' ]; //主机地址   $username = $config [ 'username' ]; //用户名   $password = $config [ 'password' ]; //密码   $database = $config [ 'database' ]; //数据库   $port = $config [ 'port' ]; //端口号   $this ->mysqli = new mysqli( $host , $username , $password , $database , $port );     }   /**   * 数据查询   * @param $table 数据表   * @param null $field 字段   * @param null $where 条件   * @return mixed 查询结果数目   */   public function select( $table , $field = null, $where = null)   {   $sql = "SELECT * FROM `{$table}`" ;   //echo $sql;exit;   if (! empty ( $field )) {   $field = '`' . implode( '`,`' , $field ) . '`' ;   $sql = str_replace ( '*' , $field , $sql );   }   if (! empty ( $where )) {   $sql = $sql . ' WHERE ' . $where ;   }       $this ->result = $this ->mysqli->query( $sql );     return $this ->result;   }   /**   * @return mixed 获取全部结果   */   public function fetchAll()   {   return $this ->result->fetch_all(MYSQLI_ASSOC);   }   /**   * 插入数据   * @param $table 数据表   * @param $data 数据数组   * @return mixed 插入ID   */   public function insert( $table , $data )   {   foreach ( $data as $key => $value ) {   $data [ $key ] = $this ->mysqli->real_escape_string( $value );   }   $keys = '`' . implode( '`,`' , array_keys ( $data )) . '`' ;   $values = '\'' . implode( "','" , array_values ( $data )) . '\'' ;   $sql = "INSERT INTO `{$table}`( {$keys} )VALUES( {$values} )" ;   $this ->mysqli->query( $sql );   return $this ->mysqli->insert_id;   }   /**   * 更新数据   * @param $table 数据表   * @param $data 数据数组   * @param $where 过滤条件   * @return mixed 受影响记录   */   public function update( $table , $data , $where )   {   foreach ( $data as $key => $value ) {   $data [ $key ] = $this ->mysqli->real_escape_string( $value );   }   $sets = array ();   foreach ( $data as $key => $value ) {   $kstr = '`' . $key . '`' ;   $vstr = '\'' . $value . '\'' ;   array_push ( $sets , $kstr . '=' . $vstr );   }   $kav = implode( ',' , $sets );   $sql = "UPDATE `{$table}` SET {$kav} WHERE {$where}" ;     $this ->mysqli->query( $sql );   return $this ->mysqli->affected_rows;   }   /**   * 删除数据   * @param $table 数据表   * @param $where 过滤条件   * @return mixed 受影响记录   */   public function delete ( $table , $where )   {   $sql = "DELETE FROM `{$table}` WHERE {$where}" ;   $this ->mysqli->query( $sql );   return $this ->mysqli->affected_rows;   } }

index.php

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 <?php require_once 'Redis2.class.php' ; $redis = new \Redis2( '127.0.0.1' , '6379' , '' , '15' ); $order_sn = 'SN' .time(). 'T' .rand(10000000,99999999); $use_mysql = 1; //是否使用数据库,1使用,2不使用 if ( $use_mysql == 1){   /*   * //数据表   * CREATE TABLE `order` (   * `ordersn` varchar(255) NOT NULL DEFAULT '',   * `status` varchar(255) NOT NULL DEFAULT '',   * `createtime` varchar(255) NOT NULL DEFAULT '',   * `id` int(11) unsigned NOT NULL AUTO_INCREMENT,   * PRIMARY KEY (`id`)   * ) ENGINE=InnoDB AUTO_INCREMENT=27 DEFAULT CHARSET=utf8mb4;   */   require_once 'db.class.php' ;   $mysql = new \mysql();   $mysql ->connect();   $data = [ 'ordersn' => $order_sn , 'status' =>0, 'createtime' => date ( 'Y-m-d H:i:s' ,time())];   $mysql ->insert( 'order' , $data ); } $list = [ $order_sn , $use_mysql ]; $key = implode( ':' , $list ); $redis ->setex( $key ,3, 'redis延迟任务' ); //3秒后回调 $test_del = false; //测试删除缓存后是否会有过期回调。结果:没有回调 if ( $test_del == true){   //sleep(1);   $redis -> delete ( $order_sn ); } echo $order_sn ; /*   * 测试其他key会不会有回调,结果:有回调   * $k = 'test';   * $redis2->set($k,'100');   * $redis2->expire($k,10);   * */

psubscribe.php

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 <?php ini_set ( 'default_socket_timeout' , -1); //不超时 require_once 'Redis2.class.php' ; $redis_db = '15' ; $redis = new \Redis2( '127.0.0.1' , '6379' , '' , $redis_db ); // 解决Redis客户端订阅时候超时情况 $redis ->setOption(); //当key过期的时候就看到通知,订阅的key __keyevent@<db>__:expired 这个格式是固定的,db代表的是数据库的编号,由于订阅开启之后这个库的所有key过期时间都会被推送过来,所以最好单独使用一个数据库来进行隔离 $redis ->psubscribe( array ( '__keyevent@' . $redis_db . '__:expired' ), 'keyCallback' ); // 回调函数,这里写处理逻辑 function keyCallback( $redis , $pattern , $channel , $msg ) {   echo PHP_EOL;   echo "Pattern: $pattern\n" ;   echo "Channel: $channel\n" ;   echo "Payload: $msg\n\n" ;   $list = explode ( ':' , $msg );   $order_sn = isset( $list [0])? $list [0]: '0' ;   $use_mysql = isset( $list [1])? $list [1]: '0' ;   if ( $use_mysql == 1){   require_once 'db.class.php' ;   $mysql = new \mysql();   $mysql ->connect();   $where = "ordersn = '" . $order_sn . "'" ;   $mysql ->select( 'order' , '' , $where );   $finds = $mysql ->fetchAll();   print_r( $finds );   if (isset( $finds [0][ 'status' ]) && $finds [0][ 'status' ]==0){   $data = array ( 'status' => 3);   $where = " id = " . $finds [0][ 'id' ];   $mysql ->update( 'order' , $data , $where );   }   } } //或者 /*$redis->psubscribe(array('__keyevent@'.$redis_db.'__:expired'), function ($redis, $pattern, $channel, $msg){   echo PHP_EOL;   echo "Pattern: $pattern\n";   echo "Channel: $channel\n";   echo "Payload: $msg\n\n";   //................ });*/

Redis2.class.php

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 <?php class Redis2 {   private $redis ;   public function __construct( $host = '127.0.0.1' , $port = '6379' , $password = '' , $db = '15' )   {   $this ->redis = new Redis();   $this ->redis->connect( $host , $port ); //连接Redis   $this ->redis->auth( $password ); //密码验证   $this ->redis->select( $db ); //选择数据库   }   public function setex( $key , $time , $val )   {   return $this ->redis->setex( $key , $time , $val );   }   public function set( $key , $val )   {   return $this ->redis->set( $key , $val );   }   public function get( $key )   {   return $this ->redis->get( $key );   }   public function expire( $key = null, $time = 0)   {   return $this ->redis->expire( $key , $time );   }   public function psubscribe( $patterns = array (), $callback )   {   $this ->redis->psubscribe( $patterns , $callback );   }   public function setOption()   {   $this ->redis->setOption(\Redis::OPT_READ_TIMEOUT, -1);   }   public function lRange( $key , $start , $end )   {   return $this ->redis->lRange( $key , $start , $end );   }   public function lPush( $key , $value1 , $value2 = null, $valueN = null ){   return $this ->redis->lPush( $key , $value1 , $value2 = null, $valueN = null );   }   public function delete ( $key1 , $key2 = null, $key3 = null)   {   return $this ->redis-> delete ( $key1 , $key2 = null, $key3 = null);   } }

window系统测试方法:先在cmd命令界面运行psubscribe.php,然后网页打开index.php。

使监听后台始终运行(订阅)

有个问题 做到这一步,利用 phpredis 扩展,成功在代码里实现对过期 Key 的监听,并在 psCallback()里进行回调处理。开头提出的两个需求已经实现。可是这里有个问题:redis 在执行完订阅操作后,终端进入阻塞状态,需要一直挂在那。且此订阅脚本需要人为在命令行执行,不符合实际需求。

实际上,我们对过期监听回调的需求,是希望它像守护进程一样,在后台运行,当有过期事件的消息时,触发回调函数。使监听后台始终运行 希望像守护进程一样在后台一样,

我是这样实现的。

Linux中有一个nohup命令。功能就是不挂断地运行命令。同时nohup把脚本程序的所有输出,都放到当前目录的nohup.out文件中,如果文件不可写,则放到<用户主目录>/nohup.out 文件中。那么有了这个命令以后,不管我们终端窗口是否关闭,都能够让我们的php脚本一直运行。

编写psubscribe.php文件:

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 <?php #! /usr/bin/env php ini_set ( 'default_socket_timeout' , -1); //不超时 require_once 'Redis2.class.php' ; $redis_db = '15' ; $redis = new \Redis2( '127.0.0.1' , '6379' , '' , $redis_db ); // 解决Redis客户端订阅时候超时情况 $redis ->setOption(); //当key过期的时候就看到通知,订阅的key __keyevent@<db>__:expired 这个格式是固定的,db代表的是数据库的编号,由于订阅开启之后这个库的所有key过期时间都会被推送过来,所以最好单独使用一个数据库来进行隔离 $redis ->psubscribe( array ( '__keyevent@' . $redis_db . '__:expired' ), 'keyCallback' ); // 回调函数,这里写处理逻辑 function keyCallback( $redis , $pattern , $channel , $msg ) {   echo PHP_EOL;   echo "Pattern: $pattern\n" ;   echo "Channel: $channel\n" ;   echo "Payload: $msg\n\n" ;   $list = explode ( ':' , $msg );   $order_sn = isset( $list [0])? $list [0]: '0' ;   $use_mysql = isset( $list [1])? $list [1]: '0' ;   if ( $use_mysql == 1){   require_once 'db.class.php' ;   $mysql = new \mysql();   $mysql ->connect();   $where = "ordersn = '" . $order_sn . "'" ;   $mysql ->select( 'order' , '' , $where );   $finds = $mysql ->fetchAll();   print_r( $finds );   if (isset( $finds [0][ 'status' ]) && $finds [0][ 'status' ]==0){   $data = array ( 'status' => 3);   $where = " id = " . $finds [0][ 'id' ];   $mysql ->update( 'order' , $data , $where );   }   } } //或者 /*$redis->psubscribe(array('__keyevent@'.$redis_db.'__:expired'), function ($redis, $pattern, $channel, $msg){   echo PHP_EOL;   echo "Pattern: $pattern\n";   echo "Channel: $channel\n";   echo "Payload: $msg\n\n";   //................ });*/

注意:我们在开头,申明 php 编译器的路径:

?
1 #! /usr/bin/env php

这是执行 php 脚本所必须的。

然后,nohup 不挂起执行 psubscribe.php,注意 末尾的 &

?
1 2 [root@chokingwin HiGirl]# nohup ./psubscribe.php & [1] 4456 nohup: ignoring input and appending output to `nohup.out'

说明:脚本确实已经在 4456 号进程上跑起来。

查看下nohup.out cat 一下 nohuo.out,看下是否有过期输出:

?
1 2 3 4 [root@chokingwin HiGirl]# cat nohup.out Pattern:__keyevent@0__:expired Channel: __keyevent@0__:expired Payload: name

运行index.php ,3秒后效果如上即成功

遇到问题:使用命令行模式开启监控脚本 ,一段时间后报错 :Error while sending QUERY packet. PID=xxx

解决方法:由于等待消息队列是一个长连接,而等待回调前有个数据库连接,数据库的wait_timeout=28800,所以只要下一条消息离上一条消息超过8小时,就会出现这个错误,把wait_timeout设置成10,并且捕获异常,发现真实的报错是 MySQL server has gone away ,
所以只要处理完所有业务逻辑后主动关闭数据库连接,即数据库连接主动close掉就可以解决问题

yii解决方法如下:

?
1 Yii:: $app ->db->close();

查看进程方法:

?
1 2 3 4 5 ps -aux|grep psubscribe.php   a:显示所有程序 u:以用户为主的格式来显示 x:显示所有程序,不以终端机来区分

查看jobs进程ID:[ jobs -l ]命令

?
1 2 3 www@iZ232eoxo41Z:~/tinywan $ jobs -l [1]- 1365 Stopped (tty output) sudo nohup psubscribe.php > /dev/null 2>&1 [2]+ 1370 Stopped (tty output) sudo nohup psubscribe.php > /dev/null 2>&1

终止后台运行的进程方法:

?
1 kill -9 进程号

清空 nohup.out文件方法:

?
1 cat /dev/null > nohup.out

我们在使用nohup的时候,一般都和&配合使用,但是在实际使用过程中,很多人后台挂上程序就这样不管了,其实这样有可能在当前账户非正常退出或者结束的时候,命令还是自己结束了。

所以在使用nohup命令后台运行命令之后,我们需要做以下操作:

1.先回车,退出nohup的提示。

2.然后执行exit正常退出当前账户。
3.然后再去链接终端。使得程序后台正常运行。

我们应该每次都使用exit退出,而不应该每次在nohup执行成功后直接关闭终端。这样才能保证命令一直在后台运行。

总结

以上所述是小编给大家介绍的使用PHP+Redis实现延迟任务,实现自动取消订单功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

原文链接:https://www.cnblogs.com/a609251438/p/11898872.html

可以去百度分享获取分享代码输入这里。
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

【腾讯云】云服务器产品特惠热卖中
搜索
标签列表
    关注我们

    了解等多精彩内容