async Task <PersistedMessage> IPersist.RescheduleAsync(Guid Id, TimeSpan fromNow, CancellationToken token) { if (this.serviceDown) { throw new ServiceEndpointDownException("Mongo Persist Service is Down"); } PersistedMessage pop = null; IMongoCollection <PersistedMessage> collection = this.GetCollection(); try { token.ThrowIfCancellationRequested(); DateTime toSet = DateTime.UtcNow.Add(fromNow); var update = Builders <PersistedMessage> .Update .Set(g => g.Status, PersistedMessageStatusOptions.ReadyToProcess) .Set(g => g.DateStamp, toSet) .Inc(g => g.RetryCount, 1); var filter = Builders <PersistedMessage> .Filter.Eq(m => m.Id, Id); pop = await collection.FindOneAndUpdateAsync(filter, update, null, token); } catch (Exception e) { this.serviceDown = true; throw new ServiceEndpointDownException("Mongo Persist Service is Down", e); } return(pop); }
async Task <PersistedMessage> IPersist.MarkAsync(Guid Id, PersistedMessageStatusOptions mark, CancellationToken token) { if (this.serviceDown) { throw new ServiceEndpointDownException("Mongo Persist Service is Down"); } PersistedMessage pop = null; IMongoCollection <PersistedMessage> collection = this.GetCollection(); try { token.ThrowIfCancellationRequested(); var update = Builders <PersistedMessage> .Update.Set(g => g.Status, mark); var filter = Builders <PersistedMessage> .Filter.Eq(m => m.Id, Id); await collection.UpdateOneAsync(filter, update, null, token); } catch (Exception e) { this.serviceDown = true; throw new ServiceEndpointDownException("Mongo Persist Service is Down", e); } return(pop); }
public async Task <bool> UpdateConfigAsync(T config, CancellationToken token) { bool toReturn = false; string serialized = Newtonsoft.Json.JsonConvert.SerializeObject(config); string typeName = Util.Util.GetTypeName(typeof(T)); PersistedMessage oldConfig = await db.PeekNextAsync("config", token); PersistedMessage newConfig = new PersistedMessage(serialized) { Headers = new Dictionary <string, string>() , MessageType = typeName , Ordinal = 0 , DateStamp = DateTime.UtcNow , Status = PersistedMessageStatusOptions.ReadyToProcess , Queue = "config" }; newConfig.Routes.Add(new Route() { EndPointName = "config", EndPointType = Publish.EndPointTypeOptions.Handler , Routed = DateTime.UtcNow }); toReturn = await db.PersistAsync(new List <PersistedMessage>() { newConfig }, token); //peek pulls latest anyway, so if this fails, no harm done if (oldConfig != null) { await db.MarkAsync(oldConfig.Id, PersistedMessageStatusOptions.Marked, token); } return(toReturn); }
async Task <PersistedMessage> IPersist.PeekAndMarkAsync(string q, PersistedMessageStatusOptions mark, CancellationToken token, Guid messageId) { if (this.serviceDown) { throw new ServiceEndpointDownException("Mongo Persist Service is Down"); } IMongoCollection <PersistedMessage> collection = this.GetCollection(); PersistedMessage pop = null; try { token.ThrowIfCancellationRequested(); FilterDefinitionBuilder <PersistedMessage> fBuilder = Builders <PersistedMessage> .Filter; //var qfilter = fBuilder.And(fBuilder.Eq(g => g.Status, PersistedMessageStatusOptions.ReadyToProcess), fBuilder.Eq(g => g.Queue, q), fBuilder.Lte(g => g.DateStamp, DateTime.UtcNow)); var qfilter = fBuilder.Eq(g => g.Id, messageId); var update = Builders <PersistedMessage> .Update.Set(g => g.Status, mark); //var sort = Builders<PersistedMessage>.Sort.Ascending(e => e.DateStamp).Ascending(e => e.Ordinal); var options = new FindOneAndUpdateOptions <PersistedMessage>() { ReturnDocument = ReturnDocument.After }; pop = await collection.FindOneAndUpdateAsync(qfilter, update, options, token); } catch (Exception e) { this.serviceDown = true; throw new ServiceEndpointDownException("Mongo Persist Service is Down", e); } return(pop); }
static string FormatQuarantinedLogEntryXmlContent(PersistedMessage message) { if (!message.IsDataAvailable || !message.DataXml.HasValue) { return(string.Empty); } var sb = new StringBuilder(); var settings = new XmlWriterSettings { Indent = true, IndentChars = " ", NewLineChars = Environment.NewLine, NewLineHandling = NewLineHandling.Replace, OmitXmlDeclaration = true }; using (var writer = XmlWriter.Create(sb, settings)) { message.DataXml.Value.WriteTo(writer); writer.Flush(); } return(HttpUtility.HtmlEncode(sb.ToString())); }
public JsonResult Publish(PersistedMessage message) { ShortBus.Bus.BroadcastMessage(message); return(Json(new { result = true }, JsonRequestBehavior.AllowGet)); //return result:true }
HandlerResponse IMessageHandler.Handle(PersistedMessage message) { TestMessage m = JsonConvert.DeserializeObject <TestMessage>(message.PayLoad); //if (message.RetryCount == 0) { // Console.WriteLine("Recieved {0}, rescheduling", m.Property); // return HandlerResponse.Retry(TimeSpan.FromMinutes(1)); //} else { Console.WriteLine("processing {0}", m.Property); return(HandlerResponse.Handled()); //} }
public RouteResponse PostMessage(PersistedMessage message) { RouteResponse toReturn = new RouteResponse() { Status = false }; try { toReturn = Bus.ReceiveMessage(message); } catch { } return(toReturn); }
public async Task <T> GetConfigAsync(CancellationToken token) { T toReturn = default(T); PersistedMessage last = await db.PeekNextAsync("config", token); if (last != null) { string serialized = last.PayLoad; toReturn = Newtonsoft.Json.JsonConvert.DeserializeObject <T>(serialized); } return(toReturn); }
async Task <bool> IPersist.ResetConnectionAsync(CancellationToken token) { this.serviceDown = false; dbInstance = null; try { await((IPersist)this).PersistAsync(new PersistedMessage[] { new PersistedMessage("Test") { Queue = "diag_test", DateStamp = DateTime.UtcNow } }, token); PersistedMessage peeked = await((IPersist)this).PeekAndMarkNextAsync("diag_test", PersistedMessageStatusOptions.Marked, token); PersistedMessage popped = await((IPersist)this).PopAsync(peeked.Id, token); } catch (Exception) { this.serviceDown = true; } return(this.serviceDown); }
async Task <PersistedMessage> IPersist.PeekNextAsync(string q, CancellationToken token) { if (this.serviceDown) { throw new ServiceEndpointDownException("Mongo Persist Service is Down"); } IMongoCollection <PersistedMessage> collection = this.GetCollection(); PersistedMessage pop = null; try { token.ThrowIfCancellationRequested(); FilterDefinitionBuilder <PersistedMessage> fBuilder = Builders <PersistedMessage> .Filter; //var qfilter = fBuilder.And(fBuilder.Eq(g => g.Status, PersistedMessageStatusOptions.ReadyToProcess), fBuilder.Eq(g => g.Queue, q), fBuilder.Lte(g => g.DateStamp, DateTime.UtcNow)); var qfilter = fBuilder.And(fBuilder.Eq(g => g.Status, PersistedMessageStatusOptions.ReadyToProcess), fBuilder.Eq(g => g.Queue, q)); var sort = Builders <PersistedMessage> .Sort.Ascending(e => e.DateStamp).Ascending(e => e.Ordinal); var options = new FindOptions <PersistedMessage>() { Sort = sort }; using (IAsyncCursor <PersistedMessage> cursor = await collection.FindAsync(qfilter, options, token)) { while (await cursor.MoveNextAsync(token)) { if (cursor.Current.Count() > 0) { pop = cursor.Current.FirstOrDefault(); } } } return(pop); } catch (Exception e) { this.serviceDown = true; throw new ServiceEndpointDownException("Mongo Persist Service is Down", e); } }
async Task <PersistedMessage> IPersist.PopAsync(Guid Id, CancellationToken token) { if (this.serviceDown) { throw new ServiceEndpointDownException("Mongo Persist Service is Down"); } PersistedMessage pop = null; IMongoCollection <PersistedMessage> collection = this.GetCollection(); try { token.ThrowIfCancellationRequested(); var filter = Builders <PersistedMessage> .Filter.Eq(m => m.Id, Id); pop = await collection.FindOneAndDeleteAsync(filter, null, token); } catch (Exception e) { this.serviceDown = true; throw new ServiceEndpointDownException("Mongo Persist Service is Down", e); } return(pop); }
//postmessage private RouteResponse Post(PersistedMessage post, string command) { RouteResponse toReturn = new RouteResponse() { PayLoad = null, Status = false }; try { string url = settings.URL + string.Format(@"/api/message/{0}", command); url = url.ToLower().Replace("localhost", Util.Util.GetLocalIP()); HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(new Uri(url)); //req.ContentType = @"application/x-www-form-urlencoded"; req.ContentType = "application/json"; req.Method = "POST"; string payload = JsonConvert.SerializeObject(post); //string postData = string.Format(@"&message={0}", payload); string postData = string.Format(@"{0}", payload); req.ContentLength = postData.Length; req.Timeout = 5000; ServicePointManager.ServerCertificateValidationCallback = delegate { return(true); }; ServicePointManager.DnsRefreshTimeout = 5000; Task <Stream> reqTask = req.GetRequestStreamAsync(); reqTask.Wait(); using (Stream reqStream = req.GetRequestStream()) using (StreamWriter sw = new StreamWriter(reqStream, System.Text.Encoding.ASCII)) { sw.Write(postData); sw.Flush(); sw.Close(); reqStream.Close(); } string responseText = null; //Task<WebResponse> resTask = req.GetResponseAsync(); //resTask.Wait(); // WebResponse response = req.GetResponse(); //using (WebResponse response = resTask.Result) using (WebResponse response = req.GetResponse()) using (Stream resStream = response.GetResponseStream()) using (StreamReader rdr = new StreamReader(resStream)) { responseText = rdr.ReadToEnd(); rdr.Close(); response.Close(); } toReturn = JsonConvert.DeserializeObject <RouteResponse>(responseText); } catch (System.AggregateException ae) { this.ImDown = true; } catch (WebException e) { this.ImDown = true; } catch (Exception e) { this.ImDown = true; } return(toReturn); }
RouteResponse IMessageRouter.Publish(PersistedMessage message) { return(this.Post(message, "PostMessage")); }