public HttpListenerModule(PSScriptExecutor scriptExecutor, string baseUrl, string authToken)
 {
     this.scriptExecutor = scriptExecutor;
     this.baseUrl        = baseUrl;
     this.authToken      = authToken ?? "";
     server = new HttpListener();
     server.Prefixes.Add(baseUrl);
     isServerStopped = false;
 }
示例#2
0
        private Task OnStartTask(string[] args)
        {
            string pathToScripts       = readAppSetting("pathToScripts");
            string modulesToLoadString = readAppSetting("modulesToLoad");

            string[] modulesToLoad     = string.IsNullOrEmpty(modulesToLoadString) ? new string[0] : modulesToLoadString.Split(',');
            string   psExecutionPolicy = readAppSetting("psExecutionPolicy");
            string   psOutputDelimiter = readAppSetting("psOutputDelimiter") ?? "";

            logInfo("Initializing service with required PowerShell modules: " + String.Join(", ", modulesToLoad));
            scriptExecutor = new PSScriptExecutor(pathToScripts, modulesToLoad, psExecutionPolicy, psOutputDelimiter);

            string httpBaseUrl   = readAppSetting("baseUrl");
            string httpAuthToken = readAppSetting("authToken");

            if (!String.IsNullOrEmpty(httpBaseUrl))
            {
                logInfo("Initializing HTTP module on URI: " + httpBaseUrl);
                httpModule = new HttpListenerModule(scriptExecutor, httpBaseUrl, httpAuthToken);
            }
            else
            {
                string msg = string.Format("Skip HTTP initialization, some config values are missing. baseUrl: {0}", httpBaseUrl);
                logWarning(msg);
            }

            string rabbitMqBaseUrl            = readAppSetting("rabbitMqBaseUrl");
            string rabbitMqUsername           = readAppSetting("rabbitMqUsername");
            string rabbitMqPassword           = readAppSetting("rabbitMqPassword");
            string rabbitMqRequestQueueName   = readAppSetting("rabbitMqRequestQueueName");
            string rabbitMqResponseExchange   = readAppSetting("rabbitMqResponseExchange");
            string rabbitMqResponseRoutingKey = readAppSetting("rabbitMqResponseRoutingKey");

            if (!String.IsNullOrEmpty(rabbitMqBaseUrl) && !String.IsNullOrEmpty(rabbitMqRequestQueueName) && !String.IsNullOrEmpty(rabbitMqResponseExchange) && !String.IsNullOrEmpty(rabbitMqResponseRoutingKey))
            {
                logInfo(string.Format("Initializing RabbitMQ module on URI: {0} with requestQueue: {1}, responseExchange: {2} and responseRoutingKey: {3}", rabbitMqBaseUrl, rabbitMqRequestQueueName, rabbitMqResponseExchange, rabbitMqResponseRoutingKey));
                rabbitMqModule = new RabbitMqModule(scriptExecutor, rabbitMqBaseUrl, rabbitMqUsername, rabbitMqPassword, rabbitMqRequestQueueName, rabbitMqResponseExchange, rabbitMqResponseRoutingKey);
            }
            else
            {
                string msg = string.Format("Skip RabbitMQ initialization, some config values are missing. baseUrl: {0}, requestQueue: {1}, responseExchange: {2} and responseRoutingKey: {3}", rabbitMqBaseUrl, rabbitMqRequestQueueName, rabbitMqResponseExchange, rabbitMqResponseRoutingKey);
                logWarning(msg);
            }

            if (httpModule == null && (rabbitMqModule == null || !rabbitMqModule.isConnected()))
            {
                string msg = "No module was initialized. Service is not usable...";
                logError(msg);
                throw new Exception(msg);
            }
            else
            {
                logInfo("The service has been initialized successfully.");
            }

            return(httpModule != null ? httpModule.StartServerThreadAsync() : null);
        }
示例#3
0
        /**
         * See also here: https://www.rabbitmq.com/dotnet-api-guide.html
         */
        public RabbitMqModule(PSScriptExecutor scriptExecutor, string baseUrl, string username, string password, string requestQueue, string responseExchange, string responseRoutingKey)
        {
            this.scriptExecutor     = scriptExecutor;
            this.baseUrl            = baseUrl;
            this.username           = username ?? "";
            this.password           = password ?? "";
            this.requestQueue       = requestQueue;
            this.responseExchange   = responseExchange;
            this.responseRoutingKey = responseRoutingKey;

            try
            {
                rabbitMqFactory = new ConnectionFactory()
                {
                    Uri = new Uri(baseUrl), UserName = username, Password = password, DispatchConsumersAsync = true
                };
                rabbitMqFactory.AutomaticRecoveryEnabled = true;
                rabbitMqConnection         = rabbitMqFactory.CreateConnection();
                rabbitMqChannel            = rabbitMqConnection.CreateModel();
                rabbitMqConsumer           = new AsyncEventingBasicConsumer(rabbitMqChannel);
                rabbitMqConsumer.Received += async(sender, args) =>
                {
                    await Task.Yield(); // Force async execution.

                    Thread handleRequestThread = new Thread(new ParameterizedThreadStart(handleRequest));
                    handleRequestThread.Start(args);
                };

                rabbitMqChannel.BasicConsume(queue: this.requestQueue,
                                             autoAck: false,
                                             consumer: rabbitMqConsumer);

                PSScriptInvoker.logInfo("Message consumer successfully started. Now waiting for requests in queue " + this.requestQueue + "...");
            }
            catch (Exception ex)
            {
                string msg = "Unexpected exception while setting up RabbitMQ connection:\n" + ex.ToString();
                PSScriptInvoker.logError(msg);
            }
        }