diff --git a/cellxgene_gateway/backend_cache.py b/cellxgene_gateway/backend_cache.py index 08d0235..e54654e 100644 --- a/cellxgene_gateway/backend_cache.py +++ b/cellxgene_gateway/backend_cache.py @@ -30,7 +30,7 @@ class BackendCache: def check_entry(self, dataset): contents = self.entry_list - matches = [c for c in contents if c.dataset == dataset] + matches = [c for c in contents if c.dataset == dataset and c.status != "terminated"] if len(matches) == 0: return None @@ -61,3 +61,7 @@ class BackendCache: time.sleep(1) # Automatic refresh is too fast, needs a second to pause return entry + + def prune(self, process): + self.entry_list.remove(process) + process.terminate() diff --git a/cellxgene_gateway/cache_entry.py b/cellxgene_gateway/cache_entry.py index 3532a33..5dda033 100644 --- a/cellxgene_gateway/cache_entry.py +++ b/cellxgene_gateway/cache_entry.py @@ -73,6 +73,15 @@ class CacheEntry: else: self.all_output += output + def terminate(self, process): + if pid != None and self.status != "terminated": + p = psutil.Process(pid) + p.terminate() + p = psutil.Process(pid + 2) + p.terminate() + self.status = "terminated" + + def serve_content(self, path): dataset = self.dataset diff --git a/cellxgene_gateway/gateway.py b/cellxgene_gateway/gateway.py index a04cbdd..04e4448 100644 --- a/cellxgene_gateway/gateway.py +++ b/cellxgene_gateway/gateway.py @@ -18,6 +18,7 @@ from flask import ( render_template, request, send_from_directory, + url_for, ) from flask_api import status from werkzeug import secure_filename @@ -187,20 +188,18 @@ def do_GET_status(): "cache_status.html", entry_list=cache.entry_list ) -pruner = PruneProcessCache(cache) -@app.route("/prune/", methods=["DELETE"]) -def do_prune(path): +@app.route("/relaunch/", methods=["GET"]) +def do_relaunch(path): dataset = get_dataset(path) match = cache.check_entry(dataset) - if match is None: - return "not found" - else: - pruner.prune(match) - return "success" + if not match is None: + match.terminate() + return redirect(url_for('view', path=path), code=301) def main(): env.validate() + pruner = PruneProcessCache(cache) background_thread = Thread(target=pruner) background_thread.start() diff --git a/cellxgene_gateway/prune_process_cache.py b/cellxgene_gateway/prune_process_cache.py index 21d2c89..6c563ec 100644 --- a/cellxgene_gateway/prune_process_cache.py +++ b/cellxgene_gateway/prune_process_cache.py @@ -19,15 +19,6 @@ class PruneProcessCache: def __init__(self, cache): self.cache = cache - def prune(self, process): - self.cache.entry_list.remove(process) - pid = process.pid - if pid != None: - p = psutil.Process(pid) - p.terminate() - p = psutil.Process(pid + 2) - p.terminate() - def __call__(self): while True: time.sleep(60) @@ -42,7 +33,7 @@ class PruneProcessCache: for process in processes_to_delete: try: - self.prune(process) + cache.prune(process) except Exception as detail: print('failed to prune process:', detail)