Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Services.Hive/3.3/HiveJanitor.cs @ 14451

Last change on this file since 14451 was 14186, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

File size: 3.7 KB
RevLine 
[7189]1#region License Information
2/* HeuristicLab
[14186]3 * Copyright (C) 2002-2016 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;
[12962]25using HeuristicLab.Services.Hive.DataAccess.Interfaces;
[7189]26
27namespace HeuristicLab.Services.Hive {
28  public class HiveJanitor {
29    private bool stop;
[12962]30    private AutoResetEvent cleanupWaitHandle;
31    private AutoResetEvent generateStatisticsWaitHandle;
[7189]32
[12962]33    private IPersistenceManager PersistenceManager {
34      get { return ServiceLocator.Instance.PersistenceManager; }
[7189]35    }
[12962]36    private IEventManager EventManager {
[7189]37      get { return ServiceLocator.Instance.EventManager; }
38    }
39
[12962]40    private IStatisticsGenerator StatisticsGenerator {
41      get { return ServiceLocator.Instance.StatisticsGenerator; }
[7189]42    }
43
44    public HiveJanitor() {
45      stop = false;
[12962]46      cleanupWaitHandle = new AutoResetEvent(false);
47      generateStatisticsWaitHandle = new AutoResetEvent(false);
[7189]48    }
49
50    public void StopJanitor() {
51      stop = true;
[12962]52      cleanupWaitHandle.Set();
53      generateStatisticsWaitHandle.Set();
[7189]54    }
55
[12962]56    public void RunCleanup() {
57      var pm = PersistenceManager;
[7189]58      while (!stop) {
59        try {
[12962]60          LogFactory.GetLogger(typeof(HiveJanitor).Namespace).Log("HiveJanitor: starting cleanup.");
[7189]61          bool cleanup = false;
[12962]62
63          var lifecycleDao = pm.LifecycleDao;
64          pm.UseTransaction(() => {
65            var lifecycle = lifecycleDao.GetLastLifecycle();
66            if (lifecycle == null
67                || DateTime.Now - lifecycle.LastCleanup > Properties.Settings.Default.CleanupInterval) {
68              lifecycleDao.UpdateLifecycle();
[7189]69              cleanup = true;
70            }
[12962]71            pm.SubmitChanges();
[7189]72          }, true);
73
74          if (cleanup) {
[12962]75            EventManager.Cleanup();
[7189]76          }
[12962]77          LogFactory.GetLogger(typeof(HiveJanitor).Namespace).Log("HiveJanitor: cleanup finished.");
[7189]78        }
79        catch (Exception e) {
80          LogFactory.GetLogger(typeof(HiveJanitor).Namespace).Log(string.Format("HiveJanitor: The following exception occured: {0}", e.ToString()));
81        }
[12962]82        cleanupWaitHandle.WaitOne(Properties.Settings.Default.CleanupInterval);
[7189]83      }
[12962]84      cleanupWaitHandle.Close();
[7189]85    }
[12962]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
Note: See TracBrowser for help on using the repository browser.