public async Task Run(TextReader input, CancellationToken cancellationToken)
        {
            metricServer.Start();
            await input.ReadLineAsync();

            while (true)
            {
                if (cancellationToken.IsCancellationRequested)
                {
                    break;
                }
                if (!(await input.ReadLineAsync() is { } line))
                {
                    break;
                }
                var fields = line.Split(",");
                try
                {
                    updateMetrics(fields);
                }
                catch (FormatException ex)
                {
                    Console.Error.WriteLine($"Couldn't parse line: '{line}'\n{ex.ToStringDemystified()}");
                }
            }
            metricServer.Stop();
        }
Ejemplo n.º 2
0
        private void OnStopping()
        {
            _logger.LogInformation("Stopping metrics listener.");

            // Perform on-stopping activities here
            _metricServer.Stop();
        }
 /// <summary>
 /// Stop metric server if enabled
 /// </summary>
 /// <returns></returns>
 public static void StopWhenEnabled(this IMetricServer server, IModuleConfig config, Serilog.ILogger logger)
 {
     if (config.EnableMetrics)
     {
         server.Stop();;
         logger.Information("Stopped prometheus metric server");
     }
 }
Ejemplo n.º 4
0
        protected virtual void Dispose(bool disposing)
        {
            if (!_disposedValue)
            {
                if (disposing)
                {
                    _collector?.Dispose();
                    _server.Stop();
                    _server.Dispose();
                }

                _disposedValue = true;
            }
        }
 protected void Application_Stop()
 {
     _metricServer?.Stop();
 }
Ejemplo n.º 6
0
        public static void Main(string[] args)
        {
#if DEBUG
            if (Debugger.IsAttached)
            {
                args = args.Skip(1).ToArray();
            }
#endif
            var env = (Environment.GetEnvironmentVariable("VSTORE_ENVIRONMENT") ?? "Production").ToLower();

            var basePath      = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
            var configuration = new ConfigurationBuilder()
                                .SetBasePath(basePath)
                                .AddJsonFile("appsettings.json")
                                .AddJsonFile($"appsettings.{env}.json")
                                .AddEnvironmentVariables("VSTORE_")
                                .Build();

            var container = Bootstrap(configuration);

            var loggerFactory = container.Resolve <ILoggerFactory>();
            var logger        = loggerFactory.CreateLogger <Program>();

            var cts = new CancellationTokenSource();
            Console.CancelKeyPress += (sender, eventArgs) =>
            {
                logger.LogInformation("Application is shutting down...");
                cts.Cancel();
                container.Dispose();

                eventArgs.Cancel = true;
            };
            var app = new CommandLineApplication {
                Name = "VStore.Worker"
            };
            app.HelpOption(CommandLine.HelpOptionTemplate);
            app.OnExecute(
                () =>
            {
                Console.WriteLine("VStore job runner.");
                app.ShowHelp();
                return(0);
            });

            var jobRunner = container.Resolve <JobRunner>();
            app.Command(
                CommandLine.Commands.Collect,
                config =>
            {
                config.Description = "Run cleanup job. See available arguments for details.";
                config.HelpOption(CommandLine.HelpOptionTemplate);
                config.Command(
                    CommandLine.Commands.Binaries,
                    commandConfig =>
                {
                    commandConfig.Description = "Collect unrefenced and expired binary files.";
                    commandConfig.HelpOption(CommandLine.HelpOptionTemplate);
                    commandConfig.Argument(CommandLine.Arguments.BatchSize, "Maximun amount of events to process for one iteration.");
                    commandConfig.Argument(CommandLine.Arguments.Delay, "Delay between collections.");
                    commandConfig.OnExecute(() => Run(commandConfig, jobRunner, cts));
                });
                config.OnExecute(() =>
                {
                    config.ShowHelp();
                    return(0);
                });
            });
            app.Command(
                CommandLine.Commands.Produce,
                config =>
            {
                config.Description = "Run produce events job. See available arguments for details.";
                config.HelpOption(CommandLine.HelpOptionTemplate);
                config.Command(
                    CommandLine.Commands.Events,
                    commandConfig =>
                {
                    commandConfig.Description = "Produce events of created versions of objects and/or binary files references.";
                    commandConfig.HelpOption(CommandLine.HelpOptionTemplate);
                    commandConfig.Argument(CommandLine.Arguments.Mode,
                                           $"Set '{CommandLine.ArgumentValues.Versions}' to produce events of created versions of objects, " +
                                           $"and '{CommandLine.ArgumentValues.Binaries}' to produce events of binary files references.");
                    commandConfig.OnExecute(() => Run(commandConfig, jobRunner, cts));
                });
            });

            var exitCode = 0;
            try
            {
                logger.LogInformation("VStore Worker started with options: {workerOptions}.", args.Length != 0 ? string.Join(" ", args) : "N/A");
                MetricServer.Start();
                exitCode = app.Execute(args);
            }
            catch (CommandParsingException ex)
            {
                ex.Command.ShowHelp();
                exitCode = 1;
            }
            catch (JobNotFoundException)
            {
                exitCode = 2;
            }
            catch (Exception ex)
            {
                logger.LogCritical(new EventId(), ex, "Unexpected error occured. See logs for details.");
                exitCode = -1;
            }
            finally
            {
                MetricServer.Stop();
                logger.LogInformation("VStore Worker is shutting down with code {workerExitCode}.", exitCode);
            }

            Environment.Exit(exitCode);
        }
 /// <inheritdoc />B
 public void Dispose()
 {
     _metricServer.Stop();
 }