def scheduler_process(queue, ppid): """ Процесс для работы планировщика """ ram_class = import_class(settings.SCHEDULER_RAM_STORAGE) persist_class = import_class(settings.SCHEDULER_PERSISTENT_STORAGE) scheduler = Scheduler( ram_storage=ram_class(), persist_storage=persist_class( settings.SCHEDULER_DB_PERSISTENT_STORAGE, pool_recycle=settings.POOL_RECYCLE, pool_size=settings.POOL_SIZE ) ) # Thread for immediate running of incoming commands Thread(target=inc_message_thread, args=(queue, scheduler, )).start() logging.info("Restoring previous events...") scheduler.restore() logging.info("Restoring completed.") period = 15 if settings.DEBUG is False else 5 start_time = time.time() counter = 0 while 1: # Kill himself if parent is killed if os.getppid() != ppid: sys.exit() #do something events = scheduler.get_events() for event in events: scheduler.mark_as_processing(event.event_id) logging.debug("Event (id=%s) marked as processing", event.event_id) logging.debug("Sending event (id=%s) to posting queue...", event.event_id) message = dict(event_id=event.event_id) message.update(event.data) send_to_queue(settings.RABBITMQ_HOST, settings.POSTING_QUEUE, message) allowance = time.time() - counter*period - start_time logging.debug(" [x] allowance = %s, counter = %s, per cent of period = %s", allowance, counter, int(allowance/period * 100)) if allowance > period/3.0: if period - allowance >= 0.0: time.sleep(period - allowance) else: time.sleep(0.1) counter = -1 start_time = time.time() else: time.sleep(period) counter += 1
def dispatch(self, message): """ Выбирает метод для обработки сообщения """ try: getattr(self, message.get('action')) except AttributeError: return self.__getattribute__(message.get('action'))(message) # Подтвердить успешное выполнение event_id = message.get('event_id') if event_id and message.get('action') != 'set_event_id': send_to_queue(settings.RABBITMQ_HOST, settings.SCHEDULER_QUEUE, dict( action="mark_as_processed", event_id=event_id ))
else: result = process_request(data) print(result) scData = dict(cData) scData['hangUp'] = True bot_data = { 'text': 'Your password has been reset and an email will be sent to your manager containingg the new password.', 'channelData': { 'hangUp': True }, 'address': jdata['address'] } send_to_queue(bot_data, 'send-to-bot') send_email(result) except Exception as e: print(str(e)) if jdata['channel'] == 'twilio': pass else: bot_data = { 'text': str(e), 'channelData': { 'routeToMainMenu': True }, 'address': jdata['address'] }