master_cron.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 = 120
  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_cron.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. # r.ltrim("list2", 0, 1)
  38. # n = r.llen(c)
  39. while 1:
  40. file = r.lpop(c)
  41. if file:
  42. g = command(file)
  43. popen(g, False)
  44. i = i+1
  45. if i >= 10:
  46. time.sleep(timeSleep)
  47. i = 0
  48. def handle():
  49. convert()
  50. handle()