Free cookie consent management tool by TermsFeed Policy Generator

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

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

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

File size: 17.4 KB
RevLine 
[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
22using System;
[800]23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using HeuristicLab.Hive.Contracts.Interfaces;
27using HeuristicLab.Hive.Contracts.BusinessObjects;
[902]28using HeuristicLab.Hive.Contracts;
[1377]29using HeuristicLab.Hive.Server.DataAccess;
[1141]30using HeuristicLab.Hive.Server.Core.InternalInterfaces;
[1468]31using HeuristicLab.DataAccess.Interfaces;
[1948]32using System.Data;
[2117]33using System.IO;
[2608]34using HeuristicLab.Tracing;
[3011]35using System.Transactions;
[3220]36using HeuristicLab.Hive.Server.LINQDataAccess;
[800]37
38namespace HeuristicLab.Hive.Server.Core {
[1141]39  class JobManager: IJobManager, IInternalJobManager {
[820]40
[3011]41    //ISessionFactory factory;
[1133]42    ILifecycleManager lifecycleManager;
[820]43
[800]44    #region IJobManager Members
45
[820]46    public JobManager() {
[3011]47      //factory = ServiceLocator.GetSessionFactory();
[1133]48      lifecycleManager = ServiceLocator.GetLifecycleManager();
49
50      lifecycleManager.RegisterStartup(new EventHandler(lifecycleManager_OnStartup));
51      lifecycleManager.RegisterStartup(new EventHandler(lifecycleManager_OnShutdown));
[820]52    }
53
[3011]54    private JobDto GetLastJobResult(Guid jobId) {     
55      return DaoLocator.JobDao.FindById(jobId);
[1170]56    }
57
[3011]58    public void ResetJobsDependingOnResults(JobDto job) {
[2608]59      HiveLogger.Info(this.ToString() + ": Setting job " + job.Id + " offline");
[3011]60      if (job != null) {
61        DaoLocator.JobDao.SetJobOffline(job);
[1468]62      }
[1139]63    }
[3011]64         
[1139]65
[1133]66    void checkForDeadJobs() {
[2608]67      HiveLogger.Info(this.ToString() + " Searching for dead Jobs");
[3011]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();
[1133]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
[1121]88    /// <summary>
89    /// returns all jobs stored in the database
90    /// </summary>
91    /// <returns></returns>
[3011]92    public ResponseList<JobDto> GetAllJobs() {
93       /*ISession session = factory.GetSessionForCurrentThread();
[967]94
[1468]95       try {
96         IJobAdapter jobAdapter =
[3011]97             session.GetDataAdapter<JobDto, IJobAdapter>();*/
[1468]98
[3011]99         ResponseList<JobDto> response = new ResponseList<JobDto>();
[1468]100
[3011]101         response.List = new List<JobDto>(DaoLocator.JobDao.FindAll());
[1468]102         response.Success = true;
103         response.StatusMessage = ApplicationConstants.RESPONSE_JOB_ALL_JOBS;
104
105         return response;
[3011]106       /*}
[1468]107       finally {
108         if (session != null)
109           session.EndSession();
[3011]110       } */
[800]111    }
112
[1121]113    /// <summary>
[2117]114    /// Gets the streamed job
115    /// </summary>
116    /// <param name="jobId"></param>
117    /// <returns></returns>
[3011]118    public Stream GetJobStreamById(Guid jobId) {           
119      return DaoLocator.JobDao.GetSerializedJobStream(jobId);
120     
[2117]121    }
122
123    /// <summary>
[2005]124    /// returns the job with the specified id
125    /// </summary>
126    /// <returns></returns>
[3011]127    public ResponseObject<JobDto> GetJobById(Guid jobId) {
128      /*ISession session = factory.GetSessionForCurrentThread();
[2005]129
[3011]130      try {             
[2005]131        IJobAdapter jobAdapter =
[3011]132            session.GetDataAdapter<JobDto, IJobAdapter>();*/
[2005]133
[3011]134        ResponseObject<JobDto> response = new ResponseObject<JobDto>();
[2005]135
[3011]136      response.Obj = DaoLocator.JobDao.FindById(jobId);
[2005]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      }
[3011]147      /*finally {
[2005]148        if (session != null)
149          session.EndSession();
150      }
[3011]151    }   */
[2005]152
[3220]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
[2005]163    /// <summary>
[1121]164    /// Adds a new job into the database
165    /// </summary>
166    /// <param name="job"></param>
167    /// <returns></returns>
[3011]168    public ResponseObject<JobDto> AddNewJob(SerializedJob job) {
169      /*ISession session = factory.GetSessionForCurrentThread();
[967]170
[1468]171      try {
172        IJobAdapter jobAdapter =
[3011]173            session.GetDataAdapter<JobDto, IJobAdapter>();*/
[1468]174
[3011]175        ResponseObject<JobDto> response = new ResponseObject<JobDto>();
[1468]176
[2082]177        if (job != null && job.JobInfo != null) {
178          if (job.JobInfo.State != State.offline) {
[1468]179            response.Success = false;
180            response.StatusMessage = ApplicationConstants.RESPONSE_JOB_JOBSTATE_MUST_BE_OFFLINE;
181            return response;
182          }
[2082]183          if (job.JobInfo.Id != Guid.Empty) {
[1468]184            response.Success = false;
185            response.StatusMessage = ApplicationConstants.RESPONSE_JOB_ID_MUST_NOT_BE_SET;
186            return response;
187          }
[2092]188          if (job.SerializedJobData == null) {
[1468]189            response.StatusMessage = ApplicationConstants.RESPONSE_JOB_JOB_NULL;
190            response.Success = false;
191            return response;
192          }
193
[2082]194          job.JobInfo.DateCreated = DateTime.Now;
[3011]195          DaoLocator.JobDao.InsertWithAttachedJob(job);
196          DaoLocator.PluginInfoDao.InsertPluginDependenciesForJob(job.JobInfo);
[3018]197         
[1468]198          response.Success = true;
[2082]199          response.Obj = job.JobInfo;
[1468]200          response.StatusMessage = ApplicationConstants.RESPONSE_JOB_JOB_ADDED;
201        } else {
[1024]202          response.Success = false;
[1120]203          response.StatusMessage = ApplicationConstants.RESPONSE_JOB_JOB_NULL;
204        }
205
[1468]206        return response;
[967]207      }
[3011]208    /*  finally {
[1468]209        if (session != null)
210          session.EndSession();
211      }
[3011]212    } */
[967]213
[1121]214    /// <summary>
215    /// Removes a job from the database
216    /// </summary>
217    /// <param name="jobId"></param>
218    /// <returns></returns>
[1449]219    public Response RemoveJob(Guid jobId) {
[3011]220      /*ISession session = factory.GetSessionForCurrentThread();
[967]221
[1468]222      try {
223        IJobAdapter jobAdapter =
[3011]224            session.GetDataAdapter<JobDto, IJobAdapter>();*/
225
[1468]226        Response response = new Response();
227
[3011]228      JobDto job = DaoLocator.JobDao.FindById(jobId);
[1468]229        if (job == null) {
230          response.Success = false;
231          response.StatusMessage = ApplicationConstants.RESPONSE_JOB_JOB_DOESNT_EXIST;
232          return response;
233        }
[3011]234        DaoLocator.JobDao.Delete(job);
[967]235        response.Success = false;
[1468]236        response.StatusMessage = ApplicationConstants.RESPONSE_JOB_JOB_REMOVED;
237
[967]238        return response;
239      }
[3011]240      /*finally {
[1468]241        if (session != null)
242          session.EndSession();
243      }
[3011]244    }   */
[967]245
[3011]246    public ResponseObject<JobDto> GetLastJobResultOf(Guid jobId) {
247      ResponseObject<JobDto> result =
248        new ResponseObject<JobDto>();
[2099]249
250       result.Obj =
251         GetLastJobResult(jobId);
252       result.Success =
253         result.Obj != null;
254
255       return result;
256    }
257
[3011]258    public ResponseObject<SerializedJob>
[2099]259      GetLastSerializedJobResultOf(Guid jobId, bool requested) {
[3011]260      /*ISession session = factory.GetSessionForCurrentThread();
[1170]261
[2099]262      ITransaction tx = null;
263
[1468]264      try {
265        IJobAdapter jobAdapter =
[3011]266            session.GetDataAdapter<JobDto, IJobAdapter>();
[1468]267
[2099]268        IJobResultsAdapter jobResultsAdapter =
269          session.GetDataAdapter<JobResult, IJobResultsAdapter>();
[1770]270
[3011]271        tx = session.BeginTransaction();                          */
[2099]272
[3011]273        ResponseObject<SerializedJob> response =
274          new ResponseObject<SerializedJob>();
[2099]275
[3011]276      JobDto job = DaoLocator.JobDao.FindById(jobId);
[1772]277        if (requested && (job.State == State.requestSnapshot || job.State == State.requestSnapshotSent)) {
[1770]278          response.Success = true;
279          response.StatusMessage = ApplicationConstants.RESPONSE_JOB_RESULT_NOT_YET_HERE;
[2099]280         
[3011]281          //tx.Commit();
282         
[1770]283          return response;
284        }
[1468]285
[3011]286        /*JobResult lastResult =
287          jobResultsAdapter.GetLastResultOf(job.Id);*/
[2099]288
[3011]289        //if (lastResult != null) {
[2099]290          response.Success = true;
291          response.StatusMessage = ApplicationConstants.RESPONSE_JOB_JOB_RESULT_SENT;
[3011]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        //}
[2099]299
[3011]300        //tx.Commit();
[1468]301        return response;
302      }
[3011]303      /*catch (Exception ex) {
[2099]304        if (tx != null)
305          tx.Rollback();
306        throw ex;
307      }
[1468]308      finally {
[2099]309        if (session != null)
[1468]310          session.EndSession();
311      }
[3011]312    }    */
[1170]313
[2099]314
[1509]315    public Response RequestSnapshot(Guid jobId) {
[3011]316     // ISession session = factory.GetSessionForCurrentThread();
[1509]317      Response response = new Response();
318     
[3011]319     /* try {
320        IJobAdapter jobAdapter = session.GetDataAdapter<JobDto, IJobAdapter>();*/
[1509]321
[3011]322        JobDto job = DaoLocator.JobDao.FindById(jobId);
[1831]323        if (job.State == State.requestSnapshot || job.State == State.requestSnapshotSent) {
[1577]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;
[3011]335        DaoLocator.JobDao.Update(job);
[1577]336
337        response.Success = true;
338        response.StatusMessage = ApplicationConstants.RESPONSE_JOB_REQUEST_SET;
339
[1509]340        return response;
341      }
[3011]342    /*  finally {
[1509]343        if (session != null)
344          session.EndSession();
345      }
[3011]346    } */
[1509]347
348    public Response AbortJob(Guid jobId) {
[3011]349      //ISession session = factory.GetSessionForCurrentThread();
[1577]350      Response response = new Response();
351
[3011]352    /*  try {
353        IJobAdapter jobAdapter = session.GetDataAdapter<JobDto, IJobAdapter>();*/
[1577]354
[3011]355//        JobDto job = jobAdapter.GetById(jobId);
356      JobDto job = DaoLocator.JobDao.FindById(jobId);
[1762]357        if (job == null) {
358          response.Success = false;
359          response.StatusMessage = ApplicationConstants.RESPONSE_JOB_JOB_DOESNT_EXIST;
360          return response; // no commit needed
361        }
[1577]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        }
[1811]367        if (job.State != State.calculating && job.State != State.requestSnapshot && job.State != State.requestSnapshotSent) {
[1577]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;
[3011]374      DaoLocator.JobDao.Update(job);
[1577]375
376        response.Success = true;
377        response.StatusMessage = ApplicationConstants.RESPONSE_JOB_ABORT_REQUEST_SET;
378
379        return response;
380      }
[3011]381      /*finally {
[1577]382        if (session != null)
383          session.EndSession();
384      }
[3011]385    }   */
[1509]386
[1799]387    public ResponseList<JobResult> GetAllJobResults(Guid jobId) {
[3011]388     /* ISession session = factory.GetSessionForCurrentThread();
[1799]389      ResponseList<JobResult> response = new ResponseList<JobResult>();
[1627]390
391      try {
392        IJobResultsAdapter jobResultAdapter =
393            session.GetDataAdapter<JobResult, IJobResultsAdapter>();
[3011]394        IJobAdapter jobAdapter = session.GetDataAdapter<JobDto, IJobAdapter>();
[1627]395
[3011]396        JobDto job = jobAdapter.GetById(jobId);
[1627]397        if (job == null) {
398          response.Success = false;
399          response.StatusMessage = ApplicationConstants.RESPONSE_JOB_JOB_DOESNT_EXIST;
400          return response;
401        }
[2099]402        response.List = new List<JobResult>(jobResultAdapter.GetResultsOf(job.Id));
[1627]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();
[3011]411      }  */
412      return new ResponseList<JobResult>();
[1627]413    }
[1932]414
[3011]415    public ResponseList<ProjectDto> GetAllProjects() {
416      /*ISession session = factory.GetSessionForCurrentThread();
417      ResponseList<ProjectDto> response = new ResponseList<ProjectDto>();
[1934]418
[1948]419      try {
420        IProjectAdapter projectAdapter =
[3011]421          session.GetDataAdapter<ProjectDto, IProjectAdapter>();
[1934]422
[3011]423        List<ProjectDto> allProjects = new List<ProjectDto>(projectAdapter.GetAll());
[1948]424        response.List = allProjects;
425        response.Success = true;
426        return response;
427      }
428      finally {
429        if (session != null)
430          session.EndSession();
[3011]431      } */
432      return null;
[1948]433    }
[1934]434
[3011]435    private Response createUpdateProject(ProjectDto project) {
436      /*ISession session = factory.GetSessionForCurrentThread();
[1948]437      Response response = new Response();
438      ITransaction tx = null;
439
440      try {
441        IProjectAdapter projectAdapter =
[3011]442          session.GetDataAdapter<ProjectDto, IProjectAdapter>();
[1948]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      }
[3011]470      return response;    */
471      return null;
[1932]472    }
473
[3011]474    public Response CreateProject(ProjectDto project) {
[1948]475      return createUpdateProject(project);
[1932]476    }
477
[3011]478    public Response ChangeProject(ProjectDto project) {
[1948]479      return createUpdateProject(project);
[1932]480    }
481
482    public Response DeleteProject(Guid projectId) {
[3011]483      /*ISession session = factory.GetSessionForCurrentThread();
[1948]484      Response response = new Response();
485      ITransaction tx = null;
486
487      try {
488        IProjectAdapter projectAdapter =
[3011]489          session.GetDataAdapter<ProjectDto, IProjectAdapter>();
[1948]490
[3011]491        ProjectDto project = projectAdapter.GetById(projectId);
[1948]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();
[3011]511      }     
512      return response; */
513      return null;
[1932]514    }
515
[3011]516    public ResponseList<JobDto> GetJobsByProject(Guid projectId) {
517      /*ISession session = factory.GetSessionForCurrentThread();
518      ResponseList<JobDto> response = new ResponseList<JobDto>();
[1948]519
520      try {
521        IJobAdapter jobAdapter =
[3011]522          session.GetDataAdapter<JobDto, IJobAdapter>();
523        List<JobDto> jobsByProject = new List<JobDto>(jobAdapter.GetJobsByProject(projectId));
[1948]524        response.List = jobsByProject;
525        response.Success = true;
526      }
527      finally {
528        if (session != null)
529          session.EndSession();
[3011]530      }
531       
532      return response;*/
533      return null;     
[1932]534    }
535
[800]536    #endregion
537  }
538}
Note: See TracBrowser for help on using the repository browser.