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