private static async void OnSubscriptionNotify(string server, ServiceEventMessage message) { #if DEBUG Console.WriteLine("Consumed OnSubscriptionNotify message"); Console.WriteLine("Server: " + server); Console.WriteLine("Message: " + message.ToString()); #endif string url = (string)message.Parameters["Url"]; string clientID = (string)message.Parameters["ClientID"]; Model.WebhookNotification notification = new Model.WebhookNotification(); MemoryStream stream = new MemoryStream(16384); new ServiceModels.WebhookNotification(notification, message).Serialise(stream); string payload = new StreamReader(stream).ReadToEnd(); bool retry = message.Parameters.ContainsKey("RequeueCount"); long requeueCount = retry ? (long)message.Parameters["RequeueCount"] : 0; bool dropMessage = false; #if DEBUG Console.WriteLine($"Sending payload to {url}: \n" + payload); #endif try { RESTClient.RESTResponse response = await RESTClient.PostAsync(url, ACCEPT_TYPE, null, notification.AcceptContentType, payload); switch ((HttpStatusCode)response.StatusCode) { case HttpStatusCode.OK: case HttpStatusCode.Created: ApplicationEventLog.Write(LogLevel.Information, $"Webhook post notification from client {clientID} to {url} successful."); break; case HttpStatusCode.BadRequest: case HttpStatusCode.Unauthorized: case HttpStatusCode.NotFound: case HttpStatusCode.MethodNotAllowed: // No retry for these failures ApplicationEventLog.Write(LogLevel.Warning, $"Non-recoverable HTTP Status code from{url}:{response.StatusCode}, discarding message."); dropMessage = true; break; default: ApplicationEventLog.Write(LogLevel.Warning, $"Unexpected HTTP Status code from{url}:{response.StatusCode}"); break; } } catch(Exception ex) { ApplicationEventLog.Write(LogLevel.Error, "Failed to post webhook", ex); } if ((MAX_POST_ATTEMPTS <= 0 || requeueCount < MAX_POST_ATTEMPTS) && !dropMessage) { BusinessLogicFactory.ServiceMessages.NackMessage(message); } }
private static void OnUpdateSubscription(string server, ServiceEventMessage message) { #if DEBUG Console.WriteLine("Consumed update subscription message"); Console.WriteLine("Server: " + server); Console.WriteLine("Message: " + message.ToString()); #endif Subscription subscription = GetSubscriptionFromMessage(message); if (subscription != null) { if (subscription.SubscriptionType == TSubscriptionType.Observation) { if (subscription.NotificationParameters != null && subscription.PropertyDefinitionID != Guid.Empty) { Client client = DataAccessFactory.Clients.GetClient(subscription.ClientID); if (client != null) { if (DataAccessFactory.Servers.SetNotificationParameters(client, subscription.ObjectDefinitionID, subscription.ObjectID, subscription.PropertyDefinitionID, subscription.NotificationParameters)) ApplicationEventLog.Write(LogLevel.Information, $"Updated notification parameters for resource /{subscription.ObjectDefinitionID}/{subscription.ObjectID}/{subscription.PropertyDefinitionID}"); else ApplicationEventLog.Write(LogLevel.Warning, $"Failed to update notification parameters for resource /{subscription.ObjectDefinitionID}/{subscription.ObjectID}/{subscription.PropertyDefinitionID}"); } else { ApplicationEventLog.Write(LogLevel.Warning, $"No client exists for {subscription.ClientID} in subscription {subscription.SubscriptionID}"); } } } DataAccessFactory.Subscriptions.SaveSubscription(subscription, TObjectState.Update); } else { ApplicationEventLog.Write(LogLevel.Warning, $"Failed to lookup subscription: {message.Parameters["SubscriptionID"]}"); } }
private static void OnObservationNotify(string server, ServiceEventMessage message) { #if DEBUG Console.WriteLine("Consumed OnObservationNotify message"); Console.WriteLine("Server: " + server); Console.WriteLine("Message: " + message.ToString()); #endif NotifySubscribers(message, TSubscriptionType.Observation); }
private static void OnDeleteSubscription(string server, ServiceEventMessage message) { #if DEBUG Console.WriteLine("Consumed delete subscription message"); Console.WriteLine("Server: " + server); Console.WriteLine("Message: " + message.ToString()); #endif Subscription subscription = GetSubscriptionFromMessage(message); if (subscription != null) { if (subscription.SubscriptionType == TSubscriptionType.Observation) { CancelObservation(subscription); } DataAccessFactory.Subscriptions.SaveSubscription(subscription, TObjectState.Delete); } else { ApplicationEventLog.Write(LogLevel.Warning, $"Failed to lookup subscription: {message.Parameters["SubscriptionID"]}"); } }
private static void OnCreateSubscription(string server, ServiceEventMessage message) { #if DEBUG Console.WriteLine("Consumed create subscription message"); Console.WriteLine("Server: " + server); Console.WriteLine("Message: " + message.ToString()); #endif Subscription subscription = GetSubscriptionFromMessage(message); if (subscription != null) { if (subscription.SubscriptionType == TSubscriptionType.Observation) { Observe(subscription); } else { // for server event types, we are already subscribed to RabbitMQ service messages. } } else { ApplicationEventLog.Write(LogLevel.Warning, $"Failed to lookup subscription: {message.Parameters["SubscriptionID"]}"); } }
private static void OnClientDisconnected(string server, ServiceEventMessage message) { #if DEBUG Console.WriteLine("Consumed OnClientDisconnected message"); Console.WriteLine("Server: " + server); Console.WriteLine("Message: " + message.ToString()); #endif NotifySubscribers(message, TSubscriptionType.ClientDisconnected); }