Free cookie consent management tool by TermsFeed Policy Generator

source: branches/WebJobManager/HeuristicLab.Clients.Hive.WebJobManager/Services/Imports/TaskDownloaderWeb.cs @ 13739

Last change on this file since 13739 was 13739, checked in by jlodewyc, 9 years ago

#2582 Overhaul Login service, done

File size: 5.0 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Threading;
26using HeuristicLab.Common;
27
28namespace HeuristicLab.Clients.Hive.WebJobManager.Services.Imports
29{
30
31    /// <summary>
32    /// Rewritten to use HiveClientWeb and HiveServiceLocatorWeb
33    /// </summary>
34    public class TaskDownloaderWeb : IDisposable
35    {
36        private HiveServiceLocatorWeb serviceLocator;
37        private IEnumerable<Guid> taskIds;
38        private ConcurrentTaskDownloaderWeb<ItemTask> taskDownloader;
39        private IDictionary<Guid, HiveTask> results;
40        private bool exceptionOccured = false;
41        private Exception currentException;
42        private ReaderWriterLockSlim resultsLock = new ReaderWriterLockSlim();
43
44        public bool IsFinished
45        {
46            get
47            {
48                try
49                {
50                    resultsLock.EnterReadLock();
51                    return results.Count == taskIds.Count();
52                }
53                finally { resultsLock.ExitReadLock(); }
54            }
55        }
56
57        public bool IsFaulted
58        {
59            get
60            {
61                return exceptionOccured;
62            }
63        }
64
65        public Exception Exception
66        {
67            get
68            {
69                return currentException;
70            }
71        }
72
73        public int FinishedCount
74        {
75            get
76            {
77                try
78                {
79                    resultsLock.EnterReadLock();
80                    return results.Count;
81                }
82                finally { resultsLock.ExitReadLock(); }
83            }
84        }
85
86        public IDictionary<Guid, HiveTask> Results
87        {
88            get
89            {
90                try
91                {
92                    resultsLock.EnterReadLock();
93                    return results;
94                }
95                finally { resultsLock.ExitReadLock(); }
96            }
97        }
98
99        public TaskDownloaderWeb(IEnumerable<Guid> jobIds, HiveServiceLocatorWeb hiv)
100        {
101            taskIds = jobIds;
102            serviceLocator = hiv;
103            taskDownloader = new ConcurrentTaskDownloaderWeb<ItemTask>(
104                Settings.Default.MaxParallelDownloads,
105                Settings.Default.MaxParallelDownloads,
106                serviceLocator);
107            taskDownloader.ExceptionOccured += new EventHandler<EventArgs<Exception>>(taskDownloader_ExceptionOccured);
108            results = new Dictionary<Guid, HiveTask>();
109           
110        }
111
112        public void StartAsync()
113        {
114            foreach (Guid taskId in taskIds)
115            {
116                taskDownloader.DownloadTaskDataAndTask(taskId,
117                  (localTask, itemTask) =>
118                  {
119                      if (localTask != null && itemTask != null)
120                      {
121                          HiveTask hiveTask = itemTask.CreateHiveTask();
122                          hiveTask.Task = localTask;
123                          try
124                          {
125                              resultsLock.EnterWriteLock();
126                              results.Add(localTask.Id, hiveTask);
127                          }
128                          finally { resultsLock.ExitWriteLock(); }
129                      }
130                  });
131            }
132        }
133
134        private void taskDownloader_ExceptionOccured(object sender, EventArgs<Exception> e)
135        {
136            OnExceptionOccured(e.Value);
137        }
138
139        public event EventHandler<EventArgs<Exception>> ExceptionOccured;
140        private void OnExceptionOccured(Exception exception)
141        {
142            exceptionOccured = true;
143            currentException = exception;
144            var handler = ExceptionOccured;
145            if (handler != null) handler(this, new EventArgs<Exception>(exception));
146        }
147
148        #region IDisposable Members
149        public void Dispose()
150        {
151            taskDownloader.ExceptionOccured -= new EventHandler<EventArgs<Exception>>(taskDownloader_ExceptionOccured);
152            resultsLock.Dispose();
153            taskDownloader.Dispose();
154        }
155        #endregion
156    }
157}
Note: See TracBrowser for help on using the repository browser.