Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add logging to setup and initialize_kalite (2) #5441

Merged
merged 8 commits into from
Apr 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ test:
- sudo mv geckodriver /home/ubuntu/bin
- export PATH="$PATH:/home/ubuntu/bin"
override:
- make assets
- make assets:
parallel: true
- make docs
- kalite start --traceback -v2
- sleep 6s # Necessary for server to be ready
- kalite status
- kalite stop --traceback -v2
- case $CIRCLE_NODE_INDEX in 0) coverage run --source=kalite --omit="kalite/testing/*,*/tests/*,*/migrations/*,kalite/packages/*" bin/kalite manage test --bdd-only ;; 1) coverage run --source=kalite --omit="kalite/testing/*,kalite/packages/*,*/tests/*,*/migrations/*" bin/kalite manage test --no-bdd;; esac:
parallel: true
# TODO: replace below with "make lint" when we're pep8
- npm install -g jshint
- jshint kalite/*/static/js/*/
post:
Expand Down
2 changes: 2 additions & 0 deletions docs/installguide/release_notes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ Bug fixes
* Respect selected date range on tabular coach report :url-issue:`5022`
* Correct summary of total exercise attempts on coach reports :url-issue:`5020`
* Do not load video into memory to check its size, just use disk stats :url-issue:`2909`
* Print server address after ``kalite start`` :url-issue:`5441`
* Log everything from automatic initialization in ``kalite start`` and ``kalite manage setup`` :url-issue:`5408`

Known issues
^^^^^^^^^^^^
Expand Down
49 changes: 27 additions & 22 deletions kalite/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ def get_pid():
TODO: This function has for historical reasons maintained to try to get
the PID of a KA Lite server without a PID file running on the same port.
The behavior is to make an HTTP request for the PID on a certain port.
This behavior is stupid, because a KA lite process may just be part of a
This behavior is stupid, because a KA Lite process may just be part of a
process pool, so it won't be able to tell the correct PID for sure,
anyways.
The behavior is also quite redundant given that `kalite start` should always
Expand Down Expand Up @@ -353,13 +353,14 @@ def get_pid():
# Probably a mis-configured KA Lite
raise NotRunning(STATUS_SERVER_CONFIGURATION_ERROR)

served_pid = -1
try:
pid = int(response.read())
served_pid = int(response.read())
except ValueError:
# Not a valid INT was returned, so probably not KA Lite
raise NotRunning(STATUS_UNKNOWN_INSTANCE)

if pid == pid:
if pid == served_pid:
return pid, LISTEN_ADDRESS, listen_port # Correct PID !
else:
# Not the correct PID, maybe KA Lite is running from somewhere else!
Expand All @@ -368,6 +369,21 @@ def get_pid():
raise NotRunning(STATUS_UNKNOW) # Could not determine


def print_server_address(port):
# Print output to user about where to find the server
addresses = get_ip_addresses(include_loopback=False)
print("To access KA Lite from another connected computer, try the following address(es):")
for addr in addresses:
print("\thttp://%s:%s/\n" % (addr, port))
print("To access KA Lite from this machine, try the following address:")
print("\thttp://127.0.0.1:%s/" % port)

for addr in get_urls_proxy(output_pipe=sys.stdout):
print("\t{}".format(addr))

print("")


class ManageThread(Thread):

def __init__(self, command, *args, **kwargs):
Expand Down Expand Up @@ -411,7 +427,7 @@ def manage(command, args=None, as_thread=False):
return thread


def start(debug=False, daemonize=True, args=[], skip_job_scheduler=False, port=None):
def start(debug=False, daemonize=True, args=[], skip_job_scheduler=False, port=None, auto_initialize=True):
"""
Start the kalite server as a daemon

Expand All @@ -421,7 +437,6 @@ def start(debug=False, daemonize=True, args=[], skip_job_scheduler=False, port=N
:param daemonize: Default True, will run in foreground if False
:param skip_job_scheduler: Skips running the job scheduler in a separate thread
"""
# TODO: Do we want to fail if running as root?

port = int(port or DEFAULT_LISTEN_PORT)

Expand All @@ -430,8 +445,6 @@ def start(debug=False, daemonize=True, args=[], skip_job_scheduler=False, port=N
else:
sys.stderr.write("Running 'kalite start' as daemon (system service)\n")

sys.stderr.write("\nStand by while the server loads its data...\n\n")

if os.path.exists(STARTUP_LOCK):
try:
pid, __ = read_pid_file(STARTUP_LOCK)
Expand Down Expand Up @@ -481,28 +494,20 @@ def start(debug=False, daemonize=True, args=[], skip_job_scheduler=False, port=N
kwargs = {}
# Truncate the file
open(SERVER_LOG, "w").truncate()
print("Going to daemon mode, logging to {0}".format(SERVER_LOG))
print("Going to daemon mode, logging to {0}\n".format(SERVER_LOG))
print_server_address(port)
kwargs['out_log'] = SERVER_LOG
kwargs['err_log'] = SERVER_LOG
become_daemon(**kwargs)
# Write the new PID
with open(PID_FILE, 'w') as f:
f.write("%d\n%d" % (os.getpid(), port))

manage('initialize_kalite')

# Print output to user about where to find the server
addresses = get_ip_addresses(include_loopback=False)
sys.stdout.write("To access KA Lite from another connected computer, try the following address(es):\n")
for addr in addresses:
sys.stdout.write("\thttp://%s:%s/\n" % (addr, port))
sys.stdout.write("To access KA Lite from this machine, try the following address:\n")
sys.stdout.write("\thttp://127.0.0.1:%s/\n" % port)

for addr in get_urls_proxy(output_pipe=sys.stdout):
sys.stdout.write("\t{}\n".format(addr))
if auto_initialize:
manage('initialize_kalite')

sys.stdout.write("\n")
if not daemonize:
print_server_address(port)

# Start the job scheduler (not Celery yet...)
cron_thread = None
Expand Down Expand Up @@ -576,7 +581,7 @@ def stop(args=[], sys_exit=True):
killed_with_force = True
except ValueError:
sys.stderr.write("Could not find PID in .pid file\n")
except OSError: # TODO: More specific exception handling
except OSError:
sys.stderr.write("Could not read .pid file\n")
if not killed_with_force:
if sys_exit:
Expand Down
6 changes: 3 additions & 3 deletions kalite/distributed/management/commands/initialize_kalite.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
"""
This is a command-line tool to execute functions helpful to testing.
"""
from django.conf import settings
import logging

from django.core.management import call_command
from django.core.management.base import BaseCommand
from django.db import DatabaseError

from fle_utils.config.models import Settings

logging = settings.LOG
logger = logging.getLogger(__name__)


class Command(BaseCommand):
Expand All @@ -32,7 +32,7 @@ def setup_server_if_needed(self):
except (DatabaseError, AssertionError):
from django import db
db.close_connection() # So that the database file is free.
logging.info("Setting up KA Lite; this may take a few minutes; please wait!\n")
logger.info("Setting up KA Lite; this may take a few minutes; please wait!\n")
call_command("setup", interactive=False)
# Double check the setup process worked ok.
assert Settings.get("database_version") == VERSION, "There was an error configuring the server. Please report the output of this command to Learning Equality."
Expand Down
Loading