Skip to content

Commit fa97a5a

Browse files
authored
Merge pull request #3 from abulhol/master
new pick_best_joe before every submission when path is dir
2 parents fc1881a + e39d7ee commit fa97a5a

File tree

1 file changed

+63
-49
lines changed

1 file changed

+63
-49
lines changed

src/jbxbalancer.py

+63-49
Original file line numberDiff line numberDiff line change
@@ -58,30 +58,74 @@ def main(args):
5858
job_queues = {joe: [] for joe in joes}
5959

6060
params={"comments": args.comments}
61+
62+
if args.url_mode or args.sample_url_mode:
6163

62-
success=False
63-
64-
# Try to submit to best server, if it fails continue until no server is left
65-
while joes and not success:
66-
try:
67-
joe = pick_best_joe(joes)
68-
except AllServersOfflineError as e:
69-
print("Failed to fetch any server: ", e , file=sys.stderr)
70-
break
71-
if args.url_mode or args.sample_url_mode:
64+
success=False
65+
66+
# Try to submit to best server, if it fails continue until no server is left
67+
while joes and not success:
68+
try:
69+
joe = pick_best_joe(joes)
70+
except AllServersOfflineError as e:
71+
print("Failed to fetch any server: ", e , file=sys.stderr)
72+
break
73+
7274
success = submit_url(args, joe, job_queues, params)
73-
else:
74-
success = submit_sample(args, joe, job_queues, params)
7575

76-
if success:
77-
break
76+
if success:
77+
break
78+
79+
joes.remove(joe)
80+
print("Trying to submit to next server")
7881

79-
joes.remove(joe)
80-
print("Trying to submit to next server")
82+
if not joes and not success:
83+
print("No more servers to submit to, submission failed", file=sys.stderr)
84+
85+
# File or directory submission
86+
else:
87+
88+
# if given a directory, collect all files
89+
if os.path.isdir(args.path_or_url):
90+
paths = [os.path.join(args.path_or_url, name) for name in os.listdir(args.path_or_url)]
91+
else:
92+
paths = [args.path_or_url]
8193

82-
if not joes and not success:
83-
print("No more servers to submit to, submission failed", file=sys.stderr)
94+
for path in paths:
8495

96+
success=False
97+
joes_clone = list(joes)
98+
99+
# Try to submit to best server, if it fails continue until no server is left
100+
while joes and not success:
101+
try:
102+
joe = pick_best_joe(joes_clone)
103+
except AllServersOfflineError as e:
104+
print("Failed to fetch any server: ", e , file=sys.stderr)
105+
break
106+
107+
name = os.path.basename(path)
108+
try:
109+
with open(path, "rb") as f:
110+
data = joe.submit_sample(f, params=params)
111+
112+
print("Submitted '{0}' with webid(s): {1} to server: {2}".format(name, ",".join(data["webids"]), joe.apiurl))
113+
for webid in data["webids"]:
114+
job_queues[joe].append(Submission(name, webid))
115+
success = True
116+
117+
except Exception as e:
118+
print("Submitting '{0}' failed: {1}".format(name, e), file=sys.stderr)
119+
120+
if success:
121+
break
122+
123+
joes_clone.remove(joe)
124+
print("Trying to submit to next server")
125+
126+
if not joes_clone and not success:
127+
print("No more servers to submit to, submission failed", file=sys.stderr)
128+
85129
def job_count():
86130
return sum(len(jobs) for jobs in job_queues.values())
87131

@@ -132,36 +176,6 @@ def submit_url(args, joe, job_queues, params):
132176
except Exception as e:
133177
print("Submitting '{0}' failed: {1}".format(args.path_or_url, e))
134178
return False
135-
136-
def submit_sample(args, joe, job_queues, params):
137-
'''
138-
Tries to commit a sample or directory to the server joe
139-
Returns true if at least one sample submission was successful, False otherwise
140-
'''
141-
142-
# if given a directory, collect all files
143-
if os.path.isdir(args.path_or_url):
144-
paths = [os.path.join(args.path_or_url, name) for name in os.listdir(args.path_or_url)]
145-
else:
146-
paths = [args.path_or_url]
147-
148-
for path in paths:
149-
name = os.path.basename(path)
150-
try:
151-
with open(path, "rb") as f:
152-
data = joe.submit_sample(f, params=params)
153-
154-
print("Submitted '{0}' with webid(s): {1} to server: {2}".format(args.path_or_url, ",".join(data["webids"]), joe.apiurl))
155-
for webid in data["webids"]:
156-
job_queues[joe].append(Submission(name, webid))
157-
except Exception as e:
158-
print("Submitting '{0}' failed: {1}".format(name, e), file=sys.stderr)
159-
160-
# If at least one submission worked, return True
161-
if job_queues[joe]:
162-
return True
163-
else:
164-
return False
165179

166180
def handle_finished_analysis(joe, submission, info, outdir):
167181
# download best run
@@ -241,7 +255,7 @@ def pick_best_joe(joes):
241255
pass
242256

243257
if not min_joes:
244-
raise AllServersOfflineError("All servers are offline.")
258+
raise AllServersOfflineError("All servers are offline or no more servers left.")
245259

246260
return random.choice(min_joes)
247261

0 commit comments

Comments
 (0)