Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Services.Hive/3.3/Manager/EventManager.cs @ 14229

Last change on this file since 14229 was 14185, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

File size: 4.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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.Linq;
24using HeuristicLab.Services.Hive.DataAccess;
25using HeuristicLab.Services.Hive.DataAccess.Interfaces;
26
27namespace HeuristicLab.Services.Hive.Manager {
28  public class EventManager : IEventManager {
29    private const string SlaveTimeout = "Slave timed out.";
30    private IPersistenceManager PersistenceManager {
31      get { return ServiceLocator.Instance.PersistenceManager; }
32    }
33
34    public void Cleanup() {
35      var pm = PersistenceManager;
36      pm.UseTransaction(() => {
37        SetTimeoutSlavesOffline(pm);
38        SetTimeoutTasksWaiting(pm);
39        DeleteObsoleteSlaves(pm);
40        pm.SubmitChanges();
41      });
42
43      pm.UseTransaction(() => {
44        FinishParentTasks(pm);
45        pm.SubmitChanges();
46      });
47    }
48
49    /// <summary>
50    /// Searches for slaves which are timed out, puts them and their task offline
51    /// </summary>
52    private void SetTimeoutSlavesOffline(IPersistenceManager pm) {
53      var slaveDao = pm.SlaveDao;
54      var slaves = slaveDao.GetOnlineSlaves();
55      foreach (var slave in slaves) {
56        if (!slave.LastHeartbeat.HasValue ||
57            (DateTime.Now - slave.LastHeartbeat.Value) >
58            Properties.Settings.Default.SlaveHeartbeatTimeout) {
59          slave.SlaveState = SlaveState.Offline;
60        }
61      }
62    }
63
64    /// <summary>
65    /// Looks for parent tasks which have FinishWhenChildJobsFinished and set their state to finished
66    /// </summary>
67    private void FinishParentTasks(IPersistenceManager pm) {
68      var resourceDao = pm.ResourceDao;
69      var taskDao = pm.TaskDao;
70      var resourceIds = resourceDao.GetAll().Select(x => x.ResourceId).ToList();
71      var parentTasksToFinish = taskDao.GetParentTasks(resourceIds, 0, true);
72      foreach (var task in parentTasksToFinish) {
73        task.State = TaskState.Finished;
74        task.StateLogs.Add(new StateLog {
75          State = task.State,
76          SlaveId = null,
77          UserId = null,
78          Exception = string.Empty,
79          DateTime = DateTime.Now
80        });
81      }
82    }
83
84    /// <summary>
85    /// Looks for task which have not sent heartbeats for some time and reschedules them for calculation
86    /// </summary>
87    private void SetTimeoutTasksWaiting(IPersistenceManager pm) {
88      var taskDao = pm.TaskDao;
89      var tasks = taskDao.GetAll().Where(x => (x.State == TaskState.Calculating && (DateTime.Now - x.LastHeartbeat) > Properties.Settings.Default.CalculatingJobHeartbeatTimeout)
90                                           || (x.State == TaskState.Transferring && (DateTime.Now - x.LastHeartbeat) > Properties.Settings.Default.TransferringJobHeartbeatTimeout));
91      foreach (var task in tasks) {
92        task.State = TaskState.Waiting;
93        task.StateLogs.Add(new StateLog {
94          State = task.State,
95          SlaveId = null,
96          UserId = null,
97          Exception = SlaveTimeout,
98          DateTime = DateTime.Now
99        });
100        task.Command = null;
101      }
102    }
103
104    /// <summary>
105    /// Searches for slaves that are disposable and deletes them if they were offline for too long
106    /// </summary>
107    private void DeleteObsoleteSlaves(IPersistenceManager pm) {
108      var slaveDao = pm.SlaveDao;
109      var downtimeDao = pm.DowntimeDao;
110      var slaveIds = slaveDao.GetAll()
111        .Where(x => x.IsDisposable.GetValueOrDefault()
112                 && x.SlaveState == SlaveState.Offline
113                 && (DateTime.Now - x.LastHeartbeat) > Properties.Settings.Default.SweepInterval)
114        .Select(x => x.ResourceId)
115        .ToList();
116      foreach (var id in slaveIds) {
117        bool downtimesAvailable = downtimeDao.GetByResourceId(id).Any();
118        if (!downtimesAvailable) {
119          slaveDao.Delete(id);
120        }
121      }
122    }
123  }
124}
Note: See TracBrowser for help on using the repository browser.