Skip to content

Commit

Permalink
Release idle agents (WebOfTrust#299)
Browse files Browse the repository at this point in the history
* add Releaser

* fix empty env

* default timer

* closing order and log

* change conflicting method name

* fix and add test
  • Loading branch information
rodolfomiranda authored Oct 3, 2024
1 parent 6e6f1f8 commit a2d9e71
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
50 changes: 48 additions & 2 deletions src/keria/app/agenting.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"""
import json
import os
import datetime
from dataclasses import asdict
from urllib.parse import urlparse, urljoin
from types import MappingProxyType
Expand Down Expand Up @@ -175,7 +176,7 @@ def __init__(self, name, bran, base="", configFile=None, configDir=None, adb=Non
self.agents = dict()

self.adb = adb if adb is not None else basing.AgencyBaser(name="TheAgency", base=base, reopen=True, temp=temp)
super(Agency, self).__init__(doers=[], always=True)
super(Agency, self).__init__(doers=[Releaser(self)], always=True)

def create(self, caid, salt=None):
ks = keeping.Keeper(name=caid,
Expand Down Expand Up @@ -231,9 +232,26 @@ def delete(self, agent):

del self.agents[agent.caid]

def shut(self, agent):
logger.info(f"closing idle agent {agent.caid}")
agent.remove(agent.doers)
self.remove([agent])
del self.agents[agent.caid]
agent.hby.ks.close(clear=False)
agent.seeker.close(clear=False)
agent.exnseeker.close(clear=False)
agent.monitor.opr.close(clear=False)
agent.notifier.noter.close(clear=False)
agent.rep.mbx.close(clear=False)
agent.registrar.rgy.close()
agent.mgr.rb.close(clear=False)
agent.hby.close(clear=False)

def get(self, caid):
if caid in self.agents:
return self.agents[caid]
agent = self.agents[caid]
agent.last = helping.nowUTC()
return agent

aaid = self.adb.agnt.get(keys=(caid,))
if aaid is None:
Expand Down Expand Up @@ -293,6 +311,8 @@ def __init__(self, hby, rgy, agentHab, agency, caid, **opts):
self.cfd = MappingProxyType(dict(self.hby.cf.get()) if self.hby.cf is not None else dict())
self.tocks = MappingProxyType(self.cfd.get("tocks", {}))

self.last = helping.nowUTC()

self.swain = delegating.Anchorer(hby=hby, proxy=agentHab)
self.counselor = Counselor(hby=hby, swain=self.swain, proxy=agentHab)
self.org = connecting.Organizer(hby=hby)
Expand Down Expand Up @@ -782,7 +802,33 @@ def recur(self, tyme):
self.registrar.processEscrows()
self.credentialer.processEscrows()
return False

class Releaser(doing.Doer):
KERIAReleaserTimeOut = "KERIA_RELEASER_TIMEOUT"
TimeoutRel = int(os.getenv(KERIAReleaserTimeOut, "86400"))
def __init__(self, agency):
""" Check open agents and close if idle for more than TimeoutRel seconds
Parameters:
agency (Agency): KERIA agent manager
"""
self.tock = 60.0
self.agents = agency.agents
self.agency = agency

super(Releaser, self).__init__(tock=self.tock)

def recur(self, tyme=None):
while True:
idle = []
for caid in self.agents:
now = helping.nowUTC()
if (now - self.agents[caid].last) > datetime.timedelta(seconds=self.TimeoutRel):
idle.append(caid)

for caid in idle:
self.agency.shut(self.agents[caid])
yield self.tock

def loadEnds(app):
opColEnd = longrunning.OperationCollectionEnd()
Expand Down
3 changes: 3 additions & 0 deletions tests/app/test_agenting.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,9 @@ def test_agency():
if os.path.exists(f'/usr/local/var/keri/adb/{base}'):
shutil.rmtree(f'/usr/local/var/keri/adb/{base}')

agency.shut(agent)
assert caid not in agency.agents
assert len(agent.doers) == 0

def test_boot_ends(helpers):
agency = agenting.Agency(name="agency", bran=None, temp=True)
Expand Down

0 comments on commit a2d9e71

Please sign in to comment.