/// <summary> /// Waits for a condition to be satisfied. /// </summary> /// <typeparam name="T"> /// The changeType of object that will be operated on. /// </typeparam> /// <param name="client"> /// The client instance this is extending. /// </param> /// <param name="poll"> /// The function to poll the client for state. /// </param> /// <param name="continuePolling"> /// A function to evaluate the state to see if it matches the condition. /// </param> /// <param name="notifyHandler"> /// A notification handler used to rais events when the status changes. /// </param> /// <param name="interval"> /// A time frame to wait between polls. /// </param> /// <param name="timeout"> /// The amount of time to wait for the condition to be satisfied. /// </param> /// <param name="cancellationToken"> /// A Cancelation Token that can be used to cancel the request. /// </param> /// <returns> /// An awaitable task. /// </returns> public static async Task WaitForCondition <T>(this IHDInsightManagementPocoClient client, Func <Task <T> > poll, Func <T, PollResult> continuePolling, Action <T> notifyHandler, TimeSpan interval, TimeSpan timeout, CancellationToken cancellationToken) { client.ArgumentNotNull("client"); poll.ArgumentNotNull("poll"); continuePolling.ArgumentNotNull("continuePolling"); var start = DateTime.Now; int pollingFailures = 0; const int MaxPollingFailuresCount = 10; T pollingResult = default(T); PollResult result = PollResult.Continue; do { try { pollingResult = await poll(); result = continuePolling(pollingResult); if (notifyHandler.IsNotNull() && pollingResult.IsNotNull()) { notifyHandler(pollingResult); } if (result == PollResult.Unknown || result == PollResult.Null) { pollingFailures++; if (result == PollResult.Null) { client.LogMessage("Poll for cluster returned no cluster. Current error weight (out of 10): " + pollingFailures.ToString(CultureInfo.InvariantCulture), Severity.Informational, Verbosity.Diagnostic); } cancellationToken.WaitForInterval(interval); } else if (result == PollResult.Continue) { pollingFailures = 0; cancellationToken.WaitForInterval(interval); } } catch (Exception ex) { ex = ex.GetFirstException(); var hlex = ex as HttpLayerException; var httpEx = ex as HttpRequestException; var webex = ex as WebException; var timeOut = ex as TimeoutException; var taskCancled = ex as TaskCanceledException; var operationCanceled = ex as OperationCanceledException; if (taskCancled.IsNotNull() && taskCancled.CancellationToken.IsNotNull() && taskCancled.CancellationToken.IsCancellationRequested) { throw; } if (operationCanceled.IsNotNull() && operationCanceled.CancellationToken.IsNotNull() && operationCanceled.CancellationToken.IsCancellationRequested) { throw; } if (hlex.IsNotNull() || httpEx.IsNotNull() || webex.IsNotNull() || taskCancled.IsNotNull() || timeOut.IsNotNull() || operationCanceled.IsNotNull()) { pollingFailures += 5; client.LogMessage("Poll for cluster a manageable exception. Current error weight (out of 10): " + pollingFailures.ToString(CultureInfo.InvariantCulture), Severity.Informational, Verbosity.Diagnostic); client.LogMessage(ex.ToString(), Severity.Informational, Verbosity.Diagnostic); if (pollingFailures >= MaxPollingFailuresCount) { client.LogMessage("Polling error weight exceeded maximum allowed. Aborting operation.", Severity.Error, Verbosity.Normal); throw; } } else { client.LogMessage("Poll for cluster returned an unmanageable exception. Aborting operation.", Severity.Error, Verbosity.Normal); client.LogException(ex); throw; } } }while ((result == PollResult.Continue || result == PollResult.Null || result == PollResult.Unknown) && DateTime.Now - start < timeout && pollingFailures <= MaxPollingFailuresCount); if (pollingFailures > MaxPollingFailuresCount) { client.LogMessage("Polling error weight exceeded maximum allowed. Aborting operation.", Severity.Error, Verbosity.Normal); } if (notifyHandler.IsNotNull() && pollingResult.IsNotNull()) { notifyHandler(pollingResult); } }
/// <summary> /// Handles incoming messages from EBS. /// </summary> private void EBSOnMessage(object sender, MessageEventArgs e) { if (!e.IsText) { return; } logger.Debug(new LogReceivedMessage { Message = e.Data, Session = EBS_SESSION }.ToJson()); string msgType = null; try { var msg = JObject.Parse(e.Data); msgType = msg["type"].ToObject <string>(); switch (msgType) { case "Authorization.Success": // successful authorization OnAuthorizationSuccess(AuthorizationSuccess.FromJson(e.Data)); break; case "PollError": // Poll request got canceled var pollError = PollError.FromJson(e.Data); logger.Warn(new LogPollAbortedEBS { Reason = pollError.Reason }.ToJson()); pollActive = false; SendMessage(Subscribers, new PollStopped().ToJson()); break; case "PollResult": // poll results OnPollResult(PollResult.FromJson(e.Data)); pollActive = false; SendMessage(Subscribers, new PollStopped().ToJson()); break; case "PollStarted": // poll successfully started from request pollActive = true; SendMessage(Subscribers, new PollStarted().ToJson()); break; default: logger.Warn(new LogReceivedUnknownMessageType { MessageType = msgType, Session = EBS_SESSION }.ToJson()); break; } } catch (Exception ex) { logger.Error(new LogMessageHandlingError { Exception = ex, MessageType = msgType, Session = EBS_SESSION }.ToJson()); } }
static async Task Main(string[] args) { try { // Try to migrate the DB to the latest schema var connString = Environment.GetEnvironmentVariable("PSQL_CONNECTION_STRING"); var connection = new NpgsqlConnection(connString); var evolve = new Evolve.Evolve(connection, msg => Console.WriteLine(msg)) { Locations = new[] { "migrations" }, SqlMigrationSuffix = ".pgsql", CommandTimeout = 600, RetryRepeatableMigrationsUntilNoError = true, }; var dev = Environment.GetEnvironmentVariable("PROPHESIZER_DEV"); bool devMode = dev == null ? false : bool.Parse(dev); if (devMode) { evolve.MustEraseOnValidationError = true; } else { evolve.IsEraseDisabled = true; // recommended in production } // Find the DB name from the connection string string dbName = "blaseball"; var splits = connString.Split(';'); foreach (var s in splits) { var halves = s.Split('='); if (halves[0] == "database") { dbName = halves[1]; } } // Add a placeholder for the DB name evolve.Placeholders.Add("${database}", dbName); evolve.Migrate(); } catch (Exception ex) { // Oh noes Console.WriteLine("Database migration failed.", ex); throw; } Prophesizer prophesizer = new Prophesizer(); int lastHour = -1; int numUpdates = 0; while (true) { var hour = DateTime.UtcNow.Hour; PollResult result = await prophesizer.Poll(); numUpdates += result.NumUpdatesProcessed; if ((hour > lastHour) || (hour == 0 && lastHour == 23)) { string[] goofs = new string[] { "\nI'm hungry.", "\nI miss blaseball.", "\nIs it time for the next Era yet?", "\nAre we there yet?", "\nSIBR are all love blaseball.", "\nThe commissioner is doing a great job." }; string g = ""; if (numUpdates == 0) { var r = new Random(); if (r.Next(100) < 10) { var i = r.Next(goofs.Length); g = goofs[i]; } } Prophesizer.ConsoleOrWebhook($"Processed {numUpdates} updates in the past hour. Last recorded game is {result.Latest.HumanReadable}.{g}"); //Prophesizer.ConsoleOrWebhook($" {prophesizer.NumNetworkOutcomes} games used the network outcomes.json file, {prophesizer.NumLocalOutcomes} did not."); lastHour = hour; numUpdates = 0; } await Task.Delay(1 * 60 * 1000); } }
// active polling is meh, should be replace by a simple notification server, but it's ok for now async Task PollLoop() { var profileInfo = (await ProfileManager.Current.GetProfileData(AccountId, ProfileDownloadType.ForceDownload, false)).ProfileInfo; if (profileInfo != null) { ProfileInfo = profileInfo; } await DownloadInboxRecords(AccountId); await QueryMissingProfiles(); while (true) { try { await DownloadFriends(false); var save = false; var lastAccountTransaction = (await PreviousAccountTransaction.DownloadLastTransactionInfo(_client, ChainType.Data, ChainId, MessageServiceInfo.MessageDataChainIndex, AccountId))?.Item; var lastReceiverTransaction = (await Receiver.DownloadLastTransactionInfo(_client, ChainType.Data, ChainId, MessageServiceInfo.MessageDataChainIndex, AccountId))?.Item; if (_lastAccountTransaction == null) { _lastAccountTransaction = lastAccountTransaction; save = true; } if (_lastReceivedTransaction == null) { _lastReceivedTransaction = lastReceiverTransaction; save = true; } var result = new PollResult(); if (lastAccountTransaction != null) { if (lastAccountTransaction.TransactionId > _lastAccountTransaction.TransactionId) { var download = new AccountTransactionDownload(AccountId, ServiceNode.GetTransactionDownloadManager(MessageServiceInfo.MessageDataChainIndex)) { MinimalTransactionId = Math.Max(1, _lastAccountTransaction.TransactionId) }; if (await Poll(download, result)) { _lastAccountTransaction = lastAccountTransaction; save = true; } } } if (lastReceiverTransaction != null) { if (lastReceiverTransaction.TransactionId > _lastReceivedTransaction.TransactionId) { var download = new ReceiverTransactionDownload(AccountId, ServiceNode.GetTransactionDownloadManager(MessageServiceInfo.MessageDataChainIndex)) { MinimalTransactionId = Math.Max(1, _lastReceivedTransaction.TransactionId) }; if (await Poll(download, result)) { _lastReceivedTransaction = lastReceiverTransaction; save = true; } } } if (result.UpdateFriends) { await DownloadFriends(false); } foreach (var index in result.Indices) { var chat = GetChat(index, true); if (!IsNodeChat(chat)) { _chats[chat.Index] = chat; GenerateSubmitAccount(index); await GenerateDefaultExchangeKeys(); } await chat.DownloadMessages(false); } if (result.Indices.Count > 0) { await UIApp.PubSub.PublishAsync(new MessageNodeRefreshEvent(this)); } if (save) { await SaveAsync(); } await Task.Delay(5000); } catch (Exception ex) { Log.HandleException(ex); } } }
public PollResult Save(PollResult item) { return(new PollResultRepository().SaveOrUpdate(item)); }
public void Delete(PollResult item) { new PollResultRepository().Delete(item); }
PollResult ParseResponse(string response) { string[] elements = response.Split(','); if (elements.Length <= 0) { if (OnInvalidResponse != null) { OnInvalidResponse(); } return(new PollResult(0, TimeSpan.Zero, 0, 0, 0, 0)); } PollResult result = new PollResult(); result.PollTime = DateTime.Now; string memstring = null; foreach (string element in elements) { string[] keyval = element.Split('='); if (keyval.Length == 2) { switch (keyval[0].Trim().ToLowerInvariant()) { case "name": if (OnShardName != null) { OnShardName(keyval[1]); } break; case "age": int hours; if (int.TryParse(keyval[1], out hours)) { result.Uptime = TimeSpan.FromHours(hours); } else { result.Uptime = TimeSpan.Zero; } break; case "clients": if (int.TryParse(keyval[1], out result.Clients)) { result.Clients--; } result.isOnline = true; // A shard is only considered online if clients is reported in the response. break; case "items": int.TryParse(keyval[1], out result.Items); break; case "chars": int.TryParse(keyval[1], out result.Chars); break; case "memory": case "mem": memstring = keyval[1].Trim(); while (memstring.Length > 0 && !char.IsLetterOrDigit(memstring[memstring.Length - 1])) { memstring = memstring.Remove(memstring.Length - 1); } break; case "ver": int.TryParse(keyval[1].Trim(), out result.Ver); break; } } } if (!string.IsNullOrWhiteSpace(memstring)) { result.Memory = ParseMemoryString(memstring); } else { result.Memory = 0; } return(result); }
private static PollResult doLinkIDPoll() { // Allow any SSL certficate ( ONLY FOR DEVELOPMENT!! ) ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(WCFUtil.AnyCertificateValidationCallback); LinkIDAuthSession linkIDSession = (LinkIDAuthSession)HttpContext.Current.Session[LINKID_SESSION]; PollResult result; if (null == linkIDSession) { // configure linkID authentication context LinkIDAuthenticationContext linkIDContext = new LinkIDAuthenticationContext(); linkIDContext.authenticationMessage = "WS Authn Message"; linkIDContext.finishedMessage = "WS Finished Message"; linkIDContext.applicationName = TestUtil.APP_NAME; linkIDContext.language = TestUtil.language; linkIDContext.identityProfile = "linkid_basic"; // attribute suggestions Dictionary <string, List <Object> > attributeSuggestions = new Dictionary <string, List <object> >(); attributeSuggestions.Add("test.attribute.string", new List <Object> { "test" }); attributeSuggestions.Add("test.attribute.multi.date", new List <Object> { DateTime.Now }); attributeSuggestions.Add("test.attribute.boolean", new List <Object> { true }); attributeSuggestions.Add("test.attribute.integer", new List <Object> { 69 }); attributeSuggestions.Add("test.attribute.double", new List <Object> { 3.14159 }); linkIDContext.attributeSuggestions = attributeSuggestions; // linkIDContext.paymentContext = new LinkIDPaymentContext(new LinkIDPaymentAmount(199, LinkIDCurrency.EUR, null)); // linkIDContext.callback = new LinkIDCallback("https://www.google.be", null, true); // start the linkID authentication linkIDSession = TestUtil.getClient().authStart(linkIDContext, HttpContext.Current.Request.UserAgent); //qr.Src = "data:image/png;base64," + linkIDSession.qrCodeInfo.qrEncoded; HttpContext.Current.Session[LINKID_SESSION] = linkIDSession; result = new PollResult(false, linkIDSession.qrCodeInfo.qrEncoded, false, null); } else { // poll linkID authentication LinkIDAuthPollResponse pollResponse = TestUtil.getClient().authPoll(linkIDSession.sessionId, TestUtil.language); String info = ""; info = "<h2>Poll response</h2>"; info += "AuthenticationState: " + pollResponse.linkIDAuthenticationState + "<br/>"; info += "PaymentState: " + pollResponse.paymentState + "<br/>"; if (null != pollResponse.linkIDAuthnResponse) { // logged in, dump user data info += pollResponse.linkIDAuthnResponse.ToString(); } result = new PollResult(true, null, null != pollResponse.linkIDAuthnResponse, info); } return(result); }
public static string pollLinkID() { PollResult result = doLinkIDPoll(); return(new JavaScriptSerializer().Serialize(result)); }
private bool Unchanged(PollResult result) { var last = Storage._.GetLast(result.PartitionKey); return(PollResult.AreSame(last, result)); }
public static string RenderPollResult(PollResult pollResult) { int totalRatings = 0; System.IO.StringWriter stringWriter = new System.IO.StringWriter(); HtmlTextWriter writer = new HtmlTextWriter(stringWriter); HtmlGenericControl divWrapper = new HtmlGenericControl("div"); divWrapper.ID = "pollContentWrapper"; HtmlGenericControl pollHeader = new HtmlGenericControl("p"); pollHeader.Attributes.Add("class", "pollDescription"); pollHeader.InnerHtml = pollResult.PollDescription; divWrapper.Controls.Add(pollHeader); Table pollResultTable = new Table(); pollResultTable.CssClass = "pollResultTable"; pollResultTable.CellPadding = 0; pollResultTable.CellSpacing = 0; divWrapper.Controls.Add(pollResultTable); foreach (PollResultOption pro in pollResult.PollResultOptionList) { TableRow row = new TableRow(); TableCell cell = new TableCell(); cell.Text = pro.PollOptionName; row.Cells.Add(cell); pollResultTable.Rows.Add(row); row = new TableRow(); cell = new TableCell(); HtmlGenericControl pbarDivWrapper = new HtmlGenericControl("div"); pbarDivWrapper.Attributes.Add("class", "pbarWrapper"); HtmlGenericControl pbarDiv = new HtmlGenericControl("div"); pbarDiv.Attributes.Add("class", "pollPbar"); pbarDiv.Style.Add(HtmlTextWriterStyle.Width, Math.Round((pro.Rating * 250d), 0).ToString() + "px"); pbarDivWrapper.Controls.Add(pbarDiv); HtmlGenericControl pbarDescr = new HtmlGenericControl("strong"); pbarDescr.InnerHtml = Math.Round(pro.Rating * 100, 0) + "%" + " (" + pro.Ratings + ")"; pbarDivWrapper.Controls.Add(pbarDescr); cell.Controls.Add(pbarDivWrapper); row.Cells.Add(cell); pollResultTable.Rows.Add(row); totalRatings += pro.Ratings; } HtmlGenericControl totalRatingsLabel = new HtmlGenericControl("div"); totalRatingsLabel.InnerHtml = "Totalt antal röster: <strong>" + totalRatings + "</strong>"; totalRatingsLabel.Style.Add(HtmlTextWriterStyle.MarginTop, "10px"); divWrapper.Controls.Add(totalRatingsLabel); divWrapper.RenderControl(writer); writer.Flush(); return(stringWriter.ToString()); }