T
T
TupaDev2021-12-25 17:30:42
Python
TupaDev, 2021-12-25 17:30:42

Just in 15 minutes to update values ​​in a DB?

I have a database with payments. And I need to do something like this every 15 times -
if the COMPLETED == false column and the CANCELED == false column, set the value of the CANCELED == true column

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
SKEPTIC, 2021-12-25
@TupaDev

Do you want to make a late payment?
Write a simple demon.
Add the created field to the database, where, when creating a payment, write down the current unixtime.

import time

t = int(time.time())

print(t)

See what this code outputs.
This value must be entered in the created field when creating a payment.
Further. The same demon will drive to the database once a minute and make the same request. Like this code:
import time
import pymysql


while True:
    connect = pymysql.connect(host='127.0.0.1', user='root', password='228', db='database_name')
    cursor = connect.cursor()

    cursor.execute(f"UPDATE payments SET CANCELED=1 WHERE CANCELED=0 AND COMPLETED=0 AND created<{time.time() - 60 * 15}")

    connect.commit()
    connect.close()

    time.sleep(60)

That's all the magic. Here is the code for mysql. For sqlite you will write by analogy.

R
rPman, 2021-12-25
@rPman

It seems to me that you are mistaken with the logic and you need to do this more than once every 15 minutes, otherwise what will happen if your event occurs a second before the end of this 15-minute interval? Those. to complete the task (which you cancel) you will have not 15 minutes but a second.
If you need to count some time from the moment of the event, then you need to store in the database the beginning of the time from which the countdown begins, then the interval end condition will be now-start_time>interval_duration, make such a finished field calculated in the view or immediately in your request.
You can create several such fields.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question