mysql消息队列/定时任务实现思路(一)

时间:2022-09-21 07:46:12

mysql消息队列/定时任务实现思路(一)

1创建一个订单表

CREATE TABLE `order_queue` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '订单的id号',
`order_id` int(11) NOT NULL,
`mobile` varchar(20) NOT NULL COMMENT '用户的手机号',
`address` varchar(100) NOT NULL COMMENT '用户的地址',
`created_at` datetime NOT NULL DEFAULT '0000-00-00 00:00:00' COMMENT '订单创建的时间',
`updated_at` datetime NOT NULL DEFAULT '0000-00-00 00:00:00' COMMENT '处理完成的时间',
`status` tinyint(2) NOT NULL COMMENT '当前状态,0 未处理,1 已处理,2处理中',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

2.order.php

<?php 
include 'class/db.php';

if(!empty($_GET['mobile'])){
$order_id = rand(10000,99999);
$insert_data = array(
'order_id'=>$order_id,
'mobile'=>$_GET['mobile'],
'created_at'=>date('Y-m-d H:i:s',time()),
'order_id'=>$order_id,
'status'=>0,
);
$db = DB::getIntance();
$res = $db->insert('order_queue',$insert_data);
if($res){
echo $insert_data['order_id']."保存成功";
}else{
echo "保存失败";
}
}else{
echo "1";
}
?>
3.goods.php

<?php 
include 'class/db.php';
$db = DB::getIntance();

$waiting = array('status'=>0,);
$lock = array('status'=>2,);
$res_lock = $db->update('order_queue',$lock,$waiting,2);

if($res_lock){
$res = $db->selectAll('order_queue',$lock);

$success = array(
'status'=>1,
'updated_at'=>date('Y-m-d H;i:s',time()),
);

$res_last = $db->update('order_queue',$success,$lock);
if($res_last){
echo "Success:".$res_last;
}else{
echo "Fail:".$res_last;
}
}else{
echo "ALL Finished";
}
?>
4.good.sh

#!/bin/bash

date "+%G-%m-%d %H:%M:%S"
cd /data/wwwroot/default/mq/
php goods.php

5.crontab -e       

*/1 * * * * /data/wwwroot/default/mq/good.sh >> /data/wwwroot/default/mq/log.log 2>&1