master_cron.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # 用于批量处理转换 运行多个转换容器
  4. import time
  5. import subprocess
  6. import os
  7. import pprint
  8. timeSleep = 15
  9. def redis():
  10. import redis
  11. host = '0.0.0.0'
  12. port = 6379
  13. pool = redis.ConnectionPool(host=host, port=int(port))
  14. return redis.Redis(connection_pool=pool)
  15. def command(file):
  16. #return 'dm call office-convert_call id=' + file
  17. return 'docker run -d -it --entrypoint python --rm -v /data/dm/container/share:/share -v /etc/hosts:/etc/hosts.main --env HOSTIP="172.30.0.6" --net=dm -v /data/dm/container/web:/www -v /data/dm/container/web/convert/static:/usr/local/convert/front/static -v /data/dm/container/web/convert/files:/usr/local/convert/runtime/files docker.dever.cc/office/convert /usr/local/convert/convert.py -f ' + file
  18. def popen(command, bg=False):
  19. string = command
  20. if bg == True:
  21. command = command + ' &'
  22. print command
  23. process = os.popen(command)
  24. output = process.read()
  25. process.close()
  26. print output
  27. return output
  28. # 文档转换
  29. def convert():
  30. r = redis()
  31. c = 'office_file'
  32. i = 0
  33. while 1:
  34. file = r.lpop(c)
  35. if file:
  36. g = command(file)
  37. popen(g, False)
  38. i = i+1
  39. if i >= 10:
  40. time.sleep(timeSleep)
  41. i = 0
  42. def handle():
  43. convert()
  44. handle()