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 |
|
---|
22 | using System;
|
---|
23 | using System.Threading;
|
---|
24 | using HeuristicLab.Hive.Contracts.Interfaces;
|
---|
25 | using HeuristicLab.Hive.Tracing;
|
---|
26 | using System.Collections.Generic;
|
---|
27 | using HeuristicLab.PluginInfrastructure.Manager;
|
---|
28 | using System.IO;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Hive.Server.Core {
|
---|
31 | public class LifecycleManager : ILifecycleManager {
|
---|
32 | private readonly string pluginDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "App_Data\\Plugins");
|
---|
33 | private AutoResetEvent waitHandle;
|
---|
34 | private Thread thread;
|
---|
35 | private bool shutdownRequested;
|
---|
36 | private PluginManager pm;
|
---|
37 |
|
---|
38 | private TimeSpan interval = new TimeSpan(0, 0, 10);
|
---|
39 | public TimeSpan Interval {
|
---|
40 | get { return interval; }
|
---|
41 | set { interval = value; }
|
---|
42 | }
|
---|
43 |
|
---|
44 | private int jobsCurrentlyTransfering = 0;
|
---|
45 | public int JobsCurrentlyTransferring {
|
---|
46 | get { return jobsCurrentlyTransfering; }
|
---|
47 | set {
|
---|
48 | if (jobsCurrentlyTransfering != value) {
|
---|
49 | jobsCurrentlyTransfering = value;
|
---|
50 | Logger.Info("JobsCurrentlyTransfering: " + jobsCurrentlyTransfering);
|
---|
51 | }
|
---|
52 | }
|
---|
53 | }
|
---|
54 |
|
---|
55 | private IEnumerable<PluginDescription> plugins;
|
---|
56 | public IEnumerable<PluginDescription> Plugins {
|
---|
57 | get { return pm.Plugins; }
|
---|
58 | }
|
---|
59 |
|
---|
60 | private byte[] configurationFile;
|
---|
61 | public byte[] ConfigurationFile {
|
---|
62 | get {
|
---|
63 | if (configurationFile == null) {
|
---|
64 | configurationFile = File.ReadAllBytes(Path.Combine(pluginDir, "HeuristicLab 3.3.exe.config"));
|
---|
65 | }
|
---|
66 | return configurationFile;
|
---|
67 | }
|
---|
68 | }
|
---|
69 |
|
---|
70 | #region ILifecycleManager Members
|
---|
71 |
|
---|
72 | public void Start() {
|
---|
73 | LoadPlugins();
|
---|
74 | Thread thread = new Thread(RunHeartbeatThread);
|
---|
75 | thread.Start();
|
---|
76 | using (ServiceLocator.GetContextFactory().GetContext()) {
|
---|
77 | OnStarted();
|
---|
78 | }
|
---|
79 | }
|
---|
80 |
|
---|
81 | private void LoadPlugins() {
|
---|
82 | pm = new PluginManager(pluginDir);
|
---|
83 | pm.DiscoverAndCheckPlugins();
|
---|
84 | pm.InitializeLifetimeService();
|
---|
85 | }
|
---|
86 |
|
---|
87 | private void RunHeartbeatThread() {
|
---|
88 | waitHandle = new AutoResetEvent(false);
|
---|
89 | shutdownRequested = false;
|
---|
90 | while (!shutdownRequested) {
|
---|
91 | using (ServiceLocator.GetContextFactory().GetContext(false)) {
|
---|
92 | OnServerHeartbeat();
|
---|
93 | }
|
---|
94 | waitHandle.WaitOne(this.Interval);
|
---|
95 | }
|
---|
96 | waitHandle.Close();
|
---|
97 | using (ServiceLocator.GetContextFactory().GetContext()) {
|
---|
98 | OnStopped();
|
---|
99 | }
|
---|
100 | }
|
---|
101 |
|
---|
102 | public void Stop() {
|
---|
103 | shutdownRequested = true;
|
---|
104 | if (waitHandle != null) waitHandle.Set();
|
---|
105 | if (thread != null) thread.Join();
|
---|
106 | thread = null;
|
---|
107 | }
|
---|
108 |
|
---|
109 | public event EventHandler ServerHeartbeat;
|
---|
110 | private void OnServerHeartbeat() {
|
---|
111 | var handler = ServerHeartbeat;
|
---|
112 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
113 | }
|
---|
114 |
|
---|
115 | public event EventHandler Started;
|
---|
116 | private void OnStarted() {
|
---|
117 | var handler = Started;
|
---|
118 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
119 | }
|
---|
120 |
|
---|
121 | public event EventHandler Stopped;
|
---|
122 | private void OnStopped() {
|
---|
123 | var handler = Stopped;
|
---|
124 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
125 | }
|
---|
126 |
|
---|
127 | #endregion
|
---|
128 | }
|
---|
129 | }
|
---|