def create_vps_server(db: Session = Depends(get_db), *, vps_profile: VpsCreateSchema): # validate vps_isp_obj = crud_isp.get(db_session=db, id=vps_profile.isp_id) if not vps_isp_obj: raise ValidationError( [ErrorWrapper(Exception('provider_name is not matched'), loc="isp_id")], model=VpsCreateSchema, ) # create base vps data rp = RedisPool() vps_spec_data = rp.get_vps_spec_value( db_session=db, isp_id=vps_profile.isp_id, os_code=vps_profile.os_code, plan_code=vps_profile.plan_code, region_code=vps_profile.region_code ) vps_config = dict( hostname=vps_profile.hostname, isp_id=vps_profile.isp_id, ssh_keys=vps_profile.ssh_keys, remark=vps_profile.remark, status=vps_profile.status, **vps_spec_data ) vps_obj = crud_vps.create(db_session=db, obj_in=vps_config, serializer=None) task = celery_app.send_task( "create_vps", args=[vps_profile.dict(), vps_obj.id] ) return dict(result=task)
def get_vps_specs( db: Session = Depends(get_db), *, isp_id: int = Query(0, alias='ispId')): rp = RedisPool() vps_raw_spec_data = rp.get_vps_spec_data(db_session=db, isp_id=isp_id) vps_spec_data = VpsSpecSchema(**vps_raw_spec_data) return dict(result=vps_spec_data)
def reload_vps_isp_config(self, vps_isp_id: int) -> bool: with session_manager() as db_session: rp = RedisPool() try: vps_raw_spec_data = rp.get_vps_spec_data(db_session=db_session, isp_id=vps_isp_id, reload=True) except: vps_raw_spec_data = None return bool(vps_raw_spec_data)
def run(self, *args, **kwargs): redis_pool = RedisPool() with session_manager() as db_session: active_task_obj_list = crud_domain_task.get_active_task(db_session=db_session).all() for active_task_obj in active_task_obj_list: active_task_data = { 'id': active_task_obj.id, 'interval': active_task_obj.interval, 'domain_id': active_task_obj.domain_id, 'domain_name': active_task_obj.domain_name } monitor_task_running_key = redis_pool.gen_task_status_key( status=redis_pool.TASK_RUNNING_STATUS, sequence=active_task_data['id'] ) monitor_already_scheduled = redis_pool.exists(monitor_task_running_key) if not monitor_already_scheduled: redis_pool.set_data_cache(monitor_task_running_key, 1, ex=active_task_data['interval']) MonitorDomainRunnerTask().delay(active_task_data)