Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Clients.Hive.JobManager/3.3/MenuItems/RunInHiveMenuItem.cs @ 17432

Last change on this file since 17432 was 17181, checked in by swagner, 5 years ago

#2875: Merged r17180 from trunk to stable

File size: 4.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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 System.Windows.Forms;
27using HeuristicLab.Clients.Hive.JobManager.Views;
28using HeuristicLab.Core;
29using HeuristicLab.MainForm;
30using HeuristicLab.Optimization;
31using HeuristicLab.Optimizer;
32using HeuristicLab.PluginInfrastructure;
33
34namespace HeuristicLab.Clients.Hive.JobManager {
35  public class RunInHiveMenuItem : HeuristicLab.MainForm.WindowsForms.MenuItem, IOptimizerUserInterfaceItemProvider {
36    public override string Name {
37      get { return "&Run In Hive"; }
38    }
39    public override IEnumerable<string> Structure {
40      get { return new string[] { "&Services", "&Hive" }; }
41    }
42
43    public override int Position {
44      get { return 10002; }
45    }
46
47    protected override void OnActiveViewChanged(object sender, EventArgs e) {
48      IContentView activeView = MainFormManager.MainForm.ActiveView as IContentView;
49
50      if (activeView != null && activeView.Content != null && !activeView.Locked) {
51        var content = activeView.Content as IItem;
52        if (content != null) {
53          Type contentType = content.GetType();
54          ToolStripItem.Enabled = ItemTask.IsTypeSupported(contentType);
55          return;
56        }
57      }
58      ToolStripItem.Enabled = false;
59    }
60
61    private IProgress progress;
62    private IItem content;
63    public override void Execute() {
64      IContentView activeView = MainFormManager.MainForm.ActiveView as IContentView;
65      content = activeView.Content as IItem;
66
67      //IOptimizer and IExecutables need some special care
68      if (content is IOptimizer) {
69        ((IOptimizer)content).Runs.Clear();
70      }
71      if (content is IExecutable) {
72        IExecutable exec = content as IExecutable;
73        if (exec.ExecutionState != ExecutionState.Prepared) {
74          exec.Prepare();
75        }
76      }
77
78      HiveClient.Instance.Refresh();
79
80      ItemTask hiveTask = ItemTask.GetItemTaskForItem(content);
81      HiveTask task = hiveTask.CreateHiveTask();
82      RefreshableJob rJob = new RefreshableJob();
83      rJob.Job.Name = content.ToString();
84      rJob.HiveTasks.Add(task);
85      task.ItemTask.ComputeInParallel = content is Experiment || content is BatchRun;
86
87      var hiveResourceSelectorDialog = new HiveResourceSelectorDialog(rJob.Job.Id, rJob.Job.ProjectId);
88
89      if (HiveClient.Instance.Projects.Count == 1) {
90        var project = HiveClient.Instance.Projects.FirstOrDefault();
91        if (project != null && project.Id != Guid.Empty)
92          hiveResourceSelectorDialog.SelectedProjectId = project.Id;
93      }
94
95      if (hiveResourceSelectorDialog.ShowDialog((UserControl)activeView) == DialogResult.OK) {
96        var selectedProject = hiveResourceSelectorDialog.SelectedProject;
97        if (selectedProject != null) {
98          rJob.Job.ProjectId = selectedProject.Id;
99          rJob.Job.ResourceIds = hiveResourceSelectorDialog.SelectedResources.Select(x => x.Id).ToList();
100
101          progress = Progress.Show(this.content, "Uploading to Hive...", ProgressMode.Indeterminate);
102          rJob.Progress = progress;
103          progress.ProgressStateChanged += progress_ProgressStateChanged;
104
105          HiveClient.StartJob(new Action<Exception>(HandleEx), rJob, new CancellationToken());
106        }
107      }
108    }
109
110    private void progress_ProgressStateChanged(object sender, EventArgs e) {
111      if (progress.ProgressState != ProgressState.Started) {
112        Progress.Hide(content);
113        progress.ProgressStateChanged -= progress_ProgressStateChanged;
114      }
115    }
116
117    private void HandleEx(Exception ex) {
118      Progress.Hide(content);
119      progress.ProgressStateChanged -= progress_ProgressStateChanged;
120      ErrorHandling.ShowErrorDialog("Error uploading tasks", ex);
121    }
122  }
123}
Note: See TracBrowser for help on using the repository browser.