Skip to content

Commit

Permalink
Merge pull request #165 from DigitalSlideArchive/delete-old-jobs
Browse files Browse the repository at this point in the history
Add an endpoint to allow admins to clean up old abandoned jobs.
  • Loading branch information
manthey authored May 25, 2022
2 parents 57ad796 + 3c1f6f8 commit 3c9b4e9
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions histomicsui/rest/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# limitations under the License.
#############################################################################

import datetime

from girder.api import access
from girder.api.describe import Description, autoDescribeRoute, describeRoute
Expand All @@ -23,6 +24,7 @@
from girder.models.folder import Folder
from girder.models.item import Item
from girder.utility.model_importer import ModelImporter
from girder_jobs.models.job import Job


def addSystemEndpoints(apiRoot):
Expand All @@ -35,7 +37,11 @@ def addSystemEndpoints(apiRoot):
apiRoot.item.route('GET', ('query',), getItemsByQuery)
# Added to the folder route
apiRoot.folder.route('GET', ('query',), getFoldersByQuery)
# Added to the system route
apiRoot.system.route('PUT', ('restart',), restartServer)
# Added to the job route
apiRoot.job.route('GET', ('old',), getOldJobs)
apiRoot.job.route('DELETE', ('old',), deleteOldJobs)
# Added to the histomicui route
HUIResourceResource(apiRoot)

Expand Down Expand Up @@ -204,6 +210,46 @@ def run(self):
return {'restarted': datetime.datetime.utcnow()}


@autoDescribeRoute(
Description('Report on old jobs.')
.param('age', 'The minimum age in days.', required=False,
dataType='int', default=1)
.param('status', 'A comma-separated list of statuses to include. Blank '
'for all.', required=False, default='0,1,2')
.errorResponse()
)
@access.admin
@boundHandler()
def getOldJobs(self, age, status):
age = datetime.datetime.utcnow() + datetime.timedelta(-age)
query = {'updated': {'$lt': age}}
if status:
query['status'] = {'$in': [int(s) for s in status.split(',')]}
return Job().find(query, force=True).count()


@autoDescribeRoute(
Description('Delete old jobs.')
.param('age', 'The minimum age in days.', required=False,
dataType='int', default=1)
.param('status', 'A comma-separated list of statuses to include. Blank '
'for all.', required=False, default='0,1,2')
.errorResponse()
)
@access.admin
@boundHandler()
def deleteOldJobs(self, age, status):
age = datetime.datetime.utcnow() + datetime.timedelta(-age)
query = {'updated': {'$lt': age}}
if status:
query['status'] = {'$in': [int(s) for s in status.split(',')]}
count = 0
for job in Job().find(query, force=True):
Job().remove(job)
count += 1
return count


class HUIResourceResource(ResourceResource):
def __init__(self, apiRoot):
super(ResourceResource, self).__init__()
Expand Down

0 comments on commit 3c9b4e9

Please sign in to comment.