Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4337 was 4337, checked in by cneumuel, 14 years ago

changed Slave.Core WCF-Proxy to stateless object

File size: 4.2 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.IO;
6using HeuristicLab.Hive.Slave.Common;
7using HeuristicLab.Hive.Slave.Communication;
8using HeuristicLab.Hive.Slave.Core.ConfigurationManager;
9using HeuristicLab.Hive.Contracts;
10using System.Xml.Serialization;
11using System.Diagnostics;
12using System.Xml;
13using HeuristicLab.Tracing;
14using HeuristicLab.Hive.Contracts.ResponseObjects;
15
16namespace HeuristicLab.Hive.Slave.Core.JobStorage {
17  public class JobStorageManager {
18
19    private static List<JobStorageInfo> storedJobsList = new List<JobStorageInfo>();
20
21    private static String path = System.IO.Directory.GetCurrentDirectory() + "\\Hive.Slave.Jobs\\";
22
23    public static void PersistObjectToDisc(String serverIP, long serverPort, Guid jobId, byte[] job) {
24      String filename = serverIP + "." + serverPort + "." + jobId.ToString();
25      JobStorageInfo info = new JobStorageInfo { JobID = jobId, ServerIP = serverIP, ServerPort = serverPort, TimeFinished = DateTime.Now };
26
27      Stream jobstream = null;
28      try {
29        jobstream = File.Create(path + filename + ".dat");
30        jobstream.Write(job, 0, job.Length);
31        storedJobsList.Add(info);
32        Logger.Info("Job " + info.JobID + " stored on the harddisc");
33      }
34      catch (Exception e) {
35        Logger.Error("Exception: ", e);
36      }
37      finally {
38        if (jobstream != null)
39          jobstream.Close();
40      }
41
42      StoreJobList();
43    }
44
45    public static void CheckAndSubmitJobsFromDisc() {
46      for (int index = storedJobsList.Count; index > 0; index--) {
47        if (WcfService.Instance.LoggedIn && (storedJobsList[index - 1].ServerIP == WcfService.Instance.ServerIp)) {
48          String filename = storedJobsList[index - 1].ServerIP + "." + storedJobsList[index - 1].ServerPort + "." + storedJobsList[index - 1].JobID.ToString();
49          Logger.Info("Sending stored job " + storedJobsList[index - 1].JobID + " to the server");
50          try {
51            byte[] job = File.ReadAllBytes(path + filename + ".dat");
52            if (WcfService.Instance.IsJobStillNeeded(storedJobsList[index - 1].JobID).StatusMessage == ResponseStatus.Ok) {
53              ResponseResultReceived res = WcfService.Instance.StoreFinishedJobResultsSync(ConfigManager.Instance.GetClientInfo().Id, storedJobsList[index - 1].JobID, job, 1.00, null, true);
54              Logger.Info("Sending of job " + storedJobsList[index - 1].JobID + " done");
55            }
56            SlaveStatusInfo.JobsProcessed++;
57            storedJobsList.Remove(storedJobsList[index - 1]);
58            File.Delete(path + filename + ".dat");
59          }
60          catch (Exception e) {
61            Logger.Error("Job not on hdd but on list - deleting job from list ", e);
62            storedJobsList.Remove(storedJobsList[index - 1]);
63            StoreJobList();
64          }
65        }
66      }
67    }
68
69    public static void StoreJobList() {
70      XmlSerializer serializer = new XmlSerializer(typeof(List<JobStorageInfo>));
71      TextWriter writer = new StreamWriter(Path.Combine(path, "list.xml"));
72      serializer.Serialize(writer, storedJobsList);
73      writer.Close();
74    }
75
76    static JobStorageManager() {
77      Logger.Info("Restoring Joblist from Harddisk");
78      if (!Directory.Exists(path))
79        Directory.CreateDirectory(path);
80
81      XmlSerializer serializer = new XmlSerializer(typeof(List<JobStorageInfo>));
82      FileStream stream = null;
83      if (File.Exists(Path.Combine(path, "list.xml"))) {
84        try {
85          stream = new FileStream(Path.Combine(path, "list.xml"), FileMode.Open);
86          XmlTextReader reader = new XmlTextReader(stream);
87          storedJobsList = (List<JobStorageInfo>)serializer.Deserialize(reader);
88          Logger.Info("Loaded " + storedJobsList.Count + " Elements");
89        }
90        catch (Exception e) {
91          Logger.Error("Exception while loading the Stored Job List", e);
92        }
93        finally {
94          if (stream != null)
95            stream.Dispose();
96        }
97      } else {
98        Logger.Info("no stored jobs on harddisk, starting new list");
99        storedJobsList = new List<JobStorageInfo>();
100      }
101    }
102  }
103}
Note: See TracBrowser for help on using the repository browser.