Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
args_list = get_func_arg(handler, runobj, context)
stdout = _DupStdout()
err = ''
val = None
old_dir = os.getcwd()
with redirect_stdout(stdout):
context.set_logger_stream(stdout)
try:
if cwd:
os.chdir(cwd)
val = handler(*args_list)
context.set_state('completed', commit=False)
except Exception as e:
err = str(e)
logger.error(traceback.format_exc())
context.set_state(error=err, commit=False)
logger.set_logger_level(old_level)
if cwd:
os.chdir(old_dir)
context.set_logger_stream(sys.stdout)
if val:
context.log_result('return', val)
context.commit()
logger.set_logger_level(old_level)
return stdout.buf.getvalue(), err
def log_and_raise(status=HTTPStatus.BAD_REQUEST, **kw):
logger.error(str(kw))
raise HTTPException(status_code=status, detail=kw)
def _build_function(db_session, function, with_mlrun):
fn = None
ready = None
try:
fn = new_function(runtime=function)
run_db = get_run_db_instance(db_session)
fn.set_db_connection(run_db)
fn.save(versioned=False)
ready = build_runtime(fn, with_mlrun)
fn.save(versioned=False)
logger.info("Fn:\n %s", fn.to_yaml())
except Exception as err:
logger.error(traceback.format_exc())
log_and_raise(HTTPStatus.BAD_REQUEST, reason="runtime error: {}".format(err))
return fn, ready
def create_pod(self, pod):
if 'pod' in dir(pod):
pod = pod.pod
pod.metadata.namespace = self.resolve_namespace(pod.metadata.namespace)
try:
resp = self.v1api.create_namespaced_pod(
pod.metadata.namespace, pod)
except ApiException as e:
logger.error('spec:\n{}'.format(pod.spec))
logger.error('failed to create pod: {}'.format(e))
raise e
logger.info(f'Pod {resp.metadata.name} created')
return resp.metadata.name, resp.metadata.namespace
def del_pod(self, name, namespace=None):
try:
api_response = self.v1api.delete_namespaced_pod(
name,
self.resolve_namespace(namespace),
grace_period_seconds=0,
propagation_policy='Background')
return api_response
except ApiException as e:
# ignore error if pod is already removed
if e.status != 404:
logger.error('failed to delete pod: {}'.format(e))
raise e
def logs(self, name, namespace=None):
try:
resp = self.v1api.read_namespaced_pod_log(
name=name, namespace=self.resolve_namespace(namespace))
except ApiException as e:
logger.error('failed to get pod logs: {}'.format(e))
raise e
return resp
def save(self, tag='', versioned=False, refresh=False):
db = self._get_db()
if not db:
logger.error('database connection is not configured')
return ''
if refresh and self._is_remote_api():
try:
meta = self.metadata
db_func = db.get_function(meta.name, meta.project, meta.tag)
if db_func and 'status' in db_func:
self.status = db_func['status']
if self.status.state and self.status.state == 'ready':
self.spec.image = get_in(db_func, 'spec.image', self.spec.image)
except Exception:
pass
tag = tag or self.metadata.tag
obj = self.to_dict()
iter = []
failed = 0
running = 0
for task in results:
if task:
state = get_in(task, ['status', 'state'])
id = get_in(task, ['metadata', 'iteration'])
struct = {'param': get_in(task, ['spec', 'parameters'], {}),
'output': get_in(task, ['status', 'results'], {}),
'state': state,
'iter': id,
}
if state == 'error':
failed += 1
err = get_in(task, ['status', 'error'], '')
logger.error('error in task {}:{} - {}'.format(
execution.uid, id, err))
elif state != 'completed':
running += 1
iter.append(struct)
if not iter:
execution.set_state('completed', commit=True)
logger.warning('warning!, zero iteration results')
return
if hasattr(pd, 'json_normalize'):
df = pd.json_normalize(iter).sort_values('iter')
else:
df = pd.io.json.json_normalize(iter).sort_values('iter')
header = df.columns.values.tolist()
fn.set_db_connection(run_db, True)
logger.info("func:\n{}".format(fn.to_yaml()))
# fn.spec.rundb = "http://mlrun-api:8080"
schedule = data.get("schedule")
if schedule:
args = (task,)
job_id = get_scheduler().add(schedule, fn, args)
get_db().store_schedule(db_session, data)
response = {"schedule": schedule, "id": job_id}
else:
run = fn.run(task, watch=False)
if run:
response = run.to_dict()
except Exception as err:
logger.error(traceback.format_exc())
log_and_raise(HTTPStatus.BAD_REQUEST, reason="runtime error: {}".format(err))
logger.info("response: %s", response)
return {
"data": response,
}
def _get_driver(self, name, namespace=None):
pods = self.get_pods(name, namespace, driver=True)
if not pods:
logger.error('no pod matches that job name')
return
k8s = self._get_k8s()
return list(pods.items())[0]