[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 |
|
---|
[1141] | 51 | public void ResetJobsDependingOnResults(Job job) {
|
---|
[1139] | 52 | List<JobResult> allJobResults = new List<JobResult>(jobResultAdapter.GetAll());
|
---|
| 53 | JobResult lastJobResult = null;
|
---|
| 54 | foreach (JobResult jR in allJobResults) {
|
---|
| 55 | if (jR.Job != null && jR.Job.Id == job.Id) {
|
---|
| 56 | if (lastJobResult != null) {
|
---|
| 57 | // if lastJobResult was before the current jobResult the lastJobResult must be updated
|
---|
| 58 | if ((jR.timestamp.Subtract(lastJobResult.timestamp)).Seconds > 0)
|
---|
| 59 | lastJobResult = jR;
|
---|
| 60 | }
|
---|
| 61 | }
|
---|
| 62 | }
|
---|
| 63 | if (lastJobResult != null) {
|
---|
| 64 | job.Client = null;
|
---|
| 65 | job.Percentage = lastJobResult.Percentage;
|
---|
| 66 | job.State = State.idle;
|
---|
| 67 | job.SerializedJob = lastJobResult.Result;
|
---|
| 68 | } else {
|
---|
| 69 | job.Client = null;
|
---|
| 70 | job.Percentage = 0;
|
---|
| 71 | job.State = State.idle;
|
---|
| 72 | }
|
---|
| 73 | jobAdapter.Update(job);
|
---|
| 74 | }
|
---|
| 75 |
|
---|
[1133] | 76 | void checkForDeadJobs() {
|
---|
| 77 | List<Job> allJobs = new List<Job>(jobAdapter.GetAll());
|
---|
| 78 | foreach (Job curJob in allJobs) {
|
---|
| 79 | if (curJob.State == State.calculating) {
|
---|
[1141] | 80 | ResetJobsDependingOnResults(curJob);
|
---|
[1133] | 81 | }
|
---|
| 82 | }
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | void lifecycleManager_OnStartup(object sender, EventArgs e) {
|
---|
| 86 | checkForDeadJobs();
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | void lifecycleManager_OnShutdown(object sender, EventArgs e) {
|
---|
| 90 | checkForDeadJobs();
|
---|
| 91 | }
|
---|
| 92 |
|
---|
[1121] | 93 | /// <summary>
|
---|
| 94 | /// returns all jobs stored in the database
|
---|
| 95 | /// </summary>
|
---|
| 96 | /// <returns></returns>
|
---|
[902] | 97 | public ResponseList<Job> GetAllJobs() {
|
---|
[907] | 98 | ResponseList<Job> response = new ResponseList<Job>();
|
---|
[967] | 99 |
|
---|
[995] | 100 | response.List = new List<Job>(jobAdapter.GetAll());
|
---|
[967] | 101 | response.Success = true;
|
---|
| 102 | response.StatusMessage = ApplicationConstants.RESPONSE_JOB_ALL_JOBS;
|
---|
| 103 |
|
---|
[907] | 104 | return response;
|
---|
[800] | 105 | }
|
---|
| 106 |
|
---|
[1121] | 107 | /// <summary>
|
---|
| 108 | /// Adds a new job into the database
|
---|
| 109 | /// </summary>
|
---|
| 110 | /// <param name="job"></param>
|
---|
| 111 | /// <returns></returns>
|
---|
[967] | 112 | public ResponseObject<Job> AddNewJob(Job job) {
|
---|
| 113 | ResponseObject<Job> response = new ResponseObject<Job>();
|
---|
| 114 |
|
---|
| 115 | if (job != null) {
|
---|
[1024] | 116 | if (job.State != State.offline) {
|
---|
| 117 | response.Success = false;
|
---|
| 118 | response.StatusMessage = ApplicationConstants.RESPONSE_JOB_JOBSTATE_MUST_BE_OFFLINE;
|
---|
| 119 | return response;
|
---|
| 120 | }
|
---|
[995] | 121 | if (job.Id != 0) {
|
---|
[967] | 122 | response.Success = false;
|
---|
| 123 | response.StatusMessage = ApplicationConstants.RESPONSE_JOB_ID_MUST_NOT_BE_SET;
|
---|
| 124 | return response;
|
---|
| 125 | }
|
---|
[1120] | 126 | if (job.SerializedJob == null) {
|
---|
| 127 | response.StatusMessage = ApplicationConstants.RESPONSE_JOB_JOB_NULL;
|
---|
| 128 | response.Success = false;
|
---|
| 129 | return response;
|
---|
| 130 | }
|
---|
| 131 |
|
---|
[995] | 132 | jobAdapter.Update(job);
|
---|
[967] | 133 | response.Success = true;
|
---|
| 134 | response.Obj = job;
|
---|
| 135 | response.StatusMessage = ApplicationConstants.RESPONSE_JOB_JOB_ADDED;
|
---|
[1120] | 136 | } else {
|
---|
| 137 | response.Success = false;
|
---|
| 138 | response.StatusMessage = ApplicationConstants.RESPONSE_JOB_JOB_NULL;
|
---|
[967] | 139 | }
|
---|
| 140 |
|
---|
| 141 | return response;
|
---|
| 142 | }
|
---|
| 143 |
|
---|
[1121] | 144 | /// <summary>
|
---|
| 145 | /// Removes a job from the database
|
---|
| 146 | /// </summary>
|
---|
| 147 | /// <param name="jobId"></param>
|
---|
| 148 | /// <returns></returns>
|
---|
[967] | 149 | public Response RemoveJob(long jobId) {
|
---|
| 150 | Response response = new Response();
|
---|
| 151 |
|
---|
[995] | 152 | Job job = jobAdapter.GetById(jobId);
|
---|
[967] | 153 | if (job == null) {
|
---|
| 154 | response.Success = false;
|
---|
| 155 | response.StatusMessage = ApplicationConstants.RESPONSE_JOB_JOB_DOESNT_EXIST;
|
---|
| 156 | return response;
|
---|
| 157 | }
|
---|
[995] | 158 | jobAdapter.Delete(job);
|
---|
[967] | 159 | response.Success = false;
|
---|
| 160 | response.StatusMessage = ApplicationConstants.RESPONSE_JOB_JOB_REMOVED;
|
---|
| 161 |
|
---|
| 162 | return response;
|
---|
| 163 | }
|
---|
| 164 |
|
---|
[800] | 165 | #endregion
|
---|
| 166 | }
|
---|
| 167 | }
|
---|