master_cron.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. print command
  25. process = os.popen(command)
  26. output = process.read()
  27. process.close()
  28. print output
  29. return output
  30. # 文档转换
  31. def convert():
  32. r = redis()
  33. c = 'office_file'
  34. i = 0
  35. while 1:
  36. file = r.lpop(c)
  37. if file:
  38. g = command(file)
  39. popen(g, False)
  40. i = i+1
  41. if i >= 10:
  42. time.sleep(timeSleep)
  43. i = 0
  44. def handle():
  45. convert()
  46. handle()