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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.IO;
|
---|
25 | using System.Threading;
|
---|
26 | using HeuristicLab.Clients.Hive;
|
---|
27 | using HeuristicLab.Clients.Hive.Jobs;
|
---|
28 | using HeuristicLab.Common;
|
---|
29 | using HeuristicLab.Core;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.HiveDrain {
|
---|
32 | /// <summary>
|
---|
33 | /// downloads all finished tasks for a job
|
---|
34 | /// </summary>
|
---|
35 | public class JobTaskDownloader {
|
---|
36 | public String RootLocation { get; set; }
|
---|
37 | public Job ParentJob { get; set; }
|
---|
38 | private ILog log;
|
---|
39 |
|
---|
40 | private static ConcurrentTaskDownloader<ItemTask> downloader =
|
---|
41 | new ConcurrentTaskDownloader<ItemTask>(HeuristicLabHiveDrainApplication.MaxParallelDownloads, HeuristicLabHiveDrainApplication.MaxParallelDownloads);
|
---|
42 |
|
---|
43 | private static int jobCount = 0;
|
---|
44 | private static bool endReached = false;
|
---|
45 | private ManualResetEvent allJobsFinished = new ManualResetEvent(false);
|
---|
46 |
|
---|
47 | private Semaphore limitSemaphore = null;
|
---|
48 |
|
---|
49 | static JobTaskDownloader() {
|
---|
50 | downloader.ExceptionOccured += new EventHandler<HeuristicLab.Common.EventArgs<Exception>>(downloader_ExceptionOccured);
|
---|
51 | }
|
---|
52 |
|
---|
53 | static void downloader_ExceptionOccured(object sender, HeuristicLab.Common.EventArgs<Exception> e) {
|
---|
54 | HiveDrainMainWindow.Log.LogMessage(DateTime.Now.ToShortTimeString() + " ### Exception occured: " + e.Value.ToString());
|
---|
55 | }
|
---|
56 |
|
---|
57 | /// <summary>
|
---|
58 | /// constructor
|
---|
59 | /// </summary>
|
---|
60 | /// <param name="path">root path for this job</param>
|
---|
61 | /// <param name="parentJob">parent job</param>
|
---|
62 | public JobTaskDownloader(string path, Job parentJob, Semaphore sem, ILog log) {
|
---|
63 | RootLocation = path;
|
---|
64 | ParentJob = parentJob;
|
---|
65 | limitSemaphore = sem;
|
---|
66 | this.log = log;
|
---|
67 | }
|
---|
68 |
|
---|
69 | /// <summary>
|
---|
70 | /// start downloading all finished tasks for the parentjob
|
---|
71 | /// </summary>
|
---|
72 | public void Start() {
|
---|
73 | string taskPath;
|
---|
74 |
|
---|
75 | IEnumerable<LightweightTask> allTasks;
|
---|
76 | allTasks = HiveServiceLocator.Instance.CallHiveService(s =>
|
---|
77 | s.GetLightweightJobTasksWithoutStateLog(ParentJob.Id));
|
---|
78 |
|
---|
79 | foreach (var lightTask in allTasks) {
|
---|
80 | if (lightTask.State == TaskState.Finished) {
|
---|
81 | if (!CheckIfTaskDownloaded(lightTask.Id, out taskPath)) {
|
---|
82 | AddDownloaderTask(lightTask.Id, taskPath);
|
---|
83 | log.LogMessage(String.Format(" Getting Id {0}: {1}", lightTask.Id, DateTime.Now.ToShortTimeString()));
|
---|
84 | } else
|
---|
85 | log.LogMessage(String.Format(" {0} => already downloaded", lightTask.Id));
|
---|
86 | } else
|
---|
87 | log.LogMessage(String.Format(" {0} => ignored ({1})", lightTask.Id, lightTask.State.ToString()));
|
---|
88 | }
|
---|
89 | endReached = true;
|
---|
90 | if (jobCount == 0)
|
---|
91 | allJobsFinished.Set();
|
---|
92 |
|
---|
93 | allJobsFinished.WaitOne();
|
---|
94 |
|
---|
95 | GC.Collect();
|
---|
96 | log.LogMessage(String.Format("All tasks for job {0} finished", ParentJob.Name));
|
---|
97 | }
|
---|
98 |
|
---|
99 | /// <summary>
|
---|
100 | /// adds a task with state finished to the downloader
|
---|
101 | /// </summary>
|
---|
102 | /// <param name="taskId"></param>
|
---|
103 | /// <param name="taskPath"></param>
|
---|
104 | private void AddDownloaderTask(Guid taskId, string taskPath) {
|
---|
105 | //wait for free slot
|
---|
106 | limitSemaphore.WaitOne();
|
---|
107 |
|
---|
108 | Interlocked.Increment(ref jobCount);
|
---|
109 | downloader.DownloadTaskDataAndTask(taskId, (task, itemTask) => {
|
---|
110 |
|
---|
111 |
|
---|
112 | log.LogMessage(String.Format("\"{0}\" - [{1}]: {2} finished", ParentJob.Name, task.Id, itemTask.Name));
|
---|
113 |
|
---|
114 |
|
---|
115 | //start serialize job
|
---|
116 | if (itemTask is OptimizerTask) {
|
---|
117 | OptimizerTask optimizerTask = itemTask as OptimizerTask;
|
---|
118 |
|
---|
119 | //add task to serializer queue
|
---|
120 | TaskSerializer.Serialize(new SerializerTask() {
|
---|
121 | Content = optimizerTask.Item as IStorableContent,
|
---|
122 | FilePath = taskPath,
|
---|
123 | OnSaved = () => {
|
---|
124 | log.LogMessage(String.Format("\"{0}\" - [{1}]: {2} saved", ParentJob.Name, task.Id, itemTask.Name));
|
---|
125 | limitSemaphore.Release();
|
---|
126 | }
|
---|
127 | });
|
---|
128 | } else {
|
---|
129 | throw new InvalidOperationException(
|
---|
130 | String.Format("Unsupported task type {0}", itemTask.GetType().Name));
|
---|
131 | }
|
---|
132 |
|
---|
133 | //this job has finished downloading
|
---|
134 | Interlocked.Decrement(ref jobCount);
|
---|
135 |
|
---|
136 | //if this was the last job
|
---|
137 | if (jobCount == 0 && endReached)
|
---|
138 | allJobsFinished.Set();
|
---|
139 | });
|
---|
140 | }
|
---|
141 |
|
---|
142 | /// <summary>
|
---|
143 | /// check if there is a task directory which is not empty
|
---|
144 | /// </summary>
|
---|
145 | /// <param name="id"></param>
|
---|
146 | /// <param name="taskPath"></param>
|
---|
147 | /// <returns></returns>
|
---|
148 | private bool CheckIfTaskDownloaded(Guid id, out string taskPath) {
|
---|
149 | DirectoryInfo dirInfo = new DirectoryInfo(RootLocation);
|
---|
150 | if (!dirInfo.Exists) {
|
---|
151 | dirInfo.Create();
|
---|
152 | }
|
---|
153 |
|
---|
154 | taskPath = Path.Combine(RootLocation, id.ToString() + ".hl");
|
---|
155 | FileInfo fileInfo = new FileInfo(taskPath);
|
---|
156 |
|
---|
157 | if (fileInfo.Exists)
|
---|
158 | return true;
|
---|
159 |
|
---|
160 | return false;
|
---|
161 | }
|
---|
162 | }
|
---|
163 | }
|
---|