2018-02-13 16:44:48 +00:00
|
|
|
import xmlrpc.client
|
2018-02-06 14:34:43 +00:00
|
|
|
from contextlib import suppress
|
|
|
|
from xmlrpc.client import Fault as SupervisorFault
|
2018-02-13 14:02:48 +00:00
|
|
|
from time import time, sleep
|
|
|
|
from functools import wraps
|
2018-01-24 11:17:15 +00:00
|
|
|
|
2018-02-09 08:58:47 +00:00
|
|
|
from tfw.config import tfwenv
|
2017-11-27 17:42:34 +00:00
|
|
|
|
|
|
|
|
2018-01-10 15:49:23 +00:00
|
|
|
def create_source_code_response_data(filename, content, language):
|
|
|
|
return {
|
|
|
|
'filename': filename,
|
|
|
|
'content': content,
|
|
|
|
'language': language
|
2018-01-24 11:17:15 +00:00
|
|
|
}
|
|
|
|
|
2018-01-29 14:28:51 +00:00
|
|
|
|
2018-01-24 11:17:15 +00:00
|
|
|
class SupervisorMixin:
|
2018-02-09 08:58:47 +00:00
|
|
|
supervisor = xmlrpc.client.ServerProxy(tfwenv.SUPERVISOR_HTTP_URI).supervisor
|
2018-01-29 14:28:51 +00:00
|
|
|
|
2018-02-06 14:34:43 +00:00
|
|
|
def stop_process(self):
|
|
|
|
with suppress(SupervisorFault):
|
|
|
|
self.supervisor.stopProcess(self.process_name)
|
|
|
|
|
|
|
|
def start_process(self):
|
|
|
|
self.supervisor.startProcess(self.process_name)
|
|
|
|
|
|
|
|
def restart_process(self):
|
|
|
|
self.stop_process()
|
|
|
|
self.start_process()
|
|
|
|
|
2018-01-29 14:28:51 +00:00
|
|
|
|
2018-02-13 14:02:48 +00:00
|
|
|
class RateLimiter:
|
|
|
|
def __init__(self, rate_per_second):
|
|
|
|
self.min_interval = 1 / float(rate_per_second)
|
|
|
|
self.last_call = time()
|
|
|
|
|
|
|
|
def __call__(self, fun):
|
|
|
|
@wraps(fun)
|
|
|
|
def wrapper(*args, **kwargs):
|
|
|
|
self._limit_rate()
|
|
|
|
fun(*args, **kwargs)
|
|
|
|
return wrapper
|
|
|
|
|
2018-02-13 15:29:45 +00:00
|
|
|
def _limit_rate(self):
|
2018-02-13 14:02:48 +00:00
|
|
|
since_last_call = time() - self.last_call
|
|
|
|
to_next_call = self.min_interval - since_last_call
|
|
|
|
self.last_call = time()
|
|
|
|
if to_next_call > 0:
|
|
|
|
sleep(to_next_call)
|