700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > 用PHP+Redis实现延迟任务 实现自动取消订单(详细教程)

用PHP+Redis实现延迟任务 实现自动取消订单(详细教程)

时间:2023-11-02 16:15:57

相关推荐

用PHP+Redis实现延迟任务 实现自动取消订单(详细教程)

后端开发|php教程

PHP,Redis,延迟任务

后端开发-php教程

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

dedecms仿站源码,vscode md快捷键,ubuntu开机转圈,tomcat怎么解除请求,scrapy爬虫线程,php全屏代码,余杭区seo网络优化,金融产品发布网站源码,企业的模板免费下载lzw

(A)业务场景:

出牌九源码,c语言编程vscode,Ubuntu制作flac,tomcat 啟動war,airtest 爬虫,php退出for循环,朔州短视频seo报价,网站在线浮动的客服代源码,仿搜索引擎模板lzw

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

mvc jsp 源码下载,右键用vscode打开文件,蓝魔ubuntu,装2个tomcat,哪些爬虫合法,固定资产管理 php,荆门床上用品seo推广,网站免费系统源码,织梦模板后台密码lzw

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

(B)服务准备:

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

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

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

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

原配置为:

notify-keyspace-events ""

更改 配置如下:

notify-keyspace-events "Ex"

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

[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

127.0.0.1, username=> oot, password=>168168, database=> est, 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

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, edis延迟任务);//3秒后回调$test_del = false;//测试删除缓存后是否会有过期回调。结果:没有回调if($test_del == true){ //sleep(1); $redis->delete($order_sn);}echo $order_sn;/* * 测试其他key会不会有回调,结果:有回调 * $k = est; * $redis2->set($k,100); * $redis2->expire($k,10); **/

psubscribe.php

setOption();//当key过期的时候就看到通知,订阅的key __keyevent@__: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]:; $use_mysql = isset($list[1])?$list[1]:; 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

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文件:

setOption();//当key过期的时候就看到通知,订阅的key __keyevent@__: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]:; $use_mysql = isset($list[1])?$list[1]:; 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 编译器的路径:

#! /usr/bin/env php

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

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

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

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

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

[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解决方法如下:

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

查看进程方法:

ps -aux|grep psubscribe.phpa:显示所有程序u:以用户为主的格式来显示x:显示所有程序,不以终端机来区分

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

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

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

kill -9 进程号

清空 nohup.out文件方法:

cat /dev/null > nohup.out

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

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

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

2.然后执行exit正常退出当前账户。

3.然后再去链接终端。使得程序后台正常运行。

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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。