remove_user_virtual_card.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. # -*- coding: utf-8 -*-
  2. # !/usr/bin/env python
  3. import datetime
  4. import os
  5. import sys
  6. import time
  7. os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'configs.production')
  8. PROJECT_ROOT = os.path.join(os.path.abspath(os.path.split(os.path.realpath(__file__))[0] + "/.."), '..')
  9. sys.path.insert(0, PROJECT_ROOT)
  10. from script.base import init_env, get_logger
  11. init_env(interactive=False)
  12. logger = get_logger(__name__)
  13. from apilib.utils_mongo import BulkHandlerEx
  14. from apps.web.user.models import UserVirtualCard
  15. def remove_documents():
  16. _cls = UserVirtualCard
  17. dealerId = '629c0612003048029f9708fb'
  18. start_time = int(time.time())
  19. while True:
  20. now_time = int(time.time())
  21. if now_time - start_time > 2 * 60 * 60:
  22. logger.debug('time is over. wait next.')
  23. return
  24. items = _cls.get_collection().find(
  25. {'dealerId': dealerId}, {'_id': 1, 'openIds': 1}).limit(2000)
  26. bulker = BulkHandlerEx(_cls.get_collection())
  27. for item in items:
  28. if 'openIds' not in item or len(item['openIds']) == 0:
  29. # print(item)
  30. bulker.delete(query_dict={'_id': item['_id']})
  31. count = len(bulker.requests)
  32. if count > 0:
  33. logger.debug('prepare to delete {} rows.'.format(count))
  34. bulker.execute()
  35. bulker = BulkHandlerEx(_cls.get_collection()) # type: BulkHandlerEx
  36. if count < 2000:
  37. break
  38. logger.debug('delete all over.')
  39. remove_documents()