Changeset 4141 for branches/3.3-HiveMigration/sources/HeuristicLab.Hive/HeuristicLab.Hive.Client.Core/3.3/PluginCache.cs
- Timestamp:
- 08/04/10 10:44:14 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/3.3-HiveMigration/sources/HeuristicLab.Hive/HeuristicLab.Hive.Client.Core/3.3/PluginCache.cs
r4119 r4141 1 1 using System; 2 2 using System.Collections.Generic; 3 using System.IO; 3 4 using System.Linq; 4 using System.Text; 5 using System.Runtime.CompilerServices; 6 using HeuristicLab.Hive.Client.Communication; 7 using HeuristicLab.Hive.Contracts.BusinessObjects; 5 8 using HeuristicLab.PluginInfrastructure; 6 using HeuristicLab.Hive.Client.Communication; 7 using HeuristicLab.Hive.Client.Common; 8 using HeuristicLab.Hive.Contracts.BusinessObjects; 9 using HeuristicLab.PluginInfrastructure.Manager; 9 10 using HeuristicLab.Tracing; 10 using System.IO;11 11 12 12 namespace HeuristicLab.Hive.Client.Core { … … 14 14 15 15 private static PluginCache instance = null; 16 17 public const string PLUGIN_REPO = @"plugins\"; 18 19 private List<PluginDescription> cachedPlugins = new List<PluginDescription>(); 20 21 private PluginManager pm = new PluginManager(PLUGIN_REPO); 22 16 23 public static PluginCache Instance { 17 24 get { … … 20 27 return instance; 21 28 } 22 } 23 24 private List<CachedHivePluginInfoDto> pluginCache; 25 26 29 } 27 30 public PluginCache() { 28 pluginCache = new List<CachedHivePluginInfoDto>(); 29 } 30 31 public void AddPlugin(CachedHivePluginInfoDto plugin) { 32 pluginCache.Add(plugin); 31 DoUpdateRun(); 33 32 } 34 33 35 public List<CachedHivePluginInfoDto> GetPlugins(List<HivePluginInfoDto> requests) { 34 private void DoUpdateRun() { 35 pm.DiscoverAndCheckPlugins(); 36 cachedPlugins = new List<PluginDescription>(pm.Plugins); 37 } 38 39 40 [MethodImpl(MethodImplOptions.Synchronized)] 41 public bool CopyPluginsForJob(List<HivePluginInfoDto> requests, Guid jobId) { 42 43 String targetDir = PLUGIN_REPO + jobId.ToString() + "\\"; 44 45 if (Directory.Exists(targetDir)) { 46 Directory.Delete(targetDir, true); 47 } 48 49 DirectoryInfo di = Directory.CreateDirectory(targetDir); 50 51 foreach (HivePluginInfoDto requestedPlugin in requests) { 52 PluginDescription pd = 53 cachedPlugins.Where( 54 cp => 55 cp.Name == requestedPlugin.Name && 56 cp.Version.Major == requestedPlugin.Version.Major && 57 cp.Version.Minor == requestedPlugin.Version.Minor && 58 cp.Version.Revision >= requestedPlugin.Version.Revision). 59 SingleOrDefault(); 60 if (pd == null) { 61 return false; 62 } 63 64 foreach (IPluginFile ipf in pd.Files) { 65 File.Copy(ipf.Name, targetDir + ipf.Name.Split('\\').Last()); 66 } 67 } 68 return true; 69 } 70 71 [MethodImpl(MethodImplOptions.Synchronized)] 72 internal void PreparePlugins(List<HivePluginInfoDto> requiredPlugins) { 36 73 Logger.Debug("Fetching plugins for job"); 37 List<CachedHivePluginInfoDto> neededPlugins = new List<CachedHivePluginInfoDto>(); 74 List<HivePluginInfoDto> localPlugins = new List<HivePluginInfoDto>(); 75 List<HivePluginInfoDto> neededPlugins = new List<HivePluginInfoDto>(); 38 76 List<HivePluginInfoDto> missingPlugins = new List<HivePluginInfoDto>(); 39 77 bool found = false; 40 41 foreach (HivePluginInfoDto info in requ ests) {78 79 foreach (HivePluginInfoDto info in requiredPlugins) { 42 80 //we MAY run in problems here - if there is a plugin twice in requests, there may be added two different versions of the plugin 43 foreach ( CachedHivePluginInfoDto cache in pluginCache) {44 if (info.Name.Equals(cache .Name) && info.Version.Equals(cache.Version)) {81 foreach (PluginDescription cachedPlugin in cachedPlugins) { 82 if (info.Name.Equals(cachedPlugin.Name)) { 45 83 Logger.Debug("Found plugin " + info.Name + ", " + info.Version); 46 neededPlugins.Add(cache); 84 localPlugins.Add(new HivePluginInfoDto() { Id = new Guid(), Name = info.Name, Version = info.Version, Update = true }); 85 neededPlugins.Add(info); 47 86 found = true; 87 48 88 break; 49 89 } 50 90 } 51 if (!found) 52 Logger.Debug(" FoundNOT found " + info.Name + ", " + info.Version);91 if (!found) { 92 Logger.Debug("Plugin NOT found " + info.Name + ", " + info.Version); 53 93 missingPlugins.Add(info); 94 } 54 95 found = false; 55 96 } 56 97 57 Logger.Debug("Requesting missing plugins"); 58 List<CachedHivePluginInfoDto> receivedPlugins = WcfService.Instance.RequestPlugins(missingPlugins); 59 Logger.Debug("Requested missing plugins"); 98 Logger.Debug("First run - Update the plugins in the cache"); 60 99 61 if (receivedPlugins != null) { 62 neededPlugins.AddRange(receivedPlugins); 63 pluginCache.AddRange(receivedPlugins); 64 } else 65 Logger.Error("Fetching of the plugins failed!"); 100 localPlugins.AddRange(missingPlugins); 66 101 67 return neededPlugins; 102 List<CachedHivePluginInfoDto> updateablePlugins = WcfService.Instance.RequestPlugins(localPlugins); 103 104 foreach (CachedHivePluginInfoDto updateablePlugin in updateablePlugins) { 105 PluginDescription pd = 106 cachedPlugins.Where(cachedPlugin => cachedPlugin.Name.Equals(updateablePlugin.Name)).SingleOrDefault(); 107 108 if (pd != null) { 109 Logger.Debug("deleting old files"); 110 foreach (IPluginFile ipf in pd.Files) { 111 File.Delete(ipf.Name); 112 } 113 } 114 115 Logger.Debug("deleted old files"); 116 Logger.Debug("creating new files"); 117 118 119 120 foreach (HivePluginFile pf in updateablePlugin.PluginFiles) { 121 File.WriteAllBytes(PLUGIN_REPO + pf.Name.Split('\\').Last(), pf.BinaryFile); 122 } 123 124 Logger.Debug("created new files"); 125 DoUpdateRun(); 126 } 68 127 } 69 128 70 71 internal void PreparePlugins(List<HivePluginInfoDto> requiredPlugins) { 72 List<HivePluginInfoDto> missingPlugins = new List<HivePluginInfoDto>(); 73 foreach (HivePluginInfoDto requiredPlugin in requiredPlugins) { 74 if(ApplicationManager.Manager.Plugins.Count(ipd => ipd.Version.Major == requiredPlugin.Version.Major && ipd.Version.Minor >= requiredPlugin.Version.Minor) == 0) { 75 missingPlugins.Add(requiredPlugin); 76 } 129 internal void DeletePluginsForJob(Guid id) { 130 try { 131 Logger.Debug("unloading..."); 132 Directory.Delete(Path.Combine(PLUGIN_REPO, id.ToString()), true); 133 } catch (Exception ex) { 134 Logger.Debug("failed while unloading " + id + " with exception " + ex); 77 135 } 78 79 Logger.Debug("Requesting missing plugins");80 List<CachedHivePluginInfoDto> receivedPlugins = WcfService.Instance.RequestPlugins(missingPlugins);81 Logger.Debug("Requested missing plugins");82 83 foreach (CachedHivePluginInfoDto receivedPlugin in receivedPlugins) {84 foreach (HivePluginFile hpf in receivedPlugin.PluginFiles) {85 Logger.Info("writing file " + hpf.Name);86 File.WriteAllBytes("C:\\temp\\" + hpf.Name.Split('\\').Last(), hpf.BinaryFile);87 }88 }89 90 136 } 91 137 }
Note: See TracChangeset
for help on using the changeset viewer.