Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1260

  • changed ExecutionTime column datatype in DB to string (because time only allows 24-hours, but TimeSpan can be more)
File size: 7.6 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 System.Threading;
32using System.Reflection;
33
34namespace HeuristicLab.Hive.Slave.Core {
35  public class PluginCache {
36    private static object locker = new object();
37    public const string ConfigFileName = "Sandbox.config";
38
39    private static PluginCache instance = null;
40
41    public string PluginCacheDir { get; set; }
42    public string PluginTempBaseDir { get; set; }
43
44    private List<PluginDescription> cachedPlugins = new List<PluginDescription>();
45
46    private PluginManager pm;
47
48    public static PluginCache Instance {
49      get {
50        if (instance == null)
51          instance = new PluginCache();
52        return instance;
53      }
54    }
55
56    public string ConfigFilePath {
57      get {
58        return Path.Combine(PluginCacheDir, ConfigFileName);
59      }
60    }
61
62    public PluginCache() {
63      PluginCacheDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "PluginCache");
64      PluginTempBaseDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "PluginTemp");
65
66      this.pm = new PluginManager(PluginCacheDir);
67      DoUpdateRun();
68    }
69
70    private void DoUpdateRun() {
71      if (!Directory.Exists(PluginCacheDir)) {
72        Directory.CreateDirectory(PluginCacheDir);
73      }
74      pm.DiscoverAndCheckPlugins();
75      cachedPlugins = new List<PluginDescription>(pm.Plugins);
76    }
77
78    [MethodImpl(MethodImplOptions.Synchronized)]
79    public void CopyPluginsForJob(List<HivePluginInfoDto> requests, Guid jobId) {
80      lock (locker) {
81        String targetDir = Path.Combine(PluginTempBaseDir, jobId.ToString());
82
83        if (Directory.Exists(targetDir)) {
84          Directory.Delete(targetDir, true);
85        }
86
87        DirectoryInfo di = Directory.CreateDirectory(targetDir);
88
89        foreach (HivePluginInfoDto requestedPlugin in requests) {
90          PluginDescription pd = cachedPlugins.Where(cp =>
91            cp.Name == requestedPlugin.Name &&
92            cp.Version.Major == requestedPlugin.Version.Major &&
93            cp.Version.Minor == requestedPlugin.Version.Minor).SingleOrDefault();
94          if (pd != null) {
95            foreach (IPluginFile ipf in pd.Files) {
96              File.Copy(ipf.Name, Path.Combine(targetDir, Path.GetFileName(ipf.Name)));
97            }
98          }
99        }
100
101        // copy config file
102        File.Copy(ConfigFilePath, Path.Combine(targetDir, ConfigFileName));
103
104        // copy files from PluginInfrastructure, which are not declared
105        string baseDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
106        CopyFile(baseDir, targetDir, "HeuristicLab.PluginInfrastructure-3.3.dll");
107        CopyFile(baseDir, targetDir, "ICSharpCode.SharpZipLib.dll");
108        CopyFile(baseDir, targetDir, "ICSharpCode.SharpZipLib License.txt");
109
110        // copy slave plugins, otherwise its not possible to register the UnhandledException handler to the appdomain
111        CopyFile(baseDir, targetDir, "HeuristicLab.Hive.Slave.Core-3.3.dll");
112        CopyFile(baseDir, targetDir, "HeuristicLab.Hive.Slave.Common-3.3.dll");
113        CopyFile(baseDir, targetDir, "HeuristicLab.Hive.Slave.Communication-3.3.dll");
114        CopyFile(baseDir, targetDir, "HeuristicLab.Hive.Slave.ExecutionEngine-3.3.dll");
115      }
116    }
117
118    private void CopyFile(string baseDir, string targetDir, string fileName) {
119      if (!File.Exists(Path.Combine(targetDir, fileName))) File.Copy(Path.Combine(baseDir, fileName), Path.Combine(targetDir, fileName));
120    }
121
122    [MethodImpl(MethodImplOptions.Synchronized)]
123    internal void PreparePlugins(List<HivePluginInfoDto> requiredPlugins) {
124      lock (locker) {
125        Logger.Debug("Fetching plugins for job");
126        List<HivePluginInfoDto> localPlugins = new List<HivePluginInfoDto>();
127        List<HivePluginInfoDto> missingPlugins = new List<HivePluginInfoDto>();
128        bool found = false;
129
130        foreach (HivePluginInfoDto info in requiredPlugins) {
131          //we MAY run in problems here - if there is a plugin twice in requests, there may be added two different versions of the plugin
132          foreach (PluginDescription cachedPlugin in cachedPlugins) {
133            if (info.Name == cachedPlugin.Name && info.Version == cachedPlugin.Version) {
134              localPlugins.Add(new HivePluginInfoDto() { Id = new Guid(), Name = info.Name, Version = info.Version, Update = true });
135              found = true;
136
137              break;
138            }
139          }
140          if (!found) {
141            Logger.Debug("Plugin NOT found " + info.Name + ", " + info.Version);
142            missingPlugins.Add(info);
143          }
144          found = false;
145        }
146
147        Logger.Debug("First run - Update the plugins in the cache");
148        localPlugins.AddRange(missingPlugins);
149        IEnumerable<CachedHivePluginInfoDto> updateablePlugins = WcfService.Instance.RequestPlugins(missingPlugins);
150
151        foreach (CachedHivePluginInfoDto updateablePlugin in updateablePlugins) {
152          PluginDescription pd = cachedPlugins.Where(cachedPlugin =>
153            cachedPlugin.Name == updateablePlugin.Name &&
154            cachedPlugin.Version.Major == updateablePlugin.Version.Major &&
155            cachedPlugin.Version.Minor == updateablePlugin.Version.Major).SingleOrDefault();
156
157          if (pd != null) {
158            foreach (IPluginFile ipf in pd.Files) {
159              Logger.Debug(string.Format("deleting {0}", Path.GetFileName(ipf.Name)));
160              File.Delete(ipf.Name);
161            }
162          }
163
164          foreach (HivePluginFile pf in updateablePlugin.PluginFiles) {
165            Logger.Debug(string.Format("writing {0}", Path.GetFileName(pf.Name)));
166            File.WriteAllBytes(Path.Combine(PluginCacheDir, Path.GetFileName(pf.Name)), pf.BinaryFile);
167          }
168        }
169
170        // download config file
171        HivePluginFile configFile = WcfService.Instance.GetConfigurationFile();
172        File.WriteAllBytes(ConfigFilePath, configFile.BinaryFile);
173
174        DoUpdateRun();
175      }
176    }
177
178    internal void DeletePluginsForJob(Guid id) {
179      try {
180        Logger.Debug("unloading...");
181        int tries = 5;
182        while (tries > 0) {
183          try {
184            Directory.Delete(Path.Combine(PluginTempBaseDir, id.ToString()), true);
185            tries = 0;
186          }
187          catch {
188            Thread.Sleep(1000);
189            tries--;
190            if (tries == 0) throw;
191          }
192        }
193      }
194      catch (Exception ex) {
195        Logger.Debug("failed while unloading " + id + " with exception " + ex);
196      }
197    }
198
199
200  }
201}
Note: See TracBrowser for help on using the repository browser.