1
+ import multiprocessing
1
2
import threading
2
3
import subprocess
3
4
import json
4
5
import os
6
+ import signal
5
7
import atexit
6
8
import shutil
7
9
import requests
@@ -60,22 +62,22 @@ def _get_command_line(self, near_root, node_dir, boot_key, boot_node_addr):
60
62
def wait_for_rpc (self , timeout = 1 ):
61
63
retry .retry (lambda : self .get_status (), timeout = timeout )
62
64
63
- def json_rpc (self , method , params ):
65
+ def json_rpc (self , method , params , timeout = 2 ):
64
66
j = {
65
67
'method' : method ,
66
68
'params' : params ,
67
69
'id' : 'dontcare' ,
68
70
'jsonrpc' : '2.0'
69
71
}
70
- r = requests .post ("http://%s:%s" % self .rpc_addr (), json = j , timeout = 1 )
72
+ r = requests .post ("http://%s:%s" % self .rpc_addr (), json = j , timeout = timeout )
71
73
r .raise_for_status ()
72
74
return json .loads (r .content )
73
75
74
76
def send_tx (self , signed_tx ):
75
77
return self .json_rpc ('broadcast_tx_async' , [base64 .b64encode (signed_tx ).decode ('utf8' )])
76
78
77
79
def get_status (self ):
78
- r = requests .get ("http://%s:%s/status" % self .rpc_addr (), timeout = 1 )
80
+ r = requests .get ("http://%s:%s/status" % self .rpc_addr (), timeout = 2 )
79
81
r .raise_for_status ()
80
82
return json .loads (r .content )
81
83
@@ -106,7 +108,7 @@ def __init__(self, port, rpc_port, near_root, node_dir):
106
108
self .node_key = Key .from_json_file (os .path .join (node_dir , "node_key.json" ))
107
109
self .signer_key = Key .from_json_file (os .path .join (node_dir , "validator_key.json" ))
108
110
109
- self .handle = None
111
+ self .pid = multiprocessing . Value ( 'i' , 0 )
110
112
111
113
atexit .register (atexit_cleanup , self )
112
114
@@ -124,14 +126,17 @@ def start(self, boot_key, boot_node_addr):
124
126
self .stderr = open (os .path .join (self .node_dir , 'stderr' ), 'a' )
125
127
cmd = self ._get_command_line (
126
128
self .near_root , self .node_dir , boot_key , boot_node_addr )
127
- self .handle = subprocess .Popen (
128
- cmd , stdout = self .stdout , stderr = self .stderr , env = env )
129
- self .wait_for_rpc ()
129
+ self .pid . value = subprocess .Popen (
130
+ cmd , stdout = self .stdout , stderr = self .stderr , env = env ). pid
131
+ self .wait_for_rpc (5 )
130
132
131
133
def kill (self ):
132
- if self .handle is not None :
133
- self .handle .kill ()
134
- self .handle = None
134
+ if self .pid .value != 0 :
135
+ os .kill (self .pid .value , signal .SIGKILL )
136
+ self .pid .value = 0
137
+
138
+ def reset_data (self ):
139
+ shutil .rmtree (os .path .join (self .node_dir , "data" ))
135
140
136
141
def cleanup (self ):
137
142
self .kill ()
0 commit comments