public static Manager_PubSubService Get() { if (Instance == null) { Instance = new Manager_PubSubService(); } return(Instance); }
public void PublishSerializedAction(string _SerializedAction, Actions.EAction _ActionServiceIdentifier, Action <string> _ErrorMessageAction = null) { bool bSuccess = false; int TrialCounter = 0; do { bSuccess = Manager_PubSubService.Get().PublishAction( _ActionServiceIdentifier, _SerializedAction, _ErrorMessageAction); if (!bSuccess) { Thread.Sleep(500); } }while (!bSuccess && TrialCounter++ < MAX_PUBLISH_FAILURE_TRIAL_COUNTS); if (!bSuccess) { _ErrorMessageAction?.Invoke("PublishSerializedAction: Manager_PubSubService.Get().PublishAction has failed " + MAX_PUBLISH_FAILURE_TRIAL_COUNTS + " times. Operation timed out."); } }
//In case of false return, operation shall be cancelled with an internal error. public bool GetClearanceForDBOperation(WebServiceBaseTimeoutableProcessor _ServiceProcessor, string _DBTableName, string _Identifier, Action <string> _ErrorMessageAction = null) { if (!_ServiceProcessor.IsDoNotGetDBClearanceSet()) { return(true); } var CreatedAction = new Action_OperationTimeout(_DBTableName, _Identifier); lock (_ServiceProcessor.RelevantTimeoutStructures) { bool bFound = false; foreach (var CTS in _ServiceProcessor.RelevantTimeoutStructures) { if (CTS.Equals(CreatedAction)) { CreatedAction = CTS; bFound = true; break; } } if (!bFound) { _ServiceProcessor.RelevantTimeoutStructures.Add(CreatedAction); } } var MemoryEntryValue = ATOMIC_DB_OP_CTRL_MEM_PREFIX + _DBTableName + "-" + _Identifier; if (_ServiceProcessor.IsUseQueueSetClearanceActionsSet() && _ServiceProcessor.TryRemoveSetClearanceAwaitItem(MemoryEntryValue)) { return(true); } int TrialCounter = 0; bool bResult; do { bResult = MemoryService.SetKeyValueConditionally( QueryParameters, new Tuple <string, BPrimitiveType>(MemoryEntryValue, new BPrimitiveType("busy")), _ErrorMessageAction); if (!bResult) { Thread.Sleep(1000); } }while (!bResult && TrialCounter++ < TIMEOUT_TRIAL_SECONDS); if (TrialCounter >= TIMEOUT_TRIAL_SECONDS) { _ErrorMessageAction?.Invoke("Atomic DB Operation Controller->GetClearanceForDBOperation: A timeout has occured for operation type " + _DBTableName + ", for ID " + _Identifier + ", existing operation has been overriden by the new request."); Manager_PubSubService.Get().PublishAction(Actions.EAction.ACTION_OPERATION_TIMEOUT, JsonConvert.SerializeObject(new Action_OperationTimeout() { TableName = _DBTableName, EntryKey = _Identifier }), _ErrorMessageAction); //Timeout for other operation has occured. return(MemoryService.SetKeyValue(QueryParameters, new Tuple <string, BPrimitiveType>[] { new Tuple <string, BPrimitiveType>(MemoryEntryValue, new BPrimitiveType("busy")) }, _ErrorMessageAction)); } return(true); }
public void StartTimeoutCheckOperation(Action <Action_OperationTimeout> _OnNotificationReceivedAction) { Subscriber = new PubSubTimeoutSubscriber(_OnNotificationReceivedAction); Manager_PubSubService.Get().AddSubscriber(Subscriber, Actions.EAction.ACTION_OPERATION_TIMEOUT); }
public static BWebServiceResponse OnRequestWebhook(HttpListenerContext _Context, string _CallerMethod, Func <ServiceUtilities.Action, bool> _HandleAction, Action <string> _ErrorMessageAction = null) { string SerializedData = null; string TopicName = null; using (var InputStream = _Context.Request.InputStream) { using (var Reader = new StreamReader(InputStream)) { var JsonMessage = Reader.ReadToEnd(); try { var Parsed = JObject.Parse(JsonMessage); if (Parsed.ContainsKey("data")) { var DataObject = (JObject)Parsed["data"]; if (DataObject.ContainsKey("topicName") /*, * DataObject.ContainsKey("namespaceName") * && DataObject.ContainsKey("requestUri") * && DataObject.ContainsKey("subscriptionName")*/) { TopicName = (string)DataObject["topicName"]; //var NamespaceName = (string)DataObject["namespaceName"]; //var RequestUri = (string)DataObject["requestUri"]; //var SubscriptionName = (string)DataObject["subscriptionName"]; _ErrorMessageAction?.Invoke($"{_CallerMethod}->OnRequest[INFO]: Action received from CloudEventSchemaV1_0. TopicName: '{TopicName}'"); } } else { _ErrorMessageAction?.Invoke(_CallerMethod + "->OnRequest[ERROR]: Invalid CloudEventSchemaV1_0 data type. Payload is: " + JsonMessage); return(BWebResponse.BadRequest("Invalid CloudEventSchemaV1_0 data type. Json Parse error occurred.")); } } catch (Exception e) { _ErrorMessageAction?.Invoke(_CallerMethod + "->OnRequest[ERROR]: Invalid CloudEventSchemaV1_0 data type. Error: " + e.Message + ", trace: " + e.StackTrace + ", payload is: " + JsonMessage); return(BWebResponse.BadRequest("Invalid CloudEventSchemaV1_0 data type. Json Parse error occurred.")); } } } if (!Manager_PubSubService.Get().ReceiveSingleMessage(TopicName, (string ReceivedTopic, string ReceivedMessage) => { var ParsedMessage = JObject.Parse(ReceivedMessage); if (ParsedMessage.ContainsKey("data")) { SerializedData = ParsedMessage["data"].ToString(); } else { SerializedData = ParsedMessage.ToString(); } if (!Manager_PubSubService.Get().DeserializeReceivedMessage(SerializedData, out Actions.EAction Action, out string SerializedAction, _ErrorMessageAction)) { _ErrorMessageAction?.Invoke(_CallerMethod + "->SubscribeAction: An error occured. Retrying."); } if (!_HandleAction.Invoke(Actions.DeserializeAction(Action, SerializedAction))) { _ErrorMessageAction?.Invoke(_CallerMethod + "->DeserializeAction: An error occured. Retrying."); } }, _ErrorMessageAction))
public static BWebServiceResponse OnRequest(HttpListenerContext _Context, string _CallerMethod, Func <ServiceUtilities.Action, bool> _HandleAction, Action <string> _ErrorMessageAction = null) { string SerializedData = null; using (var InputStream = _Context.Request.InputStream) { using (var Reader = new StreamReader(InputStream)) { var JsonMessage = Reader.ReadToEnd(); try { var Parsed = JObject.Parse(JsonMessage); if (Parsed.ContainsKey("message")) { var MessageObject = (JObject)Parsed["message"]; if (MessageObject.ContainsKey("data")) { var EncodedData = (string)MessageObject["data"]; SerializedData = Encoding.UTF8.GetString(Convert.FromBase64String(EncodedData)); } } if (SerializedData == null) { SerializedData = JsonMessage; } } catch (Exception e) { _ErrorMessageAction?.Invoke(_CallerMethod + "->OnRequest: Conversion from Base64 to string has failed with " + e.Message + ", trace: " + e.StackTrace + ", payload is: " + JsonMessage); return(BWebResponse.BadRequest("Conversion from Base64 to string has failed.")); } } } if (!Manager_PubSubService.Get().DeserializeReceivedMessage(SerializedData, out Actions.EAction Action, out string SerializedAction, _ErrorMessageAction)) { return(BWebResponse.BadRequest("Deserialization of Pub/Sub Message has failed.")); } bool bResult; try { bResult = _HandleAction.Invoke(Actions.DeserializeAction(Action, SerializedAction)); } catch (Exception e) { return(BWebResponse.BadRequest("Deserialization to Action has failed with " + e.Message + ", trace: " + e.StackTrace)); } if (bResult) { return(BWebResponse.StatusOK("Processed.")); } _ErrorMessageAction?.Invoke(_CallerMethod + "->OnRequest: An error occured. Retrying."); //Cooldown Thread.Sleep(1000); return(BWebResponse.BadRequest("An error occurred. Retrying.")); }