public List <uint256> FetchData()
        {
            this.db.StartSession();

            var result = new List <uint256>(this.N);

            foreach (uint256 input in this.data.OrderBy(d => Guid.NewGuid()).Take(this.N))
            {
                var    coinKey = new CacheKey("Coins", input.ToString());
                var    output  = new CacheOutput();
                var    dbInput = default(CacheInput);
                Status status  = this.db.Read(ref coinKey, ref dbInput, ref output, Empty.Default, 1);
                if (status == Status.PENDING)
                {
                    this.db.CompletePending(true);
                }

                uint256 outputs = status == Status.OK ? this.dBreezeSerializer.Deserialize <uint256>(output.Value.Value) : null;

                result.Add(outputs);
            }

            this.db.StopSession();

            return(result);
        }
Example #2
0
        internal void RunCommand(IrcEventArgs eventArgs, ICommand command, Match match)
        {
            if (eventArgs.Data.Message == null)
            {
                return;
            }
            string message            = eventArgs.Data.Message.TrimStart(CommandPrefix);
            string nick               = eventArgs.Data.Nick.Split('!')[0];
            bool   cached             = false;
            var    commandEventParams = eventArgs.Data.CastToIrcChannelMessageEventData();

            commandEventParams.Message = message;

            CleanupThreads();

            DoNotReportException doNotReportException =
                (DoNotReportException)Attribute.GetCustomAttribute(command.GetType(),
                                                                   typeof(DoNotReportException));
            CacheOutput cacheOutput =
                (CacheOutput)Attribute.GetCustomAttribute(command.GetType(), typeof(CacheOutput));

            bool cacheOutputMessage = cacheOutput != null;
            bool reportException    = doNotReportException == null;

            try
            {
                _log.Debug($"Handing over to {command}");
                _log.Debug($"cacheOutputMessage = {cacheOutputMessage}");
                List <string> result = null;
                if (cacheOutputMessage && _ircBot.UseCache)
                {
                    _log.Debug($"Looking up {command.CommandName} in cache");
                    result = GetCachedMessage(command.CommandName);
                    _log.Debug($"Got {result}");
                    if (result != null)
                    {
                        cached = true;
                    }
                }

                if (result == null)
                {
                    result = command.RunCommand(commandEventParams, match.Groups,
                                                _ircBot.UseCache);
                }

                if (result != null)
                {
                    if (cacheOutputMessage && _ircBot.UseCache && !cached)
                    {
                        _log.Debug($"Caching {command.CommandName} for {cacheOutput.CacheSeconds}");
                        CacheMessage(command.CommandName, result, cacheOutput.CacheSeconds);
                    }

                    if (_config.ResultSuffix != null)
                    {
                        result[result.Count - 1] = result[result.Count - 1] + _config.ResultSuffix;
                    }

                    foreach (string line in result)
                    {
                        if (line == null)
                        {
                            continue;
                        }
                        _client.SendMessage(SendType.Message, eventArgs.Data.Channel, line);
                    }
                }
            }
            catch (Exception e)
            {
                _log.Error("Stacktrace");
                _log.Error(e.Source + ": " + e.Message);
                _log.Error(e.StackTrace);

                if (reportException && e.InnerException == null) // Attribute [DoNotReportException] to suppress this
                {
                    _client.SendMessage(SendType.Message, eventArgs.Data.Channel,
                                        $"Well this is embarrassing: {e.Source}: {e.Message}");
                }
                if (e.InnerException != null)
                {
                    _log.Error(e.InnerException.StackTrace);
                    _backtraceClient?.Send(e.InnerException);
                    if (reportException)
                    {
                        _client.SendMessage(SendType.Message, eventArgs.Data.Channel,
                                            $"Well this is embarrassing: {e.InnerException.Source}: {e.InnerException.Message}");
                    }
                }

                if (_backtraceClient == null)
                {
                    return;
                }

                // We have to clear the dictionary as it doesn't like duplicate keys which are caused by the fact
                // we're reusing the Backtrace client rather than getting a new one
                _backtraceClient.Attributes.Clear();
                _backtraceClient.Attributes.Add("RequestorNick", nick);
                _backtraceClient.Attributes.Add("CommandName", command.CommandName);
                _backtraceClient.Attributes.Add("Message", message);
                _backtraceClient.Attributes.Add("cacheOutputMessage", cacheOutputMessage);
                _backtraceClient.Attributes.Add("reportException", reportException);
                _backtraceClient.Send(e);
            }
        }