Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3220 was 3220, checked in by kgrading, 14 years ago

improved the DAL further, changed minor details for the presentation (#830)

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