Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 1916 was 1775, checked in by kgrading, 15 years ago

fixed small changes in logging, messagequeues and the jobfetching (#467)

File size: 3.6 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;
12
13namespace HeuristicLab.Hive.Client.Core.JobStorage {
14  public class JobStorageManager {
15   
16    private static List<JobStorageInfo> storedJobsList = new List<JobStorageInfo>();
17    //Todo: execution path
18    //Todo: Choose a better directory name
19    private static String path = "C:\\Program Files\\HeuristicLab 3.0\\plugins\\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      if (!Directory.Exists(path))
26        Directory.CreateDirectory(path);
27           
28      Stream jobstream = null;
29      try {
30        jobstream = File.Create(path + filename + ".dat");
31        jobstream.Write(job, 0, job.Length);
32        storedJobsList.Add(info);
33        Debug.WriteLine("Job " + info.JobID + " stored on the harddisc");
34        //Logging.Instance.Info("JobStorageManager", "Job " + info.JobID + " stored on the harddisc");
35      }
36      catch (Exception e) {
37        Logging.Instance.Error("JobStorageManager", "Exception: ", e);
38      }
39      finally {
40        if(jobstream!=null)
41          jobstream.Close();
42      }
43
44      StoreJobList();
45
46    }
47
48    public static void CheckAndSubmitJobsFromDisc() {
49      for(int index=storedJobsList.Count; index > 0; index--) {
50        if (WcfService.Instance.ConnState == NetworkEnum.WcfConnState.Loggedin && (storedJobsList[index-1].ServerIP == WcfService.Instance.ServerIP && storedJobsList[index-1].ServerPort == WcfService.Instance.ServerPort)) {
51          String filename = storedJobsList[index-1].ServerIP + "." + storedJobsList[index-1].ServerPort + "." + storedJobsList[index-1].JobID.ToString();
52          Debug.WriteLine("Sending stored job " + storedJobsList[index - 1].JobID + " to the server");
53          //Logging.Instance.Info("JobStorrageManager", "Sending stored job " + storedJobsList[index - 1].JobID + " to the server");
54          byte[] job = File.ReadAllBytes(path + filename + ".dat");
55         
56          //Todo: ask server first if he really wants the job...
57          ResponseResultReceived res = WcfService.Instance.SendStoredJobResultsSync(ConfigManager.Instance.GetClientInfo().Id, storedJobsList[index-1].JobID, job, 1.00, null, true);
58          ClientStatusInfo.JobsProcessed++;
59          //TODO: has to be fixed from server side
60          //if (res.Success == true) {
61          Debug.WriteLine("Sending of job " + storedJobsList[index - 1].JobID + " done"); 
62          //Logging.Instance.Info("JobStorrageManager", "Sending of job " + storedJobsList[index - 1].JobID + " done"); 
63
64          storedJobsList.Remove(storedJobsList[index - 1]);
65          File.Delete(path + filename + ".dat");
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  }
80}
Note: See TracBrowser for help on using the repository browser.