How to use is_shutting_down method in autotest

Best Python code snippet using autotest_python

websocket.py

Source:websocket.py Github

copy

Full Screen

1# Copyright 2004-present Facebook. All Rights Reserved.2import simplejson3from stationexec.logger import log4from stationexec.station.events import emit_event, register_for_event_group, InfoEvents, \5 ActionEvents, RetrievalEvents, emit_event_non_blocking6from stationexec.utilities.ioloop_ref import IoLoop7from stationexec.utilities.uuidstr import get_uuid8from tornado import gen9from tornado.iostream import StreamClosedError10from tornado.web import HTTPError11from tornado.websocket import WebSocketClosedError, WebSocketHandler12is_shutting_down = False13class StationSocket(WebSocketHandler):14 """Tornado endpoint handler for requesting a new web socket"""15 manager = None16 uuid = None17 stationuuid = None18 def initialize(self, **kwargs):19 """Get station socket ready for operation."""20 self.manager = kwargs["socket_manager"]21 self.uuid = get_uuid()22 self.stationuuid = kwargs["stationuuid"]23 def open(self):24 self.manager.save(self)25 def prepare(self):26 global is_shutting_down27 if is_shutting_down:28 raise HTTPError()29 else:30 super(WebSocketHandler, self).prepare()31 def on_message(self, message):32 try:33 data = simplejson.loads(message)34 if not isinstance(data, dict):35 raise Exception()36 except Exception:37 data = {38 "type": "unknown",39 "message": "message"40 }41 if "_webevent" in data:42 event_type, event = data["_webevent"].split(".", 1)43 # Cast events into their enum types to verify they are valid events44 if event_type == "ActionEvents":45 event = ActionEvents[event]46 elif event_type == "InfoEvents":47 event = InfoEvents[event]48 elif event_type == "RetrievalEvents":49 self.handle_retrieval_events(RetrievalEvents[event], data)50 else:51 event = InfoEvents.WEBSOCKET_INCOMING52 data["source"] = "socket.{0}".format(self.uuid)53 data["event"] = str(event)54 emit_event_non_blocking(event, data)55 def handle_retrieval_events(self, event, data):56 data["stationuuid"] = self.stationuuid57 result = emit_event(event, data)58 out_data = {59 "target": data["_websource"],60 "result": result,61 "request_event": str(event)62 }63 emit_event_non_blocking(InfoEvents.UI_DATA_DELIVERY, out_data)64 def on_close(self):65 self.manager.delete(self)66class SocketManager(object):67 """ An object that stores all the sockets that have been opened by clients. """68 def __init__(self):69 # Stores all the sockets that have been created, so that we can communicate with them.70 self._socket_sessions = {} # type: dict71 def initialize(self):72 """Get socket manager ready for operation."""73 register_for_event_group("SocketManager", InfoEvents, self.send_all)74 def shutdown(self):75 global is_shutting_down76 is_shutting_down = True77 IoLoop().current().spawn_callback(self._close_all)78 def save(self, socket):79 """Store a new socket."""80 if is_shutting_down:81 return82 if socket.uuid not in self._socket_sessions:83 self._socket_sessions[socket.uuid] = socket84 def delete(self, socket):85 """Remove a socket from the active list."""86 if socket.uuid in self._socket_sessions:87 self._socket_sessions.pop(socket.uuid, None)88 def send_all(self, **event_data):89 """ Send a message to all open sockets. """90 if is_shutting_down:91 return92 event_data["_event"] = str(event_data["_event"])93 # Schedule it to execute on the main Tornado thread to avoid socket corruption94 IoLoop().current().spawn_callback(self._send_all, simplejson.dumps(event_data))95 @gen.coroutine96 def _send_all(self, message):97 """ Internal function. """98 closed_sockets = []99 sockets = list(self._socket_sessions.keys())100 for socket_id in sockets:101 if is_shutting_down:102 return103 try:104 yield self._socket_sessions[socket_id].write_message(message)105 except (WebSocketClosedError, StreamClosedError) as e:106 log.debug(4, "tried to write to closed socket: {0}".format(e))107 closed_sockets.append(socket_id)108 except BufferError as e:109 log.exception("socket buffer error with message '{0}'".format(message), e)110 for socket_id in closed_sockets:111 self._socket_sessions.pop(socket_id, None)112 @gen.coroutine113 def _close_all(self):114 sockets = list(self._socket_sessions.keys())115 for socket_id in sockets:116 try:117 yield self._socket_sessions[socket_id].close()118 except (WebSocketClosedError, StreamClosedError, BufferError):...

Full Screen

Full Screen

batch_worker.py

Source:batch_worker.py Github

copy

Full Screen

1from time import sleep2from kafka import KafkaConsumer3import threading4from queue import Queue, Empty5import signal6emails = Queue()7is_shutting_down = False8class Consumer(threading.Thread):9 def __init__(self):10 threading.Thread.__init__(self)11 def run(self):12 consumer = KafkaConsumer(13 "user_signups", bootstrap_servers=["localhost:9092"], group_id="group1"14 )15 for message in consumer:16 self.insert_to_buffer(message.value)17 if is_shutting_down:18 break19 consumer.close()20 def insert_to_buffer(self, message):21 print("received a message, inserting into a queue buffer")22 emails.put(message)23def process_messages():24 print("processing message in queue buffer")25 temp_emails = []26 try:27 while True:28 temp_emails.append(emails.get_nowait())29 except Empty:30 pass31 # Combine all emails in 1 call32 # This is the beauty of batch worker33 print(f"sending email to user " + str(temp_emails))34 sleep(0.5) # pretend to do work35 print(f"updating status to Waiting Confirmation for users " + str(temp_emails))36 sleep(0.5) # pretend to do work37 print("finished processing messages")38def exit_gracefully(*args, **kwargs):39 global is_shutting_down40 is_shutting_down = True41 process_messages()42 exit()43if __name__ == "__main__":44 signal.signal(signal.SIGINT, exit_gracefully)45 signal.signal(signal.SIGTERM, exit_gracefully)46 print("starting batch consumer worker")47 consumer = Consumer()48 consumer.daemon = True49 consumer.start()50 while True:51 process_messages()...

Full Screen

Full Screen

streaming_worker.py

Source:streaming_worker.py Github

copy

Full Screen

1from kafka import KafkaConsumer2from time import sleep3import signal4is_shutting_down = False5def process_message(email):6 print(f"sending email to {email}")7 sleep(0.5) # pretend to do work8 print(f"updating user {email} status to Waiting Confirmation")9 sleep(0.5) # pretend to do work10 print("finished processing message")11def graceful_exit(*args, **kwargs):12 global is_shutting_down13 is_shutting_down = True14if __name__ == "__main__":15 signal.signal(signal.SIGINT, graceful_exit)16 signal.signal(signal.SIGTERM, graceful_exit)17 print("starting streaming consumer worker")18 consumer = KafkaConsumer(19 "user_signups", bootstrap_servers=["localhost:9092"], group_id="group1"20 )21 for message in consumer:22 process_message(message.value)23 if is_shutting_down:24 break25 print("End of the program. I was killed gracefully")26 consumer.close()...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run autotest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful