Example #1
0
 def execute(self, query, *args, **kwargs):
     connection = self.connect()
     try:
         # If connection is permanent is possible that have many changes of db
         db = format_value(kwargs, "db", default_value=format_value(self.attrib, "db", default_value=""))
         connection.select_db(db)
         result = None
         with closing(connection.cursor()) as cursor:
             if format_value(kwargs, "is_sp", default_value=False):
                 cursor.callproc(query, args)
             else:
                 cursor.execute(query, args)
             if format_value(kwargs, "details", default_value=False):
                 result = {"affected_rows": connection.affected_rows(), "insert_id": connection.insert_id()}
             else:
                 result = cursor.fetchall()
         # Log the warning information
         for item in connection.show_warnings():
             # 1329L, 'No data - zero rows fetched, selected, or processed'
             if item[1] == 1329:
                 log.debug(item[2])
             else:
                 log.warning("%s - %s" % (item[1], item[2]))
         return result
     except OperationalError, oe:
         # Range 2000~2999 used to client error codes
         self.is_necessary_reprocess = oe[0] > 1999 and oe[0] < 3000
         raise oe
Example #2
0
 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
Example #3
0
def main(options):
    try:
        # File that represent the lock
        lock_file = join(gettempdir(), settings.INSTANCE_NAME) if options.lock_file is None else options.lock_file
        # Information about the own user that will run the application (Used by Unix script)
        uid = gid = None
        if options.user_owner is not None:
            try:
                password_struct = getpwnam(options.user_owner)
                uid = password_struct.pw_uid
                gid = password_struct.pw_gid
            except Exception, e:
                log.warning("User name not found or have other problem [%s]!" % e)
                exit(1)
        # Test if the lock file exist
        if exists(lock_file + ".lock"):
            log.warning("Another instance is already running, or the program was interrupted.\nCheck if program already is running and if not, please delete the file [%s.lock]." % lock_file)
            exit(1)
        SingleInstance(lock_file, uid, gid, options.is_nofork)
Example #4
0
 def update_request(self, action_name, queue_id, status, message, retry, reprocess_time, retry_update=0):
     """
     Check if have some request in the queue
     retry_update is internal use.
     """
     args = [ queue_id, status, message, retry, reprocess_time ]
     result = None
     try:
         if status != 0:
             log.warning("[%s/%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 OperationalError, oe:
         # Deadlock found when trying to get lock; try restarting transaction
         if oe[0] == 1213:
             if retry_update < 5:
                 # Sleep to give the time to unlock
                 sleep(0.5)
                 retry_update += 1
                 log.warning("Deadlock found, retrying [%s/5]" % retry_update)
                 return self.update_request(action_name, queue_id, status, message, retry, reprocess_time, retry_update)
             else:
                 log.critical("Was not possible to update the information, please check the process.")
         else:
             log.critical("An operation error occurred: %s" % str(oe))
Example #5
0
                                        if destination_item["stop_on_error"]:
                                            self.update_request(action_name, row[PROCESS_ID], result_code, return_error_message, action["retry"], action["reprocess_time"])
                                            break
                                        else:
                                            error_result = [ result_code, return_error_message ]
                                if error_result:
                                    have_error = True
                                    self.update_request(action_name, row[PROCESS_ID], error_result[0], error_result[1], action["retry"], action["reprocess_time"])
                            if not have_error:
                                self.update_request(action_name, row[PROCESS_ID], settings.PROCESS_SUCCESS, "", action["retry"], action["reprocess_time"])
                            log.info("%s Finished%s!" % (main_message, " with error" if have_error else ""), verbose=1)
                        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("[%s/%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("[%s/%s] reestablished" % (self.name, self.connection_name))
                            break
                        except Exception:
                            log.warning("Still have a error in [%s/%s] - Retry after %s seconds" % (self.name, self.connection_name, settings.RETRY_SLEEP))
        except:
            log.critical("Problem while ran the instance thread [%s]." % self.name)

    def join(self, timeout=None):
        """
        Executed when have a request to exit