O
O
OrangeXD2017-07-03 14:47:44
Task Schedulers
OrangeXD, 2017-07-03 14:47:44

How to run python Flask script with crontab?

There is a Flask application, it is necessary to run the payment verification function for each unpaid order every five minutes. Payment is verified by the presence of a transaction with 3 confirmations through the Blockr.io API.

from app.client.models import Orders, Payments, db
import requests
import json


def check_btc(num, amount):
    s = requests.get('http://btc.blockr.io/api/v1/address/txs/' + str(num))
    transactions = json.loads(s.text)['data']['txs']
    for trans in transactions:
        if trans['amount'] == amount and trans['confirmations'] >= 3:
            return True


def check_order():
    orders = Orders.query.filter_by(status=1).all()
    payment = Payments.query.filter_by(type=2).first()
    for order in orders:
        result = check_btc(payment.num, order.price_in_currency)
        if result == True:
            order.status = 2
            db.session.commit()

The problem is that SQLALchemy models cannot be imported. How can I make it work, please tell me?
Error below:
Traceback (most recent call last):                                                                                                                             
  File "app/admin/check.py", line 1, in <module>                                                                                                               
    from app.client.models import Orders, Payments, db                                                                                                         
ImportError: No module named app.client.models

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
pcdesign, 2017-07-03
@OrangeXD

IMHO, cron simply does not find a way to it.
Try like this:
Well, that is, tell the crown that they say first go to this folder, and then run the script.

D
Dmitry, 2017-07-03
@dmtrrr

there should be a command in the cron launch script that first activates the virtual environment, and then launches the script.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question