Last active
August 29, 2015 14:05
-
-
Save maksadbek/0ebff7ae9cebe2c0d735 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/python3.2 | |
from ws4py.client.threadedclient import WebSocketClient | |
import time | |
import subprocess | |
import logging | |
import logging.handlers | |
LOG_FILENAME = '/var/log/run_taxi_deamon.log' | |
# Set up a specific logger with our desired output level | |
my_logger = logging.getLogger('MyLogger') | |
my_logger.setLevel(logging.DEBUG) | |
# Add the log message handler to the logger | |
handler = logging.handlers.RotatingFileHandler( | |
LOG_FILENAME, maxBytes=5*1000, mode='w') | |
formatter = logging.Formatter("%(asctime)s - %(message)s") | |
handler.setFormatter(formatter) | |
my_logger.addHandler(handler) | |
RESP_COUNT = 0 | |
ERR_COUNT = 0 | |
DELAY = 30 | |
class Client(WebSocketClient): | |
def opened(self): | |
global ERR_COUNT | |
print('SEND PING.') | |
self.send("PING") | |
ERR_COUNT = 0 | |
def closed(self, code, reason=None): | |
print("Closed", code, reason) | |
def received_message(self, message): | |
global RESP_COUNT | |
RESP_COUNT = 0 | |
print(message) | |
def restart(): | |
try: | |
p = subprocess.Popen(["sudo", "/etc/init.d/nodeamon", "restart"], stdout=subprocess.PIPE) | |
my_logger.debug(str(p.communicate()[0])) | |
except Exception as e: | |
my_logger.debug(str(e)) | |
if __name__ == '__main__': | |
ws = None | |
while True: | |
RESP_COUNT += 1 | |
try: | |
if ws: | |
ws.close() | |
ws = Client('ws://127.0.0.1:8083') | |
ws.connect() | |
#ws.run_forever() | |
except Exception as e: | |
my_logger.debug(str(e)) | |
ERR_COUNT += 1 | |
print('waiting data...') | |
time.sleep(10) | |
print(RESP_COUNT, ERR_COUNT) | |
if RESP_COUNT > 1 or ERR_COUNT > 2: | |
RESP_COUNT, ERR_COUNT = 0, 0 | |
my_logger.debug('Restart taxi server...') | |
restart() | |
time.sleep(DELAY) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment