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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.IO;
|
---|
25 | using System.Linq;
|
---|
26 | using System.Threading;
|
---|
27 | using HeuristicLab.Clients.Hive;
|
---|
28 | using HeuristicLab.Core;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.HiveDrain {
|
---|
31 | /// <summary>
|
---|
32 | /// Retrieves all jobs and starts downloading their tasks
|
---|
33 | /// </summary>
|
---|
34 | public class JobDownloader {
|
---|
35 | public string RootLocation { get; set; }
|
---|
36 |
|
---|
37 | public string NamePattern { get; set; }
|
---|
38 |
|
---|
39 | public bool OneFile { get; set; }
|
---|
40 |
|
---|
41 | private ILog log;
|
---|
42 |
|
---|
43 | public JobDownloader(string location, string pattern, ILog log, bool oneFile = false) {
|
---|
44 | RootLocation = location;
|
---|
45 | NamePattern = pattern;
|
---|
46 | this.log = log;
|
---|
47 | OneFile = oneFile;
|
---|
48 | }
|
---|
49 |
|
---|
50 | public void Start() {
|
---|
51 | var jobsLoaded = HiveServiceLocator.Instance.CallHiveService<IEnumerable<Job>>(s => s.GetJobs());
|
---|
52 | Semaphore limitSemaphore = new Semaphore(HeuristicLabHiveDrainApplication.MaxParallelDownloads, HeuristicLabHiveDrainApplication.MaxParallelDownloads);
|
---|
53 |
|
---|
54 | var invalidChars = Path.GetInvalidFileNameChars();
|
---|
55 |
|
---|
56 | foreach (Job j in jobsLoaded) {
|
---|
57 | if (string.IsNullOrEmpty(NamePattern) || j.Name.Contains(NamePattern)) {
|
---|
58 | log.LogMessage(String.Format("\"{0}\": {1}", j.Name, j.Id));
|
---|
59 |
|
---|
60 |
|
---|
61 | var jobName = j.Name;
|
---|
62 | //handle invalid characters in fileNames
|
---|
63 | if (invalidChars.Any(c => jobName.Contains(c))) {
|
---|
64 | log.LogMessage("Job name contains characters that cannot be used as filename. Invalid characters are replaced with '_'.");
|
---|
65 | foreach (var c in invalidChars) {
|
---|
66 | jobName = jobName.Replace(c, '_');
|
---|
67 | }
|
---|
68 | }
|
---|
69 |
|
---|
70 | string jobPath = Path.Combine(RootLocation, String.Format("{0} - {1}", jobName, j.Id));
|
---|
71 |
|
---|
72 |
|
---|
73 | if (OneFile) {
|
---|
74 | JobTaskOneFileDownloader taskDownloader = new JobTaskOneFileDownloader(jobPath, j, limitSemaphore, log);
|
---|
75 | taskDownloader.Start();
|
---|
76 | }
|
---|
77 | else {
|
---|
78 | JobTaskDownloader taskDownloader = new JobTaskDownloader(jobPath, j, limitSemaphore, log);
|
---|
79 | taskDownloader.Start();
|
---|
80 | }
|
---|
81 | }
|
---|
82 | else {
|
---|
83 | log.LogMessage(String.Format("\"{0}\": {1} ---> ignored", j.Name, j.Id));
|
---|
84 | }
|
---|
85 | }
|
---|
86 | }
|
---|
87 | }
|
---|
88 | }
|
---|