[10150] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17181] | 3 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[10150] | 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;
|
---|
[17059] | 24 | using System.Linq;
|
---|
[10150] | 25 | using System.Threading;
|
---|
[17059] | 26 | using System.Windows.Forms;
|
---|
| 27 | using HeuristicLab.Clients.Hive.JobManager.Views;
|
---|
[10150] | 28 | using HeuristicLab.Core;
|
---|
| 29 | using HeuristicLab.MainForm;
|
---|
| 30 | using HeuristicLab.Optimization;
|
---|
[11083] | 31 | using HeuristicLab.Optimizer;
|
---|
[10150] | 32 | using HeuristicLab.PluginInfrastructure;
|
---|
| 33 |
|
---|
[11083] | 34 | namespace HeuristicLab.Clients.Hive.JobManager {
|
---|
[10150] | 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);
|
---|
[11083] | 55 | return;
|
---|
[10150] | 56 | }
|
---|
| 57 | }
|
---|
[11083] | 58 | ToolStripItem.Enabled = false;
|
---|
[10150] | 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();
|
---|
[11083] | 83 | rJob.Job.Name = content.ToString();
|
---|
[10150] | 84 | rJob.HiveTasks.Add(task);
|
---|
| 85 | task.ItemTask.ComputeInParallel = content is Experiment || content is BatchRun;
|
---|
| 86 |
|
---|
[17059] | 87 | var hiveResourceSelectorDialog = new HiveResourceSelectorDialog(rJob.Job.Id, rJob.Job.ProjectId);
|
---|
[10150] | 88 |
|
---|
[17059] | 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 |
|
---|
[17062] | 101 | progress = Progress.Show(this.content, "Uploading to Hive...", ProgressMode.Indeterminate);
|
---|
[17059] | 102 | rJob.Progress = progress;
|
---|
| 103 | progress.ProgressStateChanged += progress_ProgressStateChanged;
|
---|
| 104 |
|
---|
| 105 | HiveClient.StartJob(new Action<Exception>(HandleEx), rJob, new CancellationToken());
|
---|
| 106 | }
|
---|
| 107 | }
|
---|
[10150] | 108 | }
|
---|
| 109 |
|
---|
| 110 | private void progress_ProgressStateChanged(object sender, EventArgs e) {
|
---|
| 111 | if (progress.ProgressState != ProgressState.Started) {
|
---|
[17062] | 112 | Progress.Hide(content);
|
---|
[10150] | 113 | progress.ProgressStateChanged -= progress_ProgressStateChanged;
|
---|
| 114 | }
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | private void HandleEx(Exception ex) {
|
---|
[17062] | 118 | Progress.Hide(content);
|
---|
[10150] | 119 | progress.ProgressStateChanged -= progress_ProgressStateChanged;
|
---|
| 120 | ErrorHandling.ShowErrorDialog("Error uploading tasks", ex);
|
---|
| 121 | }
|
---|
| 122 | }
|
---|
| 123 | }
|
---|