private BWebServiceResponse OnRequest_Internal(HttpListenerContext _Context, Action <string> _ErrorMessageAction) { _ErrorMessageAction?.Invoke("Trying to get Optimizer Env Vars"); if (_Context.Request.HttpMethod != "GET") { _ErrorMessageAction?.Invoke("Wrong type"); return(BWebResponse.BadRequest($"This service does not accept requests of type : {_Context.Request.HttpMethod}")); } string Url = _Context.Request.RawUrl; int CopyStart = Url.LastIndexOf('/') + 1; string Podname = Url.Substring(CopyStart).TrimEnd('/'); BatchProcessingCreationService.Instance.GetBucketAndFile(Podname, out string _Bucket, out string _Filename); if (!string.IsNullOrWhiteSpace(_Bucket) && !string.IsNullOrWhiteSpace(_Filename)) { string NewConversionID_FromRelativeUrl_UrlEncoded = WebUtility.UrlEncode(_Filename); bool CanProceed = false; bool ProcessError = false; if (DatabaseService.GetItem( FileConversionDBEntry.DBSERVICE_FILE_CONVERSIONS_TABLE(), FileConversionDBEntry.KEY_NAME_CONVERSION_ID, new BPrimitiveType(NewConversionID_FromRelativeUrl_UrlEncoded), FileConversionDBEntry.Properties, out JObject ConversionObject )) { if (ConversionObject != null && ConversionObject.ContainsKey("conversionStatus")) { EInternalProcessStage ExistingStatus = (EInternalProcessStage)(int)ConversionObject["conversionStatus"]; if (ExistingStatus == EInternalProcessStage.ProcessComplete) { CanProceed = true; } else if (ExistingStatus == EInternalProcessStage.ProcessFailed) { ProcessError = true; } } } else { ProcessError = true; } if (CanProceed) { Dictionary <string, string> EnvVars = BatchProcessingCreationService.Instance.GetOptimizerEnvVars(_Bucket, _Filename, Podname, _ErrorMessageAction); if (EnvVars == null) { _ErrorMessageAction?.Invoke("EnvVars null"); return(BWebResponse.InternalError("An error occured trying to generate optimizer parameters")); } return(BWebResponse.StatusOK("success", new JObject() { ["uploadRequestUrl"] = EnvVars["CAD_PROCESS_UPLOAD_REQUEST_URL"], ["downloadHierarchyCfUrl"] = EnvVars["DOWNLOAD_HIERARCHY_CF"], ["downloadGeometryCfUrl"] = EnvVars["DOWNLOAD_GEOMETRY_CF"], ["downloadMetadataCfUrl"] = EnvVars["DOWNLOAD_METADATA_CF"] })); } else if (ProcessError) { _ErrorMessageAction?.Invoke("Cad process Failed"); return(BWebResponse.InternalError("Pixyz process has failed")); } else { _ErrorMessageAction?.Invoke("Not found"); return(BWebResponse.NotFound("Cad process has not completed yet")); } } else { _ErrorMessageAction?.Invoke("General failure"); return(BWebResponse.InternalError("An error occured trying to retreive pod details")); } }
private BWebServiceResponse OnRequest_Internal(HttpListenerContext _Context, Action <string> _ErrorMessageAction) { if (_Context.Request.HttpMethod != "POST") { _ErrorMessageAction?.Invoke("StartProcessRequest: POST methods is accepted. But received request method: " + _Context.Request.HttpMethod); return(BWebResponse.MethodNotAllowed("POST methods is accepted. But received request method: " + _Context.Request.HttpMethod)); } var NewDBEntry = new FileConversionDBEntry() { ConversionStatus = (int)EInternalProcessStage.Queued }; string NewConversionID_FromRelativeUrl_UrlEncoded = null; string BucketName = null; string RelativeFileName = null; string ZipMainAssembly = ""; using (var InputStream = _Context.Request.InputStream) { var NewObjectJson = new JObject(); using (var ResponseReader = new StreamReader(InputStream)) { try { var ParsedBody = JObject.Parse(ResponseReader.ReadToEnd()); if (!ParsedBody.ContainsKey("bucketName") || !ParsedBody.ContainsKey("rawFileRelativeUrl")) { return(BWebResponse.BadRequest("Request body must contain all necessary fields.")); } var BucketNameToken = ParsedBody["bucketName"]; var RawFileRelativeUrlToken = ParsedBody["rawFileRelativeUrl"]; if (BucketNameToken.Type != JTokenType.String || RawFileRelativeUrlToken.Type != JTokenType.String) { return(BWebResponse.BadRequest("Request body contains invalid fields.")); } if (ParsedBody.ContainsKey("zipTypeMainAssemblyFileNameIfAny")) { var ZipMainAssemblyToken = ParsedBody["zipTypeMainAssemblyFileNameIfAny"]; if (ZipMainAssemblyToken.Type != JTokenType.String) { return(BWebResponse.BadRequest("Request body contains invalid fields.")); } ZipMainAssembly = (string)ZipMainAssemblyToken; } NewDBEntry.BucketName = (string)BucketNameToken; NewConversionID_FromRelativeUrl_UrlEncoded = WebUtility.UrlEncode((string)RawFileRelativeUrlToken); BucketName = (string)BucketNameToken; RelativeFileName = (string)RawFileRelativeUrlToken; } catch (Exception e) { _ErrorMessageAction?.Invoke("Read request body stage has failed. Exception: " + e.Message + ", Trace: " + e.StackTrace); return(BWebResponse.BadRequest("Malformed request body. Request must be a valid json form.")); } } } if (BucketName == null || RelativeFileName == null) { return(BWebResponse.InternalError("No BucketName or FileName")); } BDatabaseAttributeCondition UpdateCondition = DatabaseService.BuildAttributeNotExistCondition(FileConversionDBEntry.KEY_NAME_CONVERSION_ID); //If a process was completed (success or failure) then allow reprocessing //Only stop if a process is currently busy processing or already queued if (DatabaseService.GetItem( FileConversionDBEntry.DBSERVICE_FILE_CONVERSIONS_TABLE(), FileConversionDBEntry.KEY_NAME_CONVERSION_ID, new BPrimitiveType(NewConversionID_FromRelativeUrl_UrlEncoded), FileConversionDBEntry.Properties, out JObject ConversionObject )) { if (ConversionObject != null && ConversionObject.ContainsKey("conversionStatus")) { EInternalProcessStage ExistingStatus = (EInternalProcessStage)(int)ConversionObject["conversionStatus"]; if (ExistingStatus == EInternalProcessStage.ProcessFailed || ExistingStatus == EInternalProcessStage.ProcessComplete) { UpdateCondition = null; } } } if (!DatabaseService.UpdateItem( FileConversionDBEntry.DBSERVICE_FILE_CONVERSIONS_TABLE(), FileConversionDBEntry.KEY_NAME_CONVERSION_ID, new BPrimitiveType(NewConversionID_FromRelativeUrl_UrlEncoded), JObject.Parse(JsonConvert.SerializeObject(NewDBEntry)), out JObject _ExistingObject, EBReturnItemBehaviour.DoNotReturn, UpdateCondition, _ErrorMessageAction)) { return(BWebResponse.Conflict("File is already being processed/queued.")); } try { if (BatchProcessingCreationService.Instance.StartBatchProcess(BucketName, RelativeFileName, ZipMainAssembly, out string _PodName, _ErrorMessageAction)) { //Code for initial method of starting optimizer after pixyz completes //return BWebResponse.StatusAccepted("Request has been accepted; process is now being started."); if (BatchProcessingCreationService.Instance.StartFileOptimizer(BucketName, RelativeFileName, _ErrorMessageAction)) { return(BWebResponse.StatusAccepted("Request has been accepted; process is now being started.")); } else { NewDBEntry.ConversionStatus = (int)EInternalProcessStage.ProcessFailed; if (!DatabaseService.UpdateItem( FileConversionDBEntry.DBSERVICE_FILE_CONVERSIONS_TABLE(), FileConversionDBEntry.KEY_NAME_CONVERSION_ID, new BPrimitiveType(NewConversionID_FromRelativeUrl_UrlEncoded), JObject.Parse(JsonConvert.SerializeObject(NewDBEntry)), out JObject _, EBReturnItemBehaviour.DoNotReturn, null, _ErrorMessageAction)) { return(BWebResponse.InternalError("Failed to start the batch process and experienced a Database error")); } //Try kill pixyz pod that we have succeeded in creating if (!BatchProcessingCreationService.Instance.TryKillPod(_PodName, "cip-batch")) { return(BWebResponse.InternalError("Failed to start the unreal optimizer and failed to kill pixyz pod")); } return(BWebResponse.InternalError("Failed to start the batch process and experienced a Database error")); } } else { NewDBEntry.ConversionStatus = (int)EInternalProcessStage.ProcessFailed; if (!DatabaseService.UpdateItem( FileConversionDBEntry.DBSERVICE_FILE_CONVERSIONS_TABLE(), FileConversionDBEntry.KEY_NAME_CONVERSION_ID, new BPrimitiveType(NewConversionID_FromRelativeUrl_UrlEncoded), JObject.Parse(JsonConvert.SerializeObject(NewDBEntry)), out JObject _, EBReturnItemBehaviour.DoNotReturn, null, _ErrorMessageAction)) { return(BWebResponse.InternalError("Failed to start the batch process and experienced a Database error")); } return(BWebResponse.InternalError("Failed to start the batch process")); } }