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