handle multiple projects

This commit is contained in:
simon987 2019-03-30 21:22:34 -04:00
parent 3aa187c2c4
commit 7981e0aebc
3 changed files with 30 additions and 8 deletions

View File

@ -33,14 +33,24 @@ signal.signal(signal.SIGTERM, cleanup)
def drone(ctx: WorkerContext):
global die
sorted_projects = sorted(worker.get_project_list(), key=lambda p: p["priority"], reverse=True)
while not die:
task = worker.fetch_task(1)
task = None
try:
if task is not None:
with lock:
current_tasks.add(task.id)
ctx.execute_task(task)
else:
ok = False
for project in sorted_projects:
task = worker.fetch_task(project["id"])
if task is not None:
with lock:
current_tasks.add(task.id)
ctx.execute_task(task)
ok = True
break
if not ok:
print("No tasks, waiting")
time.sleep(10)
finally:
with lock:
@ -56,8 +66,10 @@ worker = Worker.from_file(api)
if not worker:
worker = api.make_worker("drone")
worker.dump_to_file()
worker.request_access(1, True, False)
projects = worker.get_project_list()
for project in projects:
r = worker.request_access(project["id"], assign=True, submit=False)
print("Request access for %d r=%s" % (project["id"], r.text))
print("Starting %d working contexts" % (THREAD_COUNT,))
for i in range(THREAD_COUNT):

View File

@ -100,6 +100,9 @@ class Worker:
"secret": self.secret_b64
}, out)
def get_project_list(self):
return self._api.get_project_list(self).json()["content"]["projects"]
@staticmethod
def from_file(api):
if os.path.exists("worker.json"):
@ -200,6 +203,9 @@ class TaskTrackerApi:
if r.status_code == 200:
return json.loads(r.text)["content"]["secret"]
def get_project_list(self, worker: Worker):
return self._http_get("/project/list", worker)
def _http_get(self, endpoint: str, worker: Worker = None):
if worker is not None:
headers = format_headers(secret=worker.secret_b64, wid=worker.id)

View File

@ -3,6 +3,7 @@ import json
import os
import shutil
import subprocess
import traceback
from subprocess import Popen
from tt_drone.api import (Project, Worker, Task)
@ -62,7 +63,10 @@ class WorkerContext:
"verification"] if "verification" in json_result else 0).text
+ "for result" + result)
except Exception as e:
print(str(e) + traceback.format_exc())
return
else:
print(path + "/run doesn't exist!")
def _do_post_task_hooks(self, res):