Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/tools/HeuristicLab.HiveDrain/HeuristicLab.HiveDrain/JobTaskOneFileDownloader.cs @ 15350

Last change on this file since 15350 was 15350, checked in by bburlacu, 5 years ago

#2829: JobTaskOneFileDownloader: Check for exception and add sleep timeout in log update loop.

File size: 5.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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.Linq;
25using System.Threading;
26using HeuristicLab.Clients.Hive;
27using HeuristicLab.Clients.Hive.Jobs;
28using HeuristicLab.Common;
29using HeuristicLab.Core;
30using HeuristicLab.Optimization;
31
32namespace HeuristicLab.HiveDrain {
33  public class JobTaskOneFileDownloader {
34    public String RootLocation { get; set; }
35
36    public Job ParentJob { get; set; }
37
38    private ILog log;
39
40    private RunCollection results = new RunCollection();
41
42    private static ConcurrentTaskDownloader<ItemTask> downloader =
43        new ConcurrentTaskDownloader<ItemTask>(HeuristicLabHiveDrainApplication.MaxParallelDownloads, HeuristicLabHiveDrainApplication.MaxParallelDownloads);
44
45    private static int jobCount = 0;
46
47    private static bool endReached = false;
48
49    private ManualResetEvent allJobsFinished = new ManualResetEvent(false);
50
51    private Semaphore limitSemaphore = null;
52
53    static JobTaskOneFileDownloader() {
54      downloader.ExceptionOccured += downloader_ExceptionOccured;
55    }
56
57    static void downloader_ExceptionOccured(object sender, HeuristicLab.Common.EventArgs<Exception> e) {
58      HiveDrainMainWindow.Log.LogMessage(DateTime.Now.ToShortTimeString() + " ### Exception occured: " + e.Value);
59    }
60
61    public JobTaskOneFileDownloader(string path, Job parentJob, Semaphore sem, ILog log) {
62      RootLocation = path + ".hl";
63      ParentJob = parentJob;
64      limitSemaphore = sem;
65      this.log = log;
66    }
67
68    public void Start() {
69      results = new RunCollection();
70
71      var allTasks = HiveServiceLocator.Instance.CallHiveService(s => s.GetLightweightJobTasksWithoutStateLog(ParentJob.Id));
72      var totalJobCount = allTasks.Count();
73      var optimizers = new List<IOptimizer>();
74      var finishedCount = -1;
75      using (var downloader = new TaskDownloader(allTasks.Select(x => x.Id))) {
76        downloader.StartAsync();
77
78        while (!downloader.IsFinished || finishedCount < totalJobCount) {
79          if (finishedCount != downloader.FinishedCount) {
80            finishedCount = downloader.FinishedCount;
81            log.LogMessage(string.Format("Downloading/deserializing tasks... ({0}/{1} finished)", finishedCount, totalJobCount));
82          }
83
84          Thread.Sleep(500);
85
86          if (downloader.IsFaulted) {
87            throw downloader.Exception;
88          }
89        }
90
91        IDictionary<Guid, HiveTask> allHiveTasks = downloader.Results;
92        log.LogMessage("Building hive job tree...");
93        var parentTasks = allHiveTasks.Values.Where(x => !x.Task.ParentTaskId.HasValue);
94
95        foreach (var parentTask in parentTasks) {
96          BuildHiveJobTree(parentTask, allTasks, allHiveTasks);
97
98          var optimizerTask = parentTask.ItemTask as OptimizerTask;
99
100          if (optimizerTask != null) {
101            optimizers.Add(optimizerTask.Item);
102          }
103        }
104      }
105      if (!optimizers.Any()) return;
106      IStorableContent storable;
107      if (optimizers.Count > 1) {
108        var experiment = new Experiment();
109        experiment.Optimizers.AddRange(optimizers);
110        storable = experiment;
111      } else {
112        var optimizer = optimizers.First();
113        storable = optimizer as IStorableContent;
114      }
115      if (storable != null) {
116        log.LogMessage(string.Format("Save job as {0}", RootLocation));
117        ContentManager.Save(storable, RootLocation, true);
118      } else {
119        log.LogMessage(string.Format("Could not save job, content is not storable."));
120      }
121    }
122
123    private static void BuildHiveJobTree(HiveTask parentHiveTask, IEnumerable<LightweightTask> allTasks, IDictionary<Guid, HiveTask> allHiveTasks) {
124      IEnumerable<LightweightTask> childTasks = from job in allTasks
125                                                where job.ParentTaskId.HasValue && job.ParentTaskId.Value == parentHiveTask.Task.Id
126                                                orderby job.DateCreated ascending
127                                                select job;
128      foreach (LightweightTask task in childTasks) {
129        HiveTask childHiveTask = allHiveTasks[task.Id];
130        BuildHiveJobTree(childHiveTask, allTasks, allHiveTasks);
131        parentHiveTask.AddChildHiveTask(childHiveTask);
132      }
133    }
134  }
135}
Note: See TracBrowser for help on using the repository browser.