1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2008 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.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using System.Text;
|
---|
26 | using HeuristicLab.Hive.Contracts.Interfaces;
|
---|
27 | using HeuristicLab.Hive.Server.DataAccess;
|
---|
28 | using System.Timers;
|
---|
29 | using HeuristicLab.DataAccess.Interfaces;
|
---|
30 | using HeuristicLab.Tracing;
|
---|
31 | using System.Threading;
|
---|
32 |
|
---|
33 | namespace HeuristicLab.Hive.Server.Core {
|
---|
34 | class LifecycleManager : ILifecycleManager {
|
---|
35 | private AutoResetEvent waitHandle;
|
---|
36 | private Thread thread;
|
---|
37 | private bool shutdownRequested;
|
---|
38 |
|
---|
39 | private TimeSpan interval = new TimeSpan(0, 0, 10);
|
---|
40 | public TimeSpan Interval {
|
---|
41 | get { return interval; }
|
---|
42 | set { interval = value; }
|
---|
43 | }
|
---|
44 |
|
---|
45 | private int jobsCurrentlyTransfering = 0;
|
---|
46 | public int JobsCurrentlyTransferring {
|
---|
47 | get { return jobsCurrentlyTransfering; }
|
---|
48 | set {
|
---|
49 | if (jobsCurrentlyTransfering != value) {
|
---|
50 | jobsCurrentlyTransfering = value;
|
---|
51 | Logger.Info("JobsCurrentlyTransfering: " + jobsCurrentlyTransfering);
|
---|
52 | }
|
---|
53 | }
|
---|
54 | }
|
---|
55 |
|
---|
56 | #region ILifecycleManager Members
|
---|
57 |
|
---|
58 | public void Start() {
|
---|
59 | Thread thread = new Thread(RunHeartbeatThread);
|
---|
60 | thread.Start();
|
---|
61 | using (ServiceLocator.GetContextFactory().GetContext()) {
|
---|
62 | OnStarted();
|
---|
63 | }
|
---|
64 | }
|
---|
65 |
|
---|
66 | private void RunHeartbeatThread() {
|
---|
67 | waitHandle = new AutoResetEvent(false);
|
---|
68 | shutdownRequested = false;
|
---|
69 | while (!shutdownRequested) {
|
---|
70 | using (ServiceLocator.GetContextFactory().GetContext(false)) {
|
---|
71 | OnServerHeartbeat();
|
---|
72 | }
|
---|
73 | waitHandle.WaitOne(this.Interval);
|
---|
74 | }
|
---|
75 | waitHandle.Close();
|
---|
76 | using (ServiceLocator.GetContextFactory().GetContext()) {
|
---|
77 | OnStopped();
|
---|
78 | }
|
---|
79 | }
|
---|
80 |
|
---|
81 | public void Stop() {
|
---|
82 | shutdownRequested = true;
|
---|
83 | if (waitHandle != null) waitHandle.Set();
|
---|
84 | if (thread != null) thread.Join();
|
---|
85 | thread = null;
|
---|
86 | }
|
---|
87 |
|
---|
88 | public event EventHandler ServerHeartbeat;
|
---|
89 | private void OnServerHeartbeat() {
|
---|
90 | var handler = ServerHeartbeat;
|
---|
91 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
92 | }
|
---|
93 |
|
---|
94 | public event EventHandler Started;
|
---|
95 | private void OnStarted() {
|
---|
96 | var handler = Started;
|
---|
97 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
98 | }
|
---|
99 |
|
---|
100 | public event EventHandler Stopped;
|
---|
101 | private void OnStopped() {
|
---|
102 | var handler = Stopped;
|
---|
103 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
104 | }
|
---|
105 |
|
---|
106 | #endregion
|
---|
107 | }
|
---|
108 | }
|
---|