[907] | 1 | #region License Information
|
---|
[3931] | 2 |
|
---|
[907] | 3 | /* HeuristicLab
|
---|
| 4 | * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
| 5 | *
|
---|
| 6 | * This file is part of HeuristicLab.
|
---|
| 7 | *
|
---|
| 8 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 9 | * it under the terms of the GNU General Public License as published by
|
---|
| 10 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 11 | * (at your option) any later version.
|
---|
| 12 | *
|
---|
| 13 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 16 | * GNU General Public License for more details.
|
---|
| 17 | *
|
---|
| 18 | * You should have received a copy of the GNU General Public License
|
---|
| 19 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 20 | */
|
---|
[3931] | 21 |
|
---|
[907] | 22 | #endregion
|
---|
| 23 |
|
---|
| 24 | using System;
|
---|
[800] | 25 | using System.Collections.Generic;
|
---|
| 26 | using System.Linq;
|
---|
| 27 | using System.Text;
|
---|
| 28 | using HeuristicLab.Hive.Contracts.Interfaces;
|
---|
| 29 | using HeuristicLab.Hive.Contracts.BusinessObjects;
|
---|
[902] | 30 | using HeuristicLab.Hive.Contracts;
|
---|
[1377] | 31 | using HeuristicLab.Hive.Server.DataAccess;
|
---|
[1141] | 32 | using HeuristicLab.Hive.Server.Core.InternalInterfaces;
|
---|
[1468] | 33 | using HeuristicLab.DataAccess.Interfaces;
|
---|
[1948] | 34 | using System.Data;
|
---|
[2117] | 35 | using System.IO;
|
---|
[2608] | 36 | using HeuristicLab.Tracing;
|
---|
[3011] | 37 | using System.Transactions;
|
---|
[3220] | 38 | using HeuristicLab.Hive.Server.LINQDataAccess;
|
---|
[3578] | 39 | using IsolationLevel=System.Transactions.IsolationLevel;
|
---|
[800] | 40 |
|
---|
| 41 | namespace HeuristicLab.Hive.Server.Core {
|
---|
[3931] | 42 | internal class JobManager : IJobManager, IInternalJobManager {
|
---|
[3011] | 43 | //ISessionFactory factory;
|
---|
[3931] | 44 | private ILifecycleManager lifecycleManager;
|
---|
[820] | 45 |
|
---|
[800] | 46 | #region IJobManager Members
|
---|
| 47 |
|
---|
[820] | 48 | public JobManager() {
|
---|
[3011] | 49 | //factory = ServiceLocator.GetSessionFactory();
|
---|
[1133] | 50 | lifecycleManager = ServiceLocator.GetLifecycleManager();
|
---|
| 51 |
|
---|
| 52 | lifecycleManager.RegisterStartup(new EventHandler(lifecycleManager_OnStartup));
|
---|
| 53 | lifecycleManager.RegisterStartup(new EventHandler(lifecycleManager_OnShutdown));
|
---|
[820] | 54 | }
|
---|
| 55 |
|
---|
[3931] | 56 | private JobDto GetLastJobResult(Guid jobId) {
|
---|
[3011] | 57 | return DaoLocator.JobDao.FindById(jobId);
|
---|
[1170] | 58 | }
|
---|
| 59 |
|
---|
[1139] | 60 |
|
---|
[3931] | 61 | private void checkForDeadJobs() {
|
---|
[3578] | 62 | Logger.Info("Searching for dead Jobs");
|
---|
[3931] | 63 | using (
|
---|
| 64 | TransactionScope scope = new TransactionScope(TransactionScopeOption.Required,
|
---|
| 65 | new TransactionOptions
|
---|
| 66 | {IsolationLevel = ApplicationConstants.ISOLATION_LEVEL_SCOPE})) {
|
---|
[3011] | 67 | List<JobDto> allJobs = new List<JobDto>(DaoLocator.JobDao.FindAll());
|
---|
| 68 | foreach (JobDto curJob in allJobs) {
|
---|
[3931] | 69 | if (curJob.State != State.calculating && curJob.State != State.finished) {
|
---|
| 70 | DaoLocator.JobDao.SetJobOffline(curJob);
|
---|
[3011] | 71 | }
|
---|
| 72 | }
|
---|
| 73 | scope.Complete();
|
---|
| 74 | }
|
---|
| 75 | DaoLocator.DestroyContext();
|
---|
[1133] | 76 | }
|
---|
| 77 |
|
---|
[3931] | 78 | private void lifecycleManager_OnStartup(object sender, EventArgs e) {
|
---|
| 79 | Logger.Info("Startup Event Fired, Checking DB for consistency");
|
---|
[1133] | 80 | checkForDeadJobs();
|
---|
[3931] | 81 | Logger.Info("Startup Event Done");
|
---|
[1133] | 82 | }
|
---|
| 83 |
|
---|
[3931] | 84 | private void lifecycleManager_OnShutdown(object sender, EventArgs e) {
|
---|
| 85 | Logger.Info("Startup Event Fired, Checking DB for consistency");
|
---|
[1133] | 86 | checkForDeadJobs();
|
---|
[3931] | 87 | Logger.Info("Startup Event Done");
|
---|
[1133] | 88 | }
|
---|
| 89 |
|
---|
[1121] | 90 | /// <summary>
|
---|
| 91 | /// returns all jobs stored in the database
|
---|
| 92 | /// </summary>
|
---|
| 93 | /// <returns></returns>
|
---|
[3011] | 94 | public ResponseList<JobDto> GetAllJobs() {
|
---|
[3931] | 95 | ResponseList<JobDto> response = new ResponseList<JobDto>();
|
---|
[1468] | 96 |
|
---|
[3931] | 97 | response.List = new List<JobDto>(DaoLocator.JobDao.FindAll());
|
---|
| 98 | response.Success = true;
|
---|
| 99 | response.StatusMessage = ApplicationConstants.RESPONSE_JOB_ALL_JOBS;
|
---|
[1468] | 100 |
|
---|
[3931] | 101 | return response;
|
---|
[800] | 102 | }
|
---|
| 103 |
|
---|
[3578] | 104 | public ResponseList<JobDto> GetAllJobsWithFilter(State jobState, int offset, int count) {
|
---|
| 105 | ResponseList<JobDto> response = new ResponseList<JobDto>();
|
---|
| 106 | response.List = new List<JobDto>(DaoLocator.JobDao.FindWithLimitations(jobState, offset, count));
|
---|
[4042] | 107 | response.Success = true;
|
---|
[3578] | 108 | return response;
|
---|
[3931] | 109 | }
|
---|
[3578] | 110 |
|
---|
[1121] | 111 | /// <summary>
|
---|
[2117] | 112 | /// Gets the streamed job
|
---|
| 113 | /// </summary>
|
---|
| 114 | /// <param name="jobId"></param>
|
---|
| 115 | /// <returns></returns>
|
---|
[3931] | 116 | public Stream GetJobStreamById(Guid jobId) {
|
---|
[3011] | 117 | return DaoLocator.JobDao.GetSerializedJobStream(jobId);
|
---|
[2117] | 118 | }
|
---|
| 119 |
|
---|
| 120 | /// <summary>
|
---|
[2005] | 121 | /// returns the job with the specified id
|
---|
| 122 | /// </summary>
|
---|
| 123 | /// <returns></returns>
|
---|
[3011] | 124 | public ResponseObject<JobDto> GetJobById(Guid jobId) {
|
---|
[3931] | 125 | ResponseObject<JobDto> response = new ResponseObject<JobDto>();
|
---|
[2005] | 126 |
|
---|
[3011] | 127 | response.Obj = DaoLocator.JobDao.FindById(jobId);
|
---|
[3931] | 128 | if (response.Obj != null) {
|
---|
| 129 | response.Success = true;
|
---|
| 130 | response.StatusMessage = ApplicationConstants.RESPONSE_JOB_GET_JOB_BY_ID;
|
---|
[2005] | 131 | }
|
---|
[3931] | 132 | else {
|
---|
| 133 | response.Success = false;
|
---|
| 134 | response.StatusMessage = ApplicationConstants.RESPONSE_JOB_JOB_DOESNT_EXIST;
|
---|
| 135 | }
|
---|
[2005] | 136 |
|
---|
[3931] | 137 | return response;
|
---|
| 138 | }
|
---|
| 139 |
|
---|
[3578] | 140 | public ResponseObject<JobDto> GetJobByIdWithDetails(Guid jobId) {
|
---|
| 141 | ResponseObject<JobDto> job = new ResponseObject<JobDto>();
|
---|
| 142 | job.Obj = DaoLocator.JobDao.FindById(jobId);
|
---|
| 143 | if (job.Obj != null) {
|
---|
| 144 | job.Success = true;
|
---|
| 145 | job.StatusMessage = ApplicationConstants.RESPONSE_JOB_GET_JOB_BY_ID;
|
---|
| 146 |
|
---|
| 147 | job.Obj.Client = DaoLocator.ClientDao.GetClientForJob(jobId);
|
---|
[3931] | 148 | }
|
---|
| 149 | else {
|
---|
[3578] | 150 | job.Success = false;
|
---|
| 151 | job.StatusMessage = ApplicationConstants.RESPONSE_JOB_JOB_DOESNT_EXIST;
|
---|
[3931] | 152 | }
|
---|
[3578] | 153 | return job;
|
---|
| 154 | }
|
---|
| 155 |
|
---|
[3220] | 156 | public ResponseObject<JobDto> AddJobWithGroupStrings(SerializedJob job, IEnumerable<string> resources) {
|
---|
| 157 | IClientGroupDao cgd = DaoLocator.ClientGroupDao;
|
---|
| 158 | foreach (string res in resources) {
|
---|
[3931] | 159 | foreach (ClientGroupDto cg in cgd.FindByName(res)) {
|
---|
[3220] | 160 | job.JobInfo.AssignedResourceIds.Add(cg.Id);
|
---|
| 161 | }
|
---|
| 162 | }
|
---|
| 163 | return AddNewJob(job);
|
---|
| 164 | }
|
---|
| 165 |
|
---|
[2005] | 166 | /// <summary>
|
---|
[1121] | 167 | /// Adds a new job into the database
|
---|
| 168 | /// </summary>
|
---|
| 169 | /// <param name="job"></param>
|
---|
| 170 | /// <returns></returns>
|
---|
[3011] | 171 | public ResponseObject<JobDto> AddNewJob(SerializedJob job) {
|
---|
[3931] | 172 | ResponseObject<JobDto> response = new ResponseObject<JobDto>();
|
---|
[1468] | 173 |
|
---|
[3931] | 174 | if (job != null && job.JobInfo != null) {
|
---|
| 175 | if (job.JobInfo.State != State.offline) {
|
---|
[1024] | 176 | response.Success = false;
|
---|
[3931] | 177 | response.StatusMessage = ApplicationConstants.RESPONSE_JOB_JOBSTATE_MUST_BE_OFFLINE;
|
---|
| 178 | return response;
|
---|
| 179 | }
|
---|
| 180 | if (job.JobInfo.Id != Guid.Empty) {
|
---|
| 181 | response.Success = false;
|
---|
| 182 | response.StatusMessage = ApplicationConstants.RESPONSE_JOB_ID_MUST_NOT_BE_SET;
|
---|
| 183 | return response;
|
---|
| 184 | }
|
---|
| 185 | if (job.SerializedJobData == null) {
|
---|
[1120] | 186 | response.StatusMessage = ApplicationConstants.RESPONSE_JOB_JOB_NULL;
|
---|
[3931] | 187 | response.Success = false;
|
---|
| 188 | return response;
|
---|
[1120] | 189 | }
|
---|
| 190 |
|
---|
[3931] | 191 | job.JobInfo.DateCreated = DateTime.Now;
|
---|
| 192 | DaoLocator.JobDao.InsertWithAttachedJob(job);
|
---|
| 193 | DaoLocator.PluginInfoDao.InsertPluginDependenciesForJob(job.JobInfo);
|
---|
| 194 |
|
---|
| 195 | response.Success = true;
|
---|
| 196 | response.Obj = job.JobInfo;
|
---|
| 197 | response.StatusMessage = ApplicationConstants.RESPONSE_JOB_JOB_ADDED;
|
---|
[967] | 198 | }
|
---|
[3931] | 199 | else {
|
---|
| 200 | response.Success = false;
|
---|
| 201 | response.StatusMessage = ApplicationConstants.RESPONSE_JOB_JOB_NULL;
|
---|
[1468] | 202 | }
|
---|
[967] | 203 |
|
---|
[3931] | 204 | return response;
|
---|
| 205 | }
|
---|
| 206 |
|
---|
[1121] | 207 | /// <summary>
|
---|
| 208 | /// Removes a job from the database
|
---|
| 209 | /// </summary>
|
---|
| 210 | /// <param name="jobId"></param>
|
---|
| 211 | /// <returns></returns>
|
---|
[1449] | 212 | public Response RemoveJob(Guid jobId) {
|
---|
[3931] | 213 | Response response = new Response();
|
---|
[1468] | 214 |
|
---|
[3011] | 215 | JobDto job = DaoLocator.JobDao.FindById(jobId);
|
---|
[3931] | 216 | if (job == null) {
|
---|
[967] | 217 | response.Success = false;
|
---|
[3931] | 218 | response.StatusMessage = ApplicationConstants.RESPONSE_JOB_JOB_DOESNT_EXIST;
|
---|
[967] | 219 | return response;
|
---|
| 220 | }
|
---|
[3931] | 221 | DaoLocator.JobDao.Delete(job);
|
---|
| 222 | response.Success = false;
|
---|
| 223 | response.StatusMessage = ApplicationConstants.RESPONSE_JOB_JOB_REMOVED;
|
---|
[967] | 224 |
|
---|
[3931] | 225 | return response;
|
---|
| 226 | }
|
---|
| 227 |
|
---|
[3011] | 228 | public ResponseObject<JobDto> GetLastJobResultOf(Guid jobId) {
|
---|
| 229 | ResponseObject<JobDto> result =
|
---|
| 230 | new ResponseObject<JobDto>();
|
---|
[2099] | 231 |
|
---|
[3931] | 232 | result.Obj =
|
---|
| 233 | GetLastJobResult(jobId);
|
---|
| 234 | result.Success =
|
---|
| 235 | result.Obj != null;
|
---|
[2099] | 236 |
|
---|
[3931] | 237 | return result;
|
---|
[2099] | 238 | }
|
---|
| 239 |
|
---|
[4042] | 240 | //Requested means: there MUST be a job result which gets sent back
|
---|
[3011] | 241 | public ResponseObject<SerializedJob>
|
---|
[4042] | 242 | GetLastSerializedJobResultOf(Guid jobId, bool requested, bool snapshot) {
|
---|
[3931] | 243 | ResponseObject<SerializedJob> response =
|
---|
| 244 | new ResponseObject<SerializedJob>();
|
---|
[1170] | 245 |
|
---|
[3011] | 246 | JobDto job = DaoLocator.JobDao.FindById(jobId);
|
---|
[4042] | 247 |
|
---|
| 248 | //if it's a snapshot but the result hasn't reached the server yet...
|
---|
| 249 | if (snapshot && (job.State == State.requestSnapshot || job.State == State.requestSnapshotSent)) {
|
---|
[3931] | 250 | response.Success = true;
|
---|
| 251 | response.StatusMessage = ApplicationConstants.RESPONSE_JOB_RESULT_NOT_YET_HERE;
|
---|
[1468] | 252 |
|
---|
| 253 | return response;
|
---|
| 254 | }
|
---|
[1170] | 255 |
|
---|
[4042] | 256 | //if it's NOT a snapshot, NEITHER request NOR is it finished
|
---|
| 257 | if(!requested && !snapshot && job.State != State.finished) {
|
---|
| 258 | response.Success = true;
|
---|
| 259 | response.StatusMessage = ApplicationConstants.RESPONSE_JOB_RESULT_NOT_YET_HERE;
|
---|
| 260 |
|
---|
| 261 | return response;
|
---|
| 262 | }
|
---|
| 263 |
|
---|
| 264 | //every other case - snapshot, job finished or it's requested
|
---|
[3931] | 265 | response.Success = true;
|
---|
| 266 | response.StatusMessage = ApplicationConstants.RESPONSE_JOB_JOB_RESULT_SENT;
|
---|
| 267 | response.Obj = new SerializedJob();
|
---|
| 268 | response.Obj.JobInfo = job;
|
---|
[4042] | 269 |
|
---|
[3931] | 270 | response.Obj.SerializedJobData =
|
---|
| 271 | DaoLocator.JobDao.GetBinaryJobFile(jobId);
|
---|
[4042] | 272 |
|
---|
[3931] | 273 | return response;
|
---|
| 274 | }
|
---|
[2099] | 275 |
|
---|
[3931] | 276 |
|
---|
[1509] | 277 | public Response RequestSnapshot(Guid jobId) {
|
---|
| 278 | Response response = new Response();
|
---|
| 279 |
|
---|
[1577] | 280 |
|
---|
[3931] | 281 | JobDto job = DaoLocator.JobDao.FindById(jobId);
|
---|
| 282 | if (job.State == State.requestSnapshot || job.State == State.requestSnapshotSent) {
|
---|
[1577] | 283 | response.Success = true;
|
---|
[3931] | 284 | response.StatusMessage = ApplicationConstants.RESPONSE_JOB_REQUEST_ALLREADY_SET;
|
---|
[1509] | 285 | return response;
|
---|
| 286 | }
|
---|
[3931] | 287 | if (job.State != State.calculating) {
|
---|
| 288 | response.Success = false;
|
---|
| 289 | response.StatusMessage = ApplicationConstants.RESPONSE_JOB_IS_NOT_BEEING_CALCULATED;
|
---|
| 290 | return response;
|
---|
[1509] | 291 | }
|
---|
[3931] | 292 | // job is in correct state
|
---|
| 293 | job.State = State.requestSnapshot;
|
---|
| 294 | DaoLocator.JobDao.Update(job);
|
---|
[1509] | 295 |
|
---|
[3931] | 296 | response.Success = true;
|
---|
| 297 | response.StatusMessage = ApplicationConstants.RESPONSE_JOB_REQUEST_SET;
|
---|
| 298 |
|
---|
| 299 | return response;
|
---|
| 300 | }
|
---|
| 301 |
|
---|
[1509] | 302 | public Response AbortJob(Guid jobId) {
|
---|
[1577] | 303 | Response response = new Response();
|
---|
| 304 |
|
---|
[3011] | 305 | JobDto job = DaoLocator.JobDao.FindById(jobId);
|
---|
[3931] | 306 | if (job == null) {
|
---|
| 307 | response.Success = false;
|
---|
| 308 | response.StatusMessage = ApplicationConstants.RESPONSE_JOB_JOB_DOESNT_EXIST;
|
---|
| 309 | return response; // no commit needed
|
---|
| 310 | }
|
---|
| 311 | if (job.State == State.abort) {
|
---|
[1577] | 312 | response.Success = true;
|
---|
[3931] | 313 | response.StatusMessage = ApplicationConstants.RESPONSE_JOB_ABORT_REQUEST_ALLREADY_SET;
|
---|
| 314 | return response; // no commit needed
|
---|
[1577] | 315 | }
|
---|
[3931] | 316 | if (job.State != State.calculating && job.State != State.requestSnapshot && job.State != State.requestSnapshotSent) {
|
---|
| 317 | response.Success = false;
|
---|
| 318 | response.StatusMessage = ApplicationConstants.RESPONSE_JOB_IS_NOT_BEEING_CALCULATED;
|
---|
| 319 | return response; // no commit needed
|
---|
[1577] | 320 | }
|
---|
[3931] | 321 | // job is in correct state
|
---|
| 322 | job.State = State.abort;
|
---|
| 323 | DaoLocator.JobDao.Update(job);
|
---|
[1509] | 324 |
|
---|
[3931] | 325 | response.Success = true;
|
---|
| 326 | response.StatusMessage = ApplicationConstants.RESPONSE_JOB_ABORT_REQUEST_SET;
|
---|
| 327 |
|
---|
| 328 | return response;
|
---|
| 329 | }
|
---|
| 330 |
|
---|
[1799] | 331 | public ResponseList<JobResult> GetAllJobResults(Guid jobId) {
|
---|
[3011] | 332 | return new ResponseList<JobResult>();
|
---|
[1627] | 333 | }
|
---|
[1932] | 334 |
|
---|
[3011] | 335 | public ResponseList<ProjectDto> GetAllProjects() {
|
---|
| 336 | return null;
|
---|
[1948] | 337 | }
|
---|
[1934] | 338 |
|
---|
[3011] | 339 | private Response createUpdateProject(ProjectDto project) {
|
---|
| 340 | return null;
|
---|
[1932] | 341 | }
|
---|
| 342 |
|
---|
[3011] | 343 | public Response CreateProject(ProjectDto project) {
|
---|
[1948] | 344 | return createUpdateProject(project);
|
---|
[1932] | 345 | }
|
---|
| 346 |
|
---|
[3011] | 347 | public Response ChangeProject(ProjectDto project) {
|
---|
[1948] | 348 | return createUpdateProject(project);
|
---|
[1932] | 349 | }
|
---|
| 350 |
|
---|
| 351 | public Response DeleteProject(Guid projectId) {
|
---|
[3011] | 352 | return null;
|
---|
[1932] | 353 | }
|
---|
| 354 |
|
---|
[3011] | 355 | public ResponseList<JobDto> GetJobsByProject(Guid projectId) {
|
---|
[3931] | 356 | return null;
|
---|
[1932] | 357 | }
|
---|
[3931] | 358 |
|
---|
| 359 | public byte[] GetSerializedJobDataById(Guid jobId) {
|
---|
| 360 | return DaoLocator.JobDao.GetBinaryJobFile(jobId);
|
---|
| 361 | }
|
---|
| 362 |
|
---|
| 363 | public void SetSerializedJobDataById(Guid jobId, byte[] data) {
|
---|
| 364 | DaoLocator.JobDao.SetBinaryJobFile(jobId, data);
|
---|
| 365 | }
|
---|
| 366 |
|
---|
[800] | 367 | #endregion
|
---|
| 368 | }
|
---|
[3931] | 369 | } |
---|