Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Hive.Client.Core/3.2/JobStorage/JobStorageManager.cs @ 3578

Last change on this file since 3578 was 3578, checked in by kgrading, 14 years ago

Removed References to HiveLogging and updated the default logging mechanism (#991)

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