def __init__(self, lock_file, uid, gid, is_nofork): lock = None self.thread_manager = ThreadManager() try: lock = FileLock(lock_file) self.thread_manager.lock = lock # Prepare to finish when receive a interrupt key/kill command if is_nofork: [ signal(signal_key, self.thread_manager.finish) for signal_key in [SIGTERM, SIGINT, SIGABRT] ] with lock: self.thread_manager.start() else: context = DaemonContext(working_directory=getcwd(), pidfile=lock, uid=uid, gid=gid, files_preserve=log.handler_file, # umask=0o002, # Uncomment for direct command-line debugging # stdout=sys.stdout, # stderr=sys.stderr, ) context.signal_map = dict([ (signal_key, self.thread_manager.finish) for signal_key in [SIGTERM, SIGINT, SIGABRT] ]) with context: self.thread_manager.start() except Exception, e: log.exception("The instance generated a error, please check the Traceback [%s]." % e)
def update_request(self, action_name, queue_id, status, message, retry, reprocess_time): """ Check if have some request in the queue """ args = [ queue_id, status, message, retry, reprocess_time ] try: if status != 0: log.warning("Thread [%s], processing action [%s] - ID: [%s] - Status [%s] - Message [%s]" % (self.name, action_name, queue_id, status, message), verbose=1) result = self.manager.execute_sp(self.attrib["sp_update_row"], details=True, *args) except: log.exception("Was not possible to record the status [%s] - [%s] in the row [%s]." % (status, message, queue_id)) return None if len(result) == 0 else result
def monitoring(self): """ Will be look until all the threads die. """ while True: try: still_alive = [ thread for pos, thread in enumerate(self.pool) if self.pool[pos].is_alive() ] if len(still_alive) == 0: message = "All threads was closed!" print >> stdout, message log.info(message) break sleep(settings.MANAGER_SLEEP) except: log.exception("Problem while monitoring the threads.")
from _mysql_exceptions import OperationalError from contextlib import closing from getpass import getuser from procsync.modules import settings, logger as log from procsync.modules.tools import format_value from sys import exit from warnings import filterwarnings filterwarnings("ignore", "No data .*") try: import MySQLdb except: log.exception("The application need the module mysqldb to run.\nSee: http://sourceforge.net/projects/mysql-python/") exit(1) def check_arguments(file_config, connection_key): """ Used in the modules.configuration.ConnectionConfig to identify how attributes will be necessary for this module """ # Set and return return { "host": file_config.get_config_value(connection_key, "host", default_value=None), "port": file_config.get_config_value(connection_key, "port", default_value=3306), "user": file_config.get_config_value(connection_key, "user", default_value=getuser()), "passwd": file_config.get_config_value(connection_key, "passwd", default_value=None), "db": file_config.get_config_value(connection_key, "db", default_value=None), "unix_socket": file_config.get_config_value( connection_key, "unix_socket", default_value=settings.MYSQL_UNIX_SOCKET ), "connect_timeout": file_config.get_config_value(
else: self.update_request(action_name, row[PROCESS_ID], settings.CONFIG_ERROR, "Tag [%s] of [%s] not exist!" % (action["tag"], action_name), action["retry"], action["reprocess_time"]) except OperationalError, oe: if oe[0] in self.CONFIG_MYSQL_ERROR: raise oe log.warning("Error with thread [%s], connection [%s] - [%s], try reconnect." % (self.name, self.connection_name, oe)) while not self.__stop_event.isSet(): try: self.__stop_event.wait(settings.RETRY_SLEEP) self.manager.reconnect() # if don't have error, will be able to back to the process log.warning("Thread [%s], connection [%s] reestablished" % (self.name, self.connection_name)) break except Exception: log.warning("Still have a error in thread [%s], connection [%s] - Retry after %s seconds" % (self.name, self.connection_name, settings.RETRY_SLEEP)) except: log.exception("Problem while ran the instance thread [%s]." % self.name) def join(self, timeout=None): """ Executed when have a request to exit """ self.__stop_event.set() Thread.join(self, timeout=timeout) def have_request(self): """ Check if have some request in the queue """ args = [ self.name, ] if self.attrib["complement"] is not None: args.extend(self.attrib["complement"]) result = self.manager.execute_sp(self.attrib["sp_search_row"], *args)