Trigger SMS and Email notifications using event settings in SQLAlchemy

Source: Internet
Author: User

I. Causes

Recently in the SMS and email notification system. Used to this piece. For example, when an order is completed, an SMS message is notified. Although it can be implemented in a direct calling interface, there are several reasons why I want to use conditional triggering

1. Due to the support of the system in the online and online and other ways to recharge, so in many places orders change status. This lets the code that triggers the notification clutter up.

2. The future expansion of the system requires a new interface. You need to add the code for the new call.

In summary, direct invocation will increase maintenance difficulty. So be prepared for the first time in the status of the order to be notified by SMS and email when the payment is successful.

Two. Module requirements

SMS and email notifications cannot affect the operation of the internal system, but the time is unknown because it is a remote call. In order to meet the requirements, the module must meet the following conditions

1. SMS, mail send using thread

2. The trigger must be performed in a state where the database transaction is complete.

Three. SQLAlchemy type of monitoring

SQLAlchemy is the database background Python interface we take. Its transaction triggering mechanism includes

Field change Trigger in 1.orm

2.orm Object state Change Trigger

3.session Change Trigger

4. Connection pool events, connection events

Four. Concrete implementation 1. Mode one

I started with an event that listened to field changes.

@event. Listens_for (Order.status,"Set", retval=True)defPay_success_reminder (target, value, OldValue, initiator):" "has been top-up successfully, to send users a reminder: return:" "     ifValue==oldvalue:returnvalueifvalue=="pay_success":#Payment Success         Try:
...... Pass exceptException as E:Pass returnValue

This event is triggered when the order status changes, unfortunately, he triggers in the transaction, of course, if the other thread in the execution of sending text messages and messages should be no harm. But for some reason I want to call after the transaction is complete.

2. Mode II

Another way to listen for an object's update event and to listen for the session commit event during update event handling. This enables the processing of the object after the commit is completed.

defPay_success_reminder2 (order_no):deffunc (session):Try:           ......           Pass                   exceptException as E:Pass        returnTruereturnfunc@event.listens_for (Order,'After_insert', raw=True) @event. Listens_for (Order,'after_update', raw=True)defPay_success_reminder1 (mapper, connection, target):#    " "has been top-up successfully, to send users a reminder: return:" "    iftarget.dict["Status"]!=target.committed_state["Status"] andtarget.dict["Status"]=="pay_success": Event.listen (target.session,"After_commit", Pay_success_reminder2 (target.dict["Order_no"]) )

If you need to pass some information to the session state listener handler, you pass the variable in the way that you want to use the function wrapper.

Trigger SMS and Email notifications using event settings in SQLAlchemy

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.