public TransactionsPollingWorkerBase(ILogger <PollingWorkerBase <Transaction> > logger, AdamantApi api, IPusher pusher, ANSContext context) : base(api, context, logger) { _pusher = pusher; }
public async void StartPolling(bool warmup) { Console.WriteLine("Start polling"); if (warmup) { Console.WriteLine("Warming up, getting current top height."); var task = AdamantApi.GetChatTransactions(0, 0); var transactions = await AdamantApi.GetChatTransactions(0, 0); var newest = transactions.FirstOrDefault(); if (newest != null) { LastHeight = newest.Height; Console.WriteLine("Received last height: {0}", LastHeight); } else { Console.WriteLine("No transactions received, starting from height 0"); } } _tokenSource = new CancellationTokenSource(); #pragma warning disable 4014 Task.Run(() => UpdateTransactionsLoop(_tokenSource.Token), _tokenSource.Token); #pragma warning restore 4014 }
static void Main(string[] args) { var builder = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true); var configuration = builder.Build(); var connectionString = configuration.GetConnectionString("Devices"); var context = DevicesContext.CreateContextWithSQLite(connectionString); Console.WriteLine("Total registered devices: {0}", context.Devices.Count()); var applePusher = new Pusher { Configuration = configuration }; var api = new AdamantApi(configuration); var worker = new AdamantPollingWorker { Delay = TimeSpan.FromSeconds(2), Pusher = applePusher, AdamantApi = api, Context = context }; if (!Boolean.TryParse(configuration["PollingOptions:Warmup"], out bool warmup)) { warmup = true; } Console.WriteLine("Any key to stop..."); applePusher.Start(); worker.StartPolling(warmup); Console.ReadKey(); applePusher.Stop(); worker.StopPolling(); }
private async Task UpdateTransactionsLoop(CancellationToken token) { while (!token.IsCancellationRequested) { Console.WriteLine("Updating... Last height: {0}", LastHeight); var transactions = await AdamantApi.GetChatTransactions(0, LastHeight); if (transactions != null && transactions.Any()) { var maxHeight = transactions.Max(t => t.Height); if (LastHeight < maxHeight) { LastHeight = maxHeight; } var task = new Task(() => HandleNewTransactions(transactions)); task.Start(); } await Task.Delay(Delay); } token.ThrowIfCancellationRequested(); }
static async Task Main(string[] args) { AppDomain.CurrentDomain.UnhandledException += Global_UnhandledException; #region Config var configuration = ConfigurationLoader.GetConfiguration(); var provider = configuration["Database:Provider"]; var connectionName = configuration["Database:ConnectionString"] ?? "devices"; var connectionString = configuration.GetConnectionString(connectionName); if (!int.TryParse(configuration["SignalsRegistration:Delay"], out int delay)) { delay = 2000; } if (!Enum.TryParse(configuration["SignalsRegistration:Startup"], out StartupMode startupMode)) { startupMode = StartupMode.database; } #endregion #region Services // Data context var context = new ANSContext(connectionString, provider); context.Database.Migrate(); // API var api = new AdamantApi(configuration); #endregion #region DI & NLog var nLogConfig = configuration["SignalsRegistration:NlogConfig"]; if (String.IsNullOrEmpty(nLogConfig)) { nLogConfig = "nlog.config"; } else { nLogConfig = Utilities.HandleUnixHomeDirectory(nLogConfig); } _logger = NLog.LogManager.LoadConfiguration(nLogConfig).GetCurrentClassLogger(); var services = new ServiceCollection(); // Application services services.AddSingleton <IConfiguration>(configuration); services.AddSingleton <AdamantApi>(); services.AddSingleton(context); services.AddSingleton <SignalsPoller>(); // Other services.AddSingleton <ILoggerFactory, LoggerFactory>(); services.AddSingleton(typeof(ILogger <>), typeof(Logger <>)); services.AddLogging(b => b.SetMinimumLevel(LogLevel.Trace)); var serviceProvider = services.BuildServiceProvider(); var loggerFactory = serviceProvider.GetRequiredService <ILoggerFactory>(); loggerFactory.AddNLog(new NLogProviderOptions { CaptureMessageTemplates = true, CaptureMessageProperties = true }); #endregion var totalDevices = context.Devices.Count(); _logger.Info("Database initialized. Total devices in db: {0}", totalDevices); _logger.Info("Starting polling. Delay: {0}ms.", delay); var address = configuration["SignalsRegistration:Address"]; if (string.IsNullOrEmpty(address)) { throw new Exception("ANS account address is required"); } var privateKey = configuration["SignalsRegistration:PrivateKey"]; if (string.IsNullOrEmpty(privateKey)) { throw new Exception("ANS account private key is required"); } var worker = serviceProvider.GetRequiredService <SignalsPoller>(); worker.Delay = TimeSpan.FromMilliseconds(delay); worker.Address = address; worker.PrivateKey = privateKey; worker.StartPolling(startupMode); if (worker.PollingTask != null) { await worker.PollingTask; } else { throw new Exception("Can't await worker"); } }
protected PollingWorkerBase(AdamantApi api, ANSContext context, ILogger <PollingWorkerBase <T> > logger) { Api = api; Context = context; Logger = logger; }
public ChatPollingWorker(ILogger <ChatPollingWorker> logger, AdamantApi api, IPusher pusher, ANSContext context) : base(logger, api, pusher, context) { }
static void Main() { AppDomain.CurrentDomain.UnhandledException += Global_UnhandledException; #region Config var configuration = ConfigurationLoader.GetConfiguration(); var provider = configuration["Database:Provider"]; var connectionName = configuration["Database:ConnectionString"] ?? "devices"; var connectionString = configuration.GetConnectionString(connectionName); if (!int.TryParse(configuration["PollingWorker:Delay"], out int delay)) { delay = 2000; } if (!Enum.TryParse(configuration["PollingWorker:Startup"], out StartupMode startupMode)) { startupMode = StartupMode.database; } #endregion #region Services // Data context _context = new ANSContext(connectionString, provider); _context.Database.Migrate(); // API var api = new AdamantApi(configuration); #endregion #region DI & NLog var nLogConfig = configuration["PollingWorker:NlogConfig"]; if (String.IsNullOrEmpty(nLogConfig)) { nLogConfig = "nlog.config"; } else { nLogConfig = Utilities.HandleUnixHomeDirectory(nLogConfig); } _logger = NLog.LogManager.LoadConfiguration(nLogConfig).GetCurrentClassLogger(); var services = new ServiceCollection(); // Application services services.AddSingleton <IConfiguration>(configuration); services.AddSingleton <AdamantApi>(); services.AddSingleton(typeof(IPusher), typeof(ApplePusher.ApplePusher)); services.AddSingleton(_context); // Polling workers services.AddSingleton <ChatPollingWorker>(); services.AddSingleton <TransferPollingWorker>(); // Other services.AddSingleton <ILoggerFactory, LoggerFactory>(); services.AddSingleton(typeof(ILogger <>), typeof(Logger <>)); services.AddLogging(b => b.SetMinimumLevel(LogLevel.Trace)); var serviceProvider = services.BuildServiceProvider(); var loggerFactory = serviceProvider.GetRequiredService <ILoggerFactory>(); loggerFactory.AddNLog(new NLogProviderOptions { CaptureMessageTemplates = true, CaptureMessageProperties = true }); #endregion var totalDevices = _context.Devices.Count(); _logger.Info("Database initialized. Total devices in db: {0}", totalDevices); _logger.Info("Starting polling. Delay: {0}ms.", delay); var applePusher = serviceProvider.GetRequiredService <IPusher>(); applePusher.OnInvalidToken += ApplePusher_OnInvalidToken; applePusher.Start(); var chatWorker = serviceProvider.GetRequiredService <ChatPollingWorker>(); chatWorker.Delay = TimeSpan.FromMilliseconds(delay); chatWorker.StartPolling(startupMode); var transferWorker = serviceProvider.GetRequiredService <TransferPollingWorker>(); transferWorker.Delay = TimeSpan.FromMilliseconds(delay); transferWorker.StartPolling(startupMode); Task.WaitAll(chatWorker.PollingTask, transferWorker.PollingTask); }
public SignalsPoller(ILogger <PollingWorkerBase <Transaction> > logger, AdamantApi api, ANSContext context) : base(api, context, logger) { }