added basic docstrings

This commit is contained in:
iLLiCiTiT 2019-10-23 18:02:53 +02:00
parent bfa61e2e98
commit 11f4451af1
4 changed files with 20 additions and 9 deletions

View file

@ -15,6 +15,7 @@ import socket_thread
def check_ftrack_url(url, log_errors=True):
"""Checks if Ftrack server is responding"""
if not url:
print('ERROR: Ftrack URL is not set!')
return None
@ -44,6 +45,7 @@ def check_ftrack_url(url, log_errors=True):
def check_mongo_url(host, port, log_error=False):
"""Checks if mongo server is responding"""
sock = None
try:
sock = socket.create_connection(
@ -114,6 +116,15 @@ def process_event_paths(event_paths):
def main_loop(ftrack_url, username, api_key, event_paths):
""" This is main loop of event handling.
Loop is handling threads which handles subprocesses of event storer and
processor. When one of threads is stopped it is tested to connect to
ftrack and mongo server. Threads are not started when ftrack or mongo
server is not accessible. When threads are started it is checked for socket
signals as heartbeat. Heartbeat must become at least once per 30sec
otherwise thread will be killed.
"""
# Set Ftrack environments
os.environ["FTRACK_SERVER"] = ftrack_url
os.environ["FTRACK_API_USER"] = username
@ -154,6 +165,7 @@ def main_loop(ftrack_url, username, api_key, event_paths):
printed_ftrack_error = False
printed_mongo_error = False
# stop threads on exit
def on_exit():
if processor_thread is not None:
processor_thread.stop()

View file

@ -48,13 +48,11 @@ class ProcessEventHub(ftrack_api.event.hub.EventHub):
sys.exit(0)
def wait(self, duration=None):
'''Wait for events and handle as they arrive.
"""Overriden wait
If *duration* is specified, then only process events until duration is
reached. *duration* is in seconds though float values can be used for
smaller values.
'''
Event are loaded from Mongo DB when queue is empty. Handled event is
set as processed in Mongo DB.
"""
started = time.time()
self.prepare_dbcon()
while True:
@ -84,6 +82,7 @@ class ProcessEventHub(ftrack_api.event.hub.EventHub):
break
def load_events(self):
"""Load not processed events sorted by stored date"""
not_processed_events = self.dbcon.find(
{"pype_data.is_processed": False}
).sort(
@ -110,8 +109,7 @@ class ProcessEventHub(ftrack_api.event.hub.EventHub):
return found
def _handle_packet(self, code, packet_identifier, path, data):
'''Handle packet received from server.'''
# if self.is_waiting:
"""Override `_handle_packet` which skip events and extend heartbeat"""
code_name = self._code_name_mapping[code]
if code_name == "event":
return

View file

@ -20,7 +20,7 @@ class StorerEventHub(ftrack_api.event.hub.EventHub):
super(StorerEventHub, self).__init__(*args, **kwargs)
def _handle_packet(self, code, packet_identifier, path, data):
'''Handle packet received from server.'''
"""Override `_handle_packet` which extend heartbeat"""
if self._code_name_mapping[code] == "heartbeat":
# Reply with heartbeat.
self.sock.sendall(b"storer")

View file

@ -9,6 +9,7 @@ from pypeapp import Logger
class SocketThread(threading.Thread):
MAX_TIMEOUT = 30
"""Thread that checks suprocess of storer of processor of events"""
def __init__(self, name, port, filepath):
super(SocketThread, self).__init__()
self.log = Logger().get_logger("SocketThread", "Event Thread")