Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Clients.Hive.Administrator/3.3/Views/ProjectJobsView.cs @ 16373

Last change on this file since 16373 was 16209, checked in by jzenisek, 6 years ago

#2839:

  • adapted job execution implementation at ProjectJobsView
  • prohibited resource checking for non-admins
File size: 15.9 KB
RevLine 
[15966]1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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.Drawing;
25using System.Linq;
[16208]26using System.Threading.Tasks;
[15966]27using System.Windows.Forms;
28using HeuristicLab.MainForm;
29using HeuristicLab.MainForm.WindowsForms;
30using HeuristicLab.Core.Views;
31using HeuristicLab.Data;
[15969]32using HeuristicLab.Clients.Hive.Views;
[16208]33using HeuristicLab.Core;
[15966]34
35namespace HeuristicLab.Clients.Hive.Administrator.Views {
36  [View("ProjectView")]
37  [Content(typeof(Project), IsDefaultView = false)]
38  public partial class ProjectJobsView : ItemView {
39    private const string JOB_ID = "Id";
40    private const string JOB_NAME = "Name";
41    private const string JOB_OWNER = "Owner";
[15969]42    private const string JOB_OWNERID = "Owner Id";
[15966]43    private const string JOB_DATECREATED = "Date Created";
44    private const string JOB_STATE = "State";
[16208]45    private const string JOB_EXECUTIONSTATE = "Execution State";
[16209]46    private const string JOB_EXECUTIONTIME = "Execution Time";
[15966]47    private const string JOB_DESCRIPTION = "Description";
[15969]48    private const string JOB_TASKCOUNT = "Tasks";
[16209]49    private const string JOB_WAITINGTASKCOUNT = "Waiting";
[15969]50    private const string JOB_CALCULATINGTASKCOUNT = "Calculating";
51    private const string JOB_FINISHEDTASKCOUNT = "Finished";
[15966]52
[15969]53    private readonly Color onlineStatusColor = Color.FromArgb(255, 189, 249, 143); // #bdf98f
54    private readonly Color onlineStatusColor2 = Color.FromArgb(255, 157, 249, 143); // #9df98f
55    private readonly Color statisticsPendingStatusColor = Color.FromArgb(255, 249, 210, 145); // #f9d291
56    private readonly Color deletionPendingStatusColor = Color.FromArgb(255, 249, 172, 144); // #f9ac90
57    private readonly Color deletionPendingStatusColor2 = Color.FromArgb(255, 249, 149, 143); // #f9958f
58
[16208]59    private IProgress progress;
60    public IProgress Progress {
61      get { return progress; }
62      set {
63        this.progress = value;
64        OnIsProgressingChanged();
65      }
66    }
67
[15966]68    public new Project Content {
69      get { return (Project)base.Content; }
70      set { base.Content = value; }
71    }
72
[15969]73    public ProjectJobsView() {
[15966]74      InitializeComponent();
[16208]75      progress = new Progress();
[15969]76
[16208]77      removeButton.Enabled = false;     
78    }
79
80    protected override void RegisterContentEvents() {
81      base.RegisterContentEvents();
[15969]82      matrixView.DataGridView.SelectionChanged += DataGridView_SelectionChanged;
[16208]83      MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().AddOperationProgressToView(this, progress);
[15966]84    }
85
[16208]86    protected override void DeregisterContentEvents() {
87      matrixView.DataGridView.SelectionChanged -= DataGridView_SelectionChanged;
88      MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().RemoveOperationProgressFromView(this, false);
89      base.DeregisterContentEvents();
90    }
91
[15969]92    private void DataGridView_SelectionChanged(object sender, EventArgs e) {
[16208]93      SetEnabledStateOfControls();
[15969]94    }
95
[15966]96    #region Overrides
97    protected override void OnContentChanged() {
98      base.OnContentChanged();
[15969]99      removeButton.Enabled = false;
[15966]100      UpdateJobs();
[16208]101      SetEnabledStateOfControls();
[15966]102    }
[16208]103
[15966]104    protected override void SetEnabledStateOfControls() {
105      base.SetEnabledStateOfControls();
106      bool enabled = Content != null && !Locked && !ReadOnly;
107
108      matrixView.Enabled = enabled;
[16208]109
110      // Buttons (start/resume, pause, stop, remove)
111      refreshButton.Enabled = startButton.Enabled = pauseButton.Enabled = stopButton.Enabled = removeButton.Enabled = false;
112
113      if (enabled && progress.ProgressState != ProgressState.Started) {
114        var jobs = GetSelectedJobs().ToList();
115        if (jobs.Any()) {
[16209]116                   
[16208]117          startButton.Enabled = jobs.All(x =>
[16209]118            !x.IsProgressing && HiveAdminClient.Instance.Tasks.ContainsKey(x.Id) && HiveAdminClient.Instance.Tasks[x.Id].Count > 0
119            && x.Job.ProjectId != Guid.Empty //&& x.Job.ResourceIds != null && x.Job.ResourceIds.Any()
[16208]120            && (x.ExecutionState == ExecutionState.Prepared || x.ExecutionState == ExecutionState.Paused));
121          pauseButton.Enabled = jobs.All(x => !x.IsProgressing && x.ExecutionState == ExecutionState.Started);
122          stopButton.Enabled = jobs.All(x => !x.IsProgressing && (x.ExecutionState == ExecutionState.Started || x.ExecutionState == ExecutionState.Paused));
123          removeButton.Enabled = jobs.All(x => !x.IsProgressing && x.Job.State == JobState.Online);
124        }
125      }
126
127      // refresh Button
128      if (Content != null && !Locked && progress.ProgressState != ProgressState.Started) {
129        refreshButton.Enabled = true;
130      }
[15966]131    }
[16208]132
[15966]133    #endregion Overrides
134
135    #region Event Handlers
136    private void ProjectJobsView_Load(object sender, EventArgs e) {
137     
138    }
139
140    private void refreshButton_Click(object sender, EventArgs e) {
[16208]141      progress.Start("Refreshing jobs...");
142      SetEnabledStateOfControls();
[16209]143      var task = System.Threading.Tasks.Task.Factory.StartNew(RefreshJobsAsync);
[16208]144
145      task.ContinueWith((t) => {
[16209]146        progress.Finish();       
147        SetEnabledStateOfControls();
[16208]148      });
[15966]149    }
150
[16208]151    private void removeButton_Click(object sender, EventArgs e) {
152      var jobs = GetSelectedJobs();
[15969]153
[16208]154      if (jobs.Any()) {
155        var result = MessageBox.Show("Do you really want to remove following job(s):\n\n"
156                                     + String.Join("\n", jobs.Select(x => x.Job.Name)),
157          "HeuristicLab Hive Administrator",
158          MessageBoxButtons.YesNo,
159          MessageBoxIcon.Question);
160
[16209]161        if (result == DialogResult.Yes) {
162          progress.Start("Removing job(s)...");
163          SetEnabledStateOfControls();
[16208]164          var task = System.Threading.Tasks.Task.Factory.StartNew(RemoveJobsAsync, jobs);
165
166          task.ContinueWith((t) => {
167            RefreshJobs();
168            progress.Finish();
[16209]169            SetEnabledStateOfControls();
[16208]170          }, TaskContinuationOptions.NotOnFaulted);
171
172          task.ContinueWith((t) => {
173            RefreshJobs();
174            progress.Finish();
[16209]175            SetEnabledStateOfControls();
176            MessageBox.Show("An error occured removing the job(s).", "HeuristicLab Hive Administrator", MessageBoxButtons.OK, MessageBoxIcon.Error);
[16208]177          }, TaskContinuationOptions.OnlyOnFaulted);
[15969]178        }
179      }
[16208]180    }
[15969]181
[16208]182    private void startButton_Click(object sender, EventArgs e) {
183      var jobs = GetSelectedJobs();
184
185      if (jobs.Any()) {
186        var result = MessageBox.Show("Do you really want to resume following job(s):\n\n"
187                                     + String.Join("\n", jobs.Select(x => x.Job.Name)),
[15969]188          "HeuristicLab Hive Administrator",
189          MessageBoxButtons.YesNo,
190          MessageBoxIcon.Question);
191
192        if (result == DialogResult.Yes) {
[16209]193          progress.Start("Resuming job(s)...");
194          SetEnabledStateOfControls();
[16208]195          var task = System.Threading.Tasks.Task.Factory.StartNew(ResumeJobsAsync, jobs);
196
197          task.ContinueWith((t) => {
[16209]198            RefreshJobs();           
[16208]199            progress.Finish();
[16209]200            SetEnabledStateOfControls();
201          }, TaskContinuationOptions.NotOnFaulted);
[16208]202
203          task.ContinueWith((t) => {
204            RefreshJobs();
205            progress.Finish();
[16209]206            SetEnabledStateOfControls();
[16208]207            MessageBox.Show("An error occured resuming the job(s).", "HeuristicLab Hive Administrator", MessageBoxButtons.OK, MessageBoxIcon.Error);
208          }, TaskContinuationOptions.OnlyOnFaulted);
[15969]209        }
[16208]210      }
[15966]211    }
212
[16208]213    private void pauseButton_Click(object sender, EventArgs e) {
214      var jobs = GetSelectedJobs();
215
216      if (jobs.Any()) {
217        var result = MessageBox.Show("Do you really want to pause following job(s):\n\n"
218                                     + String.Join("\n", jobs.Select(x => x.Job.Name)),
219          "HeuristicLab Hive Administrator",
220          MessageBoxButtons.YesNo,
221          MessageBoxIcon.Question);
222
223        if (result == DialogResult.Yes) {
[16209]224          progress.Start("Pausing job(s)...");
225          SetEnabledStateOfControls();
[16208]226          var task = System.Threading.Tasks.Task.Factory.StartNew(PauseJobsAsync, jobs);
227
228          task.ContinueWith((t) => {
[16209]229            RefreshJobs();           
[16208]230            progress.Finish();
[16209]231            SetEnabledStateOfControls();
232          }, TaskContinuationOptions.NotOnFaulted);
[16208]233
234          task.ContinueWith((t) => {
235            RefreshJobs();
236            progress.Finish();
[16209]237            SetEnabledStateOfControls();
[16208]238            MessageBox.Show("An error occured pausing the job(s).", "HeuristicLab Hive Administrator", MessageBoxButtons.OK, MessageBoxIcon.Error);
239          }, TaskContinuationOptions.OnlyOnFaulted);
240        }
241      }
[16205]242    }
243
[16208]244    private void stopButton_Click(object sender, EventArgs e) {
245      var jobs = GetSelectedJobs();
246
247      if (jobs.Any()) {
248        var result = MessageBox.Show("Do you really want to stop following job(s):\n\n"
249                                     + String.Join("\n", jobs.Select(x => x.Job.Name)),
250          "HeuristicLab Hive Administrator",
251          MessageBoxButtons.YesNo,
252          MessageBoxIcon.Question);
253
254        if (result == DialogResult.Yes) {
[16209]255          progress.Start("Stopping job(s)...");
256          SetEnabledStateOfControls();
[16208]257          var task = System.Threading.Tasks.Task.Factory.StartNew(StopJobsAsync, jobs);
258
259          task.ContinueWith((t) => {
260            RefreshJobs();
261            progress.Finish();
[16209]262            SetEnabledStateOfControls();
263          }, TaskContinuationOptions.NotOnFaulted);
[16208]264
265          task.ContinueWith((t) => {
266            RefreshJobs();
267            progress.Finish();
[16209]268            SetEnabledStateOfControls();
[16208]269            MessageBox.Show("An error occured stopping the job(s).", "HeuristicLab Hive Administrator", MessageBoxButtons.OK, MessageBoxIcon.Error);           
270          }, TaskContinuationOptions.OnlyOnFaulted);
271        }
272      }
[16205]273    }
274
[16208]275    public event EventHandler IsProgressingChanged;
276    private void OnIsProgressingChanged() {
277      var handler = IsProgressingChanged;
278      if (handler != null) handler(this, EventArgs.Empty);
[16205]279    }
[15966]280    #endregion Event Handlers
281
282    #region Helpers
[16208]283
284    private IEnumerable<RefreshableJob> GetSelectedJobs() {
285      if (Content == null || matrixView.DataGridView.SelectedRows == null || matrixView.DataGridView.SelectedRows.Count == 0)
286        return Enumerable.Empty<RefreshableJob>() ;
287
288      var jobs = new List<RefreshableJob>();
289      foreach (DataGridViewRow r in matrixView.DataGridView.SelectedRows) {
290        if (((string)r.Cells[0].Value) == JobState.Online.ToString()) {
[16209]291          jobs.Add(HiveAdminClient.Instance.Jobs[Content.Id].FirstOrDefault(x => x.Id == Guid.Parse((string) r.Cells[11].Value)));
[16208]292        }
293      }
294
295      return jobs;
296    }
297
[15969]298    private void RefreshJobs() {
299      HiveAdminClient.Instance.RefreshJobs();
300      UpdateJobs();
[16208]301      SetEnabledStateOfControls();
[15969]302    }
303
[15966]304    private StringMatrix CreateValueMatrix() {
[15978]305      if (Content == null || Content.Id == Guid.Empty)
306        return new StringMatrix();
307
[15966]308      var jobs = HiveAdminClient.Instance.Jobs[Content.Id];
[15969]309      var resources = HiveAdminClient.Instance.Resources;
[16209]310      string[,] values = new string[jobs.Count, 13];
[15966]311
312      for(int i = 0; i < jobs.Count; i++) {
[16208]313        var job = jobs.ElementAt(i);       
314        values[i, 0] = job.Job.State.ToString();
315        values[i, 1] = job.ExecutionState.ToString();
[16209]316        values[i, 2] = job.ExecutionTime.ToString();
317        values[i, 3] = job.Job.DateCreated.ToString();
318        values[i, 4] = job.Job.OwnerUsername;
319        values[i, 5] = job.Job.Name;
320        values[i, 6] = job.Job.JobCount.ToString();
321        values[i, 7] = (job.Job.JobCount - job.Job.CalculatingCount - job.Job.FinishedCount).ToString();
322        values[i, 8] = job.Job.CalculatingCount.ToString();
323        values[i, 9] = job.Job.FinishedCount.ToString();
324        values[i, 10] = job.Job.Description;       
325        values[i, 11] = job.Job.Id.ToString();
326        values[i, 12] = job.Job.OwnerUserId.ToString();
[15966]327      }
[15969]328     
[15966]329      var matrix = new StringMatrix(values);
[16209]330      matrix.ColumnNames = new string[] { JOB_STATE, JOB_EXECUTIONSTATE, JOB_EXECUTIONTIME, JOB_DATECREATED, JOB_OWNER, JOB_NAME, JOB_TASKCOUNT, JOB_WAITINGTASKCOUNT, JOB_CALCULATINGTASKCOUNT, JOB_FINISHEDTASKCOUNT, JOB_DESCRIPTION, JOB_ID, JOB_OWNERID };
[15966]331      matrix.SortableView = true;
332      return matrix;
333    }
[15969]334   
[15966]335    private void UpdateJobs() {
336      if (InvokeRequired) Invoke((Action)UpdateJobs);
337      else {
[15978]338        if(Content != null && Content.Id != null && Content.Id != Guid.Empty) {
339          var matrix = CreateValueMatrix();
340          matrixView.Content = matrix;
341          if(matrix != null) {
342            foreach (DataGridViewRow row in matrixView.DataGridView.Rows) {
343              string val = ((string)row.Cells[0].Value);
344              if (val == JobState.Online.ToString()) {
345                row.DefaultCellStyle.BackColor = onlineStatusColor;
346              } else if (val == JobState.StatisticsPending.ToString()) {
347                row.DefaultCellStyle.BackColor = statisticsPendingStatusColor;
348              } else if (val == JobState.DeletionPending.ToString()) {
349                row.DefaultCellStyle.BackColor = deletionPendingStatusColor;
350              }
351            }
352
353            matrixView.DataGridView.AutoResizeColumns();
354            matrixView.DataGridView.Columns[0].MinimumWidth = 90;
355            matrixView.DataGridView.Columns[1].MinimumWidth = 108;
356          }
[16208]357        }
[15966]358      }
359    }
[15969]360
[16208]361    private void RefreshJobsAsync() {
[16209]362      HiveAdminClient.Instance.RefreshJobs();
363      UpdateJobs();
[16208]364    }
365
366    private void ResumeJobsAsync(object jobs) {
367      var jobList = (IEnumerable<RefreshableJob>)jobs;
368      foreach (var job in jobList) {
369        progress.Status = "Resuming job \"" + job.Job.Name + "\"...";
370        HiveAdminClient.ResumeJob(job);
[16205]371      }
372    }
373
[16208]374    private void PauseJobsAsync(object jobs) {
375      var jobList = (IEnumerable<RefreshableJob>)jobs;
376      foreach (var job in jobList) {
377        progress.Status = "Pausing job \"" + job.Job.Name + "\"...";
378        HiveAdminClient.PauseJob(job);
[16205]379      }
380    }
381
[16208]382    private void StopJobsAsync(object jobs) {
383      var jobList = (IEnumerable<RefreshableJob>) jobs;
384      foreach (var job in jobList) {       
385        progress.Status = "Stopping job \"" + job.Job.Name + "\"...";
386        HiveAdminClient.StopJob(job);
[15969]387      }
388    }
389
[16208]390    private void RemoveJobsAsync(object jobs) {
391      var jobList = (IEnumerable<RefreshableJob>)jobs;
392      progress.Start();
393      foreach (var job in jobList) {
[16209]394        progress.Status = "Removing job \"" + job.Job.Name + "\"...";
[16208]395        HiveAdminClient.RemoveJob(job);
396      }
397      progress.Finish();
398    }
399
[15969]400    private void ShowHiveInformationDialog() {
401      if (InvokeRequired) Invoke((Action)ShowHiveInformationDialog);
402      else {
403        using (HiveInformationDialog dialog = new HiveInformationDialog()) {
404          dialog.ShowDialog(this);
405        }
406      }
407    }
[15966]408    #endregion Helpers
409  }
410}
Note: See TracBrowser for help on using the repository browser.