def work(self): """ Go through all datasources, and if it is configured to automatically delete old datasets, do so for all qualifying datasets :return: """ for datasource_id in self.all_modules.datasources: datasource = self.all_modules.datasources[datasource_id] # default = never expire if not datasource.get("expire-datasets", None): continue cutoff = time.time() - datasource.get("expire-datasets") datasets = self.db.fetchall( "SELECT key FROM datasets WHERE key_parent = '' AND parameters::json->>'datasource' = %s AND timestamp < %s", (datasource_id, cutoff)) # we instantiate the dataset, because its delete() method does all # the work (e.g. deleting child datasets) for us for dataset in datasets: dataset = DataSet(key=dataset["key"], db=self.db) dataset.delete() self.log.info("Deleting dataset %s/%s (expired per configuration)" % (datasource, dataset.key)) self.job.finish()
def delete_dataset(key=None): """ Delete a dataset Only available to administrators. Deletes a dataset, as well as any children linked to it, from 4CAT. Calling this on a dataset that is currently being executed is undefined behaviour. :request-param str query_key: ID of the dataset for which to return the status :request-param str ?access_token: Access token; only required if not logged in currently. :return: A dictionary with a successful `status`. :return-schema: {type=object,properties={status={type=string}}} :return-error 404: If the dataset does not exist. """ if not current_user.is_admin(): return error(403, message="Not allowed") dataset_key = request.form.get("key", "") if not key else key try: dataset = DataSet(key=dataset_key, db=db) except TypeError: return error(404, error="Dataset does not exist.") dataset.delete() return jsonify({"status": "success"})
def delete_dataset(key=None): """ Delete a dataset Only available to administrators and dataset owners. Deletes a dataset, as well as any children linked to it, from 4CAT. Also tells the backend to stop any jobs dealing with the dataset. :request-param str key: ID of the dataset to delete :request-param str ?access_token: Access token; only required if not logged in currently. :return: A dictionary with a successful `status`. :return-schema: {type=object,properties={status={type=string}}} :return-error 404: If the dataset does not exist. """ dataset_key = request.form.get("key", "") if not key else key try: dataset = DataSet(key=dataset_key, db=db) except TypeError: return error(404, error="Dataset does not exist.") if not current_user.is_admin() and not current_user.get_id() == dataset.parameters.get("user"): return error(403, message="Not allowed") # if there is an active or queued job for some child dataset, cancel and # delete it children = dataset.get_all_children() for child in children: try: job = Job.get_by_remote_ID(child.key, database=db, jobtype=child.type) call_api("cancel-job", {"remote_id": child.key, "jobtype": dataset.type, "level": BasicWorker.INTERRUPT_CANCEL}) job.finish() except JobNotFoundException: pass # now cancel and delete the job for this one (if it exists) try: job = Job.get_by_remote_ID(dataset.key, database=db, jobtype=dataset.type) call_api("cancel-job", {"remote_id": dataset.key, "jobtype": dataset.type, "level": BasicWorker.INTERRUPT_CANCEL}) except JobNotFoundException: pass # and delete the dataset and child datasets dataset.delete() return jsonify({"status": "success", "key": dataset.key})
description="Deletes a query, the corresponding job, and any sub-queries.") cli.add_argument("-k", "--key", required=True, help="Query key to delete.") cli.add_argument( "-q", "--quiet", type=bool, default=False, help="Whether to skip asking for confirmation. Defaults to false.") args = cli.parse_args() if not args.quiet: confirm = input( "This will delete the query, and any sub-queries. Are you sure? (y/n)") if confirm.strip().lower() != "y": sys.exit(0) logger = Logger() database = Database(logger=logger, appname="delete-query") # Initialize query try: parent = DataSet(key=args.key, db=database) except TypeError: print("No query found with that key.") sys.exit(1) parent.delete() print( "Done. Note that running jobs for the queries above are not stopped; you will have to wait for them to finish on their own." )