66 lines
1.9 KiB
Python
66 lines
1.9 KiB
Python
import json
|
|
import logging
|
|
import os
|
|
import time
|
|
|
|
from django_redis import get_redis_connection
|
|
from django.conf import settings
|
|
from threading import Thread
|
|
|
|
from yzk_wechat_event.settings.constant import Constant
|
|
from django.utils.module_loading import import_string
|
|
|
|
logger = logging.getLogger('apps')
|
|
|
|
|
|
class JQREventCallbackPubSub:
|
|
rc = get_redis_connection()
|
|
|
|
@classmethod
|
|
def publish(cls, data):
|
|
cls.rc.lpush(Constant.JQR_EVENT_CALLBACK_PUBSUB_CHANNEL, json.dumps(data))
|
|
|
|
@classmethod
|
|
def event_callback_listener(cls):
|
|
while True:
|
|
data = cls.rc.brpop(Constant.JQR_EVENT_CALLBACK_PUBSUB_CHANNEL, 5)
|
|
if data is None:
|
|
time.sleep(5)
|
|
continue
|
|
data = json.loads(data[1])
|
|
callback_data = json.loads(data['data'])
|
|
corpinfo = data['corpinfo']
|
|
handler = data['handler']
|
|
handler = import_string(handler)
|
|
handler(callback_data, corpinfo)
|
|
|
|
|
|
class JQRQrcodeCallbackPubSub:
|
|
rc = get_redis_connection()
|
|
|
|
@classmethod
|
|
def publish(cls, data):
|
|
cls.rc.lpush(Constant.JQR_QRCODE_PUBSUB_CHANNEL, json.dumps(data))
|
|
|
|
@classmethod
|
|
def qrcode_callback_listener(cls):
|
|
while True:
|
|
data = cls.rc.brpop(Constant.JQR_QRCODE_PUBSUB_CHANNEL, 5)
|
|
if data is None:
|
|
time.sleep(5)
|
|
continue
|
|
data = json.loads(data[1])
|
|
qrcodeid = data['qrcodeid']
|
|
userid = data['userid']
|
|
externaluserid = data['externaluserid']
|
|
corpinfo = data['corpinfo']
|
|
handler = data['handler']
|
|
handler = import_string(handler)
|
|
handler(qrcodeid, userid, externaluserid, corpinfo)
|
|
|
|
|
|
t = Thread(target=JQREventCallbackPubSub.event_callback_listener)
|
|
t.start()
|
|
t2 = Thread(target=JQRQrcodeCallbackPubSub.qrcode_callback_listener)
|
|
t2.start()
|