当前位置:网站首页>Use MySQL events to regularly perform post seven world line tasks
Use MySQL events to regularly perform post seven world line tasks
2022-07-02 12:39:00 【Time chaser】
Recently I did a recruitment project , There is a need for regular offline in the project . Before doing it, I've been thinking about using window service , Still use scheduling , In the end, I chose to use mysql Timing events , Because it's so simple and convenient .
Ideas : First create a stored procedure , Update the corresponding position status through cursor traversal , Let's create a scheduled event after , Don't talk too much nonsense, just go to the code
First create a stored procedure :
CREATE DEFINER = CURRENT_USER PROCEDURE `TimedDownLine`() BEGIN -- Declare variables ( For inserting data ) DECLARE PostID INT;# Position ID DECLARE DifferDay INT;# return int DECLARE done INT DEFAULT 0;# The basis for judging whether there is information 0 Indicates that there is data DECLARE Integral_cursor cursor for SELECT id,TIMESTAMPDIFF(DAY, ReleaseEnd, now()) AS intervalday FROM postrelease WHERE PostType=1;# Query all online position information (1 Indicates online position ,2 Indicates the offline position ) DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;# No data is returned and the status is changed to 1 -- Open cursor OPEN Integral_cursor; # Start the cycle WHILE done=0 DO #REPEAT # Cursor assignment ( The cursor will only execute downward ) FETCH Integral_cursor INTO PostID,DifferDay; IF(done=0&&DifferDay>7) THEN UPDATE postrelease SET PostType=2 where ID=PostID; end IF; # The loop ends #UNTIL done=1 END REPEAT; END WHILE; SET done=1;# Change state , Indicates the end of the cycle -- Close cursor END
Create database events :
# Check whether the database event is enabled SHOW VARIABLES LIKE 'event_scheduler' show variables like '%event%'; # Set the current event on SET GLOBAL event_scheduler = 1; # View scheduled tasks select * from mysql.event; SELECT * FROM information_schema.events; # It starts every morning select DATE_ADD(DATE_ADD(CURDATE(), INTERVAL 1 DAY), INTERVAL 13 HOUR) # Turn off scheduled tasks DROP event temp_event; # It starts every morning ( Create timed events ) CREATE EVENT IF NOT EXISTS TimeDownlines_event ON SCHEDULE EVERY 1 DAY STARTS DATE_ADD(DATE_ADD(CURDATE(), INTERVAL 1 DAY), INTERVAL 0 HOUR) ON COMPLETION PRESERVE ENABLE # Start the scheduled task on time DO CALL TimedDownLine();
In this way, it is possible to deal with some businesses rather than complex scheduled tasks , And the development is fast , Next time I have time, I will window The scheduled task of the service is written .
边栏推荐
- Performance tuning project case
- How to write a pleasing English mathematical paper
- Deep understanding of P-R curve, ROC and AUC
- JSON序列化 与 解析
- Find the common ancestor of any two numbers in a binary tree
- kubeadm join时出现错误:[ERROR Port-10250]: Port 10250 is in use [ERROR FileAvailable--etc-kubernetes-pki
- Leetcode - Sword finger offer 51 Reverse pairs in an array
- China traffic sign detection data set
- Sparkcontext: error initializing sparkcontext solution
- CPU指令集介绍
猜你喜欢
计数类DP AcWing 900. 整数划分
async/await 异步函数
drools中then部分的写法
"As a junior college student, I found out how difficult it is to counter attack after graduation."
Tas (file d'attente prioritaire)
染色法判定二分图 AcWing 860. 染色法判定二分图
[old horse of industrial control] detailed explanation of Siemens PLC TCP protocol
堆 AcWing 839. 模拟堆
线性DP AcWing 899. 编辑距离
China traffic sign detection data set
随机推荐
VLAN experiment
High performance erasure code coding
单指令多数据SIMD的SSE/AVX指令集和API
Why do programmers have the idea that code can run without moving? Is it poisonous? Or what?
Addition, deletion, modification and query of MySQL table (Advanced)
Go learning notes - multithreading
MySQL and PostgreSQL methods to grab slow SQL
Shutter encapsulated button
When uploading a file, the server reports an error: iofileuploadexception: processing of multipart / form data request failed There is no space on the device
Drools dynamically add, modify, and delete rules
AI中台技术调研
Programmers can't find jobs after the age of 35? After reading this article, you may be able to find the answer
Record the range of data that MySQL update will lock
中国交通标志检测数据集
JSON序列化 与 解析
Fluent fluent library encapsulation
There is a hidden danger in CDH: the exchange memory used by the process of this role is XX megabytes. Warning threshold: 200 bytes
Anxiety of a 211 programmer: working for 3 years with a monthly salary of less than 30000, worried about being replaced by fresh students
arcgis js 4. Add pictures to x map
CPU指令集介绍