Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive/sources/HeuristicLab.Hive/HeuristicLab.Hive.Slave.Core/3.3/ConfigurationManager/UptimeManager.cs @ 4772

Last change on this file since 4772 was 4772, checked in by cneumuel, 13 years ago

#1260

  • added LogServiceReader to display log for slave without writing to local files
  • aborted jobs with childjobs now got back to state WaitForChildJob (instead of Offline)
  • lifecyclemanager now knows about available plugins (does not yet work perfectly)
File size: 4.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.Collections.Generic;
24using System.IO;
25using System.Linq;
26using System.Xml.Serialization;
27using HeuristicLab.Calendar;
28using HeuristicLab.Hive.Contracts.BusinessObjects;
29using HeuristicLab.Hive.Contracts.ResponseObjects;
30
31namespace HeuristicLab.Hive.Slave.Core.ConfigurationManager {
32  public class UptimeManager {
33
34    private AppointmentContainer appContainer = null;
35    public AppointmentContainer AppContainer {
36      get {
37        if (appContainer == null)
38          RestoreFromHDD();
39        return appContainer;
40      }
41    }   
42    public bool CalendarAvailable { get; set; }
43
44    private static String path = System.IO.Directory.GetCurrentDirectory() + "\\plugins\\Hive.Slave.Jobs\\";
45
46    private static UptimeManager instance = null;
47    public static UptimeManager Instance {
48      get {
49        if (instance == null) {
50          instance = new UptimeManager();
51        }
52        return instance;
53      }
54    }
55
56    public UptimeManager() {
57      RestoreFromHDD();
58    }
59
60    private void PersistToHDD() {
61      XmlSerializer s = new XmlSerializer(typeof(AppointmentContainer));
62      if (!Directory.Exists(path))
63        Directory.CreateDirectory(path);
64      TextWriter w = null;
65      try {
66        w = new StreamWriter(path + "calendar.xml");
67        s.Serialize(w, AppContainer);
68      }
69      catch (Exception e) {
70        Logger.Error("Persistance of the Calendar failed!", e);
71      }
72      finally {
73        if (w != null)
74          w.Close();
75      }
76    }
77
78    private void RestoreFromHDD() {
79      XmlSerializer s = new XmlSerializer(typeof(AppointmentContainer));
80      if (File.Exists(Path.Combine(path, "calendar.xml"))) {
81        TextReader r = null;
82
83        try {
84          r = new StreamReader(path + "calendar.xml");
85          appContainer = (AppointmentContainer)s.Deserialize(r);
86          CalendarAvailable = true;
87        }
88        catch (Exception e) {
89          Logger.Error("Deserialization of Calendar failed", e);
90          Logger.Info("Starting with a new one");
91          appContainer = new AppointmentContainer();
92          CalendarAvailable = false;
93        }
94        finally {
95          if (r != null)
96            r.Close();
97        }
98      } else {
99        appContainer = new AppointmentContainer();
100      }
101    }
102
103    public bool IsAllowedToCalculate() {
104      return AppContainer.Appointments.Any(app => (DateTime.Now >= app.StartDate) && (DateTime.Now <= app.EndDate));
105    }
106
107    public bool SetAppointments(bool isLocal, bool isForced, IEnumerable<Appointment> appointments) {
108      if (!isForced && !isLocal && AppContainer.IsLocal)
109        return false;
110
111      AppContainer.Appointments = new List<Appointment>(appointments);
112      AppContainer.IsLocal = isLocal;
113      AppContainer.Updated = DateTime.Now;
114      CalendarAvailable = true;
115     
116      PersistToHDD();
117     
118      return true;
119    }
120
121    internal bool SetAppointments(bool isLocal, ResponseCalendar response) {
122      IList<Appointment> app = new List<Appointment>();
123      foreach (AppointmentDto appointmentDto in response.Appointments) {
124        app.Add(new Appointment {
125          AllDayEvent = appointmentDto.AllDayEvent,
126          EndDate = appointmentDto.EndDate,
127          StartDate = appointmentDto.StartDate,
128          Recurring = appointmentDto.Recurring,
129
130          RecurringId = appointmentDto.RecurringId,
131          Locked = true,
132          Subject = "Online",
133        });
134      }
135      return SetAppointments(isLocal, response.ForceFetch, app);
136    }
137  }
138}
Note: See TracBrowser for help on using the repository browser.