Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            Console.WriteLine("Kitsune Compiler Service Started");

            if (isDebug)
            {
                Console.WriteLine("KString service started");
            }
            while (true)
            {
                try
                {
                    var amazonCompilerSqsQueueHandler = new AmazonSQSQueueHandlers <KStringQueueModel>(kStringQueueUrl);


                    var task = amazonCompilerSqsQueueHandler.ReceiveMessageFromQueue();
                    try
                    {
                        if (task != null && task.MessageBody != null)
                        {
                            var keywordList = KStringHelper.ExtractKeyword(task.MessageBody.KString);
                            if (keywordList != null && keywordList.Any())
                            {
                                var result = KStringHelper.UpdateKeywordsToKitsuneDB(task.MessageBody.SchemaName, task.MessageBody.UserId, task.MessageBody.KID, task.MessageBody.ReferenceId, task.MessageBody.KString, keywordList);
                                if (!string.IsNullOrEmpty(result))
                                {
                                    Console.WriteLine($"Keyword Extracted : {JsonConvert.SerializeObject(task.MessageBody)}");
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(String.Format("Error during kstring queue processing, ErrorMessage : {0}, StackTrace : {1}", ex.Message, ex.StackTrace));
                    }
                    finally
                    {
                        if (task != null)
                        {
                            amazonCompilerSqsQueueHandler.DeleteMessageFromQueue(task);
                            Console.WriteLine($"Message removed : {task.MessageBody.SchemaName}");
                        }
                    }
                }
                catch (Exception ex)
                {
                    //  EventLogger.LogTrace(String.Format("Error during compilation, ErrorMessage : {0}, StackTrace : {1}", ex.Message, ex.StackTrace), null, null);
                    Console.WriteLine(String.Format("Error during kstring processing, ErrorMessage : {0}, StackTrace : {1}", ex.Message, ex.StackTrace));
                }
                finally
                {
                }
            }
        }
        static void Main(string[] args)
        {
#if DEBUG
            var builder = new ConfigurationBuilder()
                          .SetBasePath(Directory.GetCurrentDirectory())
                          .AddJsonFile("appsettings.json", true, true)
                          .AddEnvironmentVariables();
#else
            var builder = new ConfigurationBuilder()
                          .AddJsonFile($"appsettings.Production.json", true, true)
                          .AddEnvironmentVariables();
#endif
            ServiceConfiguration = builder.Build();
            string myIP = null;
            try
            {
                string hostName = Dns.GetHostName(); // Retrive the Name of HOST
                                                     // Get the IP
                myIP = Dns.GetHostEntry(hostName)?.AddressList?.Where(x => x.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)?.FirstOrDefault()?.ToString();
            }
            catch
            {
            }
            Logger.InitLogger(ServiceConfiguration.GetSection("AWSAccessKey").Value,
                              ServiceConfiguration.GetSection("AWSSecretKey").Value,
                              ServiceConfiguration.GetSection("CloudWatchLogGroup").Value,
                              myIP);

            Console.WriteLine("Started");
            Log.Information("Compiler service started");
            Console.WriteLine(ServiceConfiguration.GetSection("KitsuneCompilerSQSUrl").Value);
            Log.Information(ServiceConfiguration.GetSection("KitsuneCompilerSQSUrl").Value);



            var amazonCompilerSqsQueueHandler = new AmazonSQSQueueHandlers <CompilerServiceSQSModel>(ServiceConfiguration.GetSection("KitsuneCompilerSQSUrl").Value);
            AmazonAWSHelpers.Models.AmazonSQSMessageQueueModel <CompilerServiceSQSModel> task = null;
            string projectId    = string.Empty;
            int    buildVersion = 0;
            string user         = string.Empty;
            var    errors       = new List <BuildError>();
            List <CompileResult> buildStatus = null;
            while (true)
            {
                try
                {
                    Console.WriteLine($"Polling from the queue : {DateTime.UtcNow.ToString()}");
                    Log.Information($"Polling from the queue : {DateTime.UtcNow.ToString()}");

                    task = amazonCompilerSqsQueueHandler.ReceiveMessageFromQueue(ServiceConfiguration.GetSection("AWSAccessKey").Value, ServiceConfiguration.GetSection("AWSSecretKey").Value,
                                                                                 RegionEndpoint.GetBySystemName(ServiceConfiguration.GetSection("AWSRegion").Value));

                    if (task != null && task.MessageBody != null)
                    {
                        Console.Clear();
                        projectId    = task.MessageBody.ProjectId;
                        buildVersion = task.MessageBody.BuildVersion;
                        user         = task.MessageBody.UserEmail;


                        //Update the kitsune project status to building
                        if (APIHelpers.UpdateProjectStatus(user, projectId, buildVersion.ToString()))
                        {
                            Console.WriteLine(String.Format("Compilation processing started for project '{0}' with version {1}", projectId, buildVersion), projectId, buildVersion.ToString());
                            Log.Information(String.Format("Compilation processing started for project '{0}' with version {1}", projectId, buildVersion), projectId, buildVersion.ToString());

                            buildStatus = new BuildAndRunHelper().BuildProject(user, projectId, bool.Parse(ServiceConfiguration.GetSection("_isDev").Value), _currentCompilerVersion);
                            if (buildStatus != null && buildStatus.Any())
                            {
                                errors = new List <BuildError>();
                                foreach (var error in buildStatus)
                                {
                                    if (!error.Success)
                                    {
                                        errors.AddRange(error.ErrorMessages.Select(x => new BuildError
                                        {
                                            Column          = x.LinePosition,
                                            Line            = x.LineNumber,
                                            Message         = x.Message,
                                            ErrorStackTrace = x.Message,
                                            SourceMethod    = "KitsuneCompiler",
                                            SourcePath      = error.PageName
                                        }));
                                    }
                                }
                                APIHelpers.UpdateProjectErrorStatus(user, projectId, buildVersion, errors);
                                Console.WriteLine(String.Format("Compilation failed for project '{0}' with version {1}, Errors", projectId, buildVersion));
                                Log.Error(String.Format("Compilation failed for project '{0}' with version {1}, Errors", projectId, buildVersion));
                                foreach (var err in errors)
                                {
                                    Console.WriteLine(JsonConvert.SerializeObject(err));
                                    Log.Error(JsonConvert.SerializeObject(err));
                                }
                                buildStatus = null;
                            }
                            else
                            {
                                //UPDATE the project status
                                if (APIHelpers.UpdateBuildStatus(user, projectId, buildVersion))
                                {
                                    Console.WriteLine(string.Format("Compilation done successful"), projectId, buildVersion.ToString());
                                    Log.Information(string.Format("Compilation done successful"), projectId, buildVersion.ToString());
                                }
                            }
                        }
                        amazonCompilerSqsQueueHandler.DeleteMessageFromQueue(task, ServiceConfiguration.GetSection("AWSAccessKey").Value, ServiceConfiguration.GetSection("AWSSecretKey").Value, RegionEndpoint.GetBySystemName(ServiceConfiguration.GetSection("AWSRegion").Value));
                        Console.WriteLine(string.Format("Message removed : {0}", task?.MessageBody?.ProjectId));
                        Log.Information(string.Format("Message removed : {0}", task?.MessageBody?.ProjectId));
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(String.Format("Error during compilation, ErrorMessage : {0}, StackTrace : {1}", ex.Message, ex.StackTrace));
                    Log.Error(String.Format("Error during compilation, ErrorMessage : {0}, StackTrace : {1}", ex.Message, ex.StackTrace));
                }
            }
        }