Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HiveStatistics/sources/HeuristicLab.Services.Hive/3.3/HiveJanitor.cs @ 12394

Last change on this file since 12394 was 11204, checked in by ascheibe, 10 years ago

merged only Hive changes from trunk into branch

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