Skip to content

Commit eb163c5

Browse files
authored
Feature to get a list of long running operations (WebOfTrust#158)
* add/operations endpoint * add type filtering parameter to getOperations * getOperations catches error from Monitor.status * add unit test, cleanup * cleanup * api documentation * spec update - adding new endpoint --------- Co-authored-by: Petteri Stenius <petteri.stenius@gmail.com>
1 parent f67227a commit eb163c5

File tree

4 files changed

+247
-204
lines changed

4 files changed

+247
-204
lines changed

src/keria/app/agenting.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -757,8 +757,10 @@ def recur(self, tyme):
757757

758758

759759
def loadEnds(app):
760-
opEnd = longrunning.OperationResourceEnd()
761-
app.add_route("/operations/{name}", opEnd)
760+
opColEnd = longrunning.OperationCollectionEnd()
761+
app.add_route("/operations", opColEnd)
762+
opResEnd = longrunning.OperationResourceEnd()
763+
app.add_route("/operations/{name}", opResEnd)
762764

763765
oobiColEnd = OOBICollectionEnd()
764766
app.add_route("/oobis", oobiColEnd)

src/keria/core/longrunning.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from dataclasses import dataclass, asdict
1010

1111
import falcon
12+
import json
1213
from dataclasses_json import dataclass_json
1314
from keri import kering
1415
from keri.app.oobiing import Result
@@ -138,6 +139,26 @@ def get(self, name):
138139

139140
return operation
140141

142+
def getOperations(self, type=None):
143+
""" Return list of long running opterations, optionally filtered by type """
144+
ops = self.opr.ops.getItemIter()
145+
if type != None:
146+
ops = filter(lambda i: i[1].type == type, ops)
147+
148+
def get_status(op):
149+
try:
150+
return self.status(op)
151+
except Exception as err:
152+
# self.status may throw an exception.
153+
# Handling error by returning an operation with error status
154+
return Operation(
155+
name=f"{op.type}.{op.oid}",
156+
metadata=op.metadata,
157+
done=True,
158+
error=Status(code=500, message=f"{err}"))
159+
160+
return [get_status(op) for (_, op) in ops]
161+
141162
def rem(self, name):
142163
""" Remove tracking of the long running operation represented by name """
143164
return self.opr.ops.rem(keys=(name,))
@@ -365,6 +386,40 @@ def status(self, op):
365386
return operation
366387

367388

389+
class OperationCollectionEnd:
390+
@staticmethod
391+
def on_get(req, rep):
392+
""" Get list of long running operations
393+
394+
Parameters:
395+
req (Request): Falcon HTTP Request object
396+
rep (Response): Falcon HTTP Response object
397+
398+
---
399+
summary: Get list of long running operations
400+
parameters:
401+
- in: query
402+
name: type
403+
schema:
404+
type: string
405+
required: false
406+
description: filter list of long running operations by type
407+
responses:
408+
200:
409+
content:
410+
application/json:
411+
schema:
412+
type: array
413+
414+
"""
415+
agent = req.context.agent
416+
type = req.params.get("type")
417+
ops = agent.monitor.getOperations(type=type)
418+
rep.data = json.dumps(ops, default=lambda o: o.to_dict()).encode("utf-8")
419+
rep.content_type = "application/json"
420+
rep.status = falcon.HTTP_200
421+
422+
368423
class OperationResourceEnd:
369424
""" Single Resource REST endpoint for long running operations
370425

0 commit comments

Comments
 (0)