[7189] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[7259] | 3 | * Copyright (C) 2002-2012 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;
|
---|
[9215] | 25 | using System.Diagnostics;
|
---|
[7189] | 26 |
|
---|
| 27 | namespace HeuristicLab.Services.Hive {
|
---|
| 28 | public class HiveJanitor {
|
---|
| 29 | private bool stop;
|
---|
| 30 | private AutoResetEvent waitHandle;
|
---|
| 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 IHiveDao dao {
|
---|
| 41 | get { return ServiceLocator.Instance.HiveDao; }
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | public HiveJanitor() {
|
---|
| 45 | stop = false;
|
---|
| 46 | waitHandle = new AutoResetEvent(true);
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | public void StopJanitor() {
|
---|
| 50 | stop = true;
|
---|
| 51 | waitHandle.Set();
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | public void Run() {
|
---|
| 55 | while (!stop) {
|
---|
| 56 | try {
|
---|
| 57 | LogFactory.GetLogger(typeof(HiveJanitor).Namespace).Log("HiveJanitor: starting cleanup");
|
---|
[9215] | 58 | Trace.WriteLine("HiveJanitor: starting cleanup");
|
---|
[7189] | 59 | bool cleanup = false;
|
---|
[8266] | 60 | DateTime lastCleanup = DateTime.MinValue;
|
---|
[7189] | 61 | trans.UseTransaction(() => {
|
---|
[8266] | 62 | lastCleanup = dao.GetLastCleanup();
|
---|
[7189] | 63 | }, true);
|
---|
[8266] | 64 | if (DateTime.Now - lastCleanup > HeuristicLab.Services.Hive.Properties.Settings.Default.CleanupInterval) {
|
---|
| 65 | trans.UseTransaction(() => { dao.SetLastCleanup(DateTime.Now); }, true);
|
---|
| 66 | dao.SetLastCleanup(DateTime.Now);
|
---|
| 67 | cleanup = true;
|
---|
| 68 | }
|
---|
[7189] | 69 |
|
---|
| 70 | if (cleanup) {
|
---|
| 71 | eventManager.Cleanup();
|
---|
| 72 | }
|
---|
| 73 | LogFactory.GetLogger(typeof(HiveJanitor).Namespace).Log("HiveJanitor: cleanup finished");
|
---|
[9215] | 74 | Trace.WriteLine("HiveJanitor: cleanup finished");
|
---|
[7189] | 75 | }
|
---|
| 76 | catch (Exception e) {
|
---|
| 77 | LogFactory.GetLogger(typeof(HiveJanitor).Namespace).Log(string.Format("HiveJanitor: The following exception occured: {0}", e.ToString()));
|
---|
[9215] | 78 | Trace.WriteLine(string.Format("HiveJanitor: The following exception occured: {0}", e.ToString()));
|
---|
[7189] | 79 | }
|
---|
| 80 | waitHandle.WaitOne(HeuristicLab.Services.Hive.Properties.Settings.Default.CleanupInterval);
|
---|
| 81 | }
|
---|
| 82 | waitHandle.Close();
|
---|
| 83 | }
|
---|
| 84 | }
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 |
|
---|
| 88 |
|
---|