def main(): config = _load_config() session = get_session(config.url_path) if config.seed is None: seed = int(np.random.default_rng(None).integers(np.array([2**63]))[0]) config.seed = seed else: seed = int(config.seed) gen = np.random.default_rng(seed) if config.fixed_edges: graphs = _fixed_generation(config, gen) else: graphs = _probability_generation(config, gen) task = Task(name=config.name, task_type="GeometricGraphInstances", status=Task.STATUS_OPTIONS.FINISHED) # Convert the namespace config into database config task_config_holder = ConfigHolder.fromNamespace( config, task=task, ignored_attributes=["url_path", "name", "config"]) task.jobs = [TaskJobs(graph=graph, task=task) for graph in graphs] session.add(task) session.commit() print(len(task.jobs), "instances were created. Corresponding task is", task.id)
def process_task(config: ConfigHolder, task: Task, session: sqlalchemy.orm.Session): try: task.status = Task.STATUS_OPTIONS.PROCESSING session.commit() for subtask in tqdm.tqdm(task.children, desc=f"Task {task.id}: Processing subtasks"): if subtask.status not in [ Task.STATUS_OPTIONS.ERROR, Task.STATUS_OPTIONS.INTERRUPTED, Task.STATUS_OPTIONS.FINISHED ]: subconfig = ConfigHolder(subtask) if config.local: subconfig.local = True process_task(subconfig, subtask, session) to_process = [job for job in task.jobs if job.solution is None] process_jobs(to_process, config, session) task.status = Task.STATUS_OPTIONS.FINISHED except Exception as e: print(e) to_process = [job for job in task.jobs if job.solution is None] if str(e).lower( ) != "Backend does not support on_message callback".lower( ) and to_process: task.status = Task.STATUS_OPTIONS.ERROR task.error_message = str(e) if is_debug_env(): raise e else: task.status = Task.STATUS_OPTIONS.FINISHED finally: session.commit()
def catch_all(event): # task-received: add to database and push sse to client with new task if event["type"] == "task-received": kwargs = ast.literal_eval(event["kwargs"]) task_to_add = Task( user_sub_uid = kwargs["sub_uid"], task_id = event["uuid"], task_type = kwargs["task_type"], submit_datetime = kwargs["submit_datetime"], status = event["type"], pi_filename = kwargs["pi_filename"], obs_filename = kwargs["obs_filename"], number_states = kwargs["number_states"] ) if kwargs["task_type"] == "learning": task_to_add.change_limit = kwargs["change_limit"] task_to_add.max_iterations = kwargs["max_iterations"] else: task_to_add.lfg_filename = kwargs["lfg_filename"] try: dbsm.add_task(task_to_add) sse_add_task(task=task_to_add) except Exception as err: print("...EXCEPTION caught on add: {}".format(err)) # else update task in database and push sse to client with task change elif event["type"] in ["task-started", "task-succeeded", "task-failed"]: try: dbsm.update_task(task_id=event["uuid"], status=event["type"]) sse_update_task(task_id=event["uuid"], task_status=event["type"]) except Exception as err: print("...EXCEPTION caught on update ({0}): {1}".format(event["type"], err))
def _create_task(arg_config, session): solvers = arg_config.solvers solvers_args = arg_config.solvers_args assert len(solvers) == len(solvers_args),\ "The amount of solver arguments must match the amount of solvers" for solver in solvers: assert solver in ALL_SOLVER,\ f"Solver {solver} not found! Please make sure that all solver are properly named." task = Task(task_type="instance_test", status=Task.STATUS_OPTIONS.CREATED, name=arg_config.name) config = ConfigHolder.fromNamespace(arg_config, task=task, ignored_attributes=["url_path", "solvers", "solvers_args", "create_only", "config", "name"]) jobs = _get_instances(task, config, session) for solver, solver_args in zip(solvers, solvers_args): subtask = Task(parent=task, name=f"{solver}_test", task_type="instance_test", status=Task.STATUS_OPTIONS.CREATED) task.children.append(subtask) subconfig_namespace = configargparse.Namespace(solver=solver, solver_args=solver_args) subconfig = ConfigHolder.fromNamespace(subconfig_namespace, task=subtask) add_prev_job = (subconfig.with_start_sol is not None and subconfig.with_start_sol) if isinstance(jobs[0], TaskJobs): for task_job in jobs: prev_job = task_job if add_prev_job else None for i in range(config.repetitions): subtask.jobs.append(TaskJobs(task=subtask, graph=task_job.graph, prev_job=prev_job)) else: for graph in jobs: for i in range(config.repetitions): subtask.jobs.append(TaskJobs(task=subtask, graph=graph)) session.add(task) session.commit() return task, config
def process_task(config: ConfigHolder, task: Task, session: sqlalchemy.orm.Session): try: task.status = Task.STATUS_OPTIONS.PROCESSING session.commit() for subtask in tqdm.tqdm(task.children, desc=f"Task {task.id}: Processing subtasks"): if subtask.status not in [Task.STATUS_OPTIONS.ERROR, Task.STATUS_OPTIONS.INTERRUPTED, Task.STATUS_OPTIONS.PROCESSING, Task.STATUS_OPTIONS.FINISHED]: process_task(ConfigHolder(subtask), subtask, session) to_process = [job for job in task.jobs if job.solution is None] process_jobs(to_process, config, session) task.status = Task.STATUS_OPTIONS.FINISHED session.commit() except Exception as e: print(e) task.status = Task.STATUS_OPTIONS.ERROR task.error_message = str(e) session.commit()
def form_handler(): from database import Task task_list_id = request.form["task_list_id"] task_list_name = request.form["task_list_name"] task_text = request.form["task_text"] # Create a new task new_task = Task() new_task.label = task_text new_task.isDone = False new_task.task_list_id = task_list_id # Make the link between Task and TaskList db.session.add(new_task) db.session.commit() return redirect(url_for("generate_form", task_list_name=task_list_name))
def post(self): state = 0 message = "succeed" args = post_reqparse.parse_args() new_task = Task(title=args['title'], timeend=args['timeend'], uid=current_user.id) if 'content' in args: new_task.content = args['content'] db.session.add(new_task) db.session.commit() return { 'status': state, 'message': message, 'data': make_public_task(new_task) }
def create_task(file): if file.document: try: Session = sessionmaker(bind=engine) Session = Session() raw = file.document.file_id print(raw) file_info = bot.get_file(raw) downloaded_file = bot.download_file(file_info.file_path) with open(raw + ".tex", 'wb') as new_file: new_file.write(downloaded_file) last_exam = Session.query(Exam).filter_by( author=file.from_user.username).all()[-1] last_exam.tasks_num += 1 Session.commit() task = Task(TexSoup(open(raw + ".tex").read()), last_exam.exam_id) bot.send_message(file.chat.id, task.question) os.remove(raw + ".tex") #exam.task_list.append(task) #print(exam.task_list) Session.add(task) Session.commit() except: bot.send_message( file.chat.id, "Документ не валиден, пожалуйста проверьте формат.")
class TaskService: def __init__(self): self.model = Task() def create(self, params): return self.model.create_task(params) def update(self, item_id, params): return self.model.update_task(item_id, params) def delete(self, item_id): return self.model.delete_task(item_id) def list(self): response = self.model.list_tasks() return response
def create_task(body, **kwargs): """Create a new task""" new_task = Task(body=body, **kwargs) #TODO: Handle arg overflow db.session.add(new_task) db.session.commit() print("Created!") return (new_task.id, new_task.user, new_task.modified)
def get_task(lesson, tasknum, student_id): student = Student.select().where(Student.telegram_id == student_id) task = Task.select().where((Task.lyceum_group == student.lyceum_group) & (Task.lesson == lesson) & (Task.number == tasknum)) if not task: add_tasks_from_lesson(lesson) return msg return msg
def main(): config = _load_config() session = get_session(config.url_path) graphs = [] if config.seed is None: seed = int(np.random.default_rng(None).integers(np.array([2**63]))[0]) config.seed = seed else: seed = int(config.seed) gen = np.random.default_rng(seed) counter = 0 assert len(config.vertex_shape) % 2 == 0 shapes = np.array(config.vertex_shape).reshape( round(len(config.vertex_shape) / 2), 2) l_shapes = len(shapes) for n in range(config.min_n, config.max_n, config.steps_n): size = config.cel_min while size <= config.cel_max: for i in range(config.repetitions): graph = create_random_celest_graph( n, vertex_shape=shapes[counter % l_shapes], celest_bounds=(size, size + config.cel_range), seed=gen) if config.min_m <= graph.edge_amount and ( not config.max_m or config.max_m >= graph.edge_amount): graphs.append(graph) counter += 1 size += config.cel_step task = Task(name=config.name, task_type="CelestialGraphInstances", status=Task.STATUS_OPTIONS.FINISHED) # Convert the namespace config into database config task_config_holder = ConfigHolder.fromNamespace( config, task=task, ignored_attributes=["url_path", "name", "config"]) task.jobs = [TaskJobs(graph=graph, task=task) for graph in graphs] session.add(task) session.commit() print(counter, "instances were created. Corresponding task is", task.id)
def api_task_enqueue(): try: token = request.json["token"] except KeyError: return abort(400) name = oddb.db.check_api_token(token) if name: task = Task(request.json["website_id"], request.json["url"], request.json["priority"], request.json["callback_type"], json.dumps(request.json["callback_args"])) oddb.logger.info("API force enqueue by " + name + "\n(" + str(task.to_json()) + ")") oddb.taskManager.queue_task(task) return "" else: return abort(403)
def admin_rescan_website(website_id): require_role("admin") website = db.get_website_by_id(website_id) if website: priority = request.args.get( "priority") if "priority" in request.args else 1 task = Task(website_id, website.url, priority) taskManager.queue_task(task) flash("Enqueued rescan task", "success") else: flash("Website does not exist", "danger") return redirect("/website/" + str(website_id))
def _get_task_and_config(session, arg_config): task = None config = None if arg_config.url_path: if hasattr(arg_config, "task") and arg_config.task is not None: task = session.query(Task).filter(Task.id == arg_config.task).one() if arg_config.override_config and \ input(f"Are you sure to override the configs for {task.id}? (y/N)").lower() in ["y", "yes"]: print(f"Override config from task {task.id})") for task_config in task.configs: session.delete(task_config) arg_config.override_config = False config = ConfigHolder.fromNamespace(arg_config, task, [ "override_config", "url_path", "PreEvolveInteractive", "create_only" ]) session.add(config) session.commit() else: print("Using config from database") config = ConfigHolder(task) else: if input("New Task will be created (Y/n)?").lower() in [ "", "yes", "y" ]: print("Will create a new task.") task = Task(task_type="instance_evolver", status=Task.STATUS_OPTIONS.CREATED, name=arg_config.name) session.add(task) session.commit() arg_config.task = task.id config = ConfigHolder.fromNamespace(arg_config, task, ignored_attributes=[ "url_path", "create_only", "name", "override_config" ]) session.add_all(config.database_configs) session.commit() savepath = input( f"Task ID is {task.id}. Type a filepath to save the ID in a config file (default: Skip save): " ) if savepath: _save_task_file(savepath, config, task) else: config = arg_config return task, config
def _get_task_and_config(session, arg_config): if session is not None: task = Task(task_type="instance_evolver_greedy", status=Task.STATUS_OPTIONS.CREATED, name=arg_config.name) config = ConfigHolder.fromNamespace( arg_config, task, ignored_attributes=["create_only", "url_path", "name"]) session.add(task) session.commit() else: task = None config = arg_config return task, config
def index(): if request.method == 'POST': # POST task_text = request.form['task_text'] new_task = Task(task_text) db.session.add(new_task) db.session.commit() return redirect('/') else: # GET tasks = Task.query.order_by(Task.created).all() return render_template('index.html', tasks=tasks)
def submit_task(self, name, kind, arguments): print("Received task:", (name, kind)) now = str(time.time()) input_file = "input.%s.%s.%s.json" % (name, kind, now) output_file = "output.%s.%s.%s.json" % (name, kind, now) save_to_file(input_file, arguments) id = self.task_collection.add( Task(id=None, created_on=None, name=name, kind=kind, is_complete=False, input_file=input_file, output_file=output_file)) return id
def create_task(project_id): if 'username' not in login_session: return redirect('/login') if request.method == 'POST': # why can't I pass the params dict? params = {"user_id": 1, "project_id": project_id, "title": request.form['title'], "description": request.form['description']} task = Task(user_id=login_session['userid'], project_id=project_id, title=request.form['title'], description=request.form['description']) session.add(task) session.commit() return redirect(url_for('view_project', project_id=project_id)) else: return render_template('createtask.html', project_id=project_id)
def save_task(request): if not request.json: return 'No Data to Save', 400 missing_field = missing_required_data(request.json) if missing_field: return f'Missing Required Field: {missing_field}', 400 user = User.query.filter_by(username=USERNAME).first() list_name = request.json['list'] task_list = List.query.filter_by(name=list_name, user=user) if not task_list: return f'List Not Found: {list_name}' new_task = Task(title=request.json['title'], description=request.json['description'], completed=False, list=task_list) db.session.add(new_task) db.session.commit() return f'Task Created: {new_task.id}', 201
def try_enqueue(url): url = os.path.join(url, "") url = od_util.get_top_directory(url) if not od_util.is_valid_url(url): return "<strong>Error:</strong> Invalid url. Make sure to include the appropriate scheme.", "warning" website = db.get_website_by_url(url) if website: return "Website already exists", "danger" website = db.website_exists(url) if website: return "A parent directory of this url has already been posted", "danger" if db.is_blacklisted(url): return "<strong>Error:</strong> " \ "Sorry, this website has been blacklisted. If you think " \ "this is an error, please <a href='/contribute'>contact me</a>.", "danger" if not od_util.is_od(url): return "<strong>Error:</strong>" \ "The anti-spam algorithm determined that the submitted url is not " \ "an open directory or the server is not responding. If you think " \ "this is an error, please <a href='/contribute'>contact me</a>.", "danger" website_id = db.insert_website( Website( url, str(request.remote_addr + "_" + request.headers.get("X-Forwarded-For", "")), request.user_agent)) task = Task(website_id, url, priority=1) taskManager.queue_task(task) return "The website has been added to the queue", "success"
# session won't be persisted into the database until you call # session.commit(). If you're not happy about the changes, you can # revert all of them back to the last commit by calling # session.rollback() session = DBSession() user1 = User(name="Luki", email="*****@*****.**") session.add(user1) session.commit() items = [] items.append(Project(user_id=1, name="Project 1")) items.append( Task(user_id=1, project_id=1, title="Ein Titel", description="Eine Beschreibung")) items.append( Task(user_id=1, project_id=1, title="Noch Ein Titel", description="Noch Eine Beschreibung")) for item in items: session.add(item) session.commit() print("done")
def before_request(): g.db = connect_db(app.config['DB']) User.set_db(g.db) Task.set_db(g.db) Comment.set_db(g.db) Project.set_db(g.db)
def __init__(self): self.model = Task()