22import os
33import uuid
44from pathlib import Path
5-
5+ import humps
66# from ..._resource import BaseClient
77from pprint import pprint
88from typing import Optional
1818
1919class Job (SyncAPIResource ):
2020
21+ def create (self , project_id , name = '' , group_id = 0 ):
22+ data = {
23+ 'projectId' : project_id
24+ }
25+ if name :
26+ data ['name' ] = name
27+ if group_id :
28+ data ['bohrGroupId' ] = group_id
29+ try :
30+ data = self ._client .post (f'/openapi/v1/job/create' , json = data , params = self ._client .params )
31+ data = data .json ()
32+ except Exception as e :
33+ raise e
34+ return data .get ("data" , {})
35+
2136 def detail (self , job_id ):
2237 log .info (f"detail job { job_id } " )
2338 response = self ._client .get (f"/openapi/v1/job/{ job_id } " )
@@ -43,7 +58,6 @@ def submit(
4358 ):
4459 # log.info(f"submit job {name},project_id:{project_id}")
4560 data = self .create_job (project_id , job_name , job_group_id )
46- print (data )
4761 if work_dir != "" :
4862 if not os .path .exists (work_dir ):
4963 raise FileNotFoundError
@@ -73,38 +87,39 @@ def submit(
7387 )
7488 return self .insert (job_add_request .to_dict ())
7589
76- def insert (self , data ):
77- # log.info(f"insert job {data}")
78- response = self ._client .post ("/openapi/v2/job/add" , json = data )
79- pprint (response .request )
80- print (response .json ())
90+ def insert (self , ** kwargs ):
91+ camel_data = {humps .camelize (k ): v for k , v in kwargs .items ()}
92+ if not isinstance (camel_data ['ossPath' ], list ):
93+ camel_data ['ossPath' ] = [camel_data ['ossPath' ]]
94+ if 'logFile' in camel_data :
95+ camel_data ['logFiles' ] = camel_data ['logFile' ]
96+ if 'logFiles' in camel_data and not isinstance (camel_data ['logFiles' ], list ):
97+ camel_data ['logFiles' ] = [camel_data ['logFiles' ]]
98+ response = self ._client .post ("/openapi/v2/job/add" , json = camel_data )
99+ return response .json ().get ("data" )
81100
82101 def delete (self , job_id ):
83102 # log.info(f"delete job {job_id}")
84103 response = self ._client .post (f"/openapi/v1/job/del/{ job_id } " )
85- pprint (response .request )
86- print (response .json ())
104+
87105
88106 def terminate (self , job_id ):
89107 # log.info(f"terminate job {job_id}")
90108 response = self ._client .post (f"/openapi/v1/job/terminate/{ job_id } " )
91- pprint (response .request )
92- print (response .json ())
109+
93110
94111 def kill (self , job_id ):
95112 # log.info(f"kill job {job_id}")
96113 response = self ._client .post (f"/openapi/v1/job/kill/{ job_id } " )
97- pprint (response .request )
98- print (response .json ())
114+
99115
100116 def log (self , job_id , log_file = "STDOUTERR" , page = - 1 , page_size = 8192 ):
101117 # log.info(f"log job {job_id}")
102118 response = self ._client .get (
103119 f"/openapi/v1/job/{ job_id } /log" ,
104120 params = {"logFile" : log_file , "page" : page , "pageSize" : page_size },
105121 )
106- pprint (response .request )
107- print (response .json ().get ("data" )["log" ])
122+
108123 return response .json ().get ("data" )["log" ]
109124
110125 def create_job (
@@ -132,8 +147,7 @@ def create_job(
132147 "bohrGroupId" : group_id ,
133148 }
134149 response = self ._client .post (f"/openapi/v1/job/create" , json = data )
135- pprint (response .request )
136- print (response .json ())
150+
137151 return response .json ().get ("data" )
138152
139153 def create_job_group (self , project_id , job_group_name ):
@@ -142,9 +156,8 @@ def create_job_group(self, project_id, job_group_name):
142156 "/openapi/v1/job_group/add" ,
143157 json = {"name" : job_group_name , "projectId" : project_id },
144158 )
145- pprint (response .request )
146- print (response .json ())
147-
159+ return response .json ().get ("data" )
160+
148161 def upload (
149162 self ,
150163 file_path : str ,
0 commit comments