[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;
|
---|
[1377] | 29 | using HeuristicLab.Hive.Server.DataAccess;
|
---|
[1141] | 30 | using HeuristicLab.Hive.Server.Core.InternalInterfaces;
|
---|
[1468] | 31 | using HeuristicLab.DataAccess.Interfaces;
|
---|
[800] | 32 |
|
---|
| 33 | namespace HeuristicLab.Hive.Server.Core {
|
---|
[1141] | 34 | class JobManager: IJobManager, IInternalJobManager {
|
---|
[820] | 35 |
|
---|
[1468] | 36 | ISessionFactory factory;
|
---|
[1133] | 37 | ILifecycleManager lifecycleManager;
|
---|
[820] | 38 |
|
---|
[800] | 39 | #region IJobManager Members
|
---|
| 40 |
|
---|
[820] | 41 | public JobManager() {
|
---|
[1468] | 42 | factory = ServiceLocator.GetSessionFactory();
|
---|
[1133] | 43 | lifecycleManager = ServiceLocator.GetLifecycleManager();
|
---|
| 44 |
|
---|
| 45 | lifecycleManager.RegisterStartup(new EventHandler(lifecycleManager_OnStartup));
|
---|
| 46 | lifecycleManager.RegisterStartup(new EventHandler(lifecycleManager_OnShutdown));
|
---|
[820] | 47 | }
|
---|
| 48 |
|
---|
[1170] | 49 | private JobResult GetLastJobResult(Job job) {
|
---|
[1468] | 50 | ISession session = factory.GetSessionForCurrentThread();
|
---|
| 51 |
|
---|
| 52 | try {
|
---|
| 53 | IJobResultsAdapter jobResultAdapter =
|
---|
| 54 | session.GetDataAdapter<JobResult, IJobResultsAdapter>();
|
---|
| 55 |
|
---|
| 56 | List<JobResult> allJobResults = new List<JobResult>(jobResultAdapter.GetResultsOf(job));
|
---|
| 57 | JobResult lastJobResult = null;
|
---|
| 58 | foreach (JobResult jR in allJobResults) {
|
---|
| 59 | // if lastJobResult was before the current jobResult the lastJobResult must be updated
|
---|
| 60 | if (lastJobResult == null ||
|
---|
| 61 | (jR.timestamp > lastJobResult.timestamp))
|
---|
| 62 | lastJobResult = jR;
|
---|
| 63 | }
|
---|
| 64 | return lastJobResult;
|
---|
[1139] | 65 | }
|
---|
[1468] | 66 | finally {
|
---|
| 67 | if (session != null)
|
---|
| 68 | session.EndSession();
|
---|
| 69 | }
|
---|
[1170] | 70 | }
|
---|
| 71 |
|
---|
| 72 | public void ResetJobsDependingOnResults(Job job) {
|
---|
[1468] | 73 | ISession session = factory.GetSessionForCurrentThread();
|
---|
[1160] | 74 |
|
---|
[1468] | 75 | try {
|
---|
| 76 | IJobAdapter jobAdapter =
|
---|
| 77 | session.GetDataAdapter<Job, IJobAdapter>();
|
---|
[1160] | 78 |
|
---|
[1468] | 79 | JobResult lastJobResult = GetLastJobResult(job);
|
---|
| 80 | if (lastJobResult != null) {
|
---|
| 81 | job.Percentage = lastJobResult.Percentage;
|
---|
| 82 | job.SerializedJob = lastJobResult.Result;
|
---|
| 83 | } else {
|
---|
| 84 | job.Percentage = 0;
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | job.Client = null;
|
---|
| 88 | job.State = State.offline;
|
---|
| 89 |
|
---|
| 90 | jobAdapter.Update(job);
|
---|
| 91 | }
|
---|
| 92 | finally {
|
---|
| 93 | if (session != null)
|
---|
| 94 | session.EndSession();
|
---|
| 95 | }
|
---|
[1139] | 96 | }
|
---|
| 97 |
|
---|
[1133] | 98 | void checkForDeadJobs() {
|
---|
[1468] | 99 | ISession session = factory.GetSessionForCurrentThread();
|
---|
| 100 |
|
---|
| 101 | try {
|
---|
| 102 | IJobAdapter jobAdapter =
|
---|
| 103 | session.GetDataAdapter<Job, IJobAdapter>();
|
---|
| 104 |
|
---|
| 105 | List<Job> allJobs = new List<Job>(jobAdapter.GetAll());
|
---|
| 106 | foreach (Job curJob in allJobs) {
|
---|
| 107 | if (curJob.State == State.calculating) {
|
---|
| 108 | ResetJobsDependingOnResults(curJob);
|
---|
| 109 | }
|
---|
| 110 | }
|
---|
| 111 | }
|
---|
| 112 | finally {
|
---|
| 113 | if (session != null)
|
---|
| 114 | session.EndSession();
|
---|
| 115 | }
|
---|
[1133] | 116 | }
|
---|
| 117 |
|
---|
| 118 | void lifecycleManager_OnStartup(object sender, EventArgs e) {
|
---|
| 119 | checkForDeadJobs();
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | void lifecycleManager_OnShutdown(object sender, EventArgs e) {
|
---|
| 123 | checkForDeadJobs();
|
---|
| 124 | }
|
---|
| 125 |
|
---|
[1121] | 126 | /// <summary>
|
---|
| 127 | /// returns all jobs stored in the database
|
---|
| 128 | /// </summary>
|
---|
| 129 | /// <returns></returns>
|
---|
[902] | 130 | public ResponseList<Job> GetAllJobs() {
|
---|
[1468] | 131 | ISession session = factory.GetSessionForCurrentThread();
|
---|
[967] | 132 |
|
---|
[1468] | 133 | try {
|
---|
| 134 | IJobAdapter jobAdapter =
|
---|
| 135 | session.GetDataAdapter<Job, IJobAdapter>();
|
---|
| 136 |
|
---|
| 137 | ResponseList<Job> response = new ResponseList<Job>();
|
---|
| 138 |
|
---|
| 139 | response.List = new List<Job>(jobAdapter.GetAll());
|
---|
| 140 | response.Success = true;
|
---|
| 141 | response.StatusMessage = ApplicationConstants.RESPONSE_JOB_ALL_JOBS;
|
---|
| 142 |
|
---|
| 143 | return response;
|
---|
| 144 | }
|
---|
| 145 | finally {
|
---|
| 146 | if (session != null)
|
---|
| 147 | session.EndSession();
|
---|
| 148 | }
|
---|
[800] | 149 | }
|
---|
| 150 |
|
---|
[1121] | 151 | /// <summary>
|
---|
| 152 | /// Adds a new job into the database
|
---|
| 153 | /// </summary>
|
---|
| 154 | /// <param name="job"></param>
|
---|
| 155 | /// <returns></returns>
|
---|
[967] | 156 | public ResponseObject<Job> AddNewJob(Job job) {
|
---|
[1468] | 157 | ISession session = factory.GetSessionForCurrentThread();
|
---|
[967] | 158 |
|
---|
[1468] | 159 | try {
|
---|
| 160 | IJobAdapter jobAdapter =
|
---|
| 161 | session.GetDataAdapter<Job, IJobAdapter>();
|
---|
| 162 |
|
---|
| 163 | ResponseObject<Job> response = new ResponseObject<Job>();
|
---|
| 164 |
|
---|
| 165 | if (job != null) {
|
---|
| 166 | if (job.State != State.offline) {
|
---|
| 167 | response.Success = false;
|
---|
| 168 | response.StatusMessage = ApplicationConstants.RESPONSE_JOB_JOBSTATE_MUST_BE_OFFLINE;
|
---|
| 169 | return response;
|
---|
| 170 | }
|
---|
| 171 | if (job.Id != Guid.Empty) {
|
---|
| 172 | response.Success = false;
|
---|
| 173 | response.StatusMessage = ApplicationConstants.RESPONSE_JOB_ID_MUST_NOT_BE_SET;
|
---|
| 174 | return response;
|
---|
| 175 | }
|
---|
| 176 | if (job.SerializedJob == null) {
|
---|
| 177 | response.StatusMessage = ApplicationConstants.RESPONSE_JOB_JOB_NULL;
|
---|
| 178 | response.Success = false;
|
---|
| 179 | return response;
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | job.DateCreated = DateTime.Now;
|
---|
| 183 | jobAdapter.Update(job);
|
---|
| 184 | response.Success = true;
|
---|
| 185 | response.Obj = job;
|
---|
| 186 | response.StatusMessage = ApplicationConstants.RESPONSE_JOB_JOB_ADDED;
|
---|
| 187 | } else {
|
---|
[1024] | 188 | response.Success = false;
|
---|
[1120] | 189 | response.StatusMessage = ApplicationConstants.RESPONSE_JOB_JOB_NULL;
|
---|
| 190 | }
|
---|
| 191 |
|
---|
[1468] | 192 | return response;
|
---|
[967] | 193 | }
|
---|
[1468] | 194 | finally {
|
---|
| 195 | if (session != null)
|
---|
| 196 | session.EndSession();
|
---|
| 197 | }
|
---|
[967] | 198 | }
|
---|
| 199 |
|
---|
[1121] | 200 | /// <summary>
|
---|
| 201 | /// Removes a job from the database
|
---|
| 202 | /// </summary>
|
---|
| 203 | /// <param name="jobId"></param>
|
---|
| 204 | /// <returns></returns>
|
---|
[1449] | 205 | public Response RemoveJob(Guid jobId) {
|
---|
[1468] | 206 | ISession session = factory.GetSessionForCurrentThread();
|
---|
[967] | 207 |
|
---|
[1468] | 208 | try {
|
---|
| 209 | IJobAdapter jobAdapter =
|
---|
| 210 | session.GetDataAdapter<Job, IJobAdapter>();
|
---|
| 211 | Response response = new Response();
|
---|
| 212 |
|
---|
| 213 | Job job = jobAdapter.GetById(jobId);
|
---|
| 214 | if (job == null) {
|
---|
| 215 | response.Success = false;
|
---|
| 216 | response.StatusMessage = ApplicationConstants.RESPONSE_JOB_JOB_DOESNT_EXIST;
|
---|
| 217 | return response;
|
---|
| 218 | }
|
---|
| 219 | jobAdapter.Delete(job);
|
---|
[967] | 220 | response.Success = false;
|
---|
[1468] | 221 | response.StatusMessage = ApplicationConstants.RESPONSE_JOB_JOB_REMOVED;
|
---|
| 222 |
|
---|
[967] | 223 | return response;
|
---|
| 224 | }
|
---|
[1468] | 225 | finally {
|
---|
| 226 | if (session != null)
|
---|
| 227 | session.EndSession();
|
---|
| 228 | }
|
---|
[967] | 229 | }
|
---|
| 230 |
|
---|
[1449] | 231 | public ResponseObject<JobResult> GetLastJobResultOf(Guid jobId) {
|
---|
[1468] | 232 | ISession session = factory.GetSessionForCurrentThread();
|
---|
[1170] | 233 |
|
---|
[1468] | 234 | try {
|
---|
| 235 | IJobAdapter jobAdapter =
|
---|
| 236 | session.GetDataAdapter<Job, IJobAdapter>();
|
---|
| 237 |
|
---|
| 238 | ResponseObject<JobResult> response = new ResponseObject<JobResult>();
|
---|
| 239 | response.Success = true;
|
---|
| 240 | response.StatusMessage = ApplicationConstants.RESPONSE_JOB_JOB_RESULT_SENT;
|
---|
| 241 | response.Obj = GetLastJobResult(jobAdapter.GetById(jobId));
|
---|
| 242 |
|
---|
| 243 | return response;
|
---|
| 244 | }
|
---|
| 245 | finally {
|
---|
| 246 | if(session != null)
|
---|
| 247 | session.EndSession();
|
---|
| 248 | }
|
---|
[1170] | 249 | }
|
---|
| 250 |
|
---|
[800] | 251 | #endregion
|
---|
| 252 | }
|
---|
| 253 | }
|
---|