Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4772 was 4772, checked in by cneumuel, 13 years ago

#1260

  • added LogServiceReader to display log for slave without writing to local files
  • aborted jobs with childjobs now got back to state WaitForChildJob (instead of Offline)
  • lifecyclemanager now knows about available plugins (does not yet work perfectly)
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;
31
32namespace HeuristicLab.Hive.Slave.Core.JobStorage {
33  public class JobStorageManager {
34
35    private static List<JobStorageInfo> storedJobsList = new List<JobStorageInfo>();
36
37    private static String path = System.IO.Directory.GetCurrentDirectory() + "\\Hive.Slave.Jobs\\";
38
39    public static void PersistObjectToDisc(String serverIP, long serverPort, Guid jobId, byte[] job) {
40      String filename = serverIP + "." + serverPort + "." + jobId.ToString();
41      JobStorageInfo info = new JobStorageInfo { JobID = jobId, ServerIP = serverIP, ServerPort = serverPort, TimeFinished = DateTime.Now };
42
43      Stream jobstream = null;
44      try {
45        jobstream = File.Create(path + filename + ".dat");
46        jobstream.Write(job, 0, job.Length);
47        storedJobsList.Add(info);
48        Logger.Info("Job " + info.JobID + " stored on the harddisc");
49      }
50      catch (Exception e) {
51        Logger.Error("Exception: ", e);
52      }
53      finally {
54        if (jobstream != null)
55          jobstream.Close();
56      }
57
58      StoreJobList();
59    }
60
61    public static void CheckAndSubmitJobsFromDisc() {
62      for (int index = storedJobsList.Count; index > 0; index--) {
63        if (storedJobsList[index - 1].ServerIP == WcfService.Instance.ServerIp) {
64          String filename = storedJobsList[index - 1].ServerIP + "." + storedJobsList[index - 1].ServerPort + "." + storedJobsList[index - 1].JobID.ToString();
65          Logger.Info("Sending stored job " + storedJobsList[index - 1].JobID + " to the server");
66          try {
67            byte[] job = File.ReadAllBytes(path + filename + ".dat");
68            if (WcfService.Instance.IsJobStillNeeded(storedJobsList[index - 1].JobID).StatusMessage == ResponseStatus.Ok) {
69              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
70              Logger.Info("Sending of job " + storedJobsList[index - 1].JobID + " done");
71            }
72            SlaveStatusInfo.JobsProcessed++;
73            storedJobsList.Remove(storedJobsList[index - 1]);
74            File.Delete(path + filename + ".dat");
75          }
76          catch (Exception e) {
77            Logger.Error("Job not on hdd but on list - deleting job from list ", e);
78            storedJobsList.Remove(storedJobsList[index - 1]);
79            StoreJobList();
80          }
81        }
82      }
83    }
84
85    public static void StoreJobList() {
86      XmlSerializer serializer = new XmlSerializer(typeof(List<JobStorageInfo>));
87      TextWriter writer = new StreamWriter(Path.Combine(path, "list.xml"));
88      serializer.Serialize(writer, storedJobsList);
89      writer.Close();
90    }
91
92    static JobStorageManager() {
93      Logger.Info("Restoring Joblist from Harddisk");
94      if (!Directory.Exists(path))
95        Directory.CreateDirectory(path);
96
97      XmlSerializer serializer = new XmlSerializer(typeof(List<JobStorageInfo>));
98      FileStream stream = null;
99      if (File.Exists(Path.Combine(path, "list.xml"))) {
100        try {
101          stream = new FileStream(Path.Combine(path, "list.xml"), FileMode.Open);
102          XmlTextReader reader = new XmlTextReader(stream);
103          storedJobsList = (List<JobStorageInfo>)serializer.Deserialize(reader);
104          Logger.Info("Loaded " + storedJobsList.Count + " Elements");
105        }
106        catch (Exception e) {
107          Logger.Error("Exception while loading the Stored Job List", e);
108        }
109        finally {
110          if (stream != null)
111            stream.Dispose();
112        }
113      } else {
114        Logger.Info("no stored jobs on harddisk, starting new list");
115        storedJobsList = new List<JobStorageInfo>();
116      }
117    }
118  }
119}
Note: See TracBrowser for help on using the repository browser.