]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/python/osrf/system.py
LP1999823: Bump libtool library version
[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     @staticmethod
66     def net_disconnect():
67         network = osrf.net.get_network_handle()
68         network.disconnect()
69
70     @staticmethod
71     def connect(**kwargs):
72         """ Connects to the opensrf network 
73             Options:
74                 config_file
75                 config_context
76                 connect_cache
77                 resource
78         """
79
80         network = System.net_connect(**kwargs)
81
82         # load the domain-wide settings file
83         osrf.set.load(osrf.conf.get('domain'))
84
85         if kwargs.get('connect_cache'):
86             System.connect_cache()
87
88         return network
89
90
91     @staticmethod
92     def connect_cache():
93         ''' Initializes the cache connections '''
94         cache_servers = osrf.set.get('cache.global.servers.server')
95         if cache_servers:
96             if not isinstance(cache_servers, list):
97                 cache_servers = [cache_servers]
98             if not osrf.cache.CacheClient.get_client():
99                 osrf.cache.CacheClient.connect(cache_servers)
100
101     ''' 
102     @return 0 if child, pid if parent
103     '''
104     @staticmethod
105     def daemonize(parent_exit=True):
106         pid = os.fork() 
107         if pid == 0:
108             try:
109                 os.chdir('/')
110                 os.setsid()
111                 sys.stdin.close()
112                 sys.stdin = open(os.devnull)
113                 sys.stdout.close()
114                 sys.stdout = open(os.devnull)
115                 sys.stderr.close()
116                 sys.stderr = open(os.devnull)
117             except (OSError, ValueError):
118                 pass
119         elif parent_exit:
120             os._exit(0)
121
122         return pid
123
124