Free cookie consent management tool by TermsFeed Policy Generator

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

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

improved the DAL further, changed minor details for the presentation (#830)

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