예제 #1
0
파일: worker.py 프로젝트: jcsy521/ydws
class Worker(object):
    # queue block timeout, do not make it to long and too short.
    BLOCK_TIMEOUT = 1

    def __init__(self, queue, name=None):
        self.queue = queue
        if name is None:
            self.name = id(self)
        else:
            self.name = name
        self.thread = None
        self.db = None
        self.is_alive = False
        self.mt = MT(self.queue)
    
    def start(self):
        #self.db = get_connection()
        self.thread = Thread(target=self.run)
        self.is_alive = True
        self.thread.start()

    def send_sms(self, sms):
        #mt = MT(self.queue)
        self.mt.send_sms(sms) 

    def run(self):
        while self.is_alive: # TODO: and not self.queue.empty()?
            try:
                if self.queue.qsize() > 0 :
                    s = time.time()
                    sms = self.queue.get(True, self.BLOCK_TIMEOUT)
                    logging.info("Get sms from queue used time :%s", time.time() - s)
                    self.send_sms(sms)
                    logging.info("Send sms to Gateway used time :%s", time.time() - s)
                else:
                    time.sleep(0.1)
            except Exception as e:
                logging.info("[Worker] get exception :%s", e.args)

    def stop(self):
        self.is_alive = False

    def join(self):
        self.thread.join()
        #self.db.close()
        self.mt.db.close()
        logging.warn("Worker %s joined.", self.name)

    def __del__(self):
        if self.thread and self.thread.is_alive():
            self.stop()
            self.join()
예제 #2
0
파일: server.py 프로젝트: jcsy521/ydws
def run_send_failed_mt_thread():
    logging.info("Send failed mt thread started.")
    INTERVAL = int(ConfHelper.SMS_CONF.failed_mt_interval)
    status = ErrorCode.FAILED
    mt = MT()
    try:
        while True:
            time.sleep(INTERVAL)
            status = mt.fetch_failed_mt_sms()
            if status == ErrorCode.SUCCESS:
                INTERVAL = int(ConfHelper.SMS_CONF.failed_mt_interval)
            else:
                INTERVAL = INTERVAL * 2
                if INTERVAL > 600:
                    INTERVAL = 600
    except Exception as e:
        logging.exception("Start send failed MT thread failed: %s", e.args)
예제 #3
0
파일: server.py 프로젝트: jcsy521/ydws
def run_send_mt_thread():
    logging.info("MT thread started.")
    #time interval 3 second
    INTERVAL = 3
    status = ErrorCode.FAILED
    mt = MT()
    try:
        while True:
            time.sleep(INTERVAL)
            status = mt.fetch_mt_sms()
            if status == ErrorCode.SUCCESS:
                INTERVAL = 3
            else:
                INTERVAL = INTERVAL * 2
                if INTERVAL > 600:
                    INTERVAL = 600
    except Exception as e:
        logging.exception("Start MT thread failed: %s", e.args)
예제 #4
0
파일: worker.py 프로젝트: jcsy521/ydws
 def __init__(self, queue, name=None):
     self.queue = queue
     if name is None:
         self.name = id(self)
     else:
         self.name = name
     self.thread = None
     self.db = None
     self.is_alive = False
     self.mt = MT(self.queue)
예제 #5
0
파일: server.py 프로젝트: jcsy521/ydws
def add_sms_to_queue_thread(queue):
    logging.info("[SMS] add_sms_to_queue thread started.")
    #time interval 3 second
    INTERVAL = 0.1 
    status = ErrorCode.FAILED
    mt = MT(queue)
    try:
        while True:
            time.sleep(INTERVAL)
            if queue.qsize() > 0:
                continue
            status = mt.add_sms_to_queue()
            if status == ErrorCode.SUCCESS:
                INTERVAL = 0.1 
            else:
                INTERVAL = INTERVAL * 2
                if INTERVAL > 600:
                    INTERVAL = 600
    except Exception as e:
        logging.exception("[SMS] Start add_sms_to_queue thread failed: %s", 
                          e.args)