-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathremote_tarantool_server.py
94 lines (72 loc) · 2.67 KB
/
remote_tarantool_server.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
from __future__ import print_function
import sys
import os
import random
import string
import time
from .tarantool_admin import TarantoolAdmin
# a time during which try to acquire a lock
AWAIT_TIME = 60 # seconds
# on which port bind a socket for binary protocol
BINARY_PORT = 3301
def get_random_string():
return ''.join(random.choice(string.ascii_lowercase) for _ in range(16))
class RemoteTarantoolServer(object):
def __init__(self):
self.host = os.environ['REMOTE_TARANTOOL_HOST']
self.args = {}
self.args['primary'] = BINARY_PORT
self.args['admin'] = os.environ['REMOTE_TARANTOOL_CONSOLE_PORT']
assert(self.args['primary'] != self.args['admin'])
# a name to using for a lock
self.whoami = get_random_string()
self.admin = TarantoolAdmin(self.host, self.args['admin'])
self.lock_is_acquired = False
# emulate stopped server
self.acquire_lock()
self.admin.execute('box.cfg{listen = box.NULL}')
def acquire_lock(self):
deadline = time.time() + AWAIT_TIME
while True:
res = self.admin.execute('return acquire_lock("%s")' % self.whoami)
ok = res[0]
err = res[1] if not ok else None
if ok:
break
if time.time() > deadline:
raise RuntimeError('can not acquire "%s" lock: %s' % (
self.whoami, str(err)))
print('waiting to acquire "%s" lock' % self.whoami,
file=sys.stderr)
time.sleep(1)
self.lock_is_acquired = True
def touch_lock(self):
assert(self.lock_is_acquired)
res = self.admin.execute('return touch_lock("%s")' % self.whoami)
ok = res[0]
err = res[1] if not ok else None
if not ok:
raise RuntimeError('can not update "%s" lock: %s' % (
self.whoami, str(err)))
def release_lock(self):
res = self.admin.execute('return release_lock("%s")' % self.whoami)
ok = res[0]
err = res[1] if not ok else None
if not ok:
raise RuntimeError('can not release "%s" lock: %s' % (
self.whoami, str(err)))
self.lock_is_acquired = False
def start(self):
if not self.lock_is_acquired:
self.acquire_lock()
self.admin.execute('box.cfg{listen = "0.0.0.0:%s"}' %
self.args['primary'])
def stop(self):
self.admin.execute('box.cfg{listen = box.NULL}')
self.release_lock()
def is_started(self):
return self.lock_is_acquired
def clean(self):
pass
def __del__(self):
self.admin.disconnect()