Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.2/sources/HeuristicLab.Hive.Client.Core/3.2/PluginCache.cs @ 4140

Last change on this file since 4140 was 4140, checked in by kgrading, 14 years ago

#828 added various improvements to the plugin cache manager, the execution engine, the transaction handling on the serverside and the server console

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