2424from websockets .exceptions import ConnectionClosed
2525
2626THREADS = int (os .environ .get ('CUSTOMIZER_THREADS' , 1 ))
27- TIMEOUT = int (os .environ .get ('CUSTOMIZER_JOB_TIMEOUT' , 1200 ))
27+ TIMEOUT = int (os .environ .get ('CUSTOMIZER_JOB_TIMEOUT' , 2400 ))
2828QUEUESIZE = int (os .environ .get ('CUSTOMIZER_QUEUE_LENGTH' , 20 ))
2929TEMPLATEDIR = pkg_resources .resource_filename (__name__ , 'templates' )
3030
@@ -71,10 +71,19 @@ def field_type(entry, default_value):
7171 return "file"
7272 elif isinstance (default_value , bool ):
7373 return "bool"
74+ if entry == "parts_list" :
75+ return "parts_list"
7476 else :
7577 return "string"
7678
7779
80+ def all_parts ():
81+ return [* ALL_PARTS , * [f"logo/CustomLogo/MainCase{ l } -{ inv } -{ mn } "
82+ for l in ["" , "Lid" ]
83+ for inv in ["inverted" , "normal" ]
84+ for mn in ["main" , "highlight" ]]]
85+
86+
7887templates .env .filters ['field_type' ] = field_type
7988
8089
@@ -90,9 +99,9 @@ def models(root):
9099
91100async def queue_runner ():
92101 while True :
93- fun , arg = await queue .get ()
94- logging .info (f"running { arg } " )
95- await fun (arg )
102+ fun , kwargs = await queue .get ()
103+ logging .info (f"running { kwargs } " )
104+ await fun (** kwargs )
96105
97106
98107@app .on_event ('startup' )
@@ -175,12 +184,10 @@ def package_to_zip(source: Path, target: Path):
175184 zf .write (name , zipped_name )
176185
177186
178- async def run_job (uid , parts = None ):
187+ async def run_job (uid = None , parts = None ):
179188 global job_durations
180189 starttime = datetime .datetime .now ()
181190
182- if parts == None :
183- parts = ALL_PARTS
184191 dir_to_work = Path (tempfile .gettempdir ()) / uid
185192 logfile = dir_to_work / "log.txt"
186193 variables_file = dir_to_work / "variables.json"
@@ -192,6 +199,16 @@ async def run_job(uid, parts=None):
192199 "parts" : parts ,
193200 "status" : "working" ,
194201 }
202+
203+ if parts == None :
204+ parts = ALL_PARTS
205+ if job_config .use_custom_logo :
206+ parts .extend ([f"logo/CustomLogo/MainCase{ l } -{ inv } -{ mn } "
207+ for l in ["" , "Lid" ]
208+ for inv in ["inverted" , "normal" ]
209+ for mn in ["main" , "highlight" ]])
210+ info ["parts" ] = parts
211+
195212 write_info_json = lambda : json .dump (info , info_file .open ("wt" ))
196213
197214 with tempfile .TemporaryDirectory () as temp :
@@ -202,13 +219,6 @@ async def run_job(uid, parts=None):
202219 await copy_build_files_to_build_dir (dir_to_work , temp , job_config .use_custom_logo )
203220
204221 try :
205- if job_config .use_custom_logo :
206- parts .extend ([f"logo/CustomLogo/MainCase{ l } -{ inv } -{ mn } "
207- for l in ["" , "Lid" ]
208- for inv in ["inverted" , "normal" ]
209- for mn in ["main" , "highlight" ]])
210- info ["parts" ] = parts
211- write_info_json ()
212222
213223 make_targets = [f'export/{ name } .stl' for name in parts ]
214224
@@ -282,6 +292,7 @@ async def _as_form(**data):
282292@as_form
283293class CustomVariables (BaseModel ):
284294 use_custom_logo : bool = True
295+ fix_svg : bool = False
285296 MainCase_back_rider : bool = True
286297 MainCase_top_rider : bool = True
287298 MainCase_back_rider_cable : bool = True
@@ -313,7 +324,8 @@ class RunningJob(CustomVariables):
313324def form_get (request : Request ):
314325 # TODO: Switch to CustomVariables.schema for the generation of the <form>
315326 variables = CustomVariables ()
316- fields = [("main_case_logo_svg" , "" ), ("main_case_lid_logo_svg" , "" ), * variables .dict ().items ()]
327+ fields = [("main_case_logo_svg" , "" ), ("main_case_lid_logo_svg" , "" ), ("parts_list" , all_parts ()),
328+ * variables .dict ().items ()]
317329 return templates .TemplateResponse ('customizer.html' , context = {'request' : request , 'fields' : fields })
318330
319331
@@ -371,7 +383,10 @@ async def jobstate(websocket: WebSocket, uid: uuid.UUID):
371383async def form_post (request : Request ,
372384 main_case_logo_svg : Optional [bytes ] = File (None ),
373385 main_case_lid_logo_svg : Optional [bytes ] = File (None ),
386+ parts_list : Optional [list ] = None ,
374387 variables : CustomVariables = Depends (CustomVariables .as_form )):
388+ if set (parts_list ) - set (all_parts ()):
389+ raise HTTPException (status_code = 400 , detail = "Only valid parts can be generated" )
375390 uid = str (uuid .uuid4 ())
376391 work_dir = Path (tempfile .gettempdir ()) / uid
377392 logging .info (work_dir )
@@ -395,7 +410,7 @@ async def form_post(request: Request,
395410 variables_json_file .open ("w" ).write (variables .json ())
396411
397412 try :
398- queue .put_nowait ((run_job , uid ))
413+ queue .put_nowait ((run_job , dict ( uid = uid , parts = parts_list ) ))
399414 except asyncio .QueueFull :
400415 return HTTPException (status_code = 503 , detail = "Queue is full, please try again later" )
401416
0 commit comments