def prune_logs(self): session = Session() if session.query(Log).count() > 10: logger.debug("Pruning logs for {}".format(self.uid)) too_old = datetime.now() - timedelta(days=3) # logger.debug(too_old) logger.debug("Found {} logs to be pruned".format( session.query(Log).filter(Log.date < too_old).count())) session.query(Log).filter(Log.date < too_old).delete() session.commit() session.close()
def get_setting(key): """Get setting value from database. Return None or value.""" session = Session() value = session.query(Setting).get(key) session.close() return value
def execute_task(id): session = Session() task = session.query(Task).get(id) logger.debug("Processing task with ID: {}", id) task_uid = task.task_id task_module = importlib.import_module("chronos.tasks.{}".format(task_uid)) task_id_dict = {"task_id": id} logger.debug("Starting task with ID: {}", id) task.time_started = datetime.datetime.now() task.status = "STARTED" session.commit() event.trigger("task_started", task_id_dict) arguments = {**json.loads(task.task_arguments), **task_id_dict} task.output = task_module.run(json.dumps(arguments), event) event.trigger("task_finished", task_id_dict) task.time_finished = datetime.datetime.now() task.status = "FINISHED" session.commit() logger.debug("Finished task with ID: {}", id) session.close() return
def logs(self, limit=10): """Find all log entries for script""" logs = [] session = Session() for log in (session.query(Log).filter(Log.script == self.uid).order_by( Log.date.desc()).limit(limit).all()): try: stdout = log.text.decode("utf-8") except AttributeError: stdout = log.text try: stderr = log.error.decode("utf-8") except AttributeError: stderr = log.error logs.append({ "stdout": stdout, "stderr": stderr, "date": log.date, "exitcode": log.exitcode, }) session.close() return logs
def get_all_settings(): """Get all settings from database.""" session = Session() all_settings = session.query(Setting).all() session.close() return session
def run(arguments, event): session = Session() for script in session.query(ScriptModel).all(): script = Script(script.uid) script.prune_logs() return
def enable(self): session = Session() script_from_database = session.query(ScriptModel).get(self.uid) script_from_database.enabled = True session.commit() session.close() self.enabled = True event.trigger("script_updated", self.__dict__()) event.trigger("action_complete", {"action": "enable", "uid": self.uid})
def scripts(): scripts = [] session = Session() for s in session.query(ScriptModel).all(): scripts.append(chronos.script.Script(s.uid).to_dict()) session.close() return jsonify(scripts), 200
def set_setting(key, value): """Update setting or create new.""" session = Session() if get_setting(key) is None: session.add(Setting(key=key, value=value)) logger.debug("Created new 'setting': {} with value: '{}'", key, value) else: session.query(Setting).get(key).value = value logger.debug("Updated 'setting': {} with value: '{}'", key, value) session.commit() session.close()
def run(arguments, event): arguments = json.loads(arguments) uid = arguments["uid"] script = Script(uid) session = Session() # Remove script folder shutil.rmtree(script.folder) # Remove all logs from script session.query(Log).filter(Log.script == script.uid).delete() # Delete metadata session.delete(script.db) session.commit() session.close() event.trigger("action_complete", {"action": "delete", "uid": script.uid}) event.trigger("script_deleted", {"uid": script.uid}) return uid
def put(self, uid): """Update script.""" args = request.get_json(force=True) session = Session() try: script = chronos.script.Script(uid) model = session.query(ScriptModel).get(uid) # Update each field if it exists try: model.name = args["name"] except KeyError: pass try: model.triggers = args["triggers"] except KeyError: pass try: model.enabled = args["enabled"] except KeyError: pass try: script.write_contents(args["contents"]) except KeyError: pass try: script.write_requirements(args["requirements"]) except KeyError: pass session.commit() return "OK", 200 except KeyError: return None, 404
def execute_next_task(): session = Session() tasks = session.query(Task).filter(Task.status == "WAITING") # logger.debug("Executing next available task...") for task in tasks: if task.priority == "NOW": task_thread = threading.Thread(target=execute_task(task.id)) task_thread.start() # logger.debug("Next task has been scheduled") return if tasks.count() > 0: task_thread = threading.Thread(target=execute_task(tasks[0].id)) task_thread.start() # logger.debug("Next task has been scheduled") session.close()
def __init__(self, uid): """Initialise script class given UID.""" session = Session() # UID self.uid = uid # Get database entry self.db = session.query(ScriptModel).get(uid) # Store dictionary version of model self.dict = { "name": self.db.name, "triggers": self.db.triggers, "logs": self.logs(), "created": str(self.db.created), } self.enabled = self.db.enabled # Get script folder self.folder = CHRONOS + os.path.sep + "scripts" + os.path.sep + self.uid # Get path of script self.path = self.folder + os.path.sep + self.uid + ".py" # Get path of requirements file self.requirements = self.folder + os.path.sep + "requirements.txt" # Get path of execute.sh script self.execute_path = self.folder + os.path.sep + "execute.sh" # Get path of install.sh script self.install_requirements_path = self.folder + os.path.sep + "install.sh" session.close()
def prune_logs(self): session = Session() session.query(Log).filter(Log.date >= timedelta(days=3)).delete() session.commit() session.close()