Jakob Juelich | a2cb2c1 | 2014-10-09 15:11:35 -0700 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # Copyright (c) 2014 The Chromium OS Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | """This script will run optimize table for chromeos_autotest_db |
| 7 | |
| 8 | This script might have notable impact on the mysql performance as it locks |
| 9 | tables and rebuilds indexes. So be careful when running it on production |
| 10 | systems. |
| 11 | """ |
| 12 | |
| 13 | import socket |
| 14 | import subprocess |
| 15 | |
| 16 | import common |
| 17 | from autotest_lib.client.common_lib.cros.graphite import stats |
| 18 | from autotest_lib.frontend import database_settings_helper |
| 19 | from autotest_lib.scheduler import email_manager |
| 20 | |
| 21 | |
| 22 | STATS_KEY = 'db_optimize.%s' % socket.gethostname() |
| 23 | timer = stats.Timer(STATS_KEY) |
| 24 | |
| 25 | @timer.decorate |
| 26 | def main_without_exception_handling(): |
| 27 | database_settings = database_settings_helper.get_database_config() |
| 28 | command = ['mysqlcheck', |
| 29 | '-o', database_settings['NAME'], |
| 30 | '-u', database_settings['USER'], |
| 31 | '-p%s' % database_settings['PASSWORD'], |
| 32 | ] |
| 33 | subprocess.check_call(command) |
| 34 | |
| 35 | |
| 36 | def main(): |
| 37 | try: |
| 38 | main_without_exception_handling() |
| 39 | except Exception as e: |
| 40 | message = 'Uncaught exception; terminating db_optimize.' |
| 41 | email_manager.manager.log_stacktrace(message) |
| 42 | logging.exception(message) |
| 43 | raise |
| 44 | finally: |
| 45 | email_manager.manager.send_queued_emails() |
| 46 | |
| 47 | |
| 48 | if __name__ == '__main__': |
| 49 | main() |