db_optimize.py revision a2cb2c1722228e45cd8ba2b094d4952ae265be0f
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
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
13import socket
14import subprocess
15
16import common
17from autotest_lib.client.common_lib.cros.graphite import stats
18from autotest_lib.frontend import database_settings_helper
19from autotest_lib.scheduler import email_manager
20
21
22STATS_KEY = 'db_optimize.%s' % socket.gethostname()
23timer = stats.Timer(STATS_KEY)
24
25@timer.decorate
26def 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
36def 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
48if __name__ == '__main__':
49    main()
50