Пример #1
0
        /// <summary>
        /// Loads a module.
        /// </summary>
        /// <param name="dllFile">The path to the module DLL file.</param>
        /// <param name="xmlFile">The path to the module XML configuration file.</param>
        /// <param name="startImmediately">True if execution should be started immediately after configuring the module, false if module should be configured but not started.</param>
        private static void LoadAndConfigure(string dllFile, string xmlFile, bool startImmediately)
        {
            string modulePath = Path.GetDirectoryName(dllFile);

            if (!modulePath.EndsWith(Path.DirectorySeparatorChar.ToString()))
            {
                modulePath += Path.DirectorySeparatorChar;
            }

            string[] files = Directory.GetFiles(modulePath, "*.*", SearchOption.AllDirectories);

            AppDomainSetup domainSetup = new AppDomainSetup();

            domainSetup.ShadowCopyFiles   = "true";
            domainSetup.ApplicationBase   = modulePath;
            domainSetup.ConfigurationFile = dllFile + ".config";

            AppDomain domain = AppDomain.CreateDomain(dllFile, null, domainSetup);

            TaskManagerService.Logger.Log(string.Format("Module found: '{0}', configuration file: '{1}', scanning assembly for tasks...", dllFile, xmlFile));

            if (modulePath != AppDomain.CurrentDomain.BaseDirectory)
            {
                string loaderDll     = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "TaskManager.Loader.dll");
                string tempLoaderDll = Path.Combine(modulePath, "TaskManager.Loader.dll");
                string commonDll     = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "TaskManager.Common.dll");
                string tempCommonDll = Path.Combine(modulePath, "TaskManager.Common.dll");

                File.Copy(loaderDll, tempLoaderDll, true);
                File.Copy(commonDll, tempCommonDll, true);
            }

            AssemblyName loaderAssemblyName = AssemblyName.GetAssemblyName(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "TaskManager.Loader.dll"));
            Loader       loader             = (Loader)domain.CreateInstanceAndUnwrap(loaderAssemblyName.ToString(), "TaskManager.Loader");

            TaskWrapper[] tasks = loader.LoadAndConfigure(dllFile, xmlFile, TaskManagerService.Logger, AppDomain.CurrentDomain.BaseDirectory);

            ModuleData newModule = new ModuleData();

            newModule.BasePath = Path.GetDirectoryName(dllFile);
            newModule.Tasks    = tasks;
            newModule.DllFile  = dllFile;
            newModule.XmlFile  = xmlFile;
            newModule.Files    = new List <string>(files);
            newModule.Domain   = domain;

            if (startImmediately)
            {
                foreach (TaskWrapper task in newModule.Tasks)
                {
                    TaskSupervisor.ScheduleTask(task);
                }
            }

            _moduleList.Add(newModule);
        }
Пример #2
0
        /// <summary>
        /// Loads all modules.
        /// </summary>
        public static void Execute()
        {
            if (null == _moduleList)
            {
                throw new Exception("TaskManager failed to initialize.");
            }

            lock (_moduleListLock)
            {
                foreach (ModuleData module in _moduleList)
                {
                    foreach (TaskWrapper task in module.Tasks)
                    {
                        TaskManagerService.Logger.Log(string.Format("Registering task '{0}'...", task.TaskName));
                        TaskSupervisor.ScheduleTask(task);
                    }
                }
            }
        }
Пример #3
0
        /// <summary>
        /// Loads a module from a zip file.
        /// </summary>
        /// <param name="zipFile">The path to the zip file.</param>
        /// <param name="startImmediately">True if execution should be started immediately after configuring the module, false if module should be configured but not started.</param>
        private static void LoadZipAndConfigure(string zipFile, bool startImmediately)
        {
            string tempPath = null;

            int i = 0;

            while (Directory.Exists(tempPath = Path.Combine(Path.Combine(Path.GetTempPath(), "TaskManager"), Path.GetRandomFileName())))
            {
                i++;
                if (i == 10)
                {
                    throw new Exception("Failed to create a new temporary folder.");
                }
            }

            if (!tempPath.EndsWith(Path.DirectorySeparatorChar.ToString()))
            {
                tempPath += Path.DirectorySeparatorChar;
            }

            string overridePath = Path.Combine(Path.GetDirectoryName(zipFile), Path.GetFileNameWithoutExtension(zipFile)) + Path.DirectorySeparatorChar;

            string loaderDll     = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "TaskManager.Loader.dll");
            string tempLoaderDll = Path.Combine(tempPath, "TaskManager.Loader.dll");
            string commonDll     = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "TaskManager.Common.dll");
            string tempCommonDll = Path.Combine(tempPath, "TaskManager.Common.dll");

            List <string> dependencies = new List <string>();

            DirectoryInfo directoryInfo = Directory.CreateDirectory(tempPath);

            try
            {
                using (Stream fileStream = File.Open(zipFile, FileMode.Open, FileAccess.Read))
                {
                    using (ZipArchive zip = new ZipArchive(fileStream, ZipArchiveMode.Read))
                    {
                        var directory = zip.Entries;

                        foreach (var compressedFile in directory)
                        {
                            string destinationFile = Path.Combine(tempPath, compressedFile.FullName);
                            string overrideFile    = Path.Combine(overridePath, compressedFile.FullName);
                            dependencies.Add(overrideFile);

                            compressedFile.ExtractToFile(destinationFile, true);
                        }
                    }
                }

                if (Directory.Exists(overridePath))
                {
                    foreach (string overrideFile in Directory.GetFiles(overridePath, "*.*", SearchOption.AllDirectories))
                    {
                        if (!dependencies.Contains(overrideFile))
                        {
                            dependencies.Add(overrideFile);
                        }

                        dependencies.Add(Path.Combine(overridePath, overrideFile.Replace(tempPath, string.Empty)));

                        string relativeName    = overrideFile.Replace(overridePath, string.Empty);
                        string destination     = Path.Combine(tempPath, relativeName);
                        string destinationPath = Path.GetDirectoryName(destination);

                        if (!Directory.Exists(destinationPath))
                        {
                            Directory.CreateDirectory(destinationPath);
                        }

                        File.Copy(overrideFile, destination, true);
                    }
                }

                List <string> possibleFiles = new List <string>();
                foreach (string moduleFile in Directory.GetFiles(tempPath, "*" + ModuleFileDefaultExtension, SearchOption.AllDirectories))
                {
                    string xmlFile = Path.ChangeExtension(moduleFile, ConfigFileDefaultExtension);

                    if (File.Exists(xmlFile))
                    {
                        possibleFiles.Add(moduleFile);
                    }
                }

                File.Copy(loaderDll, tempLoaderDll, true);
                File.Copy(commonDll, tempCommonDll, true);

                foreach (string dllFile in possibleFiles)
                {
                    string xmlFile = Path.ChangeExtension(dllFile, ConfigFileDefaultExtension);

                    if (File.Exists(xmlFile))
                    {
                        if (!xmlFile.IsValidConfigurationFile())
                        {
                            continue;
                        }

                        string modulePath = Path.GetDirectoryName(dllFile);

                        string[] files = Directory.GetFiles(modulePath, "*.*", SearchOption.AllDirectories);

                        AppDomainSetup domainSetup = new AppDomainSetup();
                        domainSetup.ShadowCopyFiles   = "true";
                        domainSetup.ApplicationBase   = tempPath;
                        domainSetup.ConfigurationFile = dllFile + ".config";

                        AppDomain domain = AppDomain.CreateDomain(dllFile, null, domainSetup);

                        TaskWrapper[] tasks = new TaskWrapper[0];

                        try
                        {
                            TaskManagerService.Logger.Log(string.Format("Module found: '{0}', configuration file: '{1}', scanning assembly for tasks...", dllFile, xmlFile));

                            AssemblyName loaderAssemblyName = AssemblyName.GetAssemblyName(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "TaskManager.Loader.dll"));
                            Loader       loader             = (Loader)domain.CreateInstanceAndUnwrap(loaderAssemblyName.ToString(), "TaskManager.Loader");

                            tasks = loader.LoadAndConfigure(dllFile, xmlFile, TaskManagerService.Logger, AppDomain.CurrentDomain.BaseDirectory);

                            ModuleData newModule = new ModuleData();
                            newModule.BasePath     = overridePath;
                            newModule.Tasks        = tasks;
                            newModule.DllFile      = dllFile;
                            newModule.XmlFile      = xmlFile;
                            newModule.ZipFile      = zipFile;
                            newModule.ZipDirectory = tempPath;
                            newModule.Files        = new List <string>(files);
                            newModule.Files.AddRange(dependencies);
                            newModule.Files.Add(zipFile);
                            newModule.Domain = domain;

                            if (startImmediately)
                            {
                                foreach (TaskWrapper task in newModule.Tasks)
                                {
                                    TaskSupervisor.ScheduleTask(task);
                                }
                            }

                            _moduleList.Add(newModule);
                        }
                        catch (Exception ex)
                        {
                            foreach (TaskWrapper task in tasks)
                            {
                                try
                                {
                                    TaskSupervisor.RemoveTask(task);
                                }
                                catch
                                {
                                }
                            }

                            AppDomain.Unload(domain);

                            TaskManagerService.Logger.Log(string.Format("Unable to load module '{0}' from zipped file '{1}'.", dllFile, zipFile), ex);
                        }
                    }
                }

                foreach (ModuleData module in _moduleList)
                {
                    if (module.ZipFile == zipFile)
                    {
                        return;
                    }
                }

                throw new Exception(string.Format("Unable to find tasks in zipped file '{0}'.", zipFile));
            }
            catch
            {
                try
                {
                    Directory.Delete(tempPath, true);
                }
                catch (Exception ex)
                {
                    TaskManagerService.Logger.Log(string.Format("Unable to remove temporary directory '{0}'.", tempPath), ex);
                }

                throw;
            }
        }