Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.3-HiveMigration/sources/HeuristicLab.Hive/HeuristicLab.Hive.Slave.Core/3.3/PluginCache.cs @ 4269

Last change on this file since 4269 was 4269, checked in by cneumuel, 14 years ago

fixed invalid plugin-directory (#1159)

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