Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 12789 was 12789, checked in by dglaser, 9 years ago

#2388:

HeuristicLab.Services.Access.DataAccess-3.3:

  • Added a new method to the TaskDao

HeuristicLab.Services.Hive-3.3:

  • Added NewEventManager.cs
  • Updated HiveJanitor.cs
  • Updated ServiceLocator.cs
File size: 3.8 KB
RevLine 
[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
22using System;
23using System.Threading;
24using HeuristicLab.Services.Hive.DataAccess;
[12468]25using HeuristicLab.Services.Hive.DataAccess.Interfaces;
[7189]26
27namespace HeuristicLab.Services.Hive {
28  public class HiveJanitor {
29    private bool stop;
[12441]30    private AutoResetEvent cleanupWaitHandle;
31    private AutoResetEvent generateStatisticsWaitHandle;
[7189]32
[12789]33    private IPersistenceManager PersistenceManager {
34      get { return ServiceLocator.Instance.PersistenceManager; }
[7189]35    }
[12789]36    private IEventManager EventManager {
[7189]37      get { return ServiceLocator.Instance.EventManager; }
38    }
39
[12789]40    private IStatisticsGenerator StatisticsGenerator {
[12441]41      get { return ServiceLocator.Instance.StatisticsGenerator; }
42    }
43
[7189]44    public HiveJanitor() {
45      stop = false;
[12484]46      cleanupWaitHandle = new AutoResetEvent(false);
47      generateStatisticsWaitHandle = new AutoResetEvent(false);
[7189]48    }
49
50    public void StopJanitor() {
51      stop = true;
[12441]52      cleanupWaitHandle.Set();
[12468]53      generateStatisticsWaitHandle.Set();
[7189]54    }
55
[12441]56    public void RunCleanup() {
[7189]57      while (!stop) {
58        try {
[12477]59          LogFactory.GetLogger(typeof(HiveJanitor).Namespace).Log("HiveJanitor: starting cleanup.");
[7189]60          bool cleanup = false;
[12789]61          using (var pm = PersistenceManager) {
62            var lifecycleDao = pm.LifecycleDao;
63            pm.UseTransaction(() => {
64              var lifecycle = lifecycleDao.GetLastLifecycle();
65              if (lifecycle == null
66                  || DateTime.Now - lifecycle.LastCleanup > HeuristicLab.Services.Hive.Properties.Settings.Default.CleanupInterval) {
67                lifecycleDao.UpdateLifecycle();
68                cleanup = true;
69              }
70              pm.SubmitChanges();
71            }, true);
72          }
[7189]73          if (cleanup) {
[12789]74            EventManager.Cleanup();
[7189]75          }
[12477]76          LogFactory.GetLogger(typeof(HiveJanitor).Namespace).Log("HiveJanitor: cleanup finished.");
[7189]77        }
78        catch (Exception e) {
79          LogFactory.GetLogger(typeof(HiveJanitor).Namespace).Log(string.Format("HiveJanitor: The following exception occured: {0}", e.ToString()));
80        }
[12441]81        cleanupWaitHandle.WaitOne(HeuristicLab.Services.Hive.Properties.Settings.Default.CleanupInterval);
[7189]82      }
[12441]83      cleanupWaitHandle.Close();
[7189]84    }
[12441]85
86    public void RunGenerateStatistics() {
87      while (!stop) {
88        try {
[12477]89          LogFactory.GetLogger(typeof(HiveJanitor).Namespace).Log("HiveJanitor: starting generate statistics.");
[12789]90          StatisticsGenerator.GenerateStatistics();
[12477]91          LogFactory.GetLogger(typeof(HiveJanitor).Namespace).Log("HiveJanitor: generate statistics finished.");
[12441]92        }
93        catch (Exception e) {
94          LogFactory.GetLogger(typeof(HiveJanitor).Namespace).Log(string.Format("HiveJanitor: The following exception occured: {0}", e));
95        }
96
97        generateStatisticsWaitHandle.WaitOne(Properties.Settings.Default.GenerateStatisticsInterval);
98      }
99
100      generateStatisticsWaitHandle.Close();
101    }
[7189]102  }
103}
104
105
106
Note: See TracBrowser for help on using the repository browser.