Skip to content

Commit

Permalink
Merge pull request #264 from querti/return-assigned-tasks
Browse files Browse the repository at this point in the history
Add option for get_worker_tasks to also return ASSIGNED tasks
  • Loading branch information
querti authored Jul 2, 2024
2 parents 1ac4034 + bbb3474 commit e769532
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
13 changes: 10 additions & 3 deletions kobo/hub/xmlrpc/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,25 @@ def get_worker_id(request):


@validate_worker
def get_worker_tasks(request):
def get_worker_tasks(request, also_assigned=False):
"""
Get list of OPEN tasks executed on a worker.
Get list of OPEN (and maybe ASSIGNED) tasks executed on a worker.
@param also_assigned: Whether to return ASSIGNED tasks in addition to OPEN tasks.
@type also_assigned: bool
@rtype: list
"""
task_list = []

# Worker.running_tasks returns both OPEN and ASSIGNED tasks but all calls
# by the worker assume that only OPEN tasks will be returned.
# See: https://github.com/release-engineering/kobo/pull/251#issue-2183712995
for task in request.worker.running_tasks().filter(state=TASK_STATES['OPEN']).order_by("-exclusive", "-awaited", "id"):
# Old behavior can be toggled with also_assigned parameter
if also_assigned:
tasks = request.worker.running_tasks().order_by("-exclusive", "-awaited", "id")
else:
tasks = request.worker.running_tasks().filter(state=TASK_STATES['OPEN']).order_by("-exclusive", "-awaited", "id")
for task in tasks:
task_info = task.export()

# set wakeup alert
Expand Down
19 changes: 19 additions & 0 deletions tests/test_xmlrpc_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,25 @@ def test_get_worker_tasks(self):

self.assertEqual(len(tasks), 1)
self.assertEqual(tasks[0]['state'], TASK_STATES['OPEN'])

def test_get_worker_tasks_assigned(self):
for state in TASK_STATES:
Task.objects.create(
worker=self._worker,
arch=self._arch,
channel=self._channel,
owner=self._user,
state=TASK_STATES[state],
)

req = _make_request(self._worker)
tasks = worker.get_worker_tasks(req, True)

self.assertEqual(len(tasks), 2)
self.assertTrue(tasks[0]['id'] < tasks[1]['id'])

for task in tasks:
self.assertTrue(task['state'] in [TASK_STATES['ASSIGNED'], TASK_STATES['OPEN']])

def test_get_worker_tasks_check_wait(self):
t_parent = Task.objects.create(
Expand Down

0 comments on commit e769532

Please sign in to comment.