02f52ad1c4d8bf4ff9b5ba88aad712a2f1bba101
[odoo/odoo.git] / bin / pooler.py
1 ##############################################################################
2 #
3 # Copyright (c) 2006 TINY SPRL. (http://tiny.be) All Rights Reserved.
4 #
5 # WARNING: This program as such is intended to be used by professional
6 # programmers who take the whole responsability of assessing all potential
7 # consequences resulting from its eventual inadequacies and bugs
8 # End users who are looking for a ready-to-use solution with commercial
9 # garantees and support are strongly adviced to contract a Free Software
10 # Service Company
11 #
12 # This program is Free Software; you can redistribute it and/or
13 # modify it under the terms of the GNU General Public License
14 # as published by the Free Software Foundation; either version 2
15 # of the License, or (at your option) any later version.
16 #
17 # This program is distributed in the hope that it will be useful,
18 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 # GNU General Public License for more details.
21 #
22 # You should have received a copy of the GNU General Public License
23 # along with this program; if not, write to the Free Software
24 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25 #
26 ##############################################################################
27
28 import sql_db
29 import osv.osv
30 import tools
31 import addons
32
33 db_dic = {}
34 pool_dic = {}
35
36 def get_db_and_pool(db_name, force_demo=False, status={}, update_module=False):
37         if db_name in db_dic:
38                 db = db_dic[db_name]
39         else:
40                 print 'Pooler Connecting to', db_name
41                 db = sql_db.db_connect(db_name)
42                 db_dic[db_name] = db
43         
44         if db_name in pool_dic:
45                 pool = pool_dic[db_name]
46         else:
47                 pool = osv.osv.osv_pool()
48                 pool_dic[db_name] = pool
49                 addons.load_modules(db, force_demo, status, update_module)
50
51                 import report
52                 report.interface.register_all(db)
53
54                 pool.get('ir.cron')._poolJobs(db.dbname)
55         return db, pool
56
57 def restart_pool(db_name, force_demo=False, update_module=False):
58 #       del db_dic[db_name]
59         del pool_dic[db_name]
60         return get_db_and_pool(db_name, force_demo, update_module=update_module)
61
62 def close_db(db_name):
63         if db_name in db_dic:
64                 db_dic[db_name].truedb.close()
65                 del db_dic[db_name]
66         if db_name in pool_dic:
67                 del pool_dic[db_name]
68
69 def get_db_only(db_name):
70         if db_name in db_dic:
71                 db = db_dic[db_name]
72         else:
73                 db = sql_db.db_connect(db_name)
74                 db_dic[db_name] = db
75         return db
76
77 def get_db(db_name):
78 #       print "get_db", db_name
79         return get_db_and_pool(db_name)[0]
80
81 def get_pool(db_name, force_demo=False, status={}, update_module=False):
82 #       print "get_pool", db_name
83         pool = get_db_and_pool(db_name, force_demo, status, update_module)[1]
84 #       addons.load_modules(db_name, False)
85 #       if not pool.obj_list():
86 #               pool.instanciate()
87 #       print "pool", pool
88         return pool
89 #       return get_db_and_pool(db_name)[1]
90
91 def init():
92         global db
93 #       db = get_db_only(tools.config['db_name'])
94         sql_db.init()
95