2014-01-17 09:36:44 +00:00
|
|
|
#!/usr/bin/env python
|
2013-10-27 16:15:35 +00:00
|
|
|
"""
|
2014-01-17 09:14:15 +00:00
|
|
|
CLI to manage redash.
|
2013-10-27 16:15:35 +00:00
|
|
|
"""
|
2013-12-18 07:40:48 +00:00
|
|
|
import atfork
|
|
|
|
atfork.monkeypatch_os_fork_functions()
|
|
|
|
import atfork.stdlib_fixer
|
|
|
|
atfork.stdlib_fixer.fix_logging_module()
|
|
|
|
|
2013-10-25 07:26:07 +00:00
|
|
|
import argparse
|
|
|
|
import logging
|
|
|
|
import time
|
2014-01-17 09:23:06 +00:00
|
|
|
from redash import settings, app, data_manager
|
2013-10-25 07:26:07 +00:00
|
|
|
|
|
|
|
|
2014-01-17 09:23:06 +00:00
|
|
|
def start_workers():
|
2013-10-25 07:26:07 +00:00
|
|
|
try:
|
2013-12-06 13:14:39 +00:00
|
|
|
old_workers = data_manager.redis_connection.smembers('workers')
|
|
|
|
data_manager.redis_connection.delete('workers')
|
|
|
|
|
|
|
|
logging.info("Cleaning old workers: %s", old_workers)
|
|
|
|
|
2013-11-29 18:54:41 +00:00
|
|
|
data_manager.start_workers(settings.WORKERS_COUNT, settings.CONNECTION_STRING)
|
2013-10-25 07:26:07 +00:00
|
|
|
logging.info("Workers started.")
|
|
|
|
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
data_manager.refresh_queries()
|
2013-11-29 18:40:26 +00:00
|
|
|
except Exception as e:
|
|
|
|
logging.error("Something went wrong with refreshing queries...")
|
|
|
|
logging.exception(e)
|
2013-10-25 07:26:07 +00:00
|
|
|
time.sleep(60)
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
logging.warning("Exiting; waiting for threads")
|
|
|
|
data_manager.stop_workers()
|
|
|
|
|
|
|
|
|
2014-01-17 09:23:06 +00:00
|
|
|
def start_server(port, debug):
|
|
|
|
app.run(debug=debug, port=port)
|
|
|
|
|
2014-01-31 14:44:19 +00:00
|
|
|
|
2014-02-01 15:44:03 +00:00
|
|
|
def create_db(create_tables, drop_tables):
|
|
|
|
from redash.models import create_db
|
|
|
|
|
|
|
|
create_db(create_tables, drop_tables)
|
|
|
|
|
|
|
|
|
2013-10-25 07:26:07 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
channel = logging.StreamHandler()
|
|
|
|
logging.getLogger().addHandler(channel)
|
2013-12-06 13:14:39 +00:00
|
|
|
logging.getLogger().setLevel(settings.LOG_LEVEL)
|
2013-10-25 07:26:07 +00:00
|
|
|
|
|
|
|
parser = argparse.ArgumentParser()
|
2014-01-17 09:23:06 +00:00
|
|
|
subparsers = parser.add_subparsers(title='command', dest='command')
|
|
|
|
|
|
|
|
subparsers.add_parser('worker', help='start query execution workers')
|
|
|
|
server_parser = subparsers.add_parser('server', help='start api server')
|
2014-01-17 09:28:34 +00:00
|
|
|
server_parser.add_argument('--debug',
|
2014-01-17 09:23:06 +00:00
|
|
|
action='store_true',
|
|
|
|
help='start in debug mode (code reload)')
|
2014-01-17 09:28:34 +00:00
|
|
|
server_parser.add_argument('--port',
|
2014-01-17 09:23:06 +00:00
|
|
|
default=8888,
|
|
|
|
help='port to bind to')
|
2014-02-01 15:44:03 +00:00
|
|
|
db_parser = subparsers.add_parser('db', help='database operations')
|
|
|
|
db_parser.add_argument('--create-tables',
|
|
|
|
action='store_true',
|
|
|
|
help='Create all tables')
|
|
|
|
db_parser.add_argument('--drop-tables',
|
|
|
|
action='store_true',
|
|
|
|
help='Drop all tables')
|
2014-01-17 09:23:06 +00:00
|
|
|
|
2013-10-25 07:26:07 +00:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
if args.command == "worker":
|
2014-01-17 09:23:06 +00:00
|
|
|
start_workers()
|
2014-02-01 15:44:03 +00:00
|
|
|
elif args.command == 'db':
|
|
|
|
create_db(args.create_tables, args.drop_tables)
|
2014-01-17 09:23:06 +00:00
|
|
|
elif args.command == 'server':
|
2014-01-31 14:44:19 +00:00
|
|
|
start_server(args.port, args.debug)
|