-
Notifications
You must be signed in to change notification settings - Fork 15
/
provider.py
196 lines (166 loc) · 5.99 KB
/
provider.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#!/usr/bin/python
# coding=UTF-8
# ex:ts=4:sw=4:et=on
# Copyright (c) 2013, Mathijs Dumon
# All rights reserved.
# Complete license can be found in the LICENSE file.
import time, atexit, os
from traceback import print_exc
import logging
logger = logging.getLogger(__name__)
import Pyro4
import Pyro4.naming
try:
import threading as threading
except ImportError:
import dummy_threading as threading
try:
from fastrlock.rlock import FastRLock as RLock
except ImportError:
from threading import RLock
from pyxrd.generic.asynchronous.exceptions import *
from pyxrd.data.appdirs import user_log_dir
from . import settings
from .utils import start_script
from .status_thread import StatusThread
class Pyro4AsyncServerProvider(object):
"""
Provider for the Pyro4 PyXRD server
"""
_STATUS_NS_NOT_RUNNING = ("#FFFF00", "Nameserver Error", "Pyro4 Nameserver not running")
_STATUS_ERR_NS_RUNNING = ("#FF0000", "Nameserver Exception", "Exception when checking if Pyro4 Nameserver is running")
_STATUS_NS_NOT_LISTED = ("#FFFF00", "Nameserver Error", "Pyro4 PyXRD server not listed")
_STATUS_ERR_NS_LISTED = ("#FF0000", "Nameserver Exception", "Exception when checking if Pyro4 PyXRD server is listed")
_STATUS_NO_CONN_PYXRD_SERVER = ("#FFFF00", "PyXRD Server Error", "Cannot connect to Pyro4 PyXRD server")
_STATUS_ERR_CONN_PYXRD_SERVER = ("#FF0000", "PyXRD Server Exception", "Exception when connecting to Pyro4 PyXRD server")
_STATUS_SUCCESS = ("#00FF00", "Connected (Pyro4)", "Succesfully connected to Pyro4 PyXRD Server")
_updater = None
status = _STATUS_NS_NOT_RUNNING
status_lock = RLock()
NS_CACHE_TIMEOUT = 30
ns = None
ns_ts = 0
@classmethod
def _locate_ns(cls):
if cls.ns is None or time.time() - cls.ns_ts >= cls.NS_CACHE_TIMEOUT:
cls.ns = Pyro4.naming.locateNS()
cls.ns_ts = time.time()
return cls.ns
PROXY_CACHE_TIMEOUT = 30
proxy = None
proxy_ts = 0
@classmethod
def _get_proxy(cls):
if cls.proxy is None or time.time() - cls.proxy_ts >= cls.PROXY_CACHE_TIMEOUT:
if cls.proxy is None:
cls.proxy = Pyro4.Proxy("PYRONAME:%s" % settings.PYRO_NAME)
else:
cls.proxy.pyroBind()
cls.proxy_ts = time.time()
return cls.proxy
@classmethod
def check_nameserver_alive(cls):
try:
ns = cls._locate_ns()
ns.ping()
return True
except:
print_exc()
return False
@classmethod
def check_server_is_listed(cls):
try:
ns = cls._locate_ns()
objects = ns.list()
if settings.PYRO_NAME in objects:
return True
else:
return False
except:
print_exc()
return False
"""
Async Provider Implementation:
"""
@classmethod
def get_status(cls):
"""
Should return a three-tuple consisting of the status colour, label and a description:
("#FF0000", "Error", "Nameserver not running")
Status is updated periodically.
"""
# first call:
if cls._updater == None:
cls._update_status(cls._get_status())
cls._updater = StatusThread(5, cls)
cls._updater.setDaemon(True)
cls._updater.start()
return cls.status
@classmethod
def _update_status(cls, status):
with cls.status_lock:
cls.status = status
@classmethod
def _get_status(cls):
try:
if not cls.check_nameserver_alive():
return cls._STATUS_NS_NOT_RUNNING
except:
print_exc()
return cls._STATUS_ERR_NS_RUNNING
try:
if not cls.check_server_is_listed():
return cls._STATUS_NS_NOT_LISTED
except:
print_exc()
return cls._STATUS_ERR_NS_LISTED
try:
if not cls.check_server_is_alive():
return cls._STATUS_NO_CONN_PYXRD_SERVER
except:
print_exc()
return cls._STATUS_ERR_CONN_PYXRD_SERVER
return cls._STATUS_SUCCESS
@classmethod
def check_server_is_alive(cls):
try:
server = cls.get_server(auto_start=False)
return server.loopCondition()
except:
logging.info("Pyro4 PyXRD server not (yet) running!")
return False
@classmethod
def get_server(cls, auto_start=True):
if auto_start:
cls.launch_server()
try:
return Pyro4.Proxy("PYRONAME:%s" % settings.PYRO_NAME)
except:
print_exc()
logging.error("Could not connect to Pyro4 PyXRD server.")
raise ServerNotRunningException("Pyro4 PyXRD Server is not running!")
@classmethod
def launch_server(cls):
if not cls.check_server_is_alive():
log_file = os.path.join(user_log_dir('PyXRD'), 'server.log')
start_script("run_server.py", auto_kill=not settings.KEEP_SERVER_ALIVE, log_file=log_file)
ttl = 15
delay = 0.2
while not cls.check_server_is_alive():
time.sleep(delay) # wait
ttl -= 1
if ttl == 0:
raise ServerStartTimeoutExcecption("Pyro4 PyXRD Server is not running!")
logging.info("Pyro4 PyXRD server is running!")
if not settings.KEEP_SERVER_ALIVE:
atexit.register(cls.stop_server)
@classmethod
def stop_server(cls):
try:
server = cls.get_server(auto_start=False)
server.shutdown()
except:
logging.error("Error when trying to shut down Pyro4 PyXRD server!")
print_exc()
raise ServerNotRunningException("Pyro4 PyXRD Server is not running!")
pass #end of class