Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HiveStatistics/sources/HeuristicLab.Services.Hive.SelfHost/3.3/JanitorActivatorForm.cs @ 9527

Last change on this file since 9527 was 9527, checked in by pfleck, 10 years ago

#2063
Provides a GUI for SelfHosting Hive-Service to start and stop the service.
Enables start and stop Janitor with adjustable frequency.
Run Cleanup and GenerateStatistics separately on click.

File size: 5.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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
22using System;
23using System.ServiceModel;
24using System.Threading;
25using System.Threading.Tasks;
26using System.Windows.Forms;
27
28namespace HeuristicLab.Services.Hive.SelfHost {
29  public partial class JanitorActivatorForm : Form {
30    private static readonly Uri BaseAddress = new Uri(@"http://localhost/Hive-3.3/HiveService.svc");
31
32    private IEventManager eventManager {
33      get { return ServiceLocator.Instance.EventManager; }
34    }
35
36    private IStatisticsGenerator statisticsGenerator {
37      get { return ServiceLocator.Instance.StatisticsGenerator; }
38    }
39
40    private Thread janitorThread;
41
42    private bool janitorRunning;
43
44    private readonly EventWaitHandle janitorWaitHandle;
45
46    private readonly ServiceHost host;
47
48    private bool serviceRunning;
49
50    public JanitorActivatorForm() {
51      InitializeComponent();
52      janitorFrequency_ValueChanged(null, null);
53
54      janitorWaitHandle = new AutoResetEvent(false);
55
56      host = new ServiceHost(typeof(HiveService), BaseAddress);
57    }
58
59    private void activateJanitorButton_Click(object sender, EventArgs e) {
60      activateJanitorButton.Enabled = false;
61
62      try {
63        janitorRunning = !janitorRunning;
64
65        if (janitorRunning && janitorThread == null) {
66          janitorThread = new Thread(RunEventManager) {
67            IsBackground = true
68          };
69          janitorThread.Start();
70        }
71
72        janitorWaitHandle.Set();
73      }
74      catch (Exception exc) {
75        WriteLine(exc.Message);
76        WriteLine(exc.StackTrace);
77        janitorRunning = false;
78        janitorThread = null;
79      }
80
81      WriteLine(janitorRunning ? "Activated Janitor" : "Janitor deactivated");
82
83      activateJanitorButton.Text = janitorRunning ? "Stop Janitor" : "Start Janitor";
84
85      activateJanitorButton.Enabled = true;
86    }
87
88    private void RunEventManager() {
89      while (janitorRunning) {
90        int frequency = 0;
91        Invoke(new Action(() => frequency = janitorFrequency.Value));
92
93        janitorWaitHandle.WaitOne(frequency * 1000);
94
95        WriteLine("Run Janitor");
96        lock (eventManager) {
97          eventManager.Cleanup();
98        }
99        lock (statisticsGenerator) {
100          statisticsGenerator.GenerateStatistics();
101        }
102      }
103      janitorThread = null;
104    }
105
106    private async void runServiceButton_Click(object sender, EventArgs e) {
107      runServiceButton.Enabled = false;
108
109      try {
110        await StartService();
111
112        WriteLine(serviceRunning ? "Service is ready at " + BaseAddress : "Service is closed");
113      }
114      catch (Exception exc) {
115        WriteLine(exc.Message);
116        WriteLine(exc.StackTrace);
117        serviceRunning = false;
118      }
119
120      runServiceButton.Text = serviceRunning ? "Stop Service" : "Start Service";
121      runServiceButton.Enabled = true;
122    }
123
124    private Task StartService() {
125      return Task.Run(() => {
126        serviceRunning = !serviceRunning;
127
128        if (serviceRunning) {
129          host.Open();
130        } else {
131          host.Close();
132        }
133      });
134    }
135
136    private async void runJanitorButton_Click(object sender, EventArgs e) {
137      runCleanupButton.Enabled = false;
138
139      await Task.Run(() => {
140        lock (eventManager) {
141          WriteLine("Run Cleanup");
142          eventManager.Cleanup();
143        }
144      });
145
146      runCleanupButton.Enabled = true;
147    }
148
149    private async void generateStatisticsButton_Click(object sender, EventArgs e) {
150      generateStatisticsButton.Enabled = false;
151
152      await Task.Run(() => {
153        lock (eventManager) {
154          WriteLine("Generate Statistics");
155          statisticsGenerator.GenerateStatistics();
156        }
157      });
158
159      generateStatisticsButton.Enabled = true;
160    }
161
162    private void janitorFrequency_ValueChanged(object sender, EventArgs e) {
163      frequencyTextBox.Text = string.Format("{0} sek", janitorFrequency.Value);
164    }
165
166    private void WriteLine(string msg) {
167      if (InvokeRequired) {
168        Invoke(new Action<string>(WriteLine), msg);
169        return;
170      }
171      outputTextbox.AppendText(string.Format("{0}: {1}{2}", DateTime.Now.ToLongTimeString(), msg, Environment.NewLine));
172    }
173  }
174}
Note: See TracBrowser for help on using the repository browser.