yzy_job.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. from datetime import datetime
  4. from sqlalchemy.orm import Session
  5. from utils import *
  6. from utils.redis_util import *
  7. from models import *
  8. from exceptions import *
  9. from database import get_local_db
  10. from extensions import logger
  11. from common import YzyApi
  12. from config import settings
  13. import traceback
  14. def proc():
  15. if settings.IS_DEV:
  16. return
  17. lock_key = "yzy_job_proc"
  18. if redis_lock(lock_key):
  19. logger.info(datetime.now())
  20. '''
  21. redirect_url = "{}/leader/index".format(settings.YJXP_WEB_ROOT_PATH) # 业务页面
  22. detail_url = YzyApi.format_redirect_url(redirect_url)
  23. yzy_user_id = "eb4kehgy6wj4qn0jhx1dk6" # 暂时写死梦梅的账号
  24. description = "预案名称: 茂名市自然灾害救助应急预案\n响应级别: Ⅰ级响应\n消息内容: 单位您好!《茂名市自然灾害救助应急预案》现已全面启动,特此通知您单位迅速响应,全力做好预案工作要点:负责救灾工作宣传报道协调工作。"
  25. # ret = YzyApi.send_textcard_message(yzy_user_id, "预案响应消息", description, detail_url)
  26. # logger.info(ret)
  27. data = {
  28. "yzy_userid": yzy_user_id,
  29. "mobile": "13528373954",
  30. "content": description,
  31. "recorded_by": 1,
  32. "detail_url": detail_url,
  33. "foreign_key": "1",
  34. "from_scenario": "yjya",
  35. "title": "预案响应消息"
  36. }
  37. if settings.IS_STAGE:
  38. YzyApi.add_to_msg_queue(db, data)
  39. db.close()
  40. '''
  41. redis_unlock(lock_key)
  42. def yzy_msg_queue_proc(db = get_local_db()):
  43. if settings.IS_DEV:
  44. return
  45. lock_key = "yzy_msg_queue_job"
  46. if redis_lock(lock_key):
  47. logger.info(datetime.now())
  48. rows = db.query(YzyMsgQueue).filter(YzyMsgQueue.sent_status == 0).limit(20).all()
  49. for row in rows:
  50. try:
  51. resp = YzyApi.send_textcard_message(row.yzy_userid, row.title, row.content, row.detail_url)
  52. logger.info(resp)
  53. row.sent_time = datetime.now()
  54. if resp['errcode'] == 0:
  55. row.sent_status = 1
  56. if 'jobid' not in resp:
  57. resp['jobid'] = ''
  58. row.errmsg = resp['jobid']
  59. else:
  60. row.sent_status = 9
  61. row.errmsg = resp['errmsg']
  62. db.commit()
  63. except Exception as e:
  64. traceback.print_exc()
  65. redis_unlock(lock_key)