Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.3-Hive/sources/HeuristicLab.Hive/HeuristicLab.Hive.Slave.Core/3.3/JobStorage/JobStorageManager.cs @ 4424

Last change on this file since 4424 was 4424, checked in by cneumuel, 14 years ago
  • Added and updated License Information in every file
  • Sort and remove usings in every file
  • Deleted obsolete DataAccess.ADOHelper
  • Deleted some obsolete files
File size: 5.0 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.Xml;
26using System.Xml.Serialization;
27using HeuristicLab.Hive.Contracts;
28using HeuristicLab.Hive.Contracts.ResponseObjects;
29using HeuristicLab.Hive.Slave.Communication;
30using HeuristicLab.Hive.Slave.Core.ConfigurationManager;
31using HeuristicLab.Tracing;
32
33namespace HeuristicLab.Hive.Slave.Core.JobStorage {
34  public class JobStorageManager {
35
36    private static List<JobStorageInfo> storedJobsList = new List<JobStorageInfo>();
37
38    private static String path = System.IO.Directory.GetCurrentDirectory() + "\\Hive.Slave.Jobs\\";
39
40    public static void PersistObjectToDisc(String serverIP, long serverPort, Guid jobId, byte[] job) {
41      String filename = serverIP + "." + serverPort + "." + jobId.ToString();
42      JobStorageInfo info = new JobStorageInfo { JobID = jobId, ServerIP = serverIP, ServerPort = serverPort, TimeFinished = DateTime.Now };
43
44      Stream jobstream = null;
45      try {
46        jobstream = File.Create(path + filename + ".dat");
47        jobstream.Write(job, 0, job.Length);
48        storedJobsList.Add(info);
49        Logger.Info("Job " + info.JobID + " stored on the harddisc");
50      }
51      catch (Exception e) {
52        Logger.Error("Exception: ", e);
53      }
54      finally {
55        if (jobstream != null)
56          jobstream.Close();
57      }
58
59      StoreJobList();
60    }
61
62    public static void CheckAndSubmitJobsFromDisc() {
63      for (int index = storedJobsList.Count; index > 0; index--) {
64        if (storedJobsList[index - 1].ServerIP == WcfService.Instance.ServerIp) {
65          String filename = storedJobsList[index - 1].ServerIP + "." + storedJobsList[index - 1].ServerPort + "." + storedJobsList[index - 1].JobID.ToString();
66          Logger.Info("Sending stored job " + storedJobsList[index - 1].JobID + " to the server");
67          try {
68            byte[] job = File.ReadAllBytes(path + filename + ".dat");
69            if (WcfService.Instance.IsJobStillNeeded(storedJobsList[index - 1].JobID).StatusMessage == ResponseStatus.Ok) {
70              ResponseResultReceived res = WcfService.Instance.StoreFinishedJobResultsSync(ConfigManager.Instance.GetClientInfo().Id, storedJobsList[index - 1].JobID, job, TimeSpan.Zero, null, true); // unfortunately we do not have the correct ExecutionTime since we would need to unzip the job for that
71              Logger.Info("Sending of job " + storedJobsList[index - 1].JobID + " done");
72            }
73            SlaveStatusInfo.JobsProcessed++;
74            storedJobsList.Remove(storedJobsList[index - 1]);
75            File.Delete(path + filename + ".dat");
76          }
77          catch (Exception e) {
78            Logger.Error("Job not on hdd but on list - deleting job from list ", e);
79            storedJobsList.Remove(storedJobsList[index - 1]);
80            StoreJobList();
81          }
82        }
83      }
84    }
85
86    public static void StoreJobList() {
87      XmlSerializer serializer = new XmlSerializer(typeof(List<JobStorageInfo>));
88      TextWriter writer = new StreamWriter(Path.Combine(path, "list.xml"));
89      serializer.Serialize(writer, storedJobsList);
90      writer.Close();
91    }
92
93    static JobStorageManager() {
94      Logger.Info("Restoring Joblist from Harddisk");
95      if (!Directory.Exists(path))
96        Directory.CreateDirectory(path);
97
98      XmlSerializer serializer = new XmlSerializer(typeof(List<JobStorageInfo>));
99      FileStream stream = null;
100      if (File.Exists(Path.Combine(path, "list.xml"))) {
101        try {
102          stream = new FileStream(Path.Combine(path, "list.xml"), FileMode.Open);
103          XmlTextReader reader = new XmlTextReader(stream);
104          storedJobsList = (List<JobStorageInfo>)serializer.Deserialize(reader);
105          Logger.Info("Loaded " + storedJobsList.Count + " Elements");
106        }
107        catch (Exception e) {
108          Logger.Error("Exception while loading the Stored Job List", e);
109        }
110        finally {
111          if (stream != null)
112            stream.Dispose();
113        }
114      } else {
115        Logger.Info("no stored jobs on harddisk, starting new list");
116        storedJobsList = new List<JobStorageInfo>();
117      }
118    }
119  }
120}
Note: See TracBrowser for help on using the repository browser.