]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/python/osrf/system.py
0df4d73d8cc7c6407dcb425bc6f79536f5121d35
[OpenSRF.git] / src / python / osrf / system.py
1 # -----------------------------------------------------------------------
2 # Copyright (C) 2007  Georgia Public Library Service
3 # Bill Erickson <billserickson@gmail.com>
4
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 # -----------------------------------------------------------------------
15
16 import osrf.conf
17 from osrf.net import Network, set_network_handle, get_network_handle
18 import osrf.stack, osrf.log, osrf.set, osrf.cache
19 import sys, os
20
21 class System(object):
22
23     config_file = None
24     config_context = None
25
26     @staticmethod
27     def net_connect(**kwargs):
28         if get_network_handle():
29             ''' This thread already has a handle '''
30             return
31
32         config_file = kwargs.get('config_file') or System.config_file
33         config_context = kwargs.get('config_context') or System.config_context
34
35         # store the last config file info for later
36         System.config_file = config_file
37         System.config_context = config_context
38
39         # parse the config file
40         config_parser = osrf.conf.Config(config_file, config_context)
41         config_parser.parse_config()
42
43         # set up logging
44         osrf.log.initialize(
45             osrf.conf.get('loglevel'), 
46             osrf.conf.get_no_ex('syslog'),
47             osrf.conf.get_no_ex('logfile'),
48             osrf.conf.get_no_ex('client') == 'true',
49             kwargs.get('service'))
50
51         # connect to the opensrf network
52         network = Network(
53             host = osrf.conf.get('domain'),
54             port = osrf.conf.get('port'),
55             username = osrf.conf.get('username'), 
56             password = osrf.conf.get('passwd'),
57             resource = kwargs.get('resource'))
58
59         network.set_receive_callback(osrf.stack.push)
60         osrf.net.set_network_handle(network)
61         network.connect()
62
63         return network
64
65
66     @staticmethod
67     def connect(**kwargs):
68         """ Connects to the opensrf network 
69             Options:
70                 config_file
71                 config_context
72                 connect_cache
73                 resource
74         """
75
76         network = System.net_connect(**kwargs)
77
78         # load the domain-wide settings file
79         osrf.set.load(osrf.conf.get('domain'))
80
81         if kwargs.get('connect_cache'):
82             System.connect_cache()
83
84         return network
85
86
87     @staticmethod
88     def connect_cache():
89         ''' Initializes the cache connections '''
90         cache_servers = osrf.set.get('cache.global.servers.server')
91         if cache_servers:
92             if not isinstance(cache_servers, list):
93                 cache_servers = [cache_servers]
94             if not osrf.cache.CacheClient.get_client():
95                 osrf.cache.CacheClient.connect(cache_servers)
96
97     ''' 
98     @return 0 if child, pid if parent
99     '''
100     @staticmethod
101     def daemonize(parentExit=True):
102         pid = os.fork() 
103         if pid == 0:
104             os.chdir('/')
105             os.setsid()
106             sys.stdin.close()
107             sys.stdout.close()
108             sys.stderr.close()
109         elif parentExit:
110             os._exit(0)
111
112         return pid
113
114