예제 #1
0
파일: Program.cs 프로젝트: wwosik/mannheim
        private static (int?, Type) FindCommand(JobStartInfo jobStartInfo)
        {
            foreach (var assemblyFilePath in jobStartInfo.Assemblies ?? Enumerable.Empty <string>())
            {
                logger.Info("Loading assembly " + assemblyFilePath);
                try
                {
                    var assembly    = Assembly.LoadFrom(assemblyFilePath);
                    var commandType = assembly.GetTypes().FirstOrDefault(t => t.FullName == jobStartInfo.CommandName);
                    if (commandType != null)
                    {
                        logger.Info($"Found command {jobStartInfo.CommandName} in assembly {assembly.FullName}");
                        return(null, commandType);
                    }
                }
                catch (Exception ex)
                {
                    logger.Fatal(ex, "Failed to load assembly " + assemblyFilePath);
                    return((int)ExitCodes.FailedToLoadAssembly, null);
                }
            }

            logger.Fatal($"Did not find command {jobStartInfo.CommandName} in any of assemblies: {string.Join(", ", jobStartInfo.Assemblies)}");
            return((int)ExitCodes.FailedToFindCommand, null);
        }
예제 #2
0
        public TestRunner(ITestOutputHelper testOutput, string testName, JobStartInfo info = null)
        {
            this.testOutput = testOutput;
            var localAppDataFolder = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);

            this.WorkingDirectoryPath = Path.Combine(localAppDataFolder, "Mannheim.Jobs.Test", testName);
            this.Info            = info ?? new JobStartInfo();
            this.Info.Assemblies = this.Info.Assemblies ?? new List <string>();
            this.Info.Assemblies.Add(Path.Combine(Environment.CurrentDirectory, "Mannheim.Jobs.Test.dll"));
            this.Info.CommandName = typeof(T).FullName;
        }
예제 #3
0
파일: Program.cs 프로젝트: wwosik/mannheim
        public static int Main(string[] args)
        {
            try
            {
                SaveJobStatusInfo();

                logger = SetupNLog();
                if (logger == null)
                {
                    return(Exit(ExitCodes.FailedToInitializeLogging));
                }

                jobStartInfo = GetJobInfo(currentDirectory);
                if (jobStartInfo == null)
                {
                    return(Exit(ExitCodes.FailedToLoadJobStartInfo));
                }

                var(exitCode, commandType) = FindCommand(jobStartInfo);
                if (exitCode.HasValue)
                {
                    return(exitCode.Value);
                }

                if (!commandType.GetInterfaces().Any(i => i == typeof(ICommand)))
                {
                    logger.Fatal($"Indicated command type {jobStartInfo.CommandName} does not implement ICommand interface");
                    return(Exit(ExitCodes.NotACommand));
                }

                ICommandServicesConfigurator commandServicesConfigurator = null;

                var commandAttribute = commandType.GetCustomAttribute <CommandAttribute>();
                if (commandAttribute != null)
                {
                    if (!string.IsNullOrEmpty(commandAttribute.JobStartInfoType))
                    {
                        var jobStartInfoType = commandType.Assembly.GetType(commandAttribute.JobStartInfoType);
                        if (jobStartInfoType == null)
                        {
                            logger.Fatal($"Did not find JobStartInfoType {commandAttribute.JobStartInfoType} indicated by command {commandType.FullName}");
                            return(Exit(ExitCodes.FailedToLoadJobStartInfo));
                        }

                        jobStartInfo = GetJobInfo(currentDirectory, jobStartInfoType);
                        if (jobStartInfo == null)
                        {
                            return(Exit(ExitCodes.FailedToLoadJobStartInfo));
                        }
                    }

                    if (!string.IsNullOrEmpty(commandAttribute.CommandServicesConfigurator))
                    {
                        var commandServicesConfiguratorType = commandType.Assembly.GetType(commandAttribute.CommandServicesConfigurator);
                        if (commandServicesConfiguratorType == null)
                        {
                            logger.Fatal($"Did not find ICommandServicesConfigurator {commandAttribute.CommandServicesConfigurator} indicated by command {commandType.FullName}");
                            return(Exit(ExitCodes.FailedToInitializeServices));
                        }

                        try
                        {
                            commandServicesConfigurator = (ICommandServicesConfigurator)Activator.CreateInstance(commandServicesConfiguratorType);
                        }
                        catch (Exception ex)
                        {
                            logger.Fatal($"Failed to initialize ICommandServiceConfigurator {commandServicesConfiguratorType}: {ex.GetBaseException().Message}");
                            return(Exit(ExitCodes.FailedToInitializeServices));
                        }
                    }
                }

                IServiceProvider serviceProvider;
                try
                {
                    var services = new ServiceCollection();

                    services.AddLogging(l =>
                    {
                        l.ClearProviders();
                        l.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace);
                        l.AddNLog();
                    });
                    services.AddSingleton <JobStartInfo>(jobStartInfo);

                    commandServicesConfigurator?.Initialize(services);

                    serviceProvider = services.BuildServiceProvider();
                }
                catch (Exception ex)
                {
                    logger.Fatal($"Failed to initialize services: {ex.GetBaseException().Message}");
                    return(Exit(ExitCodes.FailedToInitializeServices));
                }

                ICommand command;
                try
                {
                    command = (ICommand)ActivatorUtilities.CreateInstance(serviceProvider, commandType);
                }
                catch (Exception ex)
                {
                    logger.Fatal($"Failed to initialize services: {ex.GetBaseException().Message}");
                    return(Exit(ExitCodes.FailedToInitializeCommand));
                }

                try
                {
                    logger.Info("=== STARTING COMMAND EXECUTION");
                    command.Run().Wait();
                    return(Exit(ExitCodes.Success));
                }
                catch (Exception ex)
                {
                    logger.Error("=== FAILED IN EXECUTION");
                    if (ex is AggregateException)
                    {
                        ex = ((AggregateException)ex).InnerException;
                    }
                    while (ex != null)
                    {
                        logger.Error(ex.Message);
                        logger.Error(ex.StackTrace);
                        ex = ex.InnerException;
                    }
                    return(Exit(ExitCodes.FailureInExecution));
                }
            }
            catch (Exception ex)
            {
                logger?.Fatal(ex, ex.Message);
                return(Exit(ExitCodes.GeneralError));
            }
            finally
            {
                try
                {
                    LogManager.Shutdown();
                }
                catch (Exception ex)
                {
                    Console.Error.WriteLine("Failed to shutdown logging: " + ex.GetBaseException().Message);
                }

                Console.Error.WriteLine("===== DONE");
                if (Debugger.IsAttached && !(jobStartInfo?.DisableDebugging == true))
                {
                    Console.ReadLine();
                }
            }
        }