Exemplo n.º 1
0
        static void Main(string[] args)
        {
            args = new[] { "61f57e03b30dd494c83272f1c19c6481d9e1593a138bff3e84e82ac6018bfe7f" };
            var loggerFactory = new LoggerFactory();

            loggerFactory.AddConsole();
            var logger = loggerFactory.CreateLogger("extool");

            if (args == null || args.Length == 0)
            {
                logger.LogInformation("please input block number");
                return;
            }
            var trxId = args[0].Trim();
            var configurationBuilder = new ConfigurationBuilder();

            configurationBuilder.AddJsonFile("appsettings.json");
            var configuration = configurationBuilder.Build();
            var settings      = new EXToolSettings();

            configuration.GetSection(nameof(EXToolSettings)).Bind(settings);

            var abiService = new ABIService(loggerFactory, settings.AbiBinToJsonUrl, settings.GetInfoUrl);
            IChainTrxRepository chainTrxRepository = new MongoChainTrxRepository(abiService,
                                                                                 settings.MongoClientConnectionString /*"mongodb://192.168.1.162:27017"*/,
                                                                                 settings.DatabaseName /*"EOSTEST"*/,
                                                                                 settings.TrxActionsCollectionName,
                                                                                 settings.AppliedTransactionCollectionName,
                                                                                 settings.AcceptTransactionCollectionName,
                                                                                 settings.Actionfilters);

            string message = JsonConvert.SerializeObject((object)settings);

            logger.LogInformation(message);
            logger.LogInformation("extool start ...");

            using (var httpClient = new HttpClient())
            {
                httpClient.DefaultRequestHeaders.Connection.Add("keep-alive");
                var httpResponseMessage = httpClient.PostAsync(settings.GetTransactionUrl,
                                                               ABIService.CreateStringContent(JsonConvert.SerializeObject((object)new
                {
                    id = trxId
                }))).Result;
                var responseString = httpResponseMessage.Content.ReadAsStringAsync().Result;
                logger.LogInformation(responseString);
                if (httpResponseMessage.IsSuccessStatusCode)
                {
                    chainTrxRepository.SetAppliedTransactionAsync(responseString).Wait();
                }
            }
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            var loggerFactory = new LoggerFactory();

            loggerFactory.AddConsole();
            var logger = loggerFactory.CreateLogger("Eosio.KafkaClient");
            var configurationBuilder = new ConfigurationBuilder();

            configurationBuilder.AddJsonFile("appsettings.json");
            var configuration = configurationBuilder.Build();
            var settings      = new KafkaClientSettings();

            configuration.GetSection(nameof(KafkaClientSettings)).Bind(settings);

            var conf = new Dictionary <string, object>
            {
                { "group.id", settings.ConsumerGroupId /*"eos-consumer-group"*/ },
                { "bootstrap.servers", settings.ConsumerBootstrapServers /*"192.168.1.234:9092"*/ },
                { "auto.commit.interval.ms", settings.ConsumerCommitInterval /*5000*/ },
                { "auto.offset.reset", settings.ConsumerOffsetReset /*"earliest"*/ }
            };
            var abiService = new ABIService(loggerFactory, settings.AbiBinToJsonUrl, settings.GetInfoUrl);
            IChainTrxRepository chainTrxRepository = new MongoChainTrxRepository(abiService,
                                                                                 settings.MongoClientConnectionString /*"mongodb://192.168.1.162:27017"*/,
                                                                                 settings.DatabaseName /*"EOSTEST"*/,
                                                                                 settings.TrxActionsCollectionName,
                                                                                 settings.AppliedTransactionCollectionName,
                                                                                 settings.AcceptTransactionCollectionName,
                                                                                 settings.Actionfilters);
            Dictionary <string, Func <string, Task> > consumerActions = new Dictionary <string, Func <string, Task> >();

            consumerActions.Add(settings.ConsumerAcceptTopic, (Func <string, Task>)(msgValue => chainTrxRepository.SetAcceptTransactionAsync(msgValue)));
            consumerActions.Add(settings.ConsumerAppliedTopic, (Func <string, Task>)(msgValue => chainTrxRepository.SetAppliedTransactionAsync(msgValue)));

            string message = JsonConvert.SerializeObject((object)settings);

            logger.LogInformation(message);
            logger.LogInformation("Consumer start ...");
            Task.Run(() =>
            {
                Dictionary <string, Func <Task> > reSetFuncs = new Dictionary <string, Func <Task> >();
                reSetFuncs.Add(settings.ConsumerAcceptTopic, () => chainTrxRepository.ReSetAcceptTransactionsAsync());
                reSetFuncs.Add(settings.ConsumerAppliedTopic, () => chainTrxRepository.ReSetAppliedTransactionsAsync());
                while (true)
                {
                    try
                    {
                        reSetFuncs[settings.ConsumerSubscribeTopic]().Wait();
                    }
                    catch (Exception ex)
                    {
                        logger.LogError((EventId)0, ex, "ReSetError");
                    }
                    finally
                    {
                        Thread.Sleep(60000);
                    }
                }
            });
            using (Consumer <Null, string> consumer = new Consumer <Null, string>(conf, null, new StringDeserializer(Encoding.UTF8)))
            {
                consumer.OnMessage += (_, msg) =>
                {
                    consumerActions[settings.ConsumerSubscribeTopic](msg.Value).Wait();
                    logger.LogInformation(string.Format("Read '{0}-{1}' from: {2}", (object)msg.Topic, (object)msg.Timestamp.UtcDateTime.ToLongTimeString(), (object)msg.TopicPartitionOffset));
                };
                consumer.OnError        += (_, error) => logger.LogInformation(string.Format("Error: {0}", (object)error));
                consumer.OnConsumeError += (_, msg) => logger.LogInformation(string.Format("Consume error ({0}): {1}", (object)msg.TopicPartitionOffset, (object)msg.Error));
                consumer.Subscribe(settings.ConsumerSubscribeTopic);
                while (true)
                {
                    consumer.Poll(TimeSpan.FromMilliseconds(100.0));
                }
            }
        }