DYProcessManager.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. # coding: utf-8
  2. from socket import socket, SOCK_DGRAM, AF_INET
  3. import MySQLdb
  4. from datetime import datetime
  5. from component_controler import *
  6. class ComponentStatus(object):
  7. def __init__(self):
  8. """
  9. get components infomation from database
  10. """
  11. self.rooms_path = '/home/room/server'
  12. s = socket(AF_INET, SOCK_DGRAM)
  13. s1 = socket(AF_INET, SOCK_DGRAM)
  14. s.connect(('192.168.1.1', 0))
  15. s1.connect(('114.114.114.114', 0))
  16. self.local_ip = s.getsockname()[0]
  17. self.int_ip = s1.getsockname()[0]
  18. self.conn = MySQLdb.connect(user='root', passwd='BqzUkS55bf1ULSSwSv8D', host='192.168.1.1', port=3306)
  19. self.cursor_components = self.conn.cursor()
  20. self.cursor_msgserver = self.conn.cursor()
  21. self.cursor_components.execute("use stt_config")
  22. self.cursor_components.execute('select server_name,ip from web_server_info where ip = "%s"' % self.int_ip)
  23. self.cursor_msgserver.execute("use stt_config")
  24. self.cursor_msgserver.execute('select server_id from msg_server_info where ip="%s"' % self.local_ip)
  25. self.components = {}
  26. self.msgserver = []
  27. for servername, ip in self.cursor_components.fetchall():
  28. self.components[servername] = ip
  29. for server_id in self.cursor_msgserver.fetchall():
  30. self.msgserver.append(int(server_id[0]))
  31. self.cursor_components.close()
  32. self.conn.close()
  33. def deamon(self, debug=False):
  34. for component_name in self.components:
  35. component = Action(component_name)
  36. if debug:
  37. print component_name
  38. continue
  39. if not component.isalived():
  40. content = '%s was shutdown.' % component_name
  41. writelog(content)
  42. component.start()
  43. content = '%s was down, now get starting.' % component_name
  44. writelog(content)
  45. for msgserver_num in self.msgserver:
  46. msgserver_name = 'MsgServer' + str(msgserver_num)
  47. component = Action(msgserver_name)
  48. if debug:
  49. print msgserver_name
  50. continue
  51. if not component.isalived():
  52. content = '%s was shutdown.' % msgserver_name
  53. writelog(content)
  54. component.start()
  55. content = '%s was down, now get starting.' % msgserver_name
  56. writelog(content)
  57. def writelog(content, logfile='Process_deamon.log'):
  58. f = open(logfile, 'a')
  59. f.write('%s %s\n' % (datetime.now(), content))
  60. f.close()
  61. if __name__ == '__main__':
  62. if len(sys.argv) == 2:
  63. if sys.argv[1] == 'debug':
  64. ProcessDaemon = ComponentStatus()
  65. ProcessDaemon.deamon(debug=True)
  66. sys.exit()
  67. else:
  68. print 'Illegal Segment! Now Exit...'
  69. while 1:
  70. try:
  71. ProcessDaemon = ComponentStatus()
  72. except Exception, e:
  73. # sys.exit(1)
  74. writelog(e, logfile='deamon_status.log')
  75. pass
  76. ProcessDaemon.deamon()
  77. time.sleep(10)