[907] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2008 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;
|
---|
[800] | 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using System.Text;
|
---|
| 26 | using HeuristicLab.Hive.Contracts.Interfaces;
|
---|
| 27 | using HeuristicLab.Hive.Contracts.BusinessObjects;
|
---|
[902] | 28 | using HeuristicLab.Hive.Contracts;
|
---|
[967] | 29 | using HeuristicLab.Hive.Server.Core.InternalInterfaces.DataAccess;
|
---|
[1141] | 30 | using HeuristicLab.Hive.Server.Core.InternalInterfaces;
|
---|
[800] | 31 |
|
---|
| 32 | namespace HeuristicLab.Hive.Server.Core {
|
---|
[1141] | 33 | class JobManager: IJobManager, IInternalJobManager {
|
---|
[820] | 34 |
|
---|
[967] | 35 | IJobAdapter jobAdapter;
|
---|
[1139] | 36 | IJobResultsAdapter jobResultAdapter;
|
---|
[1133] | 37 | ILifecycleManager lifecycleManager;
|
---|
[820] | 38 |
|
---|
[800] | 39 | #region IJobManager Members
|
---|
| 40 |
|
---|
[820] | 41 | public JobManager() {
|
---|
[967] | 42 | jobAdapter = ServiceLocator.GetJobAdapter();
|
---|
[1139] | 43 | jobResultAdapter = ServiceLocator.GetJobResultsAdapter();
|
---|
[1133] | 44 |
|
---|
| 45 | lifecycleManager = ServiceLocator.GetLifecycleManager();
|
---|
| 46 |
|
---|
| 47 | lifecycleManager.RegisterStartup(new EventHandler(lifecycleManager_OnStartup));
|
---|
| 48 | lifecycleManager.RegisterStartup(new EventHandler(lifecycleManager_OnShutdown));
|
---|
[820] | 49 | }
|
---|
| 50 |
|
---|
[1170] | 51 | private JobResult GetLastJobResult(Job job) {
|
---|
[1160] | 52 | List<JobResult> allJobResults = new List<JobResult>(jobResultAdapter.GetResultsOf(job));
|
---|
[1139] | 53 | JobResult lastJobResult = null;
|
---|
| 54 | foreach (JobResult jR in allJobResults) {
|
---|
[1160] | 55 | // if lastJobResult was before the current jobResult the lastJobResult must be updated
|
---|
[1170] | 56 | if (lastJobResult == null ||
|
---|
[1160] | 57 | (jR.timestamp > lastJobResult.timestamp))
|
---|
| 58 | lastJobResult = jR;
|
---|
[1139] | 59 | }
|
---|
[1170] | 60 | return lastJobResult;
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | public void ResetJobsDependingOnResults(Job job) {
|
---|
| 64 | JobResult lastJobResult = GetLastJobResult(job);
|
---|
[1139] | 65 | if (lastJobResult != null) {
|
---|
| 66 | job.Percentage = lastJobResult.Percentage;
|
---|
| 67 | job.SerializedJob = lastJobResult.Result;
|
---|
| 68 | } else {
|
---|
| 69 | job.Percentage = 0;
|
---|
| 70 | }
|
---|
[1160] | 71 |
|
---|
| 72 | job.Client = null;
|
---|
| 73 | job.State = State.offline;
|
---|
| 74 |
|
---|
[1139] | 75 | jobAdapter.Update(job);
|
---|
| 76 | }
|
---|
| 77 |
|
---|
[1133] | 78 | void checkForDeadJobs() {
|
---|
| 79 | List<Job> allJobs = new List<Job>(jobAdapter.GetAll());
|
---|
| 80 | foreach (Job curJob in allJobs) {
|
---|
| 81 | if (curJob.State == State.calculating) {
|
---|
[1141] | 82 | ResetJobsDependingOnResults(curJob);
|
---|
[1133] | 83 | }
|
---|
| 84 | }
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | void lifecycleManager_OnStartup(object sender, EventArgs e) {
|
---|
| 88 | checkForDeadJobs();
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | void lifecycleManager_OnShutdown(object sender, EventArgs e) {
|
---|
| 92 | checkForDeadJobs();
|
---|
| 93 | }
|
---|
| 94 |
|
---|
[1121] | 95 | /// <summary>
|
---|
| 96 | /// returns all jobs stored in the database
|
---|
| 97 | /// </summary>
|
---|
| 98 | /// <returns></returns>
|
---|
[902] | 99 | public ResponseList<Job> GetAllJobs() {
|
---|
[907] | 100 | ResponseList<Job> response = new ResponseList<Job>();
|
---|
[967] | 101 |
|
---|
[995] | 102 | response.List = new List<Job>(jobAdapter.GetAll());
|
---|
[967] | 103 | response.Success = true;
|
---|
| 104 | response.StatusMessage = ApplicationConstants.RESPONSE_JOB_ALL_JOBS;
|
---|
| 105 |
|
---|
[907] | 106 | return response;
|
---|
[800] | 107 | }
|
---|
| 108 |
|
---|
[1121] | 109 | /// <summary>
|
---|
| 110 | /// Adds a new job into the database
|
---|
| 111 | /// </summary>
|
---|
| 112 | /// <param name="job"></param>
|
---|
| 113 | /// <returns></returns>
|
---|
[967] | 114 | public ResponseObject<Job> AddNewJob(Job job) {
|
---|
| 115 | ResponseObject<Job> response = new ResponseObject<Job>();
|
---|
| 116 |
|
---|
| 117 | if (job != null) {
|
---|
[1024] | 118 | if (job.State != State.offline) {
|
---|
| 119 | response.Success = false;
|
---|
| 120 | response.StatusMessage = ApplicationConstants.RESPONSE_JOB_JOBSTATE_MUST_BE_OFFLINE;
|
---|
| 121 | return response;
|
---|
| 122 | }
|
---|
[995] | 123 | if (job.Id != 0) {
|
---|
[967] | 124 | response.Success = false;
|
---|
| 125 | response.StatusMessage = ApplicationConstants.RESPONSE_JOB_ID_MUST_NOT_BE_SET;
|
---|
| 126 | return response;
|
---|
| 127 | }
|
---|
[1120] | 128 | if (job.SerializedJob == null) {
|
---|
| 129 | response.StatusMessage = ApplicationConstants.RESPONSE_JOB_JOB_NULL;
|
---|
| 130 | response.Success = false;
|
---|
| 131 | return response;
|
---|
| 132 | }
|
---|
| 133 |
|
---|
[1170] | 134 | job.DateCreated = DateTime.Now;
|
---|
[995] | 135 | jobAdapter.Update(job);
|
---|
[967] | 136 | response.Success = true;
|
---|
| 137 | response.Obj = job;
|
---|
| 138 | response.StatusMessage = ApplicationConstants.RESPONSE_JOB_JOB_ADDED;
|
---|
[1120] | 139 | } else {
|
---|
| 140 | response.Success = false;
|
---|
| 141 | response.StatusMessage = ApplicationConstants.RESPONSE_JOB_JOB_NULL;
|
---|
[967] | 142 | }
|
---|
| 143 |
|
---|
| 144 | return response;
|
---|
| 145 | }
|
---|
| 146 |
|
---|
[1121] | 147 | /// <summary>
|
---|
| 148 | /// Removes a job from the database
|
---|
| 149 | /// </summary>
|
---|
| 150 | /// <param name="jobId"></param>
|
---|
| 151 | /// <returns></returns>
|
---|
[967] | 152 | public Response RemoveJob(long jobId) {
|
---|
| 153 | Response response = new Response();
|
---|
| 154 |
|
---|
[995] | 155 | Job job = jobAdapter.GetById(jobId);
|
---|
[967] | 156 | if (job == null) {
|
---|
| 157 | response.Success = false;
|
---|
| 158 | response.StatusMessage = ApplicationConstants.RESPONSE_JOB_JOB_DOESNT_EXIST;
|
---|
| 159 | return response;
|
---|
| 160 | }
|
---|
[995] | 161 | jobAdapter.Delete(job);
|
---|
[967] | 162 | response.Success = false;
|
---|
| 163 | response.StatusMessage = ApplicationConstants.RESPONSE_JOB_JOB_REMOVED;
|
---|
| 164 |
|
---|
| 165 | return response;
|
---|
| 166 | }
|
---|
| 167 |
|
---|
[1172] | 168 | public ResponseObject<JobResult> GetLastJobResultOf(long jobId) {
|
---|
[1170] | 169 | ResponseObject<JobResult> response = new ResponseObject<JobResult>();
|
---|
| 170 | response.Success = true;
|
---|
| 171 | response.StatusMessage = ApplicationConstants.RESPONSE_JOB_JOB_RESULT_SENT;
|
---|
| 172 | response.Obj = GetLastJobResult(jobAdapter.GetById(jobId));
|
---|
| 173 |
|
---|
| 174 | return response;
|
---|
| 175 | }
|
---|
| 176 |
|
---|
[800] | 177 | #endregion
|
---|
| 178 | }
|
---|
| 179 | }
|
---|