import os

import atexit
import multiprocessing
import cherrypy
import platform
from django.conf import settings
from django.core.management import call_command
from kolibri.content.utils import paths
from kolibri.content.utils.annotation import update_channel_metadata_cache
from kolibri.deployment.default.wsgi import application


def start_background_workers():
    p = multiprocessing.Process(target=call_command, args=("qcluster",))

    # note: atexit normally only runs when python exits normally, aka doesn't
    # exit through a signal. However, this function gets run because cherrypy
    # catches all the various signals, and runs the atexit callbacks.
    atexit.register(p.terminate)

    p.start()


def start():

    # TODO(aronasorman): move to install/plugin-enabling scripts, and remove from here
    call_command("collectstatic", interactive=False)
    call_command("collectstatic_js_reverse", interactive=False)
    call_command("migrate", interactive=False, database="default")
    call_command("migrate", interactive=False, database="ormq")

    # start the qcluster process
    # don't run on windows; we don't run a full cluster there.
    if platform.system() != "Windows":
        start_background_workers()

    update_channel_metadata_cache()

    run_server()

def run_server():

    # Mount the application
    cherrypy.tree.graft(application, "/")

    serve_static_dir(settings.STATIC_ROOT, settings.STATIC_URL)
    serve_static_dir(settings.CONTENT_DATABASE_DIR, paths.get_content_database_url("/"))
    serve_static_dir(settings.CONTENT_STORAGE_DIR, paths.get_content_storage_url("/"))

    # Unsubscribe the default server
    cherrypy.server.unsubscribe()

    cherrypy.config.update({'server.socket_host': "0.0.0.0",
                            'server.socket_port': 8080,
                            'server.thread_pool': 30,
                            'log.screen': True})

    # Instantiate a new server object
    server = cherrypy._cpserver.Server()

    # Subscribe this server
    server.subscribe()

    # Start the server engine (Option 1 *and* 2)

    cherrypy.engine.start()
    cherrypy.engine.block()

def serve_static_dir(root, url):

    static_handler = cherrypy.tools.staticdir.handler(
        section="/",
        dir=os.path.split(root)[1],
        root=os.path.abspath(os.path.split(root)[0])
    )
    cherrypy.tree.mount(static_handler, url)

@@ -1,15 +1,4 @@
-import os%0A%0A
 impo
@@ -37,24 +37,18 @@
 %0Aimport 
-cherrypy
+os
 %0Aimport 
@@ -55,16 +55,33 @@
 platform
+%0A%0Aimport cherrypy
 %0Afrom dj
@@ -1696,33 +1696,124 @@
     
-cherrypy.config.update(%7B'
+# Instantiate a new server object%0A    server = cherrypy._cpserver.Server()%0A%0A    # Configure the server settings%0A    
 serv
@@ -1826,18 +1826,18 @@
 ket_host
-':
+ =
  %220.0.0.
@@ -1842,39 +1842,13 @@
 0.0%22
-,%0A                            '
+%0A    
 serv
@@ -1865,46 +1865,20 @@
 port
-':
+ =
  8080
-,
 %0A    
-                        '
 serv
@@ -1895,143 +1895,13 @@
 pool
-':
+ =
  30
-,%0A                            'log.screen': True%7D)%0A%0A    # Instantiate a new server object%0A    server = cherrypy._cpserver.Server()
 %0A%0A  
@@ -1999,17 +1999,16 @@
 and* 2)%0A
-%0A
     cher

Configure Cherrypy in such a way that the settings actually do something