private BWebServiceResponse OnRequest_Internal(HttpListenerContext _Context, Action <string> _ErrorMessageAction) { //Any verb is accepted. var StartTimestamp = MSSinceEpoch(); if (!DatabaseService.ScanTable( ScheduledUrlTaskDBEntry.DBSERVICE_SCHEDULED_URL_TASKS_TABLE(), out List <JObject> URLTasks_UrlEncoded, _ErrorMessageAction)) { return(BWebResponse.StatusOK("Table does not exist or ScanTable operation has failed.")); //Still ok. } if (URLTasks_UrlEncoded.Count == 0) { return(BWebResponse.StatusOK("There is no task in the database.")); } var URLTasks = new List <ScheduledUrlTaskDBEntry>(); foreach (var Current in URLTasks_UrlEncoded) { URLTasks.Add(JsonConvert.DeserializeObject <ScheduledUrlTaskDBEntry>(Current.ToString())); } long RemainedMS = 50000 - (MSSinceEpoch() - StartTimestamp); //60-50=10 seconds is for possible delays. while (RemainedMS > 0) { var BeforeTS = MSSinceEpoch(); SecondCheck(_Context, URLTasks, _ErrorMessageAction); var Diff = MSSinceEpoch() - BeforeTS; if (Diff < 10000) { Thread.Sleep(10000 - (int)Diff); } RemainedMS -= (MSSinceEpoch() - BeforeTS); } return(BWebResponse.StatusAccepted("Request has been accepted.")); }
private BWebServiceResponse UpdateRawFile(HttpListenerContext _Context, Action <string> _ErrorMessageAction) { if (!CommonMethods.TryParsingRequestFor( _Context, out JObject UpdatedFileEntryJson, true, out FileEntry CheckForGenerateUploadUrl, out BWebServiceResponse FailureResponse, _ErrorMessageAction)) { return(FailureResponse); } bool bGenerateUploadUrl = CheckForGenerateUploadUrl.bGenerateUploadUrl; if (bGenerateUploadUrl) { UpdatedFileEntryJson[FileEntry.FILE_ENTRY_FILE_TYPE_PROPERTY] = CheckForGenerateUploadUrl.FileEntryFileType.TrimStart('.').ToLower(); } else { if (UpdatedFileEntryJson.ContainsKey(FileEntry.FILE_ENTRY_NAME_PROPERTY) || UpdatedFileEntryJson.ContainsKey(FileEntry.FILE_ENTRY_FILE_TYPE_PROPERTY) || UpdatedFileEntryJson.ContainsKey(FileEntry.ZIP_MAIN_ASSEMBLY_FILE_NAME_IF_ANY_PROPERTY) || UpdatedFileEntryJson.ContainsKey(FileEntry.DATA_SOURCE_PROPERTY)) { return(BWebResponse.BadRequest("Name|type related fields can only be set when " + FileEntry.GENERATE_UPLOAD_URL_PROPERTY + " option is true.")); } } if (!CommonMethods.TryGettingAllInfo( DatabaseService, RequestedModelID, RequestedRevisionIndex, out ModelDBEntry ModelObject, out Revision RevisionObject, out int _, out FailureResponse, _ErrorMessageAction)) { return(FailureResponse); } if (RevisionObject.FileEntry.FileEntryName != null && RevisionObject.FileEntry.FileEntryName.Length > 0) { if (UpdatedFileEntryJson.ContainsKey(FileEntry.FILE_ENTRY_NAME_PROPERTY) || UpdatedFileEntryJson.ContainsKey(FileEntry.FILE_ENTRY_FILE_TYPE_PROPERTY) || UpdatedFileEntryJson.ContainsKey(FileEntry.ZIP_MAIN_ASSEMBLY_FILE_NAME_IF_ANY_PROPERTY) || UpdatedFileEntryJson.ContainsKey(FileEntry.DATA_SOURCE_PROPERTY)) { return(BWebResponse.BadRequest("File entry (raw) must be deleted before updating.")); } } RevisionObject.FileEntry.Merge(UpdatedFileEntryJson); RevisionObject.FileEntry.FileEntryFileType = RevisionObject.FileEntry.FileEntryFileType.ToLower().TrimStart('.'); RevisionObject.FileEntry.SetRelativeUrls_GetCommonUrlPart_FileEntryFileTypePreSet(RequestedModelID, RequestedRevisionIndex); RevisionObject.FileEntry.FileEntryCreationTime = CommonMethods.GetTimeAsCreationTime(); ModelObject.MRVLastUpdateTime = RevisionObject.FileEntry.FileEntryCreationTime; string UploadUrl_IfRequested = null; if (bGenerateUploadUrl && !FileService.CreateSignedURLForUpload( out UploadUrl_IfRequested, CadFileStorageBucketName, RevisionObject.FileEntry.RawFileRelativeUrl, FileEntry.RAW_FILE_UPLOAD_CONTENT_TYPE, FileEntry.EXPIRY_MINUTES, _ErrorMessageAction)) { return(BWebResponse.InternalError("Signed url generation has failed.")); } Controller_DeliveryEnsurer.Get().DB_UpdateItem_FireAndForget( _Context, ModelDBEntry.DBSERVICE_MODELS_TABLE(), ModelDBEntry.KEY_NAME_MODEL_ID, new BPrimitiveType(RequestedModelID), JObject.Parse(JsonConvert.SerializeObject(ModelObject))); Controller_ModelActions.Get().BroadcastModelAction(new Action_ModelRevisionFileEntryUpdated ( RequestedModelID, RequestedRevisionIndex, ModelObject.ModelOwnerUserID, ModelObject.ModelSharedWithUserIDs, AuthorizedUser.UserID, UpdatedFileEntryJson ), _ErrorMessageAction); var ResultObject = new JObject(); if (bGenerateUploadUrl) { ResultObject[FileEntry.FILE_UPLOAD_URL_PROPERTY] = UploadUrl_IfRequested; ResultObject[FileEntry.FILE_UPLOAD_CONTENT_TYPE_PROPERTY] = FileEntry.RAW_FILE_UPLOAD_CONTENT_TYPE; ResultObject[FileEntry.FILE_DOWNLOAD_UPLOAD_EXPIRY_MINUTES_PROPERTY] = FileEntry.EXPIRY_MINUTES; } return(BWebResponse.StatusAccepted("Update raw file request has been accepted.", ResultObject)); }
private BWebServiceResponse OnRequest_Internal(HttpListenerContext _Context, Action <string> _ErrorMessageAction) { if (_Context.Request.HttpMethod != "POST") { _ErrorMessageAction?.Invoke("StopProcessRequest: 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)); } string BucketName = null; string RelativeFilename = null; string ConversionID_FromRelativeUrl_UrlEncoded = null; 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.")); } BucketName = (string)BucketNameToken; ConversionID_FromRelativeUrl_UrlEncoded = WebUtility.UrlEncode((string)RawFileRelativeUrlToken); 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.")); } } } //Temporarily, TODO: Change this when the implementation is in place. if (!DatabaseService.DeleteItem( FileConversionDBEntry.DBSERVICE_FILE_CONVERSIONS_TABLE(), FileConversionDBEntry.KEY_NAME_CONVERSION_ID, new BPrimitiveType(ConversionID_FromRelativeUrl_UrlEncoded), out JObject _, EBReturnItemBehaviour.DoNotReturn, _ErrorMessageAction)) { if (!DatabaseService.GetItem( FileConversionDBEntry.DBSERVICE_FILE_CONVERSIONS_TABLE(), FileConversionDBEntry.KEY_NAME_CONVERSION_ID, new BPrimitiveType(ConversionID_FromRelativeUrl_UrlEncoded), FileConversionDBEntry.Properties, out JObject _ReturnObject, _ErrorMessageAction) || _ReturnObject != null) { return(BWebResponse.InternalError("Database error.")); } } if (!BatchProcessingCreationService.Instance.StopBatchProcess(BucketName, RelativeFilename, _ErrorMessageAction)) { return(BWebResponse.InternalError("Failed to stop pod or connect to kubernetess")); } return(BWebResponse.StatusAccepted("Request has been accepted; process is now being stopped.")); }
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")); } }