Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Hive.Server.Core/3.2/JobManager.cs @ 1574

Last change on this file since 1574 was 1530, checked in by gkronber, 16 years ago

Moved source files of plugins Hive ... Visualization.Test into version-specific sub-folders. #576

File size: 8.2 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using HeuristicLab.Hive.Contracts.Interfaces;
27using HeuristicLab.Hive.Contracts.BusinessObjects;
28using HeuristicLab.Hive.Contracts;
29using HeuristicLab.Hive.Server.DataAccess;
30using HeuristicLab.Hive.Server.Core.InternalInterfaces;
31using HeuristicLab.DataAccess.Interfaces;
32
33namespace HeuristicLab.Hive.Server.Core {
34  class JobManager: IJobManager, IInternalJobManager {
35
36    ISessionFactory factory;
37    ILifecycleManager lifecycleManager;
38
39    #region IJobManager Members
40
41    public JobManager() {
42      factory = ServiceLocator.GetSessionFactory();
43      lifecycleManager = ServiceLocator.GetLifecycleManager();
44
45      lifecycleManager.RegisterStartup(new EventHandler(lifecycleManager_OnStartup));
46      lifecycleManager.RegisterStartup(new EventHandler(lifecycleManager_OnShutdown));
47    }
48
49    private JobResult GetLastJobResult(Job job) {
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;
65      }
66      finally {
67        if (session != null)
68          session.EndSession();
69      }
70    }
71
72    public void ResetJobsDependingOnResults(Job job) {
73      ISession session = factory.GetSessionForCurrentThread();
74
75      try {
76        IJobAdapter jobAdapter =
77            session.GetDataAdapter<Job, IJobAdapter>();
78
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      }
96    }
97
98    void checkForDeadJobs() {
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       }
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
126    /// <summary>
127    /// returns all jobs stored in the database
128    /// </summary>
129    /// <returns></returns>
130    public ResponseList<Job> GetAllJobs() {
131       ISession session = factory.GetSessionForCurrentThread();
132
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       }
149    }
150
151    /// <summary>
152    /// Adds a new job into the database
153    /// </summary>
154    /// <param name="job"></param>
155    /// <returns></returns>
156    public ResponseObject<Job> AddNewJob(Job job) {
157      ISession session = factory.GetSessionForCurrentThread();
158
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 {
188          response.Success = false;
189          response.StatusMessage = ApplicationConstants.RESPONSE_JOB_JOB_NULL;
190        }
191
192        return response;
193      }
194      finally {
195        if (session != null)
196          session.EndSession();
197      }
198    }
199
200    /// <summary>
201    /// Removes a job from the database
202    /// </summary>
203    /// <param name="jobId"></param>
204    /// <returns></returns>
205    public Response RemoveJob(Guid jobId) {
206      ISession session = factory.GetSessionForCurrentThread();
207
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);
220        response.Success = false;
221        response.StatusMessage = ApplicationConstants.RESPONSE_JOB_JOB_REMOVED;
222
223        return response;
224      }
225      finally {
226        if (session != null)
227          session.EndSession();
228      }
229    }
230
231    public ResponseObject<JobResult> GetLastJobResultOf(Guid jobId, bool requested) {
232      ISession session = factory.GetSessionForCurrentThread();
233
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      }
249    }
250
251    public Response RequestSnapshot(Guid jobId) {
252      ISession session = factory.GetSessionForCurrentThread();
253      Response response = new Response();
254     
255      try {
256       
257
258        return response;
259      }
260      finally {
261        if (session != null)
262          session.EndSession();
263      }
264    }
265
266    public Response AbortJob(Guid jobId) {
267      throw new NotImplementedException();
268    }
269
270    #endregion
271  }
272}
Note: See TracBrowser for help on using the repository browser.