Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4772 was 4772, checked in by cneumuel, 13 years ago

#1260

  • added LogServiceReader to display log for slave without writing to local files
  • aborted jobs with childjobs now got back to state WaitForChildJob (instead of Offline)
  • lifecyclemanager now knows about available plugins (does not yet work perfectly)
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;
31
32namespace HeuristicLab.Hive.Slave.Core {
33  public class PluginCache {
34
35    private static PluginCache instance = null;
36
37    public string PluginRepositoryDir { get; set; }
38
39    private List<PluginDescription> cachedPlugins = new List<PluginDescription>();
40
41    private PluginManager pm;
42
43    public static PluginCache Instance {
44      get {
45        if (instance == null)
46          instance = new PluginCache();
47        return instance;
48      }
49    }
50
51    public PluginCache() {
52      PluginRepositoryDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "plugins");
53     
54      this.pm = new PluginManager(PluginRepositoryDir);
55      DoUpdateRun();
56    }
57
58    private void DoUpdateRun() {
59      if (!Directory.Exists(PluginRepositoryDir)) {
60        Directory.CreateDirectory(PluginRepositoryDir);
61      }
62      pm.DiscoverAndCheckPlugins();
63      cachedPlugins = new List<PluginDescription>(pm.Plugins);
64    }
65
66    [MethodImpl(MethodImplOptions.Synchronized)]
67    public bool CopyPluginsForJob(List<HivePluginInfoDto> requests, Guid jobId) {
68      String targetDir = Path.Combine(PluginRepositoryDir, jobId.ToString());
69
70      if (Directory.Exists(targetDir)) {
71        Directory.Delete(targetDir, true);
72      }
73
74      DirectoryInfo di = Directory.CreateDirectory(targetDir);
75
76      foreach (HivePluginInfoDto requestedPlugin in requests) {
77        PluginDescription pd = cachedPlugins.Where(cp =>
78          cp.Name == requestedPlugin.Name &&
79          cp.Version.Major == requestedPlugin.Version.Major &&
80          cp.Version.Minor == requestedPlugin.Version.Minor &&
81          cp.Version.Revision >= requestedPlugin.Version.Revision).
82          SingleOrDefault();
83        if (pd == null) {
84          return false;
85        }
86
87        foreach (IPluginFile ipf in pd.Files) {
88          string x = targetDir + ipf.Name.Split('\\').Last();
89          string y = Path.Combine(targetDir, Path.GetFileName(ipf.Name));
90
91          //File.Copy(ipf.Name, targetDir + ipf.Name.Split('\\').Last());
92          File.Copy(ipf.Name, Path.Combine(targetDir, Path.GetFileName(ipf.Name)));
93        }
94      }
95      return true;
96    }
97
98    [MethodImpl(MethodImplOptions.Synchronized)]
99    internal void PreparePlugins(List<HivePluginInfoDto> requiredPlugins) {
100      Logger.Debug("Fetching plugins for job");
101      List<HivePluginInfoDto> localPlugins = new List<HivePluginInfoDto>();
102      List<HivePluginInfoDto> neededPlugins = new List<HivePluginInfoDto>();
103      List<HivePluginInfoDto> missingPlugins = new List<HivePluginInfoDto>();
104      bool found = false;
105
106      foreach (HivePluginInfoDto info in requiredPlugins) {
107        //we MAY run in problems here - if there is a plugin twice in requests, there may be added two different versions of the plugin
108        foreach (PluginDescription cachedPlugin in cachedPlugins) {
109          if (info.Name.Equals(cachedPlugin.Name)) {
110            Logger.Debug("Found plugin " + info.Name + ", " + info.Version);
111            localPlugins.Add(new HivePluginInfoDto() { Id = new Guid(), Name = info.Name, Version = info.Version, Update = true });
112            neededPlugins.Add(info);
113            found = true;
114
115            break;
116          }
117        }
118        if (!found) {
119          Logger.Debug("Plugin NOT found " + info.Name + ", " + info.Version);
120          missingPlugins.Add(info);
121        }
122        found = false;
123      }
124
125      Logger.Debug("First run - Update the plugins in the cache");
126      localPlugins.AddRange(missingPlugins);
127      IEnumerable<CachedHivePluginInfoDto> updateablePlugins = WcfService.Instance.RequestPlugins(localPlugins);
128
129      foreach (CachedHivePluginInfoDto updateablePlugin in updateablePlugins) {
130        PluginDescription pd = cachedPlugins.Where(cachedPlugin => cachedPlugin.Name.Equals(updateablePlugin.Name)).SingleOrDefault();
131
132        if (pd != null) {
133          Logger.Debug("deleting old files");
134          foreach (IPluginFile ipf in pd.Files) {
135            File.Delete(ipf.Name);
136          }
137        }
138
139        Logger.Debug("deleted old files");
140        Logger.Debug("creating new files");
141        foreach (HivePluginFile pf in updateablePlugin.PluginFiles) {
142          string x = PluginRepositoryDir + pf.Name.Split('\\').Last();
143          string y = Path.Combine(PluginRepositoryDir, Path.GetFileName(pf.Name));
144          File.WriteAllBytes(Path.Combine(PluginRepositoryDir, Path.GetFileName(pf.Name)), pf.BinaryFile);
145        }
146
147        Logger.Debug("created new files");
148        DoUpdateRun();
149      }
150    }
151
152    internal void DeletePluginsForJob(Guid id) {
153      try {
154        Logger.Debug("unloading...");
155        Directory.Delete(Path.Combine(PluginRepositoryDir, id.ToString()), true);
156      }
157      catch (Exception ex) {
158        Logger.Debug("failed while unloading " + id + " with exception " + ex);
159      }
160    }
161  }
162}
Note: See TracBrowser for help on using the repository browser.