[6976] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[7259] | 3 | * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[6976] | 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.Configuration;
|
---|
| 25 | using System.IO;
|
---|
| 26 | using System.Linq;
|
---|
| 27 | using System.Security.Cryptography;
|
---|
| 28 | using System.Threading;
|
---|
| 29 | using System.Threading.Tasks;
|
---|
| 30 | using HeuristicLab.Common;
|
---|
| 31 | using HeuristicLab.Core;
|
---|
[7582] | 32 | using HeuristicLab.MainForm;
|
---|
[6976] | 33 | using HeuristicLab.PluginInfrastructure;
|
---|
| 34 | using TS = System.Threading.Tasks;
|
---|
| 35 |
|
---|
| 36 | namespace HeuristicLab.Clients.Hive {
|
---|
| 37 | [Item("HiveClient", "Hive client.")]
|
---|
| 38 | public sealed class HiveClient : IContent {
|
---|
| 39 | private static HiveClient instance;
|
---|
| 40 | public static HiveClient Instance {
|
---|
| 41 | get {
|
---|
| 42 | if (instance == null) instance = new HiveClient();
|
---|
| 43 | return instance;
|
---|
| 44 | }
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | #region Properties
|
---|
| 48 | private ItemCollection<RefreshableJob> jobs;
|
---|
| 49 | public ItemCollection<RefreshableJob> Jobs {
|
---|
| 50 | get { return jobs; }
|
---|
| 51 | set {
|
---|
| 52 | if (value != jobs) {
|
---|
| 53 | jobs = value;
|
---|
| 54 | OnHiveExperimentsChanged();
|
---|
| 55 | }
|
---|
| 56 | }
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | private List<Plugin> onlinePlugins;
|
---|
| 60 | public List<Plugin> OnlinePlugins {
|
---|
| 61 | get { return onlinePlugins; }
|
---|
| 62 | set { onlinePlugins = value; }
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | private List<Plugin> alreadyUploadedPlugins;
|
---|
| 66 | public List<Plugin> AlreadyUploadedPlugins {
|
---|
| 67 | get { return alreadyUploadedPlugins; }
|
---|
| 68 | set { alreadyUploadedPlugins = value; }
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | private bool isAllowedPrivileged;
|
---|
| 72 | public bool IsAllowedPrivileged {
|
---|
| 73 | get { return isAllowedPrivileged; }
|
---|
| 74 | set { isAllowedPrivileged = value; }
|
---|
| 75 | }
|
---|
| 76 | #endregion
|
---|
| 77 |
|
---|
| 78 | private HiveClient() { }
|
---|
| 79 |
|
---|
| 80 | #region Refresh
|
---|
| 81 | public void Refresh() {
|
---|
| 82 | OnRefreshing();
|
---|
| 83 |
|
---|
| 84 | try {
|
---|
[7132] | 85 | this.IsAllowedPrivileged = HiveServiceLocator.Instance.CallHiveService((s) => s.IsAllowedPrivileged());
|
---|
[6976] | 86 |
|
---|
| 87 | var oldJobs = jobs ?? new ItemCollection<RefreshableJob>();
|
---|
| 88 | jobs = new HiveItemCollection<RefreshableJob>();
|
---|
[7132] | 89 | var jobsLoaded = HiveServiceLocator.Instance.CallHiveService<IEnumerable<Job>>(s => s.GetJobs());
|
---|
[6976] | 90 |
|
---|
| 91 | foreach (var j in jobsLoaded) {
|
---|
| 92 | var job = oldJobs.SingleOrDefault(x => x.Id == j.Id);
|
---|
| 93 | if (job == null) {
|
---|
| 94 | // new
|
---|
| 95 | jobs.Add(new RefreshableJob(j) { IsAllowedPrivileged = this.isAllowedPrivileged });
|
---|
| 96 | } else {
|
---|
| 97 | // update
|
---|
| 98 | job.Job = j;
|
---|
| 99 | job.IsAllowedPrivileged = this.isAllowedPrivileged;
|
---|
| 100 | jobs.Add(job);
|
---|
| 101 | }
|
---|
| 102 | }
|
---|
| 103 | // remove those which were not in the list of loaded hiveexperiments
|
---|
| 104 | foreach (var job in oldJobs) {
|
---|
| 105 | if (job.Id == Guid.Empty) {
|
---|
| 106 | // experiment not uploaded... keep
|
---|
| 107 | jobs.Add(job);
|
---|
| 108 | } else {
|
---|
| 109 | job.RefreshAutomatically = false; // stop results polling
|
---|
| 110 | }
|
---|
| 111 | }
|
---|
| 112 | }
|
---|
| 113 | catch {
|
---|
| 114 | jobs = null;
|
---|
| 115 | throw;
|
---|
[7582] | 116 | }
|
---|
| 117 | finally {
|
---|
[6976] | 118 | OnRefreshed();
|
---|
| 119 | }
|
---|
| 120 | }
|
---|
| 121 | public void RefreshAsync(Action<Exception> exceptionCallback) {
|
---|
| 122 | var call = new Func<Exception>(delegate() {
|
---|
| 123 | try {
|
---|
| 124 | Refresh();
|
---|
| 125 | }
|
---|
| 126 | catch (Exception ex) {
|
---|
| 127 | return ex;
|
---|
| 128 | }
|
---|
| 129 | return null;
|
---|
| 130 | });
|
---|
| 131 | call.BeginInvoke(delegate(IAsyncResult result) {
|
---|
| 132 | Exception ex = call.EndInvoke(result);
|
---|
| 133 | if (ex != null) exceptionCallback(ex);
|
---|
| 134 | }, null);
|
---|
| 135 | }
|
---|
| 136 | #endregion
|
---|
| 137 |
|
---|
| 138 | #region Store
|
---|
| 139 | public static void Store(IHiveItem item, CancellationToken cancellationToken) {
|
---|
| 140 | if (item.Id == Guid.Empty) {
|
---|
| 141 | if (item is RefreshableJob) {
|
---|
| 142 | HiveClient.Instance.UploadJob((RefreshableJob)item, cancellationToken);
|
---|
| 143 | }
|
---|
| 144 | if (item is JobPermission) {
|
---|
| 145 | var hep = (JobPermission)item;
|
---|
[7132] | 146 | hep.GrantedUserId = HiveServiceLocator.Instance.CallHiveService((s) => s.GetUserIdByUsername(hep.GrantedUserName));
|
---|
[6976] | 147 | if (hep.GrantedUserId == Guid.Empty) {
|
---|
| 148 | throw new ArgumentException(string.Format("The user {0} was not found.", hep.GrantedUserName));
|
---|
| 149 | }
|
---|
[7132] | 150 | HiveServiceLocator.Instance.CallHiveService((s) => s.GrantPermission(hep.JobId, hep.GrantedUserId, hep.Permission));
|
---|
[6976] | 151 | }
|
---|
| 152 | } else {
|
---|
| 153 | if (item is Job)
|
---|
[7132] | 154 | HiveServiceLocator.Instance.CallHiveService(s => s.UpdateJob((Job)item));
|
---|
[6976] | 155 | }
|
---|
| 156 | }
|
---|
| 157 | public static void StoreAsync(Action<Exception> exceptionCallback, IHiveItem item, CancellationToken cancellationToken) {
|
---|
| 158 | var call = new Func<Exception>(delegate() {
|
---|
| 159 | try {
|
---|
| 160 | Store(item, cancellationToken);
|
---|
| 161 | }
|
---|
| 162 | catch (Exception ex) {
|
---|
| 163 | return ex;
|
---|
| 164 | }
|
---|
| 165 | return null;
|
---|
| 166 | });
|
---|
| 167 | call.BeginInvoke(delegate(IAsyncResult result) {
|
---|
| 168 | Exception ex = call.EndInvoke(result);
|
---|
| 169 | if (ex != null) exceptionCallback(ex);
|
---|
| 170 | }, null);
|
---|
| 171 | }
|
---|
| 172 | #endregion
|
---|
| 173 |
|
---|
| 174 | #region Delete
|
---|
| 175 | public static void Delete(IHiveItem item) {
|
---|
[7059] | 176 | if (item.Id == Guid.Empty && item.GetType() != typeof(JobPermission))
|
---|
[6976] | 177 | return;
|
---|
| 178 |
|
---|
| 179 | if (item is Job)
|
---|
[7132] | 180 | HiveServiceLocator.Instance.CallHiveService(s => s.DeleteJob(item.Id));
|
---|
[7144] | 181 | if (item is RefreshableJob) {
|
---|
| 182 | RefreshableJob job = (RefreshableJob)item;
|
---|
| 183 | if (job.RefreshAutomatically) {
|
---|
| 184 | job.StopResultPolling();
|
---|
| 185 | }
|
---|
[7132] | 186 | HiveServiceLocator.Instance.CallHiveService(s => s.DeleteJob(item.Id));
|
---|
[7144] | 187 | }
|
---|
[6976] | 188 | if (item is JobPermission) {
|
---|
| 189 | var hep = (JobPermission)item;
|
---|
[7132] | 190 | HiveServiceLocator.Instance.CallHiveService(s => s.RevokePermission(hep.JobId, hep.GrantedUserId));
|
---|
[6976] | 191 | }
|
---|
| 192 | item.Id = Guid.Empty;
|
---|
| 193 | }
|
---|
| 194 | #endregion
|
---|
| 195 |
|
---|
| 196 | #region Events
|
---|
| 197 | public event EventHandler Refreshing;
|
---|
| 198 | private void OnRefreshing() {
|
---|
| 199 | EventHandler handler = Refreshing;
|
---|
| 200 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 201 | }
|
---|
| 202 | public event EventHandler Refreshed;
|
---|
| 203 | private void OnRefreshed() {
|
---|
| 204 | var handler = Refreshed;
|
---|
| 205 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 206 | }
|
---|
| 207 | public event EventHandler HiveExperimentsChanged;
|
---|
| 208 | private void OnHiveExperimentsChanged() {
|
---|
| 209 | var handler = HiveExperimentsChanged;
|
---|
| 210 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 211 | }
|
---|
| 212 | #endregion
|
---|
| 213 |
|
---|
| 214 | public static void StartJob(Action<Exception> exceptionCallback, RefreshableJob refreshableJob, CancellationToken cancellationToken) {
|
---|
| 215 | HiveClient.StoreAsync(
|
---|
| 216 | new Action<Exception>((Exception ex) => {
|
---|
| 217 | refreshableJob.ExecutionState = ExecutionState.Prepared;
|
---|
| 218 | exceptionCallback(ex);
|
---|
| 219 | }), refreshableJob, cancellationToken);
|
---|
| 220 | refreshableJob.ExecutionState = ExecutionState.Started;
|
---|
| 221 | }
|
---|
| 222 |
|
---|
| 223 | public static void PauseJob(RefreshableJob refreshableJob) {
|
---|
[7132] | 224 | HiveServiceLocator.Instance.CallHiveService(service => {
|
---|
[6976] | 225 | foreach (HiveTask task in refreshableJob.GetAllHiveTasks()) {
|
---|
| 226 | if (task.Task.State != TaskState.Finished && task.Task.State != TaskState.Aborted && task.Task.State != TaskState.Failed)
|
---|
| 227 | service.PauseTask(task.Task.Id);
|
---|
| 228 | }
|
---|
| 229 | });
|
---|
| 230 | refreshableJob.ExecutionState = ExecutionState.Paused;
|
---|
| 231 | }
|
---|
| 232 |
|
---|
| 233 | public static void StopJob(RefreshableJob refreshableJob) {
|
---|
[7132] | 234 | HiveServiceLocator.Instance.CallHiveService(service => {
|
---|
[6976] | 235 | foreach (HiveTask task in refreshableJob.GetAllHiveTasks()) {
|
---|
| 236 | if (task.Task.State != TaskState.Finished && task.Task.State != TaskState.Aborted && task.Task.State != TaskState.Failed)
|
---|
| 237 | service.StopTask(task.Task.Id);
|
---|
| 238 | }
|
---|
| 239 | });
|
---|
[7156] | 240 | refreshableJob.ExecutionState = ExecutionState.Stopped;
|
---|
[6976] | 241 | }
|
---|
| 242 |
|
---|
[7156] | 243 | public static void ResumeJob(RefreshableJob refreshableJob) {
|
---|
| 244 | HiveServiceLocator.Instance.CallHiveService(service => {
|
---|
| 245 | foreach (HiveTask task in refreshableJob.GetAllHiveTasks()) {
|
---|
[7165] | 246 | if (task.Task.State == TaskState.Paused) {
|
---|
| 247 | service.RestartTask(task.Task.Id);
|
---|
[7156] | 248 | }
|
---|
| 249 | }
|
---|
| 250 | });
|
---|
| 251 | refreshableJob.ExecutionState = ExecutionState.Started;
|
---|
| 252 | }
|
---|
| 253 |
|
---|
[6976] | 254 | #region Upload Job
|
---|
| 255 | private Semaphore taskUploadSemaphore = new Semaphore(Settings.Default.MaxParallelUploads, Settings.Default.MaxParallelUploads);
|
---|
| 256 | private static object jobCountLocker = new object();
|
---|
| 257 | private static object pluginLocker = new object();
|
---|
| 258 | private void UploadJob(RefreshableJob refreshableJob, CancellationToken cancellationToken) {
|
---|
| 259 | try {
|
---|
[8156] | 260 | refreshableJob.IsProgressing = true;
|
---|
[6976] | 261 | refreshableJob.Progress = new Progress("Connecting to server...");
|
---|
| 262 | IEnumerable<string> resourceNames = ToResourceNameList(refreshableJob.Job.ResourceNames);
|
---|
| 263 | var resourceIds = new List<Guid>();
|
---|
| 264 | foreach (var resourceName in resourceNames) {
|
---|
[7132] | 265 | Guid resourceId = HiveServiceLocator.Instance.CallHiveService((s) => s.GetResourceId(resourceName));
|
---|
[6976] | 266 | if (resourceId == Guid.Empty) {
|
---|
| 267 | throw new ResourceNotFoundException(string.Format("Could not find the resource '{0}'", resourceName));
|
---|
| 268 | }
|
---|
| 269 | resourceIds.Add(resourceId);
|
---|
| 270 | }
|
---|
| 271 |
|
---|
| 272 | foreach (OptimizerHiveTask hiveJob in refreshableJob.HiveTasks.OfType<OptimizerHiveTask>()) {
|
---|
| 273 | hiveJob.SetIndexInParentOptimizerList(null);
|
---|
| 274 | }
|
---|
| 275 |
|
---|
| 276 | // upload Job
|
---|
| 277 | refreshableJob.Progress.Status = "Uploading Job...";
|
---|
[7132] | 278 | refreshableJob.Job.Id = HiveServiceLocator.Instance.CallHiveService((s) => s.AddJob(refreshableJob.Job));
|
---|
[6976] | 279 | bool isPrivileged = refreshableJob.Job.IsPrivileged;
|
---|
[7132] | 280 | refreshableJob.Job = HiveServiceLocator.Instance.CallHiveService((s) => s.GetJob(refreshableJob.Job.Id)); // update owner and permissions
|
---|
[6976] | 281 | refreshableJob.Job.IsPrivileged = isPrivileged;
|
---|
| 282 | cancellationToken.ThrowIfCancellationRequested();
|
---|
| 283 |
|
---|
| 284 | int totalJobCount = refreshableJob.GetAllHiveTasks().Count();
|
---|
| 285 | int[] jobCount = new int[1]; // use a reference type (int-array) instead of value type (int) in order to pass the value via a delegate to task-parallel-library
|
---|
| 286 | cancellationToken.ThrowIfCancellationRequested();
|
---|
| 287 |
|
---|
| 288 | // upload plugins
|
---|
| 289 | refreshableJob.Progress.Status = "Uploading plugins...";
|
---|
[7132] | 290 | this.OnlinePlugins = HiveServiceLocator.Instance.CallHiveService((s) => s.GetPlugins());
|
---|
[6976] | 291 | this.AlreadyUploadedPlugins = new List<Plugin>();
|
---|
[7132] | 292 | Plugin configFilePlugin = HiveServiceLocator.Instance.CallHiveService((s) => UploadConfigurationFile(s, onlinePlugins));
|
---|
[6976] | 293 | this.alreadyUploadedPlugins.Add(configFilePlugin);
|
---|
| 294 | cancellationToken.ThrowIfCancellationRequested();
|
---|
| 295 |
|
---|
| 296 | if (refreshableJob.RefreshAutomatically) refreshableJob.StartResultPolling();
|
---|
| 297 |
|
---|
| 298 | // upload tasks
|
---|
| 299 | refreshableJob.Progress.Status = "Uploading tasks...";
|
---|
| 300 |
|
---|
| 301 | var tasks = new List<TS.Task>();
|
---|
| 302 | foreach (HiveTask hiveTask in refreshableJob.HiveTasks) {
|
---|
| 303 | tasks.Add(TS.Task.Factory.StartNew((hj) => {
|
---|
| 304 | UploadTaskWithChildren(refreshableJob.Progress, (HiveTask)hj, null, resourceIds, jobCount, totalJobCount, configFilePlugin.Id, refreshableJob.Job.Id, refreshableJob.Log, refreshableJob.Job.IsPrivileged, cancellationToken);
|
---|
| 305 | }, hiveTask)
|
---|
| 306 | .ContinueWith((x) => refreshableJob.Log.LogException(x.Exception), TaskContinuationOptions.OnlyOnFaulted));
|
---|
| 307 | }
|
---|
| 308 | try {
|
---|
| 309 | TS.Task.WaitAll(tasks.ToArray());
|
---|
| 310 | }
|
---|
| 311 | catch (AggregateException ae) {
|
---|
| 312 | if (!ae.InnerExceptions.All(e => e is TaskCanceledException)) throw ae; // for some reason the WaitAll throws a AggregateException containg a TaskCanceledException. i don't know where it comes from, however the tasks all finish properly, so for now just ignore it
|
---|
| 313 | }
|
---|
| 314 | refreshableJob.Job.Modified = false;
|
---|
[7582] | 315 | }
|
---|
| 316 | finally {
|
---|
[8159] | 317 | refreshableJob.IsProgressing = false;
|
---|
[8156] | 318 | refreshableJob.Progress.Finish();
|
---|
[6976] | 319 | }
|
---|
| 320 | }
|
---|
| 321 |
|
---|
| 322 | /// <summary>
|
---|
| 323 | /// Uploads the local configuration file as plugin
|
---|
| 324 | /// </summary>
|
---|
| 325 | private static Plugin UploadConfigurationFile(IHiveService service, List<Plugin> onlinePlugins) {
|
---|
| 326 | string exeFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Settings.Default.HLBinaryName);
|
---|
| 327 | string configFileName = Path.GetFileName(ConfigurationManager.OpenExeConfiguration(exeFilePath).FilePath);
|
---|
| 328 | string configFilePath = ConfigurationManager.OpenExeConfiguration(exeFilePath).FilePath;
|
---|
| 329 | byte[] hash;
|
---|
| 330 |
|
---|
| 331 | byte[] data = File.ReadAllBytes(configFilePath);
|
---|
| 332 | using (SHA1 sha1 = SHA1.Create()) {
|
---|
| 333 | hash = sha1.ComputeHash(data);
|
---|
| 334 | }
|
---|
| 335 |
|
---|
| 336 | Plugin configPlugin = new Plugin() { Name = "Configuration", Version = new Version(), Hash = hash };
|
---|
| 337 | PluginData configFile = new PluginData() { FileName = configFileName, Data = data };
|
---|
| 338 |
|
---|
| 339 | IEnumerable<Plugin> onlineConfig = onlinePlugins.Where(p => p.Hash.SequenceEqual(hash));
|
---|
| 340 |
|
---|
| 341 | if (onlineConfig.Count() > 0) {
|
---|
| 342 | return onlineConfig.First();
|
---|
| 343 | } else {
|
---|
| 344 | configPlugin.Id = service.AddPlugin(configPlugin, new List<PluginData> { configFile });
|
---|
| 345 | return configPlugin;
|
---|
| 346 | }
|
---|
| 347 | }
|
---|
| 348 |
|
---|
| 349 | /// <summary>
|
---|
| 350 | /// Uploads the given task and all its child-jobs while setting the proper parentJobId values for the childs
|
---|
| 351 | /// </summary>
|
---|
| 352 | /// <param name="parentHiveTask">shall be null if its the root task</param>
|
---|
[8165] | 353 | private void UploadTaskWithChildren(Progress progress, HiveTask hiveTask, HiveTask parentHiveTask, IEnumerable<Guid> groups, int[] taskCount, int totalJobCount, Guid configPluginId, Guid jobId, ILog log, bool isPrivileged, CancellationToken cancellationToken) {
|
---|
[6976] | 354 | taskUploadSemaphore.WaitOne();
|
---|
| 355 | bool semaphoreReleased = false;
|
---|
| 356 | try {
|
---|
| 357 | cancellationToken.ThrowIfCancellationRequested();
|
---|
| 358 | lock (jobCountLocker) {
|
---|
| 359 | taskCount[0]++;
|
---|
| 360 | }
|
---|
| 361 | TaskData taskData;
|
---|
| 362 | List<IPluginDescription> plugins;
|
---|
| 363 |
|
---|
| 364 | if (hiveTask.ItemTask.ComputeInParallel && (hiveTask.ItemTask.Item is Optimization.Experiment || hiveTask.ItemTask.Item is Optimization.BatchRun)) {
|
---|
| 365 | hiveTask.Task.IsParentTask = true;
|
---|
| 366 | hiveTask.Task.FinishWhenChildJobsFinished = true;
|
---|
| 367 | taskData = hiveTask.GetAsTaskData(true, out plugins);
|
---|
| 368 | } else {
|
---|
| 369 | hiveTask.Task.IsParentTask = false;
|
---|
| 370 | hiveTask.Task.FinishWhenChildJobsFinished = false;
|
---|
| 371 | taskData = hiveTask.GetAsTaskData(false, out plugins);
|
---|
| 372 | }
|
---|
| 373 | cancellationToken.ThrowIfCancellationRequested();
|
---|
| 374 |
|
---|
| 375 | TryAndRepeat(() => {
|
---|
| 376 | if (!cancellationToken.IsCancellationRequested) {
|
---|
| 377 | lock (pluginLocker) {
|
---|
[7132] | 378 | HiveServiceLocator.Instance.CallHiveService((s) => hiveTask.Task.PluginsNeededIds = PluginUtil.GetPluginDependencies(s, this.onlinePlugins, this.alreadyUploadedPlugins, plugins));
|
---|
[6976] | 379 | }
|
---|
| 380 | }
|
---|
[7125] | 381 | }, Settings.Default.MaxRepeatServiceCalls, "Failed to upload plugins");
|
---|
[6976] | 382 | cancellationToken.ThrowIfCancellationRequested();
|
---|
| 383 | hiveTask.Task.PluginsNeededIds.Add(configPluginId);
|
---|
| 384 | hiveTask.Task.JobId = jobId;
|
---|
| 385 | hiveTask.Task.IsPrivileged = isPrivileged;
|
---|
| 386 |
|
---|
| 387 | log.LogMessage(string.Format("Uploading task ({0} kb, {1} objects)", taskData.Data.Count() / 1024, hiveTask.ItemTask.GetObjectGraphObjects().Count()));
|
---|
| 388 | TryAndRepeat(() => {
|
---|
| 389 | if (!cancellationToken.IsCancellationRequested) {
|
---|
| 390 | if (parentHiveTask != null) {
|
---|
[7132] | 391 | hiveTask.Task.Id = HiveServiceLocator.Instance.CallHiveService((s) => s.AddChildTask(parentHiveTask.Task.Id, hiveTask.Task, taskData));
|
---|
[6976] | 392 | } else {
|
---|
[7132] | 393 | hiveTask.Task.Id = HiveServiceLocator.Instance.CallHiveService((s) => s.AddTask(hiveTask.Task, taskData, groups.ToList()));
|
---|
[6976] | 394 | }
|
---|
| 395 | }
|
---|
[7125] | 396 | }, Settings.Default.MaxRepeatServiceCalls, "Failed to add task", log);
|
---|
[6976] | 397 | cancellationToken.ThrowIfCancellationRequested();
|
---|
| 398 |
|
---|
| 399 | lock (jobCountLocker) {
|
---|
| 400 | progress.ProgressValue = (double)taskCount[0] / totalJobCount;
|
---|
| 401 | progress.Status = string.Format("Uploaded task ({0} of {1})", taskCount[0], totalJobCount);
|
---|
| 402 | }
|
---|
| 403 |
|
---|
| 404 | var tasks = new List<TS.Task>();
|
---|
| 405 | foreach (HiveTask child in hiveTask.ChildHiveTasks) {
|
---|
| 406 | tasks.Add(TS.Task.Factory.StartNew((tuple) => {
|
---|
| 407 | var arguments = (Tuple<HiveTask, HiveTask>)tuple;
|
---|
| 408 | UploadTaskWithChildren(progress, arguments.Item1, arguments.Item2, groups, taskCount, totalJobCount, configPluginId, jobId, log, isPrivileged, cancellationToken);
|
---|
| 409 | }, new Tuple<HiveTask, HiveTask>(child, hiveTask))
|
---|
| 410 | .ContinueWith((x) => log.LogException(x.Exception), TaskContinuationOptions.OnlyOnFaulted));
|
---|
| 411 | }
|
---|
| 412 | taskUploadSemaphore.Release(); semaphoreReleased = true; // the semaphore has to be release before waitall!
|
---|
| 413 | try {
|
---|
| 414 | TS.Task.WaitAll(tasks.ToArray());
|
---|
| 415 | }
|
---|
| 416 | catch (AggregateException ae) {
|
---|
| 417 | if (!ae.InnerExceptions.All(e => e is TaskCanceledException)) throw ae; // for some reason the WaitAll throws a AggregateException containg a TaskCanceledException. i don't know where it comes from, however the tasks all finish properly, so for now just ignore it
|
---|
| 418 | }
|
---|
[7582] | 419 | }
|
---|
| 420 | finally {
|
---|
[6976] | 421 | if (!semaphoreReleased) taskUploadSemaphore.Release();
|
---|
| 422 | }
|
---|
| 423 | }
|
---|
| 424 | #endregion
|
---|
| 425 |
|
---|
| 426 | #region Download Experiment
|
---|
| 427 | public static void LoadJob(RefreshableJob refreshableJob) {
|
---|
| 428 | var hiveExperiment = refreshableJob.Job;
|
---|
[8156] | 429 | refreshableJob.IsProgressing = true;
|
---|
[6976] | 430 | refreshableJob.Progress = new Progress();
|
---|
| 431 |
|
---|
| 432 | try {
|
---|
| 433 | int totalJobCount = 0;
|
---|
| 434 | IEnumerable<LightweightTask> allTasks;
|
---|
| 435 |
|
---|
| 436 | refreshableJob.Progress.Status = "Connecting to Server...";
|
---|
| 437 | // fetch all task objects to create the full tree of tree of HiveTask objects
|
---|
| 438 | refreshableJob.Progress.Status = "Downloading list of tasks...";
|
---|
[7132] | 439 | allTasks = HiveServiceLocator.Instance.CallHiveService(s => s.GetLightweightJobTasks(hiveExperiment.Id));
|
---|
[6976] | 440 | totalJobCount = allTasks.Count();
|
---|
| 441 |
|
---|
[7125] | 442 | refreshableJob.Progress.Status = "Downloading tasks...";
|
---|
[6976] | 443 | TaskDownloader downloader = new TaskDownloader(allTasks.Select(x => x.Id));
|
---|
| 444 | downloader.StartAsync();
|
---|
| 445 |
|
---|
| 446 | while (!downloader.IsFinished) {
|
---|
| 447 | refreshableJob.Progress.ProgressValue = downloader.FinishedCount / (double)totalJobCount;
|
---|
| 448 | refreshableJob.Progress.Status = string.Format("Downloading/deserializing tasks... ({0}/{1} finished)", downloader.FinishedCount, totalJobCount);
|
---|
| 449 | Thread.Sleep(500);
|
---|
| 450 |
|
---|
| 451 | if (downloader.IsFaulted) {
|
---|
| 452 | throw downloader.Exception;
|
---|
| 453 | }
|
---|
| 454 | }
|
---|
| 455 | IDictionary<Guid, HiveTask> allHiveTasks = downloader.Results;
|
---|
[7165] | 456 | var parents = allHiveTasks.Values.Where(x => !x.Task.ParentTaskId.HasValue);
|
---|
[6976] | 457 |
|
---|
[7165] | 458 | refreshableJob.Progress.Status = "Downloading/deserializing complete. Displaying tasks...";
|
---|
| 459 | // build child-task tree
|
---|
| 460 | foreach (HiveTask hiveTask in parents) {
|
---|
| 461 | BuildHiveJobTree(hiveTask, allTasks, allHiveTasks);
|
---|
| 462 | }
|
---|
[6976] | 463 |
|
---|
[7165] | 464 | refreshableJob.HiveTasks = new ItemCollection<HiveTask>(parents);
|
---|
[6976] | 465 | if (refreshableJob.IsFinished()) {
|
---|
| 466 | refreshableJob.ExecutionState = Core.ExecutionState.Stopped;
|
---|
| 467 | } else {
|
---|
| 468 | refreshableJob.ExecutionState = Core.ExecutionState.Started;
|
---|
| 469 | }
|
---|
| 470 | refreshableJob.OnLoaded();
|
---|
[7582] | 471 | }
|
---|
| 472 | finally {
|
---|
[8159] | 473 | refreshableJob.IsProgressing = false;
|
---|
[8156] | 474 | refreshableJob.Progress.Finish();
|
---|
[6976] | 475 | }
|
---|
| 476 | }
|
---|
| 477 |
|
---|
[7125] | 478 | private static void BuildHiveJobTree(HiveTask parentHiveTask, IEnumerable<LightweightTask> allTasks, IDictionary<Guid, HiveTask> allHiveTasks) {
|
---|
| 479 | IEnumerable<LightweightTask> childTasks = from job in allTasks
|
---|
| 480 | where job.ParentTaskId.HasValue && job.ParentTaskId.Value == parentHiveTask.Task.Id
|
---|
[6976] | 481 | orderby job.DateCreated ascending
|
---|
| 482 | select job;
|
---|
| 483 | foreach (LightweightTask task in childTasks) {
|
---|
[7125] | 484 | HiveTask childHiveTask = allHiveTasks[task.Id];
|
---|
| 485 | parentHiveTask.AddChildHiveTask(childHiveTask);
|
---|
| 486 | BuildHiveJobTree(childHiveTask, allTasks, allHiveTasks);
|
---|
[6976] | 487 | }
|
---|
| 488 | }
|
---|
| 489 | #endregion
|
---|
| 490 |
|
---|
| 491 | /// <summary>
|
---|
| 492 | /// Converts a string which can contain Ids separated by ';' to a enumerable
|
---|
| 493 | /// </summary>
|
---|
| 494 | private static IEnumerable<string> ToResourceNameList(string resourceNames) {
|
---|
| 495 | if (!string.IsNullOrEmpty(resourceNames)) {
|
---|
[8109] | 496 | return resourceNames.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
|
---|
[6976] | 497 | } else {
|
---|
| 498 | return new List<string>();
|
---|
| 499 | }
|
---|
| 500 | }
|
---|
| 501 |
|
---|
| 502 | public static ItemTask LoadItemJob(Guid jobId) {
|
---|
[7132] | 503 | TaskData taskData = HiveServiceLocator.Instance.CallHiveService(s => s.GetTaskData(jobId));
|
---|
[6976] | 504 | try {
|
---|
| 505 | return PersistenceUtil.Deserialize<ItemTask>(taskData.Data);
|
---|
| 506 | }
|
---|
| 507 | catch {
|
---|
| 508 | return null;
|
---|
| 509 | }
|
---|
| 510 | }
|
---|
| 511 |
|
---|
| 512 | /// <summary>
|
---|
| 513 | /// Executes the action. If it throws an exception it is repeated until repetition-count is reached.
|
---|
| 514 | /// If repetitions is -1, it is repeated infinitely.
|
---|
| 515 | /// </summary>
|
---|
| 516 | public static void TryAndRepeat(Action action, int repetitions, string errorMessage, ILog log = null) {
|
---|
| 517 | while (true) {
|
---|
| 518 | try { action(); return; }
|
---|
| 519 | catch (Exception e) {
|
---|
| 520 | if (repetitions == 0) throw new HiveException(errorMessage, e);
|
---|
| 521 | if (log != null) log.LogMessage(string.Format("{0}: {1} - will try again!", errorMessage, e.ToString()));
|
---|
| 522 | repetitions--;
|
---|
| 523 | }
|
---|
| 524 | }
|
---|
| 525 | }
|
---|
| 526 |
|
---|
| 527 | public static HiveItemCollection<JobPermission> GetJobPermissions(Guid jobId) {
|
---|
[7132] | 528 | return HiveServiceLocator.Instance.CallHiveService((service) => {
|
---|
[6976] | 529 | IEnumerable<JobPermission> jps = service.GetJobPermissions(jobId);
|
---|
| 530 | foreach (var hep in jps) {
|
---|
[7059] | 531 | hep.UnmodifiedGrantedUserNameUpdate(service.GetUsernameByUserId(hep.GrantedUserId));
|
---|
[6976] | 532 | }
|
---|
| 533 | return new HiveItemCollection<JobPermission>(jps);
|
---|
| 534 | });
|
---|
| 535 | }
|
---|
| 536 | }
|
---|
| 537 | }
|
---|