Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2077 was 2005, checked in by svonolfe, 15 years ago

Updated jobManager (#431)

File size: 16.1 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;
32using System.Data;
33
34namespace HeuristicLab.Hive.Server.Core {
35  class JobManager: IJobManager, IInternalJobManager {
36
37    ISessionFactory factory;
38    ILifecycleManager lifecycleManager;
39
40    #region IJobManager Members
41
42    public JobManager() {
43      factory = ServiceLocator.GetSessionFactory();
44      lifecycleManager = ServiceLocator.GetLifecycleManager();
45
46      lifecycleManager.RegisterStartup(new EventHandler(lifecycleManager_OnStartup));
47      lifecycleManager.RegisterStartup(new EventHandler(lifecycleManager_OnShutdown));
48    }
49
50    private JobResult GetLastJobResult(Job job) {
51      ISession session = factory.GetSessionForCurrentThread();
52
53      try {
54        IJobResultsAdapter jobResultAdapter =
55            session.GetDataAdapter<JobResult, IJobResultsAdapter>();
56
57        List<JobResult> allJobResults = new List<JobResult>(jobResultAdapter.GetResultsOf(job));
58        JobResult lastJobResult = null;
59        foreach (JobResult jR in allJobResults) {
60          // if lastJobResult was before the current jobResult the lastJobResult must be updated
61          if (lastJobResult == null ||
62              (jR.Timestamp > lastJobResult.Timestamp))
63            lastJobResult = jR;
64        }
65        return lastJobResult;
66      }
67      finally {
68        if (session != null)
69          session.EndSession();
70      }
71    }
72
73    public void ResetJobsDependingOnResults(Job job) {
74      ISession session = factory.GetSessionForCurrentThread();
75
76      try {
77        IJobAdapter jobAdapter =
78            session.GetDataAdapter<Job, IJobAdapter>();
79
80        JobResult lastJobResult = GetLastJobResult(job);
81        if (lastJobResult != null) {
82          job.Percentage = lastJobResult.Percentage;
83          job.SerializedJob = lastJobResult.Result;
84        } else {
85          job.Percentage = 0;
86        }
87
88        job.Client = null;
89        job.State = State.offline;
90
91        jobAdapter.Update(job);
92      }
93      finally {
94        if (session != null)
95          session.EndSession();
96      }
97    }
98
99    void checkForDeadJobs() {
100       ISession session = factory.GetSessionForCurrentThread();
101
102       try {
103         IJobAdapter jobAdapter =
104             session.GetDataAdapter<Job, IJobAdapter>();
105
106         List<Job> allJobs = new List<Job>(jobAdapter.GetAll());
107         foreach (Job curJob in allJobs) {
108           if (curJob.State == State.calculating) {
109             ResetJobsDependingOnResults(curJob);
110           }
111         }
112       }
113       finally {
114         if (session != null)
115           session.EndSession();
116       }
117    }
118
119    void lifecycleManager_OnStartup(object sender, EventArgs e) {
120      checkForDeadJobs();
121    }
122
123    void lifecycleManager_OnShutdown(object sender, EventArgs e) {
124      checkForDeadJobs();
125    }
126
127    /// <summary>
128    /// returns all jobs stored in the database
129    /// </summary>
130    /// <returns></returns>
131    public ResponseList<Job> GetAllJobs() {
132       ISession session = factory.GetSessionForCurrentThread();
133
134       try {
135         IJobAdapter jobAdapter =
136             session.GetDataAdapter<Job, IJobAdapter>();
137
138         ResponseList<Job> response = new ResponseList<Job>();
139
140         response.List = new List<Job>(jobAdapter.GetAll());
141         response.Success = true;
142         response.StatusMessage = ApplicationConstants.RESPONSE_JOB_ALL_JOBS;
143
144         return response;
145       }
146       finally {
147         if (session != null)
148           session.EndSession();
149       }
150    }
151
152    /// <summary>
153    /// returns the job with the specified id
154    /// </summary>
155    /// <returns></returns>
156    public ResponseObject<Job> GetJobById(Guid jobId) {
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        response.Obj = jobAdapter.GetById(jobId);
166        if (response.Obj != null) {
167          response.Success = true;
168          response.StatusMessage = ApplicationConstants.RESPONSE_JOB_GET_JOB_BY_ID;
169        } else {
170          response.Success = false;
171          response.StatusMessage = ApplicationConstants.RESPONSE_JOB_JOB_DOESNT_EXIST;
172        }
173
174        return response;
175      }
176      finally {
177        if (session != null)
178          session.EndSession();
179      }
180    }
181
182    /// <summary>
183    /// Adds a new job into the database
184    /// </summary>
185    /// <param name="job"></param>
186    /// <returns></returns>
187    public ResponseObject<Job> AddNewJob(Job job) {
188      ISession session = factory.GetSessionForCurrentThread();
189
190      try {
191        IJobAdapter jobAdapter =
192            session.GetDataAdapter<Job, IJobAdapter>();
193
194        ResponseObject<Job> response = new ResponseObject<Job>();
195
196        if (job != null) {
197          if (job.State != State.offline) {
198            response.Success = false;
199            response.StatusMessage = ApplicationConstants.RESPONSE_JOB_JOBSTATE_MUST_BE_OFFLINE;
200            return response;
201          }
202          if (job.Id != Guid.Empty) {
203            response.Success = false;
204            response.StatusMessage = ApplicationConstants.RESPONSE_JOB_ID_MUST_NOT_BE_SET;
205            return response;
206          }
207          if (job.SerializedJob == null) {
208            response.StatusMessage = ApplicationConstants.RESPONSE_JOB_JOB_NULL;
209            response.Success = false;
210            return response;
211          }
212
213          job.DateCreated = DateTime.Now;
214          jobAdapter.Update(job);
215          response.Success = true;
216          response.Obj = job;
217          response.StatusMessage = ApplicationConstants.RESPONSE_JOB_JOB_ADDED;
218        } else {
219          response.Success = false;
220          response.StatusMessage = ApplicationConstants.RESPONSE_JOB_JOB_NULL;
221        }
222
223        return response;
224      }
225      finally {
226        if (session != null)
227          session.EndSession();
228      }
229    }
230
231    /// <summary>
232    /// Removes a job from the database
233    /// </summary>
234    /// <param name="jobId"></param>
235    /// <returns></returns>
236    public Response RemoveJob(Guid jobId) {
237      ISession session = factory.GetSessionForCurrentThread();
238
239      try {
240        IJobAdapter jobAdapter =
241            session.GetDataAdapter<Job, IJobAdapter>();
242        Response response = new Response();
243
244        Job job = jobAdapter.GetById(jobId);
245        if (job == null) {
246          response.Success = false;
247          response.StatusMessage = ApplicationConstants.RESPONSE_JOB_JOB_DOESNT_EXIST;
248          return response;
249        }
250        jobAdapter.Delete(job);
251        response.Success = false;
252        response.StatusMessage = ApplicationConstants.RESPONSE_JOB_JOB_REMOVED;
253
254        return response;
255      }
256      finally {
257        if (session != null)
258          session.EndSession();
259      }
260    }
261
262    public ResponseObject<JobResult> GetLastJobResultOf(Guid jobId, bool requested) {
263      ISession session = factory.GetSessionForCurrentThread();
264
265      try {
266        IJobAdapter jobAdapter =
267            session.GetDataAdapter<Job, IJobAdapter>();
268
269        ResponseObject<JobResult> response = new ResponseObject<JobResult>();
270
271        Job job = jobAdapter.GetById(jobId);
272        if (requested && (job.State == State.requestSnapshot || job.State == State.requestSnapshotSent)) {
273          response.Success = true;
274          response.StatusMessage = ApplicationConstants.RESPONSE_JOB_RESULT_NOT_YET_HERE;
275          return response;
276        }
277         
278        response.Success = true;
279        response.StatusMessage = ApplicationConstants.RESPONSE_JOB_JOB_RESULT_SENT;
280        response.Obj = GetLastJobResult(job);
281
282        return response;
283      }
284      finally {
285        if(session != null)
286          session.EndSession();
287      }
288    }
289
290    public Response RequestSnapshot(Guid jobId) {
291      ISession session = factory.GetSessionForCurrentThread();
292      Response response = new Response();
293     
294      try {
295        IJobAdapter jobAdapter = session.GetDataAdapter<Job, IJobAdapter>();
296
297        Job job = jobAdapter.GetById(jobId);
298        if (job.State == State.requestSnapshot || job.State == State.requestSnapshotSent) {
299          response.Success = true;
300          response.StatusMessage = ApplicationConstants.RESPONSE_JOB_REQUEST_ALLREADY_SET;
301          return response; // no commit needed
302        }
303        if (job.State != State.calculating) {
304          response.Success = false;
305          response.StatusMessage = ApplicationConstants.RESPONSE_JOB_IS_NOT_BEEING_CALCULATED;
306          return response; // no commit needed
307        }
308        // job is in correct state
309        job.State = State.requestSnapshot;
310        jobAdapter.Update(job);
311
312        response.Success = true;
313        response.StatusMessage = ApplicationConstants.RESPONSE_JOB_REQUEST_SET;
314
315        return response;
316      }
317      finally {
318        if (session != null)
319          session.EndSession();
320      }
321    }
322
323    public Response AbortJob(Guid jobId) {
324      ISession session = factory.GetSessionForCurrentThread();
325      Response response = new Response();
326
327      try {
328        IJobAdapter jobAdapter = session.GetDataAdapter<Job, IJobAdapter>();
329
330        Job job = jobAdapter.GetById(jobId);
331        if (job == null) {
332          response.Success = false;
333          response.StatusMessage = ApplicationConstants.RESPONSE_JOB_JOB_DOESNT_EXIST;
334          return response; // no commit needed
335        }
336        if (job.State == State.abort) {
337          response.Success = true;
338          response.StatusMessage = ApplicationConstants.RESPONSE_JOB_ABORT_REQUEST_ALLREADY_SET;
339          return response; // no commit needed
340        }
341        if (job.State != State.calculating && job.State != State.requestSnapshot && job.State != State.requestSnapshotSent) {
342          response.Success = false;
343          response.StatusMessage = ApplicationConstants.RESPONSE_JOB_IS_NOT_BEEING_CALCULATED;
344          return response; // no commit needed
345        }
346        // job is in correct state
347        job.State = State.abort;
348        jobAdapter.Update(job);
349
350        response.Success = true;
351        response.StatusMessage = ApplicationConstants.RESPONSE_JOB_ABORT_REQUEST_SET;
352
353        return response;
354      }
355      finally {
356        if (session != null)
357          session.EndSession();
358      }
359    }
360
361    public ResponseList<JobResult> GetAllJobResults(Guid jobId) {
362      ISession session = factory.GetSessionForCurrentThread();
363      ResponseList<JobResult> response = new ResponseList<JobResult>();
364
365      try {
366        IJobResultsAdapter jobResultAdapter =
367            session.GetDataAdapter<JobResult, IJobResultsAdapter>();
368        IJobAdapter jobAdapter = session.GetDataAdapter<Job, IJobAdapter>();
369
370        Job job = jobAdapter.GetById(jobId);
371        if (job == null) {
372          response.Success = false;
373          response.StatusMessage = ApplicationConstants.RESPONSE_JOB_JOB_DOESNT_EXIST;
374          return response;
375        }
376        response.List = new List<JobResult>(jobResultAdapter.GetResultsOf(job));
377        response.Success = true;
378        response.StatusMessage = ApplicationConstants.RESPONSE_JOB_JOB_RESULT_SENT;
379
380        return response;
381      }
382      finally {
383        if(session != null)
384          session.EndSession();
385      }
386    }
387
388    public ResponseList<Project> GetAllProjects() {
389      ISession session = factory.GetSessionForCurrentThread();
390      ResponseList<Project> response = new ResponseList<Project>();
391
392      try {
393        IProjectAdapter projectAdapter =
394          session.GetDataAdapter<Project, IProjectAdapter>();
395
396        List<Project> allProjects = new List<Project>(projectAdapter.GetAll());
397        response.List = allProjects;
398        response.Success = true;
399        return response;
400      }
401      finally {
402        if (session != null)
403          session.EndSession();
404      }
405    }
406
407    private Response createUpdateProject(Project project) {
408      ISession session = factory.GetSessionForCurrentThread();
409      Response response = new Response();
410      ITransaction tx = null;
411
412      try {
413        IProjectAdapter projectAdapter =
414          session.GetDataAdapter<Project, IProjectAdapter>();
415
416        if (project.Name == null || project.Name == "") {
417          response.Success = false;
418          response.StatusMessage = ApplicationConstants.RESPONSE_JOB_PROJECT_NAME_EMPTY;
419          return response;
420        }
421        tx = session.BeginTransaction();
422        projectAdapter.Update(project);
423
424        tx.Commit();
425        response.Success = true;
426        response.StatusMessage = ApplicationConstants.RESPONSE_JOB_PROJECT_ADDED;
427      } catch (ConstraintException ce) {
428        if (tx != null)
429          tx.Rollback();
430        response.Success = false;
431        response.StatusMessage = ce.Message;
432      }
433      catch (Exception ex) {
434        if (tx != null)
435          tx.Rollback();
436        throw ex;
437      }
438      finally {
439        if (session != null)
440          session.EndSession();
441      }
442      return response;
443    }
444
445    public Response CreateProject(Project project) {
446      return createUpdateProject(project);
447    }
448
449    public Response ChangeProject(Project project) {
450      return createUpdateProject(project);
451    }
452
453    public Response DeleteProject(Guid projectId) {
454      ISession session = factory.GetSessionForCurrentThread();
455      Response response = new Response();
456      ITransaction tx = null;
457
458      try {
459        IProjectAdapter projectAdapter =
460          session.GetDataAdapter<Project, IProjectAdapter>();
461
462        Project project = projectAdapter.GetById(projectId);
463        if (project == null) {
464          response.Success = false;
465          response.StatusMessage = ApplicationConstants.RESPONSE_JOB_PROJECT_DOESNT_EXIST;
466          return response;
467        }
468        projectAdapter.Delete(project);
469        tx.Commit();
470        response.Success = true;
471        response.StatusMessage = ApplicationConstants.RESPONSE_JOB_PROJECT_DELETED;
472      }
473      catch (Exception e) {
474        if (tx != null)
475          tx.Rollback();
476        response.Success = false;
477        response.StatusMessage = e.Message;
478      }
479      finally {
480        if (session != null)
481          session.EndSession();
482      }
483      return response;
484    }
485
486    public ResponseList<Job> GetJobsByProject(Guid projectId) {
487      ISession session = factory.GetSessionForCurrentThread();
488      ResponseList<Job> response = new ResponseList<Job>();
489
490      try {
491        IJobAdapter jobAdapter =
492          session.GetDataAdapter<Job, IJobAdapter>();
493        List<Job> jobsByProject = new List<Job>(jobAdapter.GetJobsByProject(projectId));
494        response.List = jobsByProject;
495        response.Success = true;
496      }
497      finally {
498        if (session != null)
499          session.EndSession();
500      }
501      return response;
502    }
503
504    #endregion
505  }
506}
Note: See TracBrowser for help on using the repository browser.