yzy_job.py 2.6 KB

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