yzy_job.py 2.4 KB

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