mysql timing tasks automatically cancel orders

Requirements: The order status is pending payment. The order time is equal to or more than 24 hours to automatically cancel and record the execution of timing tasks.

Use mysql timing tasks

Create a stored procedure, if an error occurs, it will be automatically rolled back. Successful execution will return the number of affected rows

The order table sale_order does not show the sql executed in it. It depends on your business.

CREATE  PROCEDURE pro_upddate_sale_order(out update_count int(9))
	BEGIN
			DECLARE t_error INTEGER DEFAULT 0;-- 这里跟上面的写的不一样,但是需要在语句中根据t_error的值手动回滚下事务
			DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET t_error = 1;
		 	START TRANSACTION;
			-- 等于或超过24小时的待支付订单自动取消
					UPDATE sale_order 
						SET order_status =- 1,
						update_time = NOW( ) 
					WHERE
						order_status = 0  -- 待支付
						AND timestampdiff( HOUR, create_time, NOW( ) ) >= 24 
						AND is_deleted = 0;
						select ROW_COUNT() into update_count;
					IF t_error = 1 THEN
						ROLLBACK;
					ELSE 
					    COMMIT;
					END IF;

	END;

Create a common stored procedure for timing tasks to call other stored procedures and to insert the timing task records for execution

CREATE  PROCEDURE job_procedures()
BEGIN
-- 等于或超过24小时的待支付订单自动取消
CALL pro_upddate_sale_order(@count);
  INSERT into sys_job(job_name,created_time,job_remark,update_count) values('job_procedures',NOW(),'等于或超过24小时的待支付订单自动取消',@count);
end;
	

sys_job table structure

CREATE TABLE `sys_job` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'id',
  `job_name` varchar(100) CHARACTER SET latin1 DEFAULT NULL COMMENT '执行的job名称',
  `created_time` datetime DEFAULT NULL COMMENT '执行的时间',
  `is_delete` int(2) DEFAULT '0' COMMENT '0 未删除 1 已删除',
  `job_remark` varchar(2000) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL COMMENT '任务描述备注',
  `update_count` int(9) DEFAULT '0' COMMENT '受影响行数',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin COMMENT='Mysql 定时任务执行描述'

Create a timed task eventJob is the name of the timed task

-- 2019-11-21 00:00:00启动定时器,每隔12小时执行一次
 create  EVENT if not exists eventJob 
  on schedule every 12 hour starts timestamp '2019-11-21 00:00:00' 
  on completion PRESERVE
  do call job_procedures();

Open transaction 

-- 开启事务
ALTER EVENT eventJob ON  COMPLETION PRESERVE ENABLE;

Start timer 

SET GLOBAL event_scheduler = 1;

View timer status

SHOW VARIABLES LIKE '%sche%';

This completes this requirement. After 12 hours of verification, look at sys_job and the number of affected rows to determine whether the execution is successful.

Other operations

-- 关闭事件
ALTER EVENT eventJob ON  COMPLETION PRESERVE DISABLE;

-- 停止定时器
SET GLOBAL event_scheduler = 0;

-- 设置定时器开机自启动

找到etc/my.cnf 打开 在[mysqld]底下添加 event_scheduler = 1;

-- MySQL 更新或插入后获取受影响行数

SELECT ROW_COUNT();

-- mysql 插入一条记录后获取插入记录的主键id

SELECT LAST_INSERT_ID();

Reference article: https://blog.csdn.net/cuiyaoqiang/article/details/80075224

               https://www.jianshu.com/p/91773f8375eb

Guess you like

Origin blog.csdn.net/qq_39313596/article/details/103180666