[7189] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[12012] | 3 | * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[7189] | 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.Services.Hive.DataAccess;
|
---|
[12468] | 25 | using HeuristicLab.Services.Hive.DataAccess.Interfaces;
|
---|
[7189] | 26 |
|
---|
| 27 | namespace HeuristicLab.Services.Hive {
|
---|
| 28 | public class HiveJanitor {
|
---|
| 29 | private bool stop;
|
---|
[12441] | 30 | private AutoResetEvent cleanupWaitHandle;
|
---|
| 31 | private AutoResetEvent generateStatisticsWaitHandle;
|
---|
[7189] | 32 |
|
---|
[12468] | 33 | private ITransactionManager trans {
|
---|
[7189] | 34 | get { return ServiceLocator.Instance.TransactionManager; }
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | private IEventManager eventManager {
|
---|
| 38 | get { return ServiceLocator.Instance.EventManager; }
|
---|
| 39 | }
|
---|
| 40 |
|
---|
[12441] | 41 | private IStatisticsGenerator statisticsGenerator {
|
---|
| 42 | get { return ServiceLocator.Instance.StatisticsGenerator; }
|
---|
| 43 | }
|
---|
| 44 |
|
---|
[7189] | 45 | private IHiveDao dao {
|
---|
| 46 | get { return ServiceLocator.Instance.HiveDao; }
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | public HiveJanitor() {
|
---|
| 50 | stop = false;
|
---|
[12441] | 51 | cleanupWaitHandle = new AutoResetEvent(true);
|
---|
| 52 | generateStatisticsWaitHandle = new AutoResetEvent(true);
|
---|
[7189] | 53 | }
|
---|
| 54 |
|
---|
| 55 | public void StopJanitor() {
|
---|
| 56 | stop = true;
|
---|
[12441] | 57 | cleanupWaitHandle.Set();
|
---|
[12468] | 58 | generateStatisticsWaitHandle.Set();
|
---|
[7189] | 59 | }
|
---|
| 60 |
|
---|
[12441] | 61 | public void RunCleanup() {
|
---|
[7189] | 62 | while (!stop) {
|
---|
| 63 | try {
|
---|
| 64 | LogFactory.GetLogger(typeof(HiveJanitor).Namespace).Log("HiveJanitor: starting cleanup");
|
---|
| 65 | bool cleanup = false;
|
---|
| 66 | trans.UseTransaction(() => {
|
---|
| 67 | DateTime lastCleanup = dao.GetLastCleanup();
|
---|
| 68 | if (DateTime.Now - lastCleanup > HeuristicLab.Services.Hive.Properties.Settings.Default.CleanupInterval) {
|
---|
| 69 | dao.SetLastCleanup(DateTime.Now);
|
---|
| 70 | cleanup = true;
|
---|
| 71 | }
|
---|
| 72 | }, true);
|
---|
| 73 |
|
---|
| 74 | if (cleanup) {
|
---|
| 75 | eventManager.Cleanup();
|
---|
| 76 | }
|
---|
| 77 | LogFactory.GetLogger(typeof(HiveJanitor).Namespace).Log("HiveJanitor: cleanup finished");
|
---|
| 78 | }
|
---|
| 79 | catch (Exception e) {
|
---|
| 80 | LogFactory.GetLogger(typeof(HiveJanitor).Namespace).Log(string.Format("HiveJanitor: The following exception occured: {0}", e.ToString()));
|
---|
| 81 | }
|
---|
[12441] | 82 | cleanupWaitHandle.WaitOne(HeuristicLab.Services.Hive.Properties.Settings.Default.CleanupInterval);
|
---|
[7189] | 83 | }
|
---|
[12441] | 84 | cleanupWaitHandle.Close();
|
---|
[7189] | 85 | }
|
---|
[12441] | 86 |
|
---|
| 87 | public void RunGenerateStatistics() {
|
---|
| 88 | while (!stop) {
|
---|
| 89 | try {
|
---|
| 90 | LogFactory.GetLogger(typeof(HiveJanitor).Namespace).Log("HiveJanitor: starting generate statistics");
|
---|
| 91 | statisticsGenerator.GenerateStatistics();
|
---|
| 92 | LogFactory.GetLogger(typeof(HiveJanitor).Namespace).Log("HiveJanitor: generate statistics finished");
|
---|
| 93 | }
|
---|
| 94 | catch (Exception e) {
|
---|
| 95 | LogFactory.GetLogger(typeof(HiveJanitor).Namespace).Log(string.Format("HiveJanitor: The following exception occured: {0}", e));
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | generateStatisticsWaitHandle.WaitOne(Properties.Settings.Default.GenerateStatisticsInterval);
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | generateStatisticsWaitHandle.Close();
|
---|
| 102 | }
|
---|
[7189] | 103 | }
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 |
|
---|
| 107 |
|
---|