scripts/aware_failover.py [165:236]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
            with conn.cursor() as cursor:
                cursor.execute(sql_command)
                (is_reader, server_id, version) = cursor.fetchone()
                if is_reader > 0:
                    server_role = "reader"
                else:
                    server_role = "writer"
                cursor.close()


        # Take timestamp
        conn_end_time = time.time()

        # Close the connection
        conn.close()

        # Connected to a reader from the get go?
        if initial and is_reader > 0:
            raise Exception("You have connected to a reader endpoint, try connecting to the cluster endpoint instead.")

        # In the middle of a failover?
        if failover_detected:
            if is_reader > 0:
                # Display error
                print("[ERROR]", "%s: connected to reader (%s), DNS is stale!" % (time.strftime('%H:%M:%S %Z'), server_id))
            else:
                # Take timestamp
                failover_detected = False
                failover_end_time = conn_end_time

                # Display success
                print("[SUCCESS]", "%s: failover completed, took: ~ %d sec., connected to %s (%s, %s)" % (time.strftime('%H:%M:%S %Z'), (failover_end_time - failover_start_time), server_id, server_role, version))
        else:
            if is_reader > 0:
                # Detect failover
                failover_start_time = conn_start_time
                failover_detected = True

                # Display error
                print("[ERROR]", "%s: connected to reader (%s), DNS is stale!" % (time.strftime('%H:%M:%S %Z'), server_id))
            else:
                # Display info
                print("[INFO]", "%s: connected to %s (%s, %s)" % (time.strftime('%H:%M:%S %Z'), server_id, server_role, version))


        # No longer in the initial loop
        initial = False;

        # Wait 1 second
        time.sleep(1)


    # Trap keyboard interrupt, exit
    except KeyboardInterrupt:
        sys.exit("\nStopped by the user")


    # Deal with MySQL connection errors
    except pymysql.MySQLError as e:
        # Get the error code and message
        error_code = e.args[0]
        error_message = e.args[1]

        # Can't connect, assume failover
        if error_code == 2003 or error_code == 2005 or error_code == 2006 or error_code == 2013:
            # Detect failover
            if not failover_detected:
                failover_start_time = conn_start_time
            failover_detected = True

            # Display error
            print("[ERROR]", "%s: can't connect to the database (MySQL error: %d)!" % (time.strftime('%H:%M:%S %Z'), error_code))
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



scripts/simple_failover.py [92:159]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
        with conn.cursor() as cursor:
            cursor.execute(sql_command)
            (is_reader, server_id, version) = cursor.fetchone()
            if is_reader > 0:
                server_role = "reader"
            else:
                server_role = "writer"
            cursor.close()

        # Take timestamp
        conn_end_time = time.time()

        # Close the connection
        conn.close()

        # Connected to a reader from the get go?
        if initial and is_reader > 0:
            raise Exception("You have connected to a reader endpoint, try connecting to the cluster endpoint instead.")

        # In the middle of a failover?
        if failover_detected:
            if is_reader > 0:
                # Display error
                print("[ERROR]", "%s: connected to reader (%s), DNS is stale!" % (time.strftime('%H:%M:%S %Z'), server_id))
            else:
                # Take timestamp
                failover_detected = False
                failover_end_time = conn_end_time

                # Display success
                print("[SUCCESS]", "%s: failover completed, took: ~ %d sec., connected to %s (%s, %s)" % (time.strftime('%H:%M:%S %Z'), (failover_end_time - failover_start_time), server_id, server_role, version))
        else:
            if is_reader > 0:
                # Detect failover
                failover_start_time = conn_start_time
                failover_detected = True

                # Display error
                print("[ERROR]", "%s: connected to reader (%s), DNS is stale!" % (time.strftime('%H:%M:%S %Z'), server_id))
            else:
                # Display info
                print("[INFO]", "%s: connected to %s (%s, %s)" % (time.strftime('%H:%M:%S %Z'), server_id, server_role, version))

        # No longer in the initial loop
        initial = False;

        # Wait 1 second
        time.sleep(1)

    # Trap keyboard interrupt, exit
    except KeyboardInterrupt:
        sys.exit("\nStopped by the user")

    # Deal with MySQL connection errors
    except pymysql.MySQLError as e:
        # Get the error code and message
        error_code = e.args[0]
        error_message = e.args[1]

        # Can't connect, assume failover
        if error_code == 2003 or error_code == 2005 or error_code == 2006 or error_code == 2013:
            # Detect failover
            if not failover_detected:
                failover_start_time = conn_start_time
            failover_detected = True

            # Display error
            print("[ERROR]", "%s: can't connect to the database (MySQL error: %d)!" % (time.strftime('%H:%M:%S %Z'), error_code))
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



