我可以在其服务的子功能中调用我的调度程序功能吗?

时间:2019-07-13 15:58:05

标签: python-2.7 web2py

我有两个函数,一个叫做queue_task(),另一个叫做disburse_transaction(), queue_task只是安排了一个执行disburse_transaction的任务,现在在disburse_transaction内部,正在尝试内发出一个http请求,以防万一发生任何连接异常或任何引发异常的事情,现在在except块内,我想重新运行queue_task,可以吗,如果可以,请问如何存档?

下面是queue_task函数

def queue_task(url,data,method,loan_id):
scheduler.queue_task(disburse_transaction,
                     pvars=dict(url=url,data=data,method=method,loan_id=loan_id,count=5),
                     start_time=get_current_date(),
                     retry_failed=5)

下面是disburse_transaction函数,就像您可以看到int除外块试图重新调用queue_task一样,但是当我检查db.scheduler_task时,没有其他任务了,

def disburse_transaction(url,data,method,loan_id):
# make http request using library requests
trans_rec = db((db.holding_account.loan_id == loan_id) &(db.holding_account.transaction_status == 'Pending')).select().first()
if trans_rec:

    loan=db.loan(trans_rec.loan_id)
    if method == 'POST':
        r =None
        try:
            r = requests.post(url+'update_customer.php',json.dumps(data))
            if int(r.status_code) == 200:
                db.holding_account(trans_rec.id).update_record(transaction_status="Completed")
                db.commit()
                 # if Transaction is successful
                 # Lets get the loan_return period
                return_period = loan.loan_return_period
                accept_date = get_current_date()
                repay_amount = 0.15 * loan.amount + loan.amount
                if return_period:
                    # Now let us copy this loan record to loan_accepted table
                    add_rec = db.loan_accepted.insert(loan_id=loan.id, status='Under Service',
                                                      general_status='Accepted', accept_date=accept_date,
                                                      interest_rate=0.15, deadline=add_days(accept_date, return_period),
                                                      repay_amount=repay_amount)
                    db.commit()
                    if add_rec:
                        # When all this is done
                        update = loan.update_record(status='Accepted')
                        db.commit()
                        # Send client SMS
                        # code here

                else:
                    return None

            else:
                queue_task(url, data, POST_METHOD, loan_id)
        except:

            queue_task(url, data, POST_METHOD, loan_id)
else:
    #chances are 99.9% we shall never fall here
    return None

0 个答案:

没有答案