[4060] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[4424] | 3 | * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[4060] | 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;
|
---|
[4424] | 23 | using System.Threading;
|
---|
[4060] | 24 | using HeuristicLab.Hive.Contracts.Interfaces;
|
---|
[4710] | 25 | using HeuristicLab.Hive.Tracing;
|
---|
[4772] | 26 | using System.Collections.Generic;
|
---|
| 27 | using HeuristicLab.PluginInfrastructure.Manager;
|
---|
[4791] | 28 | using System.IO;
|
---|
[4060] | 29 |
|
---|
| 30 | namespace HeuristicLab.Hive.Server.Core {
|
---|
[5179] | 31 | public class LifecycleManager : ILifecycleManager {
|
---|
[5000] | 32 | private readonly string pluginDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "App_Data\\Plugins");
|
---|
[4423] | 33 | private AutoResetEvent waitHandle;
|
---|
| 34 | private Thread thread;
|
---|
| 35 | private bool shutdownRequested;
|
---|
[4791] | 36 | private PluginManager pm;
|
---|
[4060] | 37 |
|
---|
[4423] | 38 | private TimeSpan interval = new TimeSpan(0, 0, 10);
|
---|
| 39 | public TimeSpan Interval {
|
---|
| 40 | get { return interval; }
|
---|
| 41 | set { interval = value; }
|
---|
[4060] | 42 | }
|
---|
| 43 |
|
---|
[4423] | 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 | }
|
---|
[4060] | 53 | }
|
---|
| 54 |
|
---|
[4772] | 55 | private IEnumerable<PluginDescription> plugins;
|
---|
| 56 | public IEnumerable<PluginDescription> Plugins {
|
---|
[4791] | 57 | get { return pm.Plugins; }
|
---|
[4772] | 58 | }
|
---|
| 59 |
|
---|
[5000] | 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 |
|
---|
[4423] | 70 | #region ILifecycleManager Members
|
---|
[4060] | 71 |
|
---|
[4423] | 72 | public void Start() {
|
---|
[4791] | 73 | LoadPlugins();
|
---|
[4423] | 74 | Thread thread = new Thread(RunHeartbeatThread);
|
---|
| 75 | thread.Start();
|
---|
[4107] | 76 | using (ServiceLocator.GetContextFactory().GetContext()) {
|
---|
[4423] | 77 | OnStarted();
|
---|
[4107] | 78 | }
|
---|
[4060] | 79 | }
|
---|
| 80 |
|
---|
[4791] | 81 | private void LoadPlugins() {
|
---|
[5000] | 82 | pm = new PluginManager(pluginDir);
|
---|
[4791] | 83 | pm.DiscoverAndCheckPlugins();
|
---|
| 84 | pm.InitializeLifetimeService();
|
---|
| 85 | }
|
---|
| 86 |
|
---|
[4423] | 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();
|
---|
[4107] | 97 | using (ServiceLocator.GetContextFactory().GetContext()) {
|
---|
[4423] | 98 | OnStopped();
|
---|
[4107] | 99 | }
|
---|
[4060] | 100 | }
|
---|
| 101 |
|
---|
[4423] | 102 | public void Stop() {
|
---|
| 103 | shutdownRequested = true;
|
---|
| 104 | if (waitHandle != null) waitHandle.Set();
|
---|
| 105 | if (thread != null) thread.Join();
|
---|
| 106 | thread = null;
|
---|
[4060] | 107 | }
|
---|
| 108 |
|
---|
[4423] | 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 |
|
---|
[4060] | 127 | #endregion
|
---|
| 128 | }
|
---|
| 129 | }
|
---|