master_cron.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # 用于批量处理转换 运行多个转换容器
  4. # nohup python /data/dm/container/web/master.py 2>/dev/null &
  5. import time
  6. import subprocess
  7. import os
  8. import pprint
  9. timeSleep = 15
  10. def redis():
  11. import redis
  12. host = '0.0.0.0'
  13. port = 6379
  14. password = 'dm_redis_123'
  15. pool = redis.ConnectionPool(host=host, password=password, port=int(port))
  16. return redis.Redis(connection_pool=pool)
  17. def command(file):
  18. #return 'dm call office-convert_call id=' + file
  19. 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 registry.cn-hangzhou.aliyuncs.com/shemic/convert /usr/local/convert/convert.py -f ' + file
  20. def popen(command, bg=False):
  21. string = command
  22. if bg == True:
  23. command = command + ' &'
  24. process = os.popen(command)
  25. output = process.read()
  26. process.close()
  27. return output
  28. # 文档转换
  29. def convert():
  30. check = 'ps -ef | grep master.py | grep -v grep | wc -l'
  31. value = int(popen(check))
  32. if value > 1:
  33. return 1;
  34. r = redis()
  35. c = 'office_file'
  36. i = 0
  37. while 1:
  38. file = r.lpop(c)
  39. if file:
  40. g = command(file)
  41. popen(g, False)
  42. i = i+1
  43. if i >= 10:
  44. time.sleep(timeSleep)
  45. i = 0
  46. def handle():
  47. convert()
  48. handle()