iro/iro.py
author Sandro Knauß <knauss@netzguerilla.net>
Fri, 10 Aug 2012 13:20:42 +0200
branchdevel
changeset 286 ec5a280707f3
child 294 0e75bd39767d
permissions -rw-r--r--
extras/iro.tac: moving makeService to iro package.

from twisted.application.service import Service, MultiService
from twisted.application import internet
from twisted.web import resource, server
from twisted.internet import reactor
from twisted.python import log

from sqlalchemy import create_engine, pool

import config, install
from .view import xmlrpc
from .model import setEngine, setPool
from .controller.pool import startPool, dbPool

class IroService(Service):
    def startService(self):
        log.msg("Starting service...")
        engine = create_engine(config.main.dburl,
               poolclass = pool.SingletonThreadPool,  pool_size=dbPool.maxthreads, )

        setEngine(engine)
        startPool(reactor)
        setPool(dbPool)
        reactor.callWhenRunning(config.readConfig)
        Service.startService(self)


def makeService(cfg):
    top_service = MultiService()
    config.confFiles.insert(0, cfg["config"])
    if not install.checkConfig():
        log.err("You can create a sample configuration file by running iro-install")
        raise Exception("Please update or create your configuration file %s." % cfg["config"])
    config.init()

    if not install.checkDatabaseConnection():
        raise Exception("Can't connect to database")
    
    if not install.checkDatabase():
        raise Exception("Database not in right format. Please run iro-install --install")

    routes = [ s for s in config.configParser.sections() if not s in ["main",]]
    ao =  install.getAllRoutes(routes, False)
    for o in  ao["orphand"]:
        log.msg("Offer(%s) is orphand (no route using this offer)."%o)
    if ao["added"]:
        raise Exception("offerlist is not up-to-date.\nPlease run iro-install --update")

    root = resource.Resource()
    root = xmlrpc.appendResource(root)
    
    v2 = resource.Resource()
    v2 = xmlrpc.appendResource(root)
    root.putChild('1.0a', v2)

    internet.TCPServer(config.main.port, server.Site(root)).setServiceParent(top_service)
    IroService().setServiceParent(top_service)
    return top_service