public WorkerBase(IWorkerOptions workerOptions)
        {
            this.workerOptions = workerOptions;

            WorkerName = GetType().Name;
            Logger     = Log.ForContext("Type", WorkerName);
            Logger.Information(
                "Starting {worker}. Runs every {minutes} minutes. All options {@options}",
                this.WorkerName,
                this.workerOptions.RepeatIntervalSeconds,
                this.workerOptions);
        }
Esempio n. 2
0
        public static Task <WorkerConnection> ConnectAsync(IWorkerOptions workerOptions, ConnectionParameters connectionParameters, CancellationToken cancellation = default)
        {
            switch (workerOptions)
            {
            case IReceptionistOptions receptionistOptions:
                var workerName = workerOptions.WorkerName ?? $"{connectionParameters.WorkerType}-{Guid.NewGuid().ToString()}";
                return(ConnectAsync(receptionistOptions.SpatialOsHost, receptionistOptions.SpatialOsPort, workerName, connectionParameters, cancellation));

            case ILocatorOptions locatorOptions:
                connectionParameters.Network.UseExternalIp = true;
                return(ConnectAsync(locatorOptions, connectionParameters, cancellation));

            default:
                throw new NotImplementedException("Unrecognized option type: " + workerOptions.GetType());
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Constructs a new runner
        /// </summary>
        /// <param name="options">The options to use</param>
        /// <param name="reqchan">The request channel</param>
        /// <param name="respchan">The response channel</param>
        /// <param name="response">The expected response string</param>
        public WebRequestWorker(IWorkerOptions options, IReadChannel <bool> reqchan, IWriteChannel <RequestResult> respchan, string response)
        //: base(options, maximumrequests, response)
            : base(options, reqchan, respchan, response)
        {
            foreach (var h in options.Headers)
            {
                var parts = h?.Split(':', 2);
                if (parts == null || parts.Length < 2)
                {
                    throw new ArgumentException($"Bad header format: {h}");
                }

                m_headers.Add(parts[0], parts[1]);
            }

            m_data = string.IsNullOrEmpty(options.Body) ? null : System.Text.Encoding.UTF8.GetBytes(options.Body);
            m_initialized.TrySetResult(true);
        }
Esempio n. 4
0
        /// <summary>
        /// Constructs a new runner
        /// </summary>
        /// <param name="options">The options to use</param>
        /// <param name="reqchan">The request channel</param>
        /// <param name="respchan">The response channel</param>
        /// <param name="response">The expected response string</param>
        public CurlWorker(IWorkerOptions options, IReadChannel <bool> reqchan, IWriteChannel <RequestResult> respchan, string response)
        //: base(options, maximumrequests, response)
            : base(options, reqchan, respchan, response)
        {
            var sb = new StringBuilder();

            sb.Append($"--silent --show-error --request {EscapeAndQuoteArgument(options.Verb)} {EscapeAndQuoteArgument(options.RequestUrl)}");
            foreach (var h in options.Headers)
            {
                sb.Append($" --header {EscapeAndQuoteArgument(h)}");
            }

            if (!string.IsNullOrEmpty(options.Body))
            {
                sb.Append($" --data {EscapeAndQuoteArgument(options.Body)}");
            }

            m_commandline = sb.ToString();
            m_initialized.TrySetResult(true);
        }
Esempio n. 5
0
        /// <summary>
        /// Constructs a new runner
        /// </summary>
        /// <param name="options">The options to use</param>
        /// <param name="reqchan">The request channel</param>
        /// <param name="respchan">The response channel</param>
        /// <param name="response">The expected response string</param>
        public SocketWorker(IWorkerOptions options, IReadChannel <bool> reqchan, IWriteChannel <RequestResult> respchan, string response)
            : base(options, reqchan, respchan, response)
        {
            var sb  = new StringBuilder();
            var uri = new Uri(options.RequestUrl);

            sb.Append($"{options.Verb} {uri.PathAndQuery} HTTP/1.1{CRLF}");
            sb.Append($"Host: {uri.Host}{CRLF}");
            foreach (var h in options.Headers)
            {
                sb.Append($"{h}{CRLF}");
            }
            sb.Append(CRLF);
            if (!string.IsNullOrEmpty(options.Body))
            {
                sb.Append(options.Body);
            }

            m_data = System.Text.Encoding.UTF8.GetBytes(sb.ToString());
            m_initialized.TrySetResult(true);
        }
Esempio n. 6
0
        private static async Task RunAsync(IWorkerOptions options)
        {
            if (string.IsNullOrEmpty(options.LogFileName))
            {
                options.LogFileName = Path.Combine(Environment.CurrentDirectory, options.WorkerName ?? WorkerType + ".log");
            }

            Log.Logger = new LoggerConfiguration()
                         .Enrich.FromLogContext()
                         .MinimumLevel.Debug()
                         .WriteTo.Console()
                         .WriteTo.File(options.LogFileName)
                         .CreateLogger();

            Log.Debug($"Opened logfile {options.LogFileName}");

            var connectionParameters = new ConnectionParameters
            {
                EnableProtocolLoggingAtStartup = true,
                ProtocolLogging = new ProtocolLoggingParameters
                {
                    LogPrefix = Path.ChangeExtension(options.LogFileName, string.Empty) + "-protocol"
                },
                WorkerType             = WorkerType,
                DefaultComponentVtable = new ComponentVtable()
            };

            using (var connection = await WorkerConnection.ConnectAsync(options, connectionParameters).ConfigureAwait(false))
            {
                connection.StartSendingMetrics();

                foreach (var opList in connection.GetOpLists())
                {
                    ProcessOpList(opList);
                }
            }

            Log.Information("Disconnected from SpatialOS");
        }
Esempio n. 7
0
        /// <summary>
        /// Constructs a new runner
        /// </summary>
        /// <param name="options">The options to use</param>
        /// <param name="reqchan">The request channel</param>
        /// <param name="respchan">The response channel</param>
        /// <param name="response">The expected response string</param>
        public RunnerBase(IWorkerOptions options, IReadChannel <bool> reqchan, IWriteChannel <RequestResult> respchan, string response)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            if (string.IsNullOrWhiteSpace(options.RequestUrl) || !System.Uri.TryCreate(options.RequestUrl, UriKind.Absolute, out var _))
            {
                throw new ArgumentException($"Invalid request url: {options.RequestUrl}", nameof(options.RequestUrl));
            }
            if (string.IsNullOrWhiteSpace(options.Verb))
            {
                throw new ArgumentException(nameof(options.Verb), "Missing HTTP verb");
            }

            m_id               = System.Threading.Interlocked.Increment(ref _id);
            m_options          = options;
            m_expectedresponse = response;
            if (m_expectedresponse != null)
            {
                Result = RunAsync(reqchan, respchan);
            }
        }
Esempio n. 8
0
 /// <summary>
 /// Constructs a new runner
 /// </summary>
 /// <param name="options">The options to use</param>
 /// <param name="reqchan">The request channel</param>
 /// <param name="respchan">The response channel</param>
 /// <param name="response">The expected response string</param>
 public HttpClientWorker(IWorkerOptions options, IReadChannel <bool> reqchan, IWriteChannel <RequestResult> respchan, string response)
 //: base(options, maximumrequests, response)
     : base(options, reqchan, respchan, response)
 {
 }