yzy_job.py 2.6 KB

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