blob: 8ade192b1250a63f02aa1c67629968c31af5a018 [file] [log] [blame]
Jakob Juelicha2cb2c12014-10-09 15:11:35 -07001#!/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
8This script might have notable impact on the mysql performance as it locks
9tables and rebuilds indexes. So be careful when running it on production
10systems.
11"""
12
Fang Deng5517a8a2015-03-11 17:23:45 -070013import logging
Jakob Juelicha2cb2c12014-10-09 15:11:35 -070014import socket
15import subprocess
Fang Deng5517a8a2015-03-11 17:23:45 -070016import sys
Jakob Juelicha2cb2c12014-10-09 15:11:35 -070017
18import common
Gabe Black1e1c41b2015-02-04 23:55:15 -080019from autotest_lib.client.common_lib.cros.graphite import autotest_stats
Jakob Juelicha2cb2c12014-10-09 15:11:35 -070020from autotest_lib.frontend import database_settings_helper
21from autotest_lib.scheduler import email_manager
22
Fang Deng5517a8a2015-03-11 17:23:45 -070023# Format Appears as: [Date] [Time] - [Msg Level] - [Message]
24LOGGING_FORMAT = '%(asctime)s - %(levelname)s - %(message)s'
Jakob Juelicha2cb2c12014-10-09 15:11:35 -070025STATS_KEY = 'db_optimize.%s' % socket.gethostname()
Gabe Black1e1c41b2015-02-04 23:55:15 -080026timer = autotest_stats.Timer(STATS_KEY)
Jakob Juelicha2cb2c12014-10-09 15:11:35 -070027
28@timer.decorate
29def main_without_exception_handling():
Fang Deng5517a8a2015-03-11 17:23:45 -070030 database_settings = database_settings_helper.get_default_db_config()
Jakob Juelicha2cb2c12014-10-09 15:11:35 -070031 command = ['mysqlcheck',
32 '-o', database_settings['NAME'],
33 '-u', database_settings['USER'],
34 '-p%s' % database_settings['PASSWORD'],
35 ]
36 subprocess.check_call(command)
37
38
39def main():
Fang Deng5517a8a2015-03-11 17:23:45 -070040 logging.basicConfig(level=logging.INFO, format=LOGGING_FORMAT)
41 logging.info('Calling: %s', sys.argv)
Jakob Juelicha2cb2c12014-10-09 15:11:35 -070042 try:
43 main_without_exception_handling()
44 except Exception as e:
45 message = 'Uncaught exception; terminating db_optimize.'
46 email_manager.manager.log_stacktrace(message)
47 logging.exception(message)
48 raise
49 finally:
50 email_manager.manager.send_queued_emails()
Fang Deng5517a8a2015-03-11 17:23:45 -070051 logging.info('db_optimize completed.')
Jakob Juelicha2cb2c12014-10-09 15:11:35 -070052
53
54if __name__ == '__main__':
55 main()