yzy_job.py 2.5 KB

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