|
1 from twisted.application.service import Service, MultiService |
|
2 from twisted.application import internet |
|
3 from twisted.web import resource, server |
|
4 from twisted.internet import reactor |
|
5 from twisted.python import log |
|
6 |
|
7 from sqlalchemy import create_engine, pool |
|
8 |
|
9 import config, install |
|
10 from .view import xmlrpc |
|
11 from .model import setEngine, setPool |
|
12 from .controller.pool import startPool, dbPool |
|
13 |
|
14 class IroService(Service): |
|
15 def startService(self): |
|
16 log.msg("Starting service...") |
|
17 engine = create_engine(config.main.dburl, |
|
18 poolclass = pool.SingletonThreadPool, pool_size=dbPool.maxthreads, ) |
|
19 |
|
20 setEngine(engine) |
|
21 startPool(reactor) |
|
22 setPool(dbPool) |
|
23 reactor.callWhenRunning(config.readConfig) |
|
24 Service.startService(self) |
|
25 |
|
26 |
|
27 def makeService(cfg): |
|
28 top_service = MultiService() |
|
29 config.confFiles.insert(0, cfg["config"]) |
|
30 if not install.checkConfig(): |
|
31 log.err("You can create a sample configuration file by running iro-install") |
|
32 raise Exception("Please update or create your configuration file %s." % cfg["config"]) |
|
33 config.init() |
|
34 |
|
35 if not install.checkDatabaseConnection(): |
|
36 raise Exception("Can't connect to database") |
|
37 |
|
38 if not install.checkDatabase(): |
|
39 raise Exception("Database not in right format. Please run iro-install --install") |
|
40 |
|
41 routes = [ s for s in config.configParser.sections() if not s in ["main",]] |
|
42 ao = install.getAllRoutes(routes, False) |
|
43 for o in ao["orphand"]: |
|
44 log.msg("Offer(%s) is orphand (no route using this offer)."%o) |
|
45 if ao["added"]: |
|
46 raise Exception("offerlist is not up-to-date.\nPlease run iro-install --update") |
|
47 |
|
48 root = resource.Resource() |
|
49 root = xmlrpc.appendResource(root) |
|
50 |
|
51 v2 = resource.Resource() |
|
52 v2 = xmlrpc.appendResource(root) |
|
53 root.putChild('1.0a', v2) |
|
54 |
|
55 internet.TCPServer(config.main.port, server.Site(root)).setServiceParent(top_service) |
|
56 IroService().setServiceParent(top_service) |
|
57 return top_service |