Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4424 was 4424, checked in by cneumuel, 14 years ago
  • Added and updated License Information in every file
  • Sort and remove usings in every file
  • Deleted obsolete DataAccess.ADOHelper
  • Deleted some obsolete files
File size: 6.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.IO;
25using System.Linq;
26using System.Runtime.CompilerServices;
27using HeuristicLab.Hive.Contracts.BusinessObjects;
28using HeuristicLab.Hive.Slave.Communication;
29using HeuristicLab.PluginInfrastructure;
30using HeuristicLab.PluginInfrastructure.Manager;
31using HeuristicLab.Tracing;
32
33namespace HeuristicLab.Hive.Slave.Core {
34  public class PluginCache {
35
36    private static PluginCache instance = null;
37
38    public string PluginRepositoryDir { get; set; }
39
40    private List<PluginDescription> cachedPlugins = new List<PluginDescription>();
41
42    private PluginManager pm;
43
44    public static PluginCache Instance {
45      get {
46        if (instance == null)
47          instance = new PluginCache();
48        return instance;
49      }
50    }
51
52    public PluginCache() {
53      PluginRepositoryDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "plugins");
54     
55      this.pm = new PluginManager(PluginRepositoryDir);
56      DoUpdateRun();
57    }
58
59    private void DoUpdateRun() {
60      if (!Directory.Exists(PluginRepositoryDir)) {
61        Directory.CreateDirectory(PluginRepositoryDir);
62      }
63      pm.DiscoverAndCheckPlugins();
64      cachedPlugins = new List<PluginDescription>(pm.Plugins);
65    }
66
67    [MethodImpl(MethodImplOptions.Synchronized)]
68    public bool CopyPluginsForJob(List<HivePluginInfoDto> requests, Guid jobId) {
69      String targetDir = Path.Combine(PluginRepositoryDir, jobId.ToString());
70
71      if (Directory.Exists(targetDir)) {
72        Directory.Delete(targetDir, true);
73      }
74
75      DirectoryInfo di = Directory.CreateDirectory(targetDir);
76
77      foreach (HivePluginInfoDto requestedPlugin in requests) {
78        PluginDescription pd = cachedPlugins.Where(cp =>
79          cp.Name == requestedPlugin.Name &&
80          cp.Version.Major == requestedPlugin.Version.Major &&
81          cp.Version.Minor == requestedPlugin.Version.Minor &&
82          cp.Version.Revision >= requestedPlugin.Version.Revision).
83          SingleOrDefault();
84        if (pd == null) {
85          return false;
86        }
87
88        foreach (IPluginFile ipf in pd.Files) {
89          string x = targetDir + ipf.Name.Split('\\').Last();
90          string y = Path.Combine(targetDir, Path.GetFileName(ipf.Name));
91
92          //File.Copy(ipf.Name, targetDir + ipf.Name.Split('\\').Last());
93          File.Copy(ipf.Name, Path.Combine(targetDir, Path.GetFileName(ipf.Name)));
94        }
95      }
96      return true;
97    }
98
99    [MethodImpl(MethodImplOptions.Synchronized)]
100    internal void PreparePlugins(List<HivePluginInfoDto> requiredPlugins) {
101      Logger.Debug("Fetching plugins for job");
102      List<HivePluginInfoDto> localPlugins = new List<HivePluginInfoDto>();
103      List<HivePluginInfoDto> neededPlugins = new List<HivePluginInfoDto>();
104      List<HivePluginInfoDto> missingPlugins = new List<HivePluginInfoDto>();
105      bool found = false;
106
107      foreach (HivePluginInfoDto info in requiredPlugins) {
108        //we MAY run in problems here - if there is a plugin twice in requests, there may be added two different versions of the plugin
109        foreach (PluginDescription cachedPlugin in cachedPlugins) {
110          if (info.Name.Equals(cachedPlugin.Name)) {
111            Logger.Debug("Found plugin " + info.Name + ", " + info.Version);
112            localPlugins.Add(new HivePluginInfoDto() { Id = new Guid(), Name = info.Name, Version = info.Version, Update = true });
113            neededPlugins.Add(info);
114            found = true;
115
116            break;
117          }
118        }
119        if (!found) {
120          Logger.Debug("Plugin NOT found " + info.Name + ", " + info.Version);
121          missingPlugins.Add(info);
122        }
123        found = false;
124      }
125
126      Logger.Debug("First run - Update the plugins in the cache");
127      localPlugins.AddRange(missingPlugins);
128      IEnumerable<CachedHivePluginInfoDto> updateablePlugins = WcfService.Instance.RequestPlugins(localPlugins);
129
130      foreach (CachedHivePluginInfoDto updateablePlugin in updateablePlugins) {
131        PluginDescription pd = cachedPlugins.Where(cachedPlugin => cachedPlugin.Name.Equals(updateablePlugin.Name)).SingleOrDefault();
132
133        if (pd != null) {
134          Logger.Debug("deleting old files");
135          foreach (IPluginFile ipf in pd.Files) {
136            File.Delete(ipf.Name);
137          }
138        }
139
140        Logger.Debug("deleted old files");
141        Logger.Debug("creating new files");
142        foreach (HivePluginFile pf in updateablePlugin.PluginFiles) {
143          string x = PluginRepositoryDir + pf.Name.Split('\\').Last();
144          string y = Path.Combine(PluginRepositoryDir, Path.GetFileName(pf.Name));
145          File.WriteAllBytes(Path.Combine(PluginRepositoryDir, Path.GetFileName(pf.Name)), pf.BinaryFile);
146        }
147
148        Logger.Debug("created new files");
149        DoUpdateRun();
150      }
151    }
152
153    internal void DeletePluginsForJob(Guid id) {
154      try {
155        Logger.Debug("unloading...");
156        Directory.Delete(Path.Combine(PluginRepositoryDir, id.ToString()), true);
157      }
158      catch (Exception ex) {
159        Logger.Debug("failed while unloading " + id + " with exception " + ex);
160      }
161    }
162  }
163}
Note: See TracBrowser for help on using the repository browser.