Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3020 was 3018, checked in by kgrading, 15 years ago

added Priority and resource restricted scheduling (#907)

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