Verified Commit f730c9bc authored by Peter Stanko's avatar Peter Stanko
Browse files

Worker fixes

parent a9c5c378
......@@ -129,6 +129,7 @@ class SubmissionProcessor:
self._worker_not_available()
return None
worker = random.choice(workers) # randomly select a worker
log.debug(f"[SCHED] Scheduling submission to worker -> {worker.log_name}")
return worker
def execute_submission(self, worker: Worker):
......
......@@ -3,8 +3,8 @@ from flask_restplus import Namespace
from portal import logger
from portal.rest import rest_helpers
from portal.rest.schemas import SCHEMAS
from portal.rest.custom_resource import CustomResource
from portal.rest.schemas import SCHEMAS
from portal.tools.decorators import access_log
workers_namespace = Namespace('workers')
......@@ -65,3 +65,30 @@ class WorkerResource(CustomResource):
worker = self.find.worker(wid)
self.rest.workers(worker).update(**data)
return '', 204
@workers_namespace.route('/<string:wid>/status')
@workers_namespace.param('wid', 'Worker id')
@workers_namespace.response(404, 'Worker not found')
class WorkerStatusResource(CustomResource):
@jwt_required
# @workers_namespace.response(200, 'Worker', model=worker_schema)
@workers_namespace.response(403, 'Not allowed to access worker')
def get(self, wid: str):
self.permissions().require.sysadmin(wid)
worker = self.find.worker(wid)
return self.rest.workers(worker).worker_client.status()
@workers_namespace.route('/<string:wid>/images')
@workers_namespace.param('wid', 'Worker id')
@workers_namespace.response(404, 'Worker not found')
class WorkerImagesResource(CustomResource):
@jwt_required
# @workers_namespace.response(200, 'Worker', model=worker_schema)
@workers_namespace.response(403, 'Not allowed to access worker')
def get(self, wid: str):
self.permissions().require.sysadmin(wid)
worker = self.find.worker(wid)
return self.rest.workers(worker).worker_client.list_images()
......@@ -21,9 +21,7 @@ class WorkerService(GeneralService):
@property
def worker_client(self) -> portal.tools.worker_client.WorkerClient:
if not hasattr(self, '__worker_client'):
setattr(self, '__worker_client', portal.tools.worker_client.WorkerClient(self._entity))
return getattr(self, '__worker_client')
return portal.tools.worker_client.WorkerClient(worker=self.worker)
def _set_data(self, entity: Worker, **data) -> Worker:
allowed = ['name', 'url', 'tags', 'portal_secret', 'state']
......
......@@ -117,5 +117,7 @@ class WorkerClient:
log.debug(f"[W-API] State: {result}")
return result
def _get_url(self, path: str):
return urllib.parse.urljoin(self.worker_url, '/api/v1.0', path)
def _get_url(self, path: str) -> str:
api_path = f"/api/v1.0{path}"
urljoin = urllib.parse.urljoin(self.worker_url, api_path)
return urljoin
import urllib.parse
import pytest
from portal.database import Worker
from portal.tools.worker_client import WorkerClient
@pytest.fixture()
def mocked_worker() -> Worker:
worker = Worker(name='test-worker', url='http://localhost')
worker.portal_secret = 'test-worker-secret'
return worker
@pytest.fixture()
def worker_client(mocked_worker):
return WorkerClient(worker=mocked_worker)
def test_worker_client_init_with_worker(mocked_worker, worker_client):
worker_client = worker_client
assert worker_client.worker == mocked_worker
assert worker_client.worker_url == mocked_worker.url
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment