Forums

AppSchedualar not starting

i have this function schduled to monitor users payment on a platform, once payment is initiated, we keep the payment in check for 1hour within those moment, if the payment is successfull we credidt the user on our site. for this i got to use appchedular in my app with the implimentation as below

from apscheduler.schedulers.background import BackgroundScheduler
task_scheduler = BackgroundScheduler()

if not task_scheduler.running:
        task_scheduler.start()

def check_payment_status(payment_id):
    from .models import Wallet,Payment
    payment = Payment.objects.get(payment_id = payment_id)
    if payment.attempts>=12:
        task_scheduler.remove_job(payment_id)
        return 0
    response =  requests.get(f'{url}?tx_ref={payment_id}', headers=headers)
    result =  response.json()
    payment.attempts+=1
    payment.save()
    if result['data'] is None:
        return None
    if result['data']['status']  == 'successful':
        wallet = Wallet.objects.get(user = payment.user)
        if not payment.completed:
            if float(result['data']['amount']) >= float(payment.amount):
                wallet.balance+=float(result['data']['amount'])
                wallet.save()
                payment.completed = True
                payment.canceled = False
                payment.save()
                task_scheduler.remove_job(payment_id)
                return 1
        else:
            task_scheduler.remove_job(payment_id)
            return 0
    else:
        return 0`

we asign the task like this

task_scheduler.add_job(check_payment_status, 'interval',minutes =5,args=[instance.payment_id],id=instance.payment_id)

this works perfectly on my machine but faild on pythonanywhere with this error

RuntimeError: The scheduler seems to be running under uWSGI, but threads have been disabled. You must run uWSGI with the --enable-threads option for the

Scheduler would not work in a WSGI app on PythonAnywhere directly, you should run it as an always-on task, see this help page for more details.